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-2021 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://bugs.orthanc-server.com/show_bug.cgi?id=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
134      -DBOOST_ALL_DYN_LINK      # Expose Boost symbols into the DLL
135      -DBOOST_THREAD_BUILD_DLL
136      -DBOOST_REGEX_BUILD_DLL
137      -DBOOST_IOSTREAMS_SOURCE
138      )
139  else()
140    add_definitions(
141      # Static build of Boost (this was the only possibility in
142      # Orthanc <= 1.7.1)
143      -DBOOST_ALL_NO_LIB
144      -DBOOST_ALL_NOLIB
145      -DBOOST_DATE_TIME_NO_LIB
146      -DBOOST_THREAD_BUILD_LIB
147      -DBOOST_PROGRAM_OPTIONS_NO_LIB
148      -DBOOST_REGEX_NO_LIB
149      -DBOOST_SYSTEM_NO_LIB
150      -DBOOST_LOCALE_NO_LIB
151      )
152  endif()
153
154  add_definitions(
155    # In static builds, explicitly prevent Boost from using the system
156    # locale in lexical casts. This is notably important if
157    # "boost::lexical_cast<double>()" is applied to strings containing
158    # "," instead of "." as decimal separators. Check out function
159    # "OrthancStone::LinearAlgebra::ParseVector()".
160    -DBOOST_LEXICAL_CAST_ASSUME_C_LOCALE
161    )
162
163  set(BOOST_SOURCES
164    ${BOOST_SOURCES_DIR}/libs/system/src/error_code.cpp
165    )
166
167  if ("${CMAKE_SYSTEM_VERSION}" STREQUAL "LinuxStandardBase" OR
168      "${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
169    add_definitions(
170      -DBOOST_SYSTEM_USE_STRERROR=1
171      )
172  endif()
173
174
175  ##
176  ## Configuration of boost::thread
177  ##
178
179  if (CMAKE_SYSTEM_NAME STREQUAL "Linux" OR
180      CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR
181      CMAKE_SYSTEM_NAME STREQUAL "DragonFly" OR
182      CMAKE_SYSTEM_NAME STREQUAL "kFreeBSD" OR
183      CMAKE_SYSTEM_NAME STREQUAL "OpenBSD" OR
184      CMAKE_SYSTEM_NAME STREQUAL "PNaCl" OR
185      CMAKE_SYSTEM_NAME STREQUAL "NaCl32" OR
186      CMAKE_SYSTEM_NAME STREQUAL "NaCl64" OR
187      CMAKE_SYSTEM_NAME STREQUAL "Android")
188    list(APPEND BOOST_SOURCES
189      ${BOOST_SOURCES_DIR}/libs/atomic/src/lockpool.cpp
190      ${BOOST_SOURCES_DIR}/libs/thread/src/pthread/once.cpp
191      ${BOOST_SOURCES_DIR}/libs/thread/src/pthread/thread.cpp
192      )
193
194    if ("${CMAKE_SYSTEM_VERSION}" STREQUAL "LinuxStandardBase" OR
195        CMAKE_SYSTEM_NAME STREQUAL "PNaCl" OR
196        CMAKE_SYSTEM_NAME STREQUAL "NaCl32" OR
197        CMAKE_SYSTEM_NAME STREQUAL "NaCl64")
198      add_definitions(-DBOOST_HAS_SCHED_YIELD=1)
199    endif()
200
201    # Fix for error: "boost_1_69_0/boost/chrono/detail/inlined/mac/thread_clock.hpp:54:28:
202    # error: use of undeclared identifier 'pthread_mach_thread_np'"
203    # https://github.com/envoyproxy/envoy/pull/1785
204    if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
205      add_definitions(-D_DARWIN_C_SOURCE=1)
206    endif()
207
208  elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
209    list(APPEND BOOST_SOURCES
210      ${BOOST_SOURCES_DIR}/libs/thread/src/win32/tss_dll.cpp
211      ${BOOST_SOURCES_DIR}/libs/thread/src/win32/thread.cpp
212      ${BOOST_SOURCES_DIR}/libs/thread/src/win32/tss_pe.cpp
213      )
214
215  elseif (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
216    # No support for threads in asm.js/WebAssembly
217
218  else()
219    message(FATAL_ERROR "Support your platform here")
220  endif()
221
222
223  ##
224  ## Configuration of boost::regex
225  ##
226
227  aux_source_directory(${BOOST_SOURCES_DIR}/libs/regex/src BOOST_REGEX_SOURCES)
228
229  list(APPEND BOOST_SOURCES
230    ${BOOST_REGEX_SOURCES}
231    )
232
233
234  ##
235  ## Configuration of boost::datetime
236  ##
237
238  list(APPEND BOOST_SOURCES
239    ${BOOST_SOURCES_DIR}/libs/date_time/src/gregorian/greg_month.cpp
240    )
241
242
243  ##
244  ## Configuration of boost::filesystem and boost::iostreams
245  ##
246
247  if (CMAKE_SYSTEM_NAME STREQUAL "PNaCl" OR
248      CMAKE_SYSTEM_NAME STREQUAL "NaCl32" OR
249      CMAKE_SYSTEM_NAME STREQUAL "NaCl64" OR
250      CMAKE_SYSTEM_NAME STREQUAL "Android")
251    # boost::filesystem is not available on PNaCl
252    add_definitions(
253      -DBOOST_HAS_FILESYSTEM_V3=0
254      -D__INTEGRITY=1
255      )
256  else()
257    add_definitions(
258      -DBOOST_HAS_FILESYSTEM_V3=1
259      )
260    list(APPEND BOOST_SOURCES
261      ${BOOST_NAME}/libs/filesystem/src/codecvt_error_category.cpp
262      ${BOOST_NAME}/libs/filesystem/src/operations.cpp
263      ${BOOST_NAME}/libs/filesystem/src/path.cpp
264      ${BOOST_NAME}/libs/filesystem/src/path_traits.cpp
265      )
266
267    if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR
268        CMAKE_SYSTEM_NAME STREQUAL "OpenBSD" OR
269        CMAKE_SYSTEM_NAME STREQUAL "DragonFly")
270     list(APPEND BOOST_SOURCES
271        ${BOOST_SOURCES_DIR}/libs/filesystem/src/utf8_codecvt_facet.cpp
272        )
273
274    elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
275      list(APPEND BOOST_SOURCES
276        ${BOOST_NAME}/libs/filesystem/src/windows_file_codecvt.cpp
277        )
278    endif()
279  endif()
280
281  list(APPEND BOOST_SOURCES
282    ${BOOST_NAME}/libs/iostreams/src/file_descriptor.cpp
283    )
284
285
286  ##
287  ## Configuration of boost::locale
288  ##
289
290  if (NOT ENABLE_LOCALE)
291    message("boost::locale is disabled")
292  else()
293    set(BOOST_ICU_SOURCES
294      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/boundary.cpp
295      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/codecvt.cpp
296      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/collator.cpp
297      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/conversion.cpp
298      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/date_time.cpp
299      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/formatter.cpp
300      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/icu_backend.cpp
301      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/numeric.cpp
302      ${BOOST_SOURCES_DIR}/libs/locale/src/icu/time_zone.cpp
303      )
304
305    list(APPEND BOOST_SOURCES
306      ${BOOST_SOURCES_DIR}/libs/locale/src/encoding/codepage.cpp
307      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/generator.cpp
308      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/date_time.cpp
309      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/formatting.cpp
310      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/ids.cpp
311      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/localization_backend.cpp
312      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/message.cpp
313      ${BOOST_SOURCES_DIR}/libs/locale/src/shared/mo_lambda.cpp
314      ${BOOST_SOURCES_DIR}/libs/locale/src/util/codecvt_converter.cpp
315      ${BOOST_SOURCES_DIR}/libs/locale/src/util/default_locale.cpp
316      ${BOOST_SOURCES_DIR}/libs/locale/src/util/gregorian.cpp
317      ${BOOST_SOURCES_DIR}/libs/locale/src/util/info.cpp
318      ${BOOST_SOURCES_DIR}/libs/locale/src/util/locale_data.cpp
319      )
320
321    if (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD" OR
322        CMAKE_SYSTEM_VERSION STREQUAL "LinuxStandardBase")
323      add_definitions(
324        -DBOOST_LOCALE_NO_WINAPI_BACKEND=1
325        -DBOOST_LOCALE_NO_POSIX_BACKEND=1
326        )
327
328      list(APPEND BOOST_SOURCES
329        ${BOOST_SOURCES_DIR}/libs/locale/src/std/codecvt.cpp
330        ${BOOST_SOURCES_DIR}/libs/locale/src/std/collate.cpp
331        ${BOOST_SOURCES_DIR}/libs/locale/src/std/converter.cpp
332        ${BOOST_SOURCES_DIR}/libs/locale/src/std/numeric.cpp
333        ${BOOST_SOURCES_DIR}/libs/locale/src/std/std_backend.cpp
334        )
335
336      if (BOOST_LOCALE_BACKEND STREQUAL "gcc" OR
337          BOOST_LOCALE_BACKEND STREQUAL "libiconv")
338        add_definitions(-DBOOST_LOCALE_WITH_ICONV=1)
339      elseif (BOOST_LOCALE_BACKEND STREQUAL "icu")
340        add_definitions(-DBOOST_LOCALE_WITH_ICU=1)
341        list(APPEND BOOST_SOURCES ${BOOST_ICU_SOURCES})
342      else()
343        message(FATAL_ERROR "Unsupported value for BOOST_LOCALE_BACKEND: ${BOOST_LOCALE_BACKEND}")
344      endif()
345
346    elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" OR
347            CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR
348            CMAKE_SYSTEM_NAME STREQUAL "DragonFly" OR
349            CMAKE_SYSTEM_NAME STREQUAL "kFreeBSD" OR
350            CMAKE_SYSTEM_NAME STREQUAL "PNaCl" OR
351            CMAKE_SYSTEM_NAME STREQUAL "NaCl32" OR
352            CMAKE_SYSTEM_NAME STREQUAL "NaCl64" OR
353            CMAKE_SYSTEM_NAME STREQUAL "Emscripten") # For WebAssembly or asm.js
354      add_definitions(
355        -DBOOST_LOCALE_NO_WINAPI_BACKEND=1
356        -DBOOST_LOCALE_NO_STD_BACKEND=1
357        )
358
359      list(APPEND BOOST_SOURCES
360        ${BOOST_SOURCES_DIR}/libs/locale/src/posix/codecvt.cpp
361        ${BOOST_SOURCES_DIR}/libs/locale/src/posix/collate.cpp
362        ${BOOST_SOURCES_DIR}/libs/locale/src/posix/converter.cpp
363        ${BOOST_SOURCES_DIR}/libs/locale/src/posix/numeric.cpp
364        ${BOOST_SOURCES_DIR}/libs/locale/src/posix/posix_backend.cpp
365        )
366
367      if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten" OR
368          BOOST_LOCALE_BACKEND STREQUAL "gcc" OR
369          BOOST_LOCALE_BACKEND STREQUAL "libiconv")
370        # In WebAssembly or asm.js, we rely on the version of iconv
371        # that is shipped with the stdlib
372        add_definitions(-DBOOST_LOCALE_WITH_ICONV=1)
373      elseif (BOOST_LOCALE_BACKEND STREQUAL "icu")
374        add_definitions(-DBOOST_LOCALE_WITH_ICU=1)
375        list(APPEND BOOST_SOURCES ${BOOST_ICU_SOURCES})
376      else()
377        message(FATAL_ERROR "Unsupported value for BOOST_LOCALE_BACKEND: ${BOOST_LOCALE_BACKEND}")
378      endif()
379
380    elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
381      add_definitions(
382        -DBOOST_LOCALE_NO_POSIX_BACKEND=1
383        -DBOOST_LOCALE_NO_STD_BACKEND=1
384        )
385
386      list(APPEND BOOST_SOURCES
387        ${BOOST_SOURCES_DIR}/libs/locale/src/win32/collate.cpp
388        ${BOOST_SOURCES_DIR}/libs/locale/src/win32/converter.cpp
389        ${BOOST_SOURCES_DIR}/libs/locale/src/win32/lcid.cpp
390        ${BOOST_SOURCES_DIR}/libs/locale/src/win32/numeric.cpp
391        ${BOOST_SOURCES_DIR}/libs/locale/src/win32/win_backend.cpp
392        )
393
394      # Starting with release 0.8.2, Orthanc statically links against
395      # libiconv on Windows. Indeed, the "WCONV" library of Windows XP
396      # seems not to support properly several codepages (notably
397      # "Latin3", "Hebrew", and "Arabic"). Set "BOOST_LOCALE_BACKEND"
398      # to "wconv" to use WCONV anyway.
399
400      if (BOOST_LOCALE_BACKEND STREQUAL "libiconv")
401        add_definitions(-DBOOST_LOCALE_WITH_ICONV=1)
402      elseif (BOOST_LOCALE_BACKEND STREQUAL "icu")
403        add_definitions(-DBOOST_LOCALE_WITH_ICU=1)
404        list(APPEND BOOST_SOURCES ${BOOST_ICU_SOURCES})
405      elseif (BOOST_LOCALE_BACKEND STREQUAL "wconv")
406        message("Using Window's wconv")
407        add_definitions(-DBOOST_LOCALE_WITH_WCONV=1)
408      else()
409        message(FATAL_ERROR "Unsupported value for BOOST_LOCALE_BACKEND on Windows: ${BOOST_LOCALE_BACKEND}")
410      endif()
411
412    else()
413      message(FATAL_ERROR "Support your platform here")
414    endif()
415  endif()
416
417
418  source_group(ThirdParty\\boost REGULAR_EXPRESSION ${BOOST_SOURCES_DIR}/.*)
419
420endif()
421