comparison Resources/Orthanc/CMake/Compiler.cmake @ 266:4e9d30c19b4b

linking against orthanc framework library
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 12:32:46 +0200
parents
children 938257eebc03
comparison
equal deleted inserted replaced
265:af7108b06b4d 266:4e9d30c19b4b
1 # This file sets all the compiler-related flags
2
3 if ((CMAKE_CROSSCOMPILING AND NOT
4 "${CMAKE_SYSTEM_VERSION}" STREQUAL "CrossToolNg") OR
5 "${CMAKE_SYSTEM_VERSION}" STREQUAL "LinuxStandardBase")
6 # Cross-compilation necessarily implies standalone and static build
7 SET(STATIC_BUILD ON)
8 SET(STANDALONE_BUILD ON)
9 endif()
10
11
12 if ("${CMAKE_SYSTEM_VERSION}" STREQUAL "LinuxStandardBase")
13 # Cache the environment variables "LSB_CC" and "LSB_CXX" for further
14 # use by "ExternalProject" in CMake
15 SET(CMAKE_LSB_CC $ENV{LSB_CC} CACHE STRING "")
16 SET(CMAKE_LSB_CXX $ENV{LSB_CXX} CACHE STRING "")
17 endif()
18
19
20 if (CMAKE_COMPILER_IS_GNUCXX)
21 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-long-long")
22
23 # --std=c99 makes libcurl not to compile
24 # -pedantic gives a lot of warnings on OpenSSL
25 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -Wno-variadic-macros")
26
27 if (CMAKE_CROSSCOMPILING)
28 # http://stackoverflow.com/a/3543845/881731
29 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff -I<CMAKE_CURRENT_SOURCE_DIR> <SOURCE> <OBJECT>")
30 endif()
31
32 elseif (MSVC)
33 # Use static runtime under Visual Studio
34 # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace
35 # http://stackoverflow.com/a/6510446
36 foreach(flag_var
37 CMAKE_C_FLAGS_DEBUG
38 CMAKE_CXX_FLAGS_DEBUG
39 CMAKE_C_FLAGS_RELEASE
40 CMAKE_CXX_FLAGS_RELEASE
41 CMAKE_C_FLAGS_MINSIZEREL
42 CMAKE_CXX_FLAGS_MINSIZEREL
43 CMAKE_C_FLAGS_RELWITHDEBINFO
44 CMAKE_CXX_FLAGS_RELWITHDEBINFO)
45 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
46 string(REGEX REPLACE "/MDd" "/MTd" ${flag_var} "${${flag_var}}")
47 endforeach(flag_var)
48
49 # Add /Zm256 compiler option to Visual Studio to fix PCH errors
50 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zm256")
51
52 # New in Orthanc 1.5.5
53 if (MSVC_MULTIPLE_PROCESSES)
54 # "If you omit the processMax argument in the /MP option, the
55 # compiler obtains the number of effective processors from the
56 # operating system, and then creates one process per effective
57 # processor"
58 # https://blog.kitware.com/cmake-building-with-all-your-cores/
59 # https://docs.microsoft.com/en-us/cpp/build/reference/mp-build-with-multiple-processes
60 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
61 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
62 endif()
63
64 add_definitions(
65 -D_CRT_SECURE_NO_WARNINGS=1
66 -D_CRT_SECURE_NO_DEPRECATE=1
67 )
68
69 if (MSVC_VERSION LESS 1600)
70 # Starting with Visual Studio >= 2010 (i.e. macro _MSC_VER >=
71 # 1600), Microsoft ships a standard-compliant <stdint.h>
72 # header. For earlier versions of Visual Studio, give access to a
73 # compatibility header.
74 # http://stackoverflow.com/a/70630/881731
75 # https://en.wikibooks.org/wiki/C_Programming/C_Reference/stdint.h#External_links
76 include_directories(${ORTHANC_ROOT}/Resources/ThirdParty/VisualStudio)
77 endif()
78
79 link_libraries(netapi32)
80 endif()
81
82
83 if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" OR
84 ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
85 # In FreeBSD/OpenBSD, the "/usr/local/" folder contains the ports and need to be imported
86 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -I/usr/local/include")
87 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/local/include")
88 SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib")
89 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -L/usr/local/lib")
90 endif()
91
92
93 if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR
94 ${CMAKE_SYSTEM_NAME} STREQUAL "kFreeBSD" OR
95 ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" OR
96 ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
97
98 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD" AND
99 NOT ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
100 # The "--no-undefined" linker flag makes the shared libraries
101 # (plugins ModalityWorklists and ServeFolders) fail to compile on
102 # OpenBSD, and make the PostgreSQL plugin complain about missing
103 # "environ" global variable in FreeBSD
104 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--no-undefined")
105 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
106 endif()
107
108 if (NOT DEFINED ENABLE_PLUGINS_VERSION_SCRIPT OR
109 ENABLE_PLUGINS_VERSION_SCRIPT)
110 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--version-script=${ORTHANC_ROOT}/Plugins/Samples/Common/VersionScript.map")
111 endif()
112
113 # Remove the "-rdynamic" option
114 # http://www.mail-archive.com/cmake@cmake.org/msg08837.html
115 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
116 link_libraries(pthread)
117
118 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
119 link_libraries(rt)
120 endif()
121
122 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" AND
123 NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
124 link_libraries(dl)
125 endif()
126
127 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" AND
128 NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
129 # The "--as-needed" linker flag is not available on FreeBSD and OpenBSD
130 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--as-needed")
131 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--as-needed")
132 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed")
133 endif()
134
135 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" AND
136 NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
137 # FreeBSD/OpenBSD have just one single interface for file
138 # handling, which is 64bit clean, so there is no need to define macro
139 # for LFS (Large File Support).
140 # https://ohse.de/uwe/articles/lfs.html
141 add_definitions(
142 -D_LARGEFILE64_SOURCE=1
143 -D_FILE_OFFSET_BITS=64
144 )
145 endif()
146
147 elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
148 if (MSVC)
149 message("MSVC compiler version = " ${MSVC_VERSION} "\n")
150 # Starting Visual Studio 2013 (version 1800), it is not possible
151 # to target Windows XP anymore
152 if (MSVC_VERSION LESS 1800)
153 add_definitions(
154 -DWINVER=0x0501
155 -D_WIN32_WINNT=0x0501
156 )
157 endif()
158 else()
159 add_definitions(
160 -DWINVER=0x0501
161 -D_WIN32_WINNT=0x0501
162 )
163 endif()
164
165 add_definitions(
166 -D_CRT_SECURE_NO_WARNINGS=1
167 )
168 link_libraries(rpcrt4 ws2_32)
169
170 if (CMAKE_COMPILER_IS_GNUCXX)
171 # Some additional C/C++ compiler flags for MinGW
172 SET(MINGW_NO_WARNINGS "-Wno-unused-function -Wno-unused-variable")
173 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MINGW_NO_WARNINGS} -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast")
174 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MINGW_NO_WARNINGS}")
175
176 if (DYNAMIC_MINGW_STDLIB)
177 else()
178 # This is a patch for MinGW64
179 SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-multiple-definition -static-libgcc -static-libstdc++")
180 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--allow-multiple-definition -static-libgcc -static-libstdc++")
181 endif()
182
183 CHECK_LIBRARY_EXISTS(winpthread pthread_create "" HAVE_WIN_PTHREAD)
184 if (HAVE_WIN_PTHREAD)
185 if (DYNAMIC_MINGW_STDLIB)
186 else()
187 # This line is necessary to compile with recent versions of MinGW,
188 # otherwise "libwinpthread-1.dll" is not statically linked.
189 SET(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic")
190 endif()
191 add_definitions(-DHAVE_WIN_PTHREAD=1)
192 else()
193 add_definitions(-DHAVE_WIN_PTHREAD=0)
194 endif()
195 endif()
196
197 elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
198 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -exported_symbols_list ${ORTHANC_ROOT}/Plugins/Samples/Common/ExportedSymbols.list")
199
200 add_definitions(
201 -D_XOPEN_SOURCE=1
202 )
203 link_libraries(iconv)
204
205 elseif (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
206 message("Building using Emscripten (for WebAssembly or asm.js targets)")
207
208 # The BINARYEN_TRAP_MODE specifies what to do when divisions per
209 # zero (and similar conditions like integer overflows) are
210 # encountered: The "clamp" mode avoids throwing errors, as they
211 # cannot be properly catched by "try {} catch (...)" constructions.
212 # Setting this option to "ON" fixes error: "shared:ERROR:
213 # BINARYEN_TRAP_MODE is not supported by the LLVM wasm backend" if
214 # using the "upstream" backend of Emscripten.
215 if (NOT EMSCRIPTEN_SET_LLVM_WASM_BACKEND)
216 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s BINARYEN_TRAP_MODE='\"clamp\"'")
217 endif()
218
219 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"ccall\", \"cwrap\"]'")
220
221 elseif (CMAKE_SYSTEM_NAME STREQUAL "Android")
222
223 else()
224 message("Unknown target platform: ${CMAKE_SYSTEM_NAME}")
225 message(FATAL_ERROR "Support your platform here")
226 endif()
227
228
229 if (DEFINED ENABLE_PROFILING AND ENABLE_PROFILING)
230 if (CMAKE_COMPILER_IS_GNUCXX)
231 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
232 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
233 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
234 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -pg")
235 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")
236 else()
237 message(FATAL_ERROR "Don't know how to enable profiling on your configuration")
238 endif()
239 endif()
240
241
242 if (CMAKE_COMPILER_IS_GNUCXX)
243 # "When creating a static library using binutils (ar) and there
244 # exist a duplicate object name (e.g. a/Foo.cpp.o, b/Foo.cpp.o), the
245 # resulting static library can end up having only one of the
246 # duplicate objects. [...] This bug only happens if there are many
247 # objects." The trick consists in replacing the "r" argument
248 # ("replace") provided to "ar" (as used in CMake < 3.1) by the "q"
249 # argument ("quick append"). This is because of the fact that CMake
250 # will invoke "ar" several times with several batches of ".o"
251 # objects, and using "r" would overwrite symbols defined in
252 # preceding batches. https://cmake.org/Bug/view.php?id=14874
253 set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> <LINK_FLAGS> q <TARGET> <OBJECTS>")
254 endif()
255
256
257 if (STATIC_BUILD)
258 add_definitions(-DORTHANC_STATIC=1)
259 else()
260 add_definitions(-DORTHANC_STATIC=0)
261 endif()