1# Orthanc - A Lightweight, RESTful DICOM Store
2# Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
3# Department, University Hospital of Liege, Belgium
4# Copyright (C) 2017-2020 Osimis S.A., Belgium
5#
6# This program is free software: you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public License
8# as published by the Free Software Foundation, either version 3 of
9# the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this program. If not, see
18# <http://www.gnu.org/licenses/>.
19
20
21if (STATIC_BUILD OR NOT USE_SYSTEM_BOOST)
22  set(BOOST_STATIC 1)
23else()
24  include(FindBoost)
25
26  set(BOOST_STATIC 0)
27  #set(Boost_DEBUG 1)
28  #set(Boost_USE_STATIC_LIBS ON)
29
30  if (ENABLE_LOCALE)
31    list(APPEND ORTHANC_BOOST_COMPONENTS locale)
32  endif()
33
34  list(APPEND ORTHANC_BOOST_COMPONENTS filesystem thread system date_time regex iostreams)
35  find_package(Boost COMPONENTS ${ORTHANC_BOOST_COMPONENTS})
36
37  if (NOT Boost_FOUND)
38    foreach (item ${ORTHANC_BOOST_COMPONENTS})
39      string(TOUPPER ${item} tmp)
40
41      if (Boost_${tmp}_FOUND)
42        set(tmp2 "found")
43      else()
44        set(tmp2 "missing")
45      endif()
46
47      message("Boost component ${item} - ${tmp2}")
48    endforeach()
49
50    message(FATAL_ERROR "Unable to locate Boost on this system")
51  endif()
52
53
54  # Patch by xnox to fix issue #166 (CMake find_boost version is now
55  # broken with newer boost/cmake)
56  # https://bitbucket.org/sjodogne/orthanc/issues/166/
57  if (POLICY CMP0093)
58    set(BOOST144 1.44)
59  else()
60    set(BOOST144 104400)
61  endif()
62
63
64  # Boost releases 1.44 through 1.47 supply both V2 and V3 filesystem
65  # http://www.boost.org/doc/libs/1_46_1/libs/filesystem/v3/doc/index.htm
66  if (${Boost_VERSION} LESS ${BOOST144})
67    add_definitions(
68      -DBOOST_HAS_FILESYSTEM_V3=0
69      )
70  else()
71    add_definitions(
72      -DBOOST_HAS_FILESYSTEM_V3=1
73      -DBOOST_FILESYSTEM_VERSION=3
74      )
75  endif()
76
77  include_directories(${Boost_INCLUDE_DIRS})
78  link_libraries(${Boost_LIBRARIES})
79endif()
80
81
82if (BOOST_STATIC)
83  ##
84  ## Parameters for static compilation of Boost
85  ##
86
87  set(BOOST_NAME boost_1_69_0)
88  set(BOOST_VERSION 1.69.0)
89  set(BOOST_BCP_SUFFIX bcpdigest-1.5.6)
90  set(BOOST_MD5 "579bccc0ea4d1a261c1d0c5e27446c3d")
91  set(BOOST_URL "http://orthanc.osimis.io/ThirdPartyDownloads/${BOOST_NAME}_${BOOST_BCP_SUFFIX}.tar.gz")
92  set(BOOST_SOURCES_DIR ${CMAKE_BINARY_DIR}/${BOOST_NAME})
93
94  if (IS_DIRECTORY "${BOOST_SOURCES_DIR}")
95    set(FirstRun OFF)
96  else()
97    set(FirstRun ON)
98  endif()
99
100  DownloadPackage(${BOOST_MD5} ${BOOST_URL} "${BOOST_SOURCES_DIR}")
101
102
103  ##
104  ## Patching boost
105  ##
106
107  execute_process(
108    COMMAND ${PATCH_EXECUTABLE} -p0 -N -i
109    ${CMAKE_CURRENT_LIST_DIR}/../Patches/boost-${BOOST_VERSION}-linux-standard-base.patch
110    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
111    RESULT_VARIABLE Failure
112    )
113
114  if (FirstRun AND Failure)
115    message(FATAL_ERROR "Error while patching a file")
116  endif()
117
118
119  ##
120  ## Generic configuration of Boost
121  ##
122
123  if (CMAKE_COMPILER_IS_GNUCXX)
124    add_definitions(-isystem ${BOOST_SOURCES_DIR})
125  endif()
126
127  include_directories(
128    BEFORE ${BOOST_SOURCES_DIR}
129    )
130
131  if (ORTHANC_BUILDING_FRAMEWORK_LIBRARY)
132    add_definitions(
133      # Packaging Boost inside the Orthanc Framework DLL, not exposing
134      # the Boost symbols
135      -DBOOST_THREAD_BUILD_DLL
136      -DBOOST_REGEX_BUILD_DLL
137      )
138  else()
139    add_definitions(
140      # Static build of Boost (this was the only possibility in
141      # Orthanc <= 1.7.1)
142      -DBOOST_ALL_NO_LIB
143      -DBOOST_ALL_NOLIB
144      -DBOOST_DATE_TIME_NO_LIB
145      -DBOOST_THREAD_BUILD_LIB
146      -DBOOST_PROGRAM_OPTIONS_NO_LIB
147      -DBOOST_REGEX_NO_LIB
148      -DBOOST_SYSTEM_NO_LIB
149      -DBOOST_LOCALE_NO_LIB
150      )
151  endif()
152
153  add_definitions(
154    # In static builds, explicitly prevent Boost from using the system
155    # locale in lexical casts. This is notably important if
156    # "boost::lexical_cast<double>()" is applied to strings containing
157    # "," instead of "." as decimal separators. Check out function
158    # "OrthancStone::LinearAlgebra::ParseVector()".
159    -DBOOST_LEXICAL_CAST_ASSUME_C_LOCALE
160    )
161
162  set(BOOST_SOURCES
163    ${BOOST_SOURCES_DIR}/libs/system/src/error_code.cpp
164    )
165
166  if ("${CMAKE_SYSTEM_VERSION}" STREQUAL "LinuxStandardBase" OR
167      "${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
168    add_definitions(
169      -DBOOST_SYSTEM_USE_STRERROR=1
170      )
171  endif()
172
173
174  ##
175  ## Configuration of boost::thread
176  ##
177
178  if (CMAKE_SYSTEM_NAME STREQUAL "Linux" OR
179      CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR
180      CMAKE_SYSTEM_NAME STREQUAL "DragonFly" OR
181      CMAKE_SYSTEM_NAME STREQUAL "kFreeBSD" OR
182      CMAKE_SYSTEM_NAME STREQUAL "OpenBSD" OR
183      CMAKE_SYSTEM_NAME STREQUAL "PNaCl" OR
184      CMAKE_SYSTEM_NAME STREQUAL "NaCl32" OR
185      CMAKE_SYSTEM_NAME STREQUAL "NaCl64" OR
186      CMAKE_SYSTEM_NAME STREQUAL "Android")
187    list(APPEND BOOST_SOURCES
188      ${BOOST_SOURCES_DIR}/libs/atomic/src/lockpool.cpp
189      ${BOOST_SOURCES_DIR}/libs/thread/src/pthread/once.cpp
190      ${BOOST_SOURCES_DIR}/libs/thread/src/pthread/thread.cpp
191      )
192
193    if ("${CMAKE_SYSTEM_VERSION}" STREQUAL "LinuxStandardBase" OR
194        CMAKE_SYSTEM_NAME STREQUAL "PNaCl" OR
195        CMAKE_SYSTEM_NAME STREQUAL "NaCl32" OR
196        CMAKE_SYSTEM_NAME STREQUAL "NaCl64")
197      add_definitions(-DBOOST_HAS_SCHED_YIELD=1)
198    endif()
199
200    # Fix for error: "boost_1_69_0/boost/chrono/detail/inlined/mac/thread_clock.hpp:54:28:
201    # error: use of undeclared identifier 'pthread_mach_thread_np'"
202    # https://github.com/envoyproxy/envoy/pull/1785
203    if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
204      add_definitions(-D_DARWIN_C_SOURCE=1)
205    endif()
206
207  elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
208    list(APPEND BOOST_SOURCES
209      ${BOOST_SOURCES_DIR}/libs/thread/src/win32/tss_dll.cpp
210      ${BOOST_SOURCES_DIR}/libs/thread/src/win32/thread.cpp
211      ${BOOST_SOURCES_DIR}/libs/thread/src/win32/tss_pe.cpp
212      )
213
214  elseif (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
215    # No support for threads in asm.js/WebAssembly
216
217  else()
218    message(FATAL_ERROR "Support your platform here")
219  endif()
220
221
222  ##
223  ## Configuration of boost::regex
224  ##
225
226  aux_source_directory(${BOOST_SOURCES_DIR}/libs/regex/src BOOST_REGEX_SOURCES)
227
228  list(APPEND BOOST_SOURCES
229    ${BOOST_REGEX_SOURCES}
230    )
231
232
233  ##
234  ## Configuration of boost::datetime
235  ##
236
237  list(APPEND BOOST_SOURCES
238    ${BOOST_SOURCES_DIR}/libs/date_time/src/gregorian/greg_month.cpp
239    )
240
241
242  ##
243  ## Configuration of boost::filesystem and boost::iostreams
244  ##
245
246  if (CMAKE_SYSTEM_NAME STREQUAL "PNaCl" OR
247      CMAKE_SYSTEM_NAME STREQUAL "NaCl32" OR
248      CMAKE_SYSTEM_NAME STREQUAL "NaCl64" OR
249      CMAKE_SYSTEM_NAME STREQUAL "Android")
250    # boost::filesystem is not available on PNaCl
251    add_definitions(
252      -DBOOST_HAS_FILESYSTEM_V3=0
253      -D__INTEGRITY=1
254      )
255  else()
256    add_definitions(
257      -DBOOST_HAS_FILESYSTEM_V3=1
258      )
259    list(APPEND BOOST_SOURCES
260      ${BOOST_NAME}/libs/filesystem/src/codecvt_error_category.cpp
261      ${BOOST_NAME}/libs/filesystem/src/operations.cpp
262      ${BOOST_NAME}/libs/filesystem/src/path.cpp
263      ${BOOST_NAME}/libs/filesystem/src/path_traits.cpp
264      )
265
266    if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR
267        CMAKE_SYSTEM_NAME STREQUAL "OpenBSD" OR
268        CMAKE_SYSTEM_NAME STREQUAL "DragonFly")
269     list(APPEND BOOST_SOURCES
270        ${BOOST_SOURCES_DIR}/libs/filesystem/src/utf8_codecvt_facet.cpp
271        )
272
273    elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
274      list(APPEND BOOST_SOURCES
275        ${BOOST_NAME}/libs/filesystem/src/windows_file_codecvt.cpp
276        )
277    endif()
278  endif()
279
280  list(APPEND BOOST_SOURCES
281    ${BOOST_NAME}/libs/iostreams/src/file_descriptor.cpp
282    )
283
284
285  ##
286  ## Configuration of boost::locale
287  ##
288
289  if (NOT ENABLE_LOCALE)
290    message("boost::locale is disabled")
291  else()
292    set(BOOST_ICU_SOURCES
293      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/boundary.cpp
294      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/codecvt.cpp
295      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/collator.cpp
296      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/conversion.cpp
297      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/date_time.cpp
298      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/formatter.cpp
299      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/icu_backend.cpp
300      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/numeric.cpp
301      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/time_zone.cpp
302      )
303
304    list(APPEND BOOST_SOURCES
305      ${BOOST_SOURCES_DIR}/libs/locale/src/encoding/codepage.cpp
306      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/generator.cpp
307      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/date_time.cpp
308      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/formatting.cpp
309      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/ids.cpp
310      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/localization_backend.cpp
311      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/message.cpp
312      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/mo_lambda.cpp
313      ${BOOST_SOURCES_DIR}/libs/locale/src/util/codecvt_converter.cpp
314      ${BOOST_SOURCES_DIR}/libs/locale/src/util/default_locale.cpp
315      ${BOOST_SOURCES_DIR}/libs/locale/src/util/gregorian.cpp
316      ${BOOST_SOURCES_DIR}/libs/locale/src/util/info.cpp
317      ${BOOST_SOURCES_DIR}/libs/locale/src/util/locale_data.cpp
318      )
319
320    if (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD" OR
321        CMAKE_SYSTEM_VERSION STREQUAL "LinuxStandardBase")
322      add_definitions(
323        -DBOOST_LOCALE_NO_WINAPI_BACKEND=1
324        -DBOOST_LOCALE_NO_POSIX_BACKEND=1
325        )
326
327      list(APPEND BOOST_SOURCES
328        ${BOOST_SOURCES_DIR}/libs/locale/src/std/codecvt.cpp
329        ${BOOST_SOURCES_DIR}/libs/locale/src/std/collate.cpp
330        ${BOOST_SOURCES_DIR}/libs/locale/src/std/converter.cpp
331        ${BOOST_SOURCES_DIR}/libs/locale/src/std/numeric.cpp
332        ${BOOST_SOURCES_DIR}/libs/locale/src/std/std_backend.cpp
333        )
334
335      if (BOOST_LOCALE_BACKEND STREQUAL "gcc" OR
336          BOOST_LOCALE_BACKEND STREQUAL "libiconv")
337        add_definitions(-DBOOST_LOCALE_WITH_ICONV=1)
338      elseif (BOOST_LOCALE_BACKEND STREQUAL "icu")
339        add_definitions(-DBOOST_LOCALE_WITH_ICU=1)
340        list(APPEND BOOST_SOURCES ${BOOST_ICU_SOURCES})
341      else()
342        message(FATAL_ERROR "Unsupported value for BOOST_LOCALE_BACKEND: ${BOOST_LOCALE_BACKEND}")
343      endif()
344
345    elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" OR
346            CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR
347            CMAKE_SYSTEM_NAME STREQUAL "DragonFly" OR
348            CMAKE_SYSTEM_NAME STREQUAL "kFreeBSD" OR
349            CMAKE_SYSTEM_NAME STREQUAL "PNaCl" OR
350            CMAKE_SYSTEM_NAME STREQUAL "NaCl32" OR
351            CMAKE_SYSTEM_NAME STREQUAL "NaCl64" OR
352            CMAKE_SYSTEM_NAME STREQUAL "Emscripten") # For WebAssembly or asm.js
353      add_definitions(
354        -DBOOST_LOCALE_NO_WINAPI_BACKEND=1
355        -DBOOST_LOCALE_NO_STD_BACKEND=1
356        )
357
358      list(APPEND BOOST_SOURCES
359        ${BOOST_SOURCES_DIR}/libs/locale/src/posix/codecvt.cpp
360        ${BOOST_SOURCES_DIR}/libs/locale/src/posix/collate.cpp
361        ${BOOST_SOURCES_DIR}/libs/locale/src/posix/converter.cpp
362        ${BOOST_SOURCES_DIR}/libs/locale/src/posix/numeric.cpp
363        ${BOOST_SOURCES_DIR}/libs/locale/src/posix/posix_backend.cpp
364        )
365
366      if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten" OR
367          BOOST_LOCALE_BACKEND STREQUAL "gcc" OR
368          BOOST_LOCALE_BACKEND STREQUAL "libiconv")
369        # In WebAssembly or asm.js, we rely on the version of iconv
370        # that is shipped with the stdlib
371        add_definitions(-DBOOST_LOCALE_WITH_ICONV=1)
372      elseif (BOOST_LOCALE_BACKEND STREQUAL "icu")
373        add_definitions(-DBOOST_LOCALE_WITH_ICU=1)
374        list(APPEND BOOST_SOURCES ${BOOST_ICU_SOURCES})
375      else()
376        message(FATAL_ERROR "Unsupported value for BOOST_LOCALE_BACKEND: ${BOOST_LOCALE_BACKEND}")
377      endif()
378
379    elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
380      add_definitions(
381        -DBOOST_LOCALE_NO_POSIX_BACKEND=1
382        -DBOOST_LOCALE_NO_STD_BACKEND=1
383        )
384
385      list(APPEND BOOST_SOURCES
386        ${BOOST_SOURCES_DIR}/libs/locale/src/win32/collate.cpp
387        ${BOOST_SOURCES_DIR}/libs/locale/src/win32/converter.cpp
388        ${BOOST_SOURCES_DIR}/libs/locale/src/win32/lcid.cpp
389        ${BOOST_SOURCES_DIR}/libs/locale/src/win32/numeric.cpp
390        ${BOOST_SOURCES_DIR}/libs/locale/src/win32/win_backend.cpp
391        )
392
393      # Starting with release 0.8.2, Orthanc statically links against
394      # libiconv on Windows. Indeed, the "WCONV" library of Windows XP
395      # seems not to support properly several codepages (notably
396      # "Latin3", "Hebrew", and "Arabic"). Set "BOOST_LOCALE_BACKEND"
397      # to "wconv" to use WCONV anyway.
398
399      if (BOOST_LOCALE_BACKEND STREQUAL "libiconv")
400        add_definitions(-DBOOST_LOCALE_WITH_ICONV=1)
401      elseif (BOOST_LOCALE_BACKEND STREQUAL "icu")
402        add_definitions(-DBOOST_LOCALE_WITH_ICU=1)
403        list(APPEND BOOST_SOURCES ${BOOST_ICU_SOURCES})
404      elseif (BOOST_LOCALE_BACKEND STREQUAL "wconv")
405        message("Using Window's wconv")
406        add_definitions(-DBOOST_LOCALE_WITH_WCONV=1)
407      else()
408        message(FATAL_ERROR "Unsupported value for BOOST_LOCALE_BACKEND on Windows: ${BOOST_LOCALE_BACKEND}")
409      endif()
410
411    else()
412      message(FATAL_ERROR "Support your platform here")
413    endif()
414  endif()
415
416
417  source_group(ThirdParty\\boost REGULAR_EXPRESSION ${BOOST_SOURCES_DIR}/.*)
418
419endif()
420