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
21
22##
23## Check whether the parent script sets the mandatory variables
24##
25
26if (NOT DEFINED ORTHANC_FRAMEWORK_SOURCE OR
27    (NOT ORTHANC_FRAMEWORK_SOURCE STREQUAL "system" AND
28     NOT ORTHANC_FRAMEWORK_SOURCE STREQUAL "hg" AND
29     NOT ORTHANC_FRAMEWORK_SOURCE STREQUAL "web" AND
30     NOT ORTHANC_FRAMEWORK_SOURCE STREQUAL "archive" AND
31     NOT ORTHANC_FRAMEWORK_SOURCE STREQUAL "path"))
32  message(FATAL_ERROR "The variable ORTHANC_FRAMEWORK_SOURCE must be set to \"system\", \"hg\", \"web\", \"archive\" or \"path\"")
33endif()
34
35
36##
37## Detection of the requested version
38##
39
40if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "hg" OR
41    ORTHANC_FRAMEWORK_SOURCE STREQUAL "archive" OR
42    ORTHANC_FRAMEWORK_SOURCE STREQUAL "web")
43  if (NOT DEFINED ORTHANC_FRAMEWORK_VERSION)
44    message(FATAL_ERROR "The variable ORTHANC_FRAMEWORK_VERSION must be set")
45  endif()
46
47  if (DEFINED ORTHANC_FRAMEWORK_MAJOR OR
48      DEFINED ORTHANC_FRAMEWORK_MINOR OR
49      DEFINED ORTHANC_FRAMEWORK_REVISION OR
50      DEFINED ORTHANC_FRAMEWORK_MD5)
51    message(FATAL_ERROR "Some internal variable has been set")
52  endif()
53
54  set(ORTHANC_FRAMEWORK_MD5 "")
55
56  if (NOT DEFINED ORTHANC_FRAMEWORK_BRANCH)
57    if (ORTHANC_FRAMEWORK_VERSION STREQUAL "mainline")
58      set(ORTHANC_FRAMEWORK_BRANCH "default")
59      set(ORTHANC_FRAMEWORK_MAJOR 999)
60      set(ORTHANC_FRAMEWORK_MINOR 999)
61      set(ORTHANC_FRAMEWORK_REVISION 999)
62
63    else()
64      set(ORTHANC_FRAMEWORK_BRANCH "Orthanc-${ORTHANC_FRAMEWORK_VERSION}")
65
66      set(RE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$")
67      string(REGEX REPLACE ${RE} "\\1" ORTHANC_FRAMEWORK_MAJOR ${ORTHANC_FRAMEWORK_VERSION})
68      string(REGEX REPLACE ${RE} "\\2" ORTHANC_FRAMEWORK_MINOR ${ORTHANC_FRAMEWORK_VERSION})
69      string(REGEX REPLACE ${RE} "\\3" ORTHANC_FRAMEWORK_REVISION ${ORTHANC_FRAMEWORK_VERSION})
70
71      if (NOT ORTHANC_FRAMEWORK_MAJOR MATCHES "^[0-9]+$" OR
72          NOT ORTHANC_FRAMEWORK_MINOR MATCHES "^[0-9]+$" OR
73          NOT ORTHANC_FRAMEWORK_REVISION MATCHES "^[0-9]+$")
74        message("Bad version of the Orthanc framework: ${ORTHANC_FRAMEWORK_VERSION}")
75      endif()
76
77      if (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.3.1")
78        set(ORTHANC_FRAMEWORK_MD5 "dac95bd6cf86fb19deaf4e612961f378")
79      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.3.2")
80        set(ORTHANC_FRAMEWORK_MD5 "d0ccdf68e855d8224331f13774992750")
81      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.4.0")
82        set(ORTHANC_FRAMEWORK_MD5 "81e15f34d97ac32bbd7d26e85698835a")
83      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.4.1")
84        set(ORTHANC_FRAMEWORK_MD5 "9b6f6114264b17ed421b574cd6476127")
85      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.4.2")
86        set(ORTHANC_FRAMEWORK_MD5 "d1ee84927dcf668e60eb5868d24b9394")
87      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.5.0")
88        set(ORTHANC_FRAMEWORK_MD5 "4429d8d9dea4ff6648df80ec3c64d79e")
89      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.5.1")
90        set(ORTHANC_FRAMEWORK_MD5 "099671538865e5da96208b37494d6718")
91      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.5.2")
92        set(ORTHANC_FRAMEWORK_MD5 "8867050f3e9a1ce6157c1ea7a9433b1b")
93      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.5.3")
94        set(ORTHANC_FRAMEWORK_MD5 "bf2f5ed1adb8b0fc5f10d278e68e1dfe")
95      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.5.4")
96        set(ORTHANC_FRAMEWORK_MD5 "404baef5d4c43e7c5d9410edda8ef5a5")
97      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.5.5")
98        set(ORTHANC_FRAMEWORK_MD5 "cfc437e0687ae4bd725fd93dc1f08bc4")
99      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.5.6")
100        set(ORTHANC_FRAMEWORK_MD5 "3c29de1e289b5472342947168f0105c0")
101      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.5.7")
102        set(ORTHANC_FRAMEWORK_MD5 "e1b76f01116d9b5d4ac8cc39980560e3")
103      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.5.8")
104        set(ORTHANC_FRAMEWORK_MD5 "82323e8c49a667f658a3639ea4dbc336")
105      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.6.0")
106        set(ORTHANC_FRAMEWORK_MD5 "eab428d6e53f61e847fa360bb17ebe25")
107      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.6.1")
108        set(ORTHANC_FRAMEWORK_MD5 "3971f5de96ba71dc9d3f3690afeaa7c0")
109      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.7.0")
110        set(ORTHANC_FRAMEWORK_MD5 "ce5f689e852b01d3672bd3d2f952a5ef")
111      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.7.1")
112        set(ORTHANC_FRAMEWORK_MD5 "3c171217f930abe80246997bdbcaf7cc")
113      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.7.2")
114        set(ORTHANC_FRAMEWORK_MD5 "328f94dcbd78c169655a13f7ad58a2c2")
115      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.7.3")
116        set(ORTHANC_FRAMEWORK_MD5 "3f1ba9502ec7c5449971d3b56087bcde")
117      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.7.4")
118        set(ORTHANC_FRAMEWORK_MD5 "19fcb7c21876af86546baa048a22c6c0")
119      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.8.0")
120        set(ORTHANC_FRAMEWORK_MD5 "f8ec7554ef5d23ea4ce474b1e8214de9")
121      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.8.1")
122        set(ORTHANC_FRAMEWORK_MD5 "db094f96399cbe8b9bbdbce34884c220")
123      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.8.2")
124        set(ORTHANC_FRAMEWORK_MD5 "8bfa10e66c9931e74111be0bfb1f4548")
125      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.9.0")
126        set(ORTHANC_FRAMEWORK_MD5 "cea0b02ce184671eaf1bd668beefbf28")
127      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.9.1")
128        set(ORTHANC_FRAMEWORK_MD5 "08eebc66ef93c3b40115c38501db5fbd")
129      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "1.9.2")
130        set(ORTHANC_FRAMEWORK_MD5 "3ea66c09f64aca990016683b6375734e")
131
132      # Below this point are development snapshots that were used to
133      # release some plugin, before an official release of the Orthanc
134      # framework was available. Here is the command to be used to
135      # generate a proper archive:
136      #
137      #   $ hg archive /tmp/Orthanc-`hg id -i | sed 's/\+//'`.tar.gz
138      #
139      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "ae0e3fd609df")
140        # DICOMweb 1.1 (framework pre-1.6.0)
141        set(ORTHANC_FRAMEWORK_MD5 "7e09e9b530a2f527854f0b782d7e0645")
142      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "82652c5fc04f")
143        # Stone Web viewer 1.0 (framework pre-1.8.1)
144        set(ORTHANC_FRAMEWORK_MD5 "d77331d68917e66a3f4f9b807bbdab7f")
145      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "4a3ba4bf4ba7")
146        # PostgreSQL 3.3 (framework pre-1.8.2)
147        set(ORTHANC_FRAMEWORK_MD5 "2d82bddf06f9cfe82095495cb3b8abde")
148      elseif (ORTHANC_FRAMEWORK_VERSION STREQUAL "23ad1b9c7800")
149        # For "Toolbox::ReadJson()" and "Toolbox::Write{...}Json()" (pre-1.9.0)
150        set(ORTHANC_FRAMEWORK_MD5 "9af92080e57c60dd288eba46ce606c00")
151      endif()
152    endif()
153  endif()
154
155elseif (ORTHANC_FRAMEWORK_SOURCE STREQUAL "path")
156  message("Using the Orthanc framework from a path of the filesystem. Assuming mainline version.")
157  set(ORTHANC_FRAMEWORK_MAJOR 999)
158  set(ORTHANC_FRAMEWORK_MINOR 999)
159  set(ORTHANC_FRAMEWORK_REVISION 999)
160endif()
161
162
163
164##
165## Detection of the third-party software
166##
167
168if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "hg")
169  find_program(ORTHANC_FRAMEWORK_HG hg)
170
171  if (${ORTHANC_FRAMEWORK_HG} MATCHES "ORTHANC_FRAMEWORK_HG-NOTFOUND")
172    message(FATAL_ERROR "Please install Mercurial")
173  endif()
174endif()
175
176
177if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "archive" OR
178    ORTHANC_FRAMEWORK_SOURCE STREQUAL "web")
179  if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
180    find_program(ORTHANC_FRAMEWORK_7ZIP 7z
181      PATHS
182      "$ENV{ProgramFiles}/7-Zip"
183      "$ENV{ProgramW6432}/7-Zip"
184      )
185
186    if (${ORTHANC_FRAMEWORK_7ZIP} MATCHES "ORTHANC_FRAMEWORK_7ZIP-NOTFOUND")
187      message(FATAL_ERROR "Please install the '7-zip' software (http://www.7-zip.org/)")
188    endif()
189
190  else()
191    find_program(ORTHANC_FRAMEWORK_TAR tar)
192    if (${ORTHANC_FRAMEWORK_TAR} MATCHES "ORTHANC_FRAMEWORK_TAR-NOTFOUND")
193      message(FATAL_ERROR "Please install the 'tar' package")
194    endif()
195  endif()
196endif()
197
198
199
200##
201## Case of the Orthanc framework specified as a path on the filesystem
202##
203
204if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "path")
205  if (NOT DEFINED ORTHANC_FRAMEWORK_ROOT OR
206      ORTHANC_FRAMEWORK_ROOT STREQUAL "")
207    message(FATAL_ERROR "The variable ORTHANC_FRAMEWORK_ROOT must provide the path to the sources of Orthanc")
208  endif()
209
210  if (NOT EXISTS ${ORTHANC_FRAMEWORK_ROOT})
211    message(FATAL_ERROR "Non-existing directory: ${ORTHANC_FRAMEWORK_ROOT}")
212  endif()
213endif()
214
215
216
217##
218## Case of the Orthanc framework cloned using Mercurial
219##
220
221if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "hg")
222  if (NOT STATIC_BUILD AND NOT ALLOW_DOWNLOADS)
223    message(FATAL_ERROR "CMake is not allowed to download from Internet. Please set the ALLOW_DOWNLOADS option to ON")
224  endif()
225
226  set(ORTHANC_ROOT ${CMAKE_BINARY_DIR}/orthanc)
227
228  if (EXISTS ${ORTHANC_ROOT})
229    message("Updating the Orthanc source repository using Mercurial")
230    execute_process(
231      COMMAND ${ORTHANC_FRAMEWORK_HG} pull
232      WORKING_DIRECTORY ${ORTHANC_ROOT}
233      RESULT_VARIABLE Failure
234      )
235  else()
236    message("Forking the Orthanc source repository using Mercurial")
237    execute_process(
238      COMMAND ${ORTHANC_FRAMEWORK_HG} clone "https://hg.orthanc-server.com/orthanc/"
239      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
240      RESULT_VARIABLE Failure
241      )
242  endif()
243
244  if (Failure OR NOT EXISTS ${ORTHANC_ROOT})
245    message(FATAL_ERROR "Cannot fork the Orthanc repository")
246  endif()
247
248  message("Setting branch of the Orthanc repository to: ${ORTHANC_FRAMEWORK_BRANCH}")
249
250  execute_process(
251    COMMAND ${ORTHANC_FRAMEWORK_HG} update -c ${ORTHANC_FRAMEWORK_BRANCH}
252    WORKING_DIRECTORY ${ORTHANC_ROOT}
253    RESULT_VARIABLE Failure
254    )
255
256  if (Failure)
257    message(FATAL_ERROR "Error while running Mercurial")
258  endif()
259endif()
260
261
262
263##
264## Case of the Orthanc framework provided as a source archive on the
265## filesystem
266##
267
268if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "archive")
269  if (NOT DEFINED ORTHANC_FRAMEWORK_ARCHIVE OR
270      ORTHANC_FRAMEWORK_ARCHIVE STREQUAL "")
271    message(FATAL_ERROR "The variable ORTHANC_FRAMEWORK_ARCHIVE must provide the path to the sources of Orthanc")
272  endif()
273endif()
274
275
276
277##
278## Case of the Orthanc framework downloaded from the Web
279##
280
281if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "web")
282  if (DEFINED ORTHANC_FRAMEWORK_URL)
283    string(REGEX REPLACE "^.*/" "" ORTHANC_FRAMEMORK_FILENAME "${ORTHANC_FRAMEWORK_URL}")
284  else()
285    # Default case: Download from the official Web site
286    set(ORTHANC_FRAMEMORK_FILENAME Orthanc-${ORTHANC_FRAMEWORK_VERSION}.tar.gz)
287    set(ORTHANC_FRAMEWORK_URL "http://orthanc.osimis.io/ThirdPartyDownloads/orthanc-framework/${ORTHANC_FRAMEMORK_FILENAME}")
288  endif()
289
290  set(ORTHANC_FRAMEWORK_ARCHIVE "${CMAKE_SOURCE_DIR}/ThirdPartyDownloads/${ORTHANC_FRAMEMORK_FILENAME}")
291
292  if (NOT EXISTS "${ORTHANC_FRAMEWORK_ARCHIVE}")
293    if (NOT STATIC_BUILD AND NOT ALLOW_DOWNLOADS)
294      message(FATAL_ERROR "CMake is not allowed to download from Internet. Please set the ALLOW_DOWNLOADS option to ON")
295    endif()
296
297    message("Downloading: ${ORTHANC_FRAMEWORK_URL}")
298
299    file(DOWNLOAD
300      "${ORTHANC_FRAMEWORK_URL}" "${ORTHANC_FRAMEWORK_ARCHIVE}"
301      SHOW_PROGRESS EXPECTED_MD5 "${ORTHANC_FRAMEWORK_MD5}"
302      TIMEOUT 60
303      INACTIVITY_TIMEOUT 60
304      )
305  else()
306    message("Using local copy of: ${ORTHANC_FRAMEWORK_URL}")
307  endif()
308endif()
309
310
311
312
313##
314## Uncompressing the Orthanc framework, if it was retrieved from a
315## source archive on the filesystem, or from the official Web site
316##
317
318if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "archive" OR
319    ORTHANC_FRAMEWORK_SOURCE STREQUAL "web")
320
321  if (NOT DEFINED ORTHANC_FRAMEWORK_ARCHIVE OR
322      NOT DEFINED ORTHANC_FRAMEWORK_VERSION OR
323      NOT DEFINED ORTHANC_FRAMEWORK_MD5)
324    message(FATAL_ERROR "Internal error")
325  endif()
326
327  if (ORTHANC_FRAMEWORK_MD5 STREQUAL "")
328    message(FATAL_ERROR "Unknown release of Orthanc: ${ORTHANC_FRAMEWORK_VERSION}")
329  endif()
330
331  file(MD5 ${ORTHANC_FRAMEWORK_ARCHIVE} ActualMD5)
332
333  if (NOT "${ActualMD5}" STREQUAL "${ORTHANC_FRAMEWORK_MD5}")
334    message(FATAL_ERROR "The MD5 hash of the Orthanc archive is invalid: ${ORTHANC_FRAMEWORK_ARCHIVE}")
335  endif()
336
337  set(ORTHANC_ROOT "${CMAKE_BINARY_DIR}/Orthanc-${ORTHANC_FRAMEWORK_VERSION}")
338
339  if (NOT IS_DIRECTORY "${ORTHANC_ROOT}")
340    if (NOT ORTHANC_FRAMEWORK_ARCHIVE MATCHES ".tar.gz$")
341      message(FATAL_ERROR "Archive should have the \".tar.gz\" extension: ${ORTHANC_FRAMEWORK_ARCHIVE}")
342    endif()
343
344    message("Uncompressing: ${ORTHANC_FRAMEWORK_ARCHIVE}")
345
346    if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
347      # How to silently extract files using 7-zip
348      # http://superuser.com/questions/331148/7zip-command-line-extract-silently-quietly
349
350      execute_process(
351        COMMAND ${ORTHANC_FRAMEWORK_7ZIP} e -y ${ORTHANC_FRAMEWORK_ARCHIVE}
352        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
353        RESULT_VARIABLE Failure
354        OUTPUT_QUIET
355        )
356
357      if (Failure)
358        message(FATAL_ERROR "Error while running the uncompression tool")
359      endif()
360
361      get_filename_component(TMP_FILENAME "${ORTHANC_FRAMEWORK_ARCHIVE}" NAME)
362      string(REGEX REPLACE ".gz$" "" TMP_FILENAME2 "${TMP_FILENAME}")
363
364      execute_process(
365        COMMAND ${ORTHANC_FRAMEWORK_7ZIP} x -y ${TMP_FILENAME2}
366        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
367        RESULT_VARIABLE Failure
368        OUTPUT_QUIET
369        )
370
371    else()
372      execute_process(
373        COMMAND sh -c "${ORTHANC_FRAMEWORK_TAR} xfz ${ORTHANC_FRAMEWORK_ARCHIVE}"
374        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
375        RESULT_VARIABLE Failure
376        )
377    endif()
378
379    if (Failure)
380      message(FATAL_ERROR "Error while running the uncompression tool")
381    endif()
382
383    if (NOT IS_DIRECTORY "${ORTHANC_ROOT}")
384      message(FATAL_ERROR "The Orthanc framework was not uncompressed at the proper location. Check the CMake instructions.")
385    endif()
386  endif()
387endif()
388
389
390
391##
392## Determine the path to the sources of the Orthanc framework
393##
394
395if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "archive" OR
396    ORTHANC_FRAMEWORK_SOURCE STREQUAL "hg" OR
397    ORTHANC_FRAMEWORK_SOURCE STREQUAL "web")
398  if (NOT DEFINED ORTHANC_ROOT OR
399      NOT DEFINED ORTHANC_FRAMEWORK_MAJOR OR
400      NOT DEFINED ORTHANC_FRAMEWORK_MINOR OR
401      NOT DEFINED ORTHANC_FRAMEWORK_REVISION)
402    message(FATAL_ERROR "Internal error in the DownloadOrthancFramework.cmake file")
403  endif()
404
405  unset(ORTHANC_FRAMEWORK_ROOT CACHE)
406
407  if ("${ORTHANC_FRAMEWORK_MAJOR}.${ORTHANC_FRAMEWORK_MINOR}.${ORTHANC_FRAMEWORK_REVISION}" VERSION_LESS "1.7.2")
408    set(ORTHANC_FRAMEWORK_ROOT "${ORTHANC_ROOT}/Core" CACHE
409      STRING "Path to the Orthanc framework source directory")
410    set(ENABLE_PLUGINS_VERSION_SCRIPT OFF)
411  else()
412    set(ORTHANC_FRAMEWORK_ROOT "${ORTHANC_ROOT}/OrthancFramework/Sources" CACHE
413      STRING "Path to the Orthanc framework source directory")
414  endif()
415
416  unset(ORTHANC_ROOT)
417endif()
418
419if (NOT ORTHANC_FRAMEWORK_SOURCE STREQUAL "system")
420  if (NOT EXISTS ${ORTHANC_FRAMEWORK_ROOT}/OrthancException.h OR
421      NOT EXISTS ${ORTHANC_FRAMEWORK_ROOT}/../Resources/CMake/OrthancFrameworkParameters.cmake)
422    message(FATAL_ERROR "Directory not containing the source code of the Orthanc framework: ${ORTHANC_FRAMEWORK_ROOT}")
423  endif()
424endif()
425
426
427
428##
429## Case of the Orthanc framework installed as a shared library in a
430## GNU/Linux distribution (typically Debian). New in Orthanc 1.7.2.
431##
432
433if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "system")
434  set(ORTHANC_FRAMEWORK_LIBDIR "" CACHE PATH "")
435  set(ORTHANC_FRAMEWORK_USE_SHARED ON CACHE BOOL "Whether to use the shared library or the static library")
436  set(ORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES "" CACHE STRING "Additional libraries to link against, separated by whitespaces, typically needed if using the static library (a common minimal value is \"boost_filesystem boost_iostreams boost_locale boost_regex boost_thread jsoncpp pugixml uuid\")")
437
438  if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND
439      CMAKE_COMPILER_IS_GNUCXX) # MinGW
440    set(DYNAMIC_MINGW_STDLIB ON)   # Disable static linking against libc (to throw exceptions)
441    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++")
442    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++")
443  endif()
444
445  include(CheckIncludeFile)
446  include(CheckIncludeFileCXX)
447  include(FindPythonInterp)
448  include(${CMAKE_CURRENT_LIST_DIR}/Compiler.cmake)
449  include(${CMAKE_CURRENT_LIST_DIR}/DownloadPackage.cmake)
450  include(${CMAKE_CURRENT_LIST_DIR}/AutoGeneratedCode.cmake)
451  set(EMBED_RESOURCES_PYTHON ${CMAKE_CURRENT_LIST_DIR}/EmbedResources.py)
452
453  if (ORTHANC_FRAMEWORK_USE_SHARED)
454    list(GET CMAKE_FIND_LIBRARY_PREFIXES 0 Prefix)
455    list(GET CMAKE_FIND_LIBRARY_SUFFIXES 0 Suffix)
456  else()
457    list(GET CMAKE_FIND_LIBRARY_PREFIXES 0 Prefix)
458    list(GET CMAKE_FIND_LIBRARY_SUFFIXES 1 Suffix)
459  endif()
460
461  # The "OrthancFramework" library must be the first one to be included
462  if ("${ORTHANC_FRAMEWORK_LIBDIR}" STREQUAL "")
463    set(ORTHANC_FRAMEWORK_LIBRARIES ${Prefix}OrthancFramework${Suffix})
464  else ()
465    set(ORTHANC_FRAMEWORK_LIBRARIES ${ORTHANC_FRAMEWORK_LIBDIR}/${Prefix}OrthancFramework${Suffix})
466  endif()
467
468  if (NOT ORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES STREQUAL "")
469    # https://stackoverflow.com/a/5272993/881731
470    string(REPLACE " " ";" tmp ${ORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES})
471    list(APPEND ORTHANC_FRAMEWORK_LIBRARIES ${tmp})
472  endif()
473
474  # Look for the version of the mandatory dependency JsonCpp (cf. JsonCppConfiguration.cmake)
475  if (CMAKE_CROSSCOMPILING)
476    set(JSONCPP_INCLUDE_DIR ${ORTHANC_FRAMEWORK_ROOT}/..)
477  else()
478    find_path(JSONCPP_INCLUDE_DIR json/reader.h
479      ${ORTHANC_FRAMEWORK_ROOT}/..
480      /usr/include/jsoncpp
481      /usr/local/include/jsoncpp
482      )
483  endif()
484
485  message("JsonCpp include dir: ${JSONCPP_INCLUDE_DIR}")
486  include_directories(${JSONCPP_INCLUDE_DIR})
487
488  CHECK_INCLUDE_FILE_CXX(${JSONCPP_INCLUDE_DIR}/json/reader.h HAVE_JSONCPP_H)
489  if (NOT HAVE_JSONCPP_H)
490    message(FATAL_ERROR "Please install the libjsoncpp-dev package")
491  endif()
492
493  # Switch to the C++11 standard if the version of JsonCpp is 1.y.z
494  # (same as variable JSONCPP_CXX11 in the source code of Orthanc)
495  if (EXISTS ${JSONCPP_INCLUDE_DIR}/json/version.h)
496    file(STRINGS
497      "${JSONCPP_INCLUDE_DIR}/json/version.h"
498      JSONCPP_VERSION_MAJOR1 REGEX
499      ".*define JSONCPP_VERSION_MAJOR.*")
500
501    if (NOT JSONCPP_VERSION_MAJOR1)
502      message(FATAL_ERROR "Unable to extract the major version of JsonCpp")
503    endif()
504
505    string(REGEX REPLACE
506      ".*JSONCPP_VERSION_MAJOR.*([0-9]+)$" "\\1"
507      JSONCPP_VERSION_MAJOR ${JSONCPP_VERSION_MAJOR1})
508    message("JsonCpp major version: ${JSONCPP_VERSION_MAJOR}")
509
510    if (JSONCPP_VERSION_MAJOR GREATER 0)
511      message("Switching to C++11 standard, as version of JsonCpp is >= 1.0.0")
512      if (CMAKE_COMPILER_IS_GNUCXX)
513        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
514      elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
515        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
516      endif()
517    endif()
518  else()
519    message("Unable to detect the major version of JsonCpp, assuming < 1.0.0")
520  endif()
521
522  # Look for Orthanc framework shared library
523  include(CheckCXXSymbolExists)
524
525  if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
526    set(ORTHANC_FRAMEWORK_INCLUDE_DIR ${ORTHANC_FRAMEWORK_ROOT})
527  else()
528    find_path(ORTHANC_FRAMEWORK_INCLUDE_DIR OrthancFramework.h
529      /usr/include/orthanc-framework
530      /usr/local/include/orthanc-framework
531      ${ORTHANC_FRAMEWORK_ROOT}
532      )
533  endif()
534
535  if (${ORTHANC_FRAMEWORK_INCLUDE_DIR} STREQUAL "ORTHANC_FRAMEWORK_INCLUDE_DIR-NOTFOUND")
536    message(FATAL_ERROR "Cannot locate the OrthancFramework.h header")
537  endif()
538
539  message("Orthanc framework include dir: ${ORTHANC_FRAMEWORK_INCLUDE_DIR}")
540  include_directories(${ORTHANC_FRAMEWORK_INCLUDE_DIR})
541
542  if (ORTHANC_FRAMEWORK_USE_SHARED)
543    set(CMAKE_REQUIRED_INCLUDES "${ORTHANC_FRAMEWORK_INCLUDE_DIR}")
544    set(CMAKE_REQUIRED_LIBRARIES "${ORTHANC_FRAMEWORK_LIBRARIES}")
545
546    check_cxx_symbol_exists("Orthanc::InitializeFramework" "OrthancFramework.h" HAVE_ORTHANC_FRAMEWORK)
547    if (NOT HAVE_ORTHANC_FRAMEWORK)
548      message(FATAL_ERROR "Cannot find the Orthanc framework")
549    endif()
550
551    unset(CMAKE_REQUIRED_INCLUDES)
552    unset(CMAKE_REQUIRED_LIBRARIES)
553  endif()
554endif()
555