comparison OrthancFramework/Resources/CMake/Compiler.cmake @ 4044:d25f4c0fa160 framework

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