Mercurial > hg > orthanc-java
comparison Resources/Orthanc/CMake/Compiler.cmake @ 0:3ecef5782f2c
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 18 Oct 2023 17:59:44 +0200 |
parents | |
children | 443d5b7d733e |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3ecef5782f2c |
---|---|
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-2023 Osimis S.A., Belgium | |
5 # Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
6 # | |
7 # This program is free software: you can redistribute it and/or | |
8 # modify it under the terms of the GNU Lesser General Public License | |
9 # as published by the Free Software Foundation, either version 3 of | |
10 # the License, or (at your option) any later version. | |
11 # | |
12 # This program is distributed in the hope that it will be useful, but | |
13 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 # Lesser General Public License for more details. | |
16 # | |
17 # You should have received a copy of the GNU Lesser General Public | |
18 # License along with this program. If not, see | |
19 # <http://www.gnu.org/licenses/>. | |
20 | |
21 | |
22 # This file sets all the compiler-related flags | |
23 | |
24 | |
25 # Save the current compiler flags to the cache every time cmake configures the project | |
26 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "compiler flags" FORCE) | |
27 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "compiler flags" FORCE) | |
28 | |
29 | |
30 include(CheckLibraryExists) | |
31 | |
32 if ((CMAKE_CROSSCOMPILING AND NOT | |
33 "${CMAKE_SYSTEM_VERSION}" STREQUAL "CrossToolNg") OR | |
34 "${CMAKE_SYSTEM_VERSION}" STREQUAL "LinuxStandardBase") | |
35 # Cross-compilation necessarily implies standalone and static build | |
36 SET(STATIC_BUILD ON) | |
37 SET(STANDALONE_BUILD ON) | |
38 endif() | |
39 | |
40 | |
41 if ("${CMAKE_SYSTEM_VERSION}" STREQUAL "LinuxStandardBase") | |
42 # Cache the environment variables "LSB_CC" and "LSB_CXX" for further | |
43 # use by "ExternalProject" in CMake | |
44 SET(CMAKE_LSB_CC $ENV{LSB_CC} CACHE STRING "") | |
45 SET(CMAKE_LSB_CXX $ENV{LSB_CXX} CACHE STRING "") | |
46 endif() | |
47 | |
48 | |
49 if (CMAKE_COMPILER_IS_GNUCXX) | |
50 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-long-long") | |
51 | |
52 # --std=c99 makes libcurl not to compile | |
53 # -pedantic gives a lot of warnings on OpenSSL | |
54 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -Wno-variadic-macros") | |
55 | |
56 if (CMAKE_CROSSCOMPILING) | |
57 # http://stackoverflow.com/a/3543845/881731 | |
58 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff -I<CMAKE_CURRENT_SOURCE_DIR> <SOURCE> <OBJECT>") | |
59 endif() | |
60 | |
61 elseif (MSVC) | |
62 # Use static runtime under Visual Studio | |
63 # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace | |
64 # http://stackoverflow.com/a/6510446 | |
65 foreach(flag_var | |
66 CMAKE_C_FLAGS_DEBUG | |
67 CMAKE_CXX_FLAGS_DEBUG | |
68 CMAKE_C_FLAGS_RELEASE | |
69 CMAKE_CXX_FLAGS_RELEASE | |
70 CMAKE_C_FLAGS_MINSIZEREL | |
71 CMAKE_CXX_FLAGS_MINSIZEREL | |
72 CMAKE_C_FLAGS_RELWITHDEBINFO | |
73 CMAKE_CXX_FLAGS_RELWITHDEBINFO) | |
74 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") | |
75 string(REGEX REPLACE "/MDd" "/MTd" ${flag_var} "${${flag_var}}") | |
76 endforeach(flag_var) | |
77 | |
78 # Add /Zm256 compiler option to Visual Studio to fix PCH errors | |
79 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zm256") | |
80 | |
81 # New in Orthanc 1.5.5 | |
82 if (MSVC_MULTIPLE_PROCESSES) | |
83 # "If you omit the processMax argument in the /MP option, the | |
84 # compiler obtains the number of effective processors from the | |
85 # operating system, and then creates one process per effective | |
86 # processor" | |
87 # https://blog.kitware.com/cmake-building-with-all-your-cores/ | |
88 # https://docs.microsoft.com/en-us/cpp/build/reference/mp-build-with-multiple-processes | |
89 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP") | |
90 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") | |
91 endif() | |
92 | |
93 add_definitions( | |
94 -D_CRT_SECURE_NO_WARNINGS=1 | |
95 -D_CRT_SECURE_NO_DEPRECATE=1 | |
96 ) | |
97 | |
98 if (MSVC_VERSION LESS 1600) | |
99 # Starting with Visual Studio >= 2010 (i.e. macro _MSC_VER >= | |
100 # 1600), Microsoft ships a standard-compliant <stdint.h> | |
101 # header. For earlier versions of Visual Studio, give access to a | |
102 # compatibility header. | |
103 # http://stackoverflow.com/a/70630/881731 | |
104 # https://en.wikibooks.org/wiki/C_Programming/C_Reference/stdint.h#External_links | |
105 include_directories(${CMAKE_CURRENT_LIST_DIR}/../../Resources/ThirdParty/VisualStudio) | |
106 endif() | |
107 | |
108 link_libraries(netapi32) | |
109 endif() | |
110 | |
111 | |
112 if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" OR | |
113 ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD") | |
114 # In FreeBSD/OpenBSD, the "/usr/local/" folder contains the ports and need to be imported | |
115 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -I/usr/local/include") | |
116 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/local/include") | |
117 SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib") | |
118 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -L/usr/local/lib") | |
119 endif() | |
120 | |
121 | |
122 if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR | |
123 ${CMAKE_SYSTEM_NAME} STREQUAL "kFreeBSD" OR | |
124 ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" OR | |
125 ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD") | |
126 | |
127 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD" AND | |
128 NOT ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD") | |
129 # The "--no-undefined" linker flag makes the shared libraries | |
130 # (plugins ModalityWorklists and ServeFolders) fail to compile on | |
131 # OpenBSD, and make the PostgreSQL plugin complain about missing | |
132 # "environ" global variable in FreeBSD | |
133 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--no-undefined") | |
134 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined") | |
135 endif() | |
136 | |
137 # Remove the "-rdynamic" option | |
138 # http://www.mail-archive.com/cmake@cmake.org/msg08837.html | |
139 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") | |
140 link_libraries(pthread) | |
141 | |
142 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD") | |
143 link_libraries(rt) | |
144 endif() | |
145 | |
146 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" AND | |
147 NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD") | |
148 link_libraries(dl) | |
149 endif() | |
150 | |
151 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" AND | |
152 NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD") | |
153 # The "--as-needed" linker flag is not available on FreeBSD and OpenBSD | |
154 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--as-needed") | |
155 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--as-needed") | |
156 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed") | |
157 endif() | |
158 | |
159 if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" AND | |
160 NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD") | |
161 # FreeBSD/OpenBSD have just one single interface for file | |
162 # handling, which is 64bit clean, so there is no need to define macro | |
163 # for LFS (Large File Support). | |
164 # https://ohse.de/uwe/articles/lfs.html | |
165 add_definitions( | |
166 -D_LARGEFILE64_SOURCE=1 | |
167 -D_FILE_OFFSET_BITS=64 | |
168 ) | |
169 endif() | |
170 | |
171 elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") | |
172 if (MSVC) | |
173 message("MSVC compiler version = " ${MSVC_VERSION} "\n") | |
174 # Starting Visual Studio 2013 (version 1800), it is not possible | |
175 # to target Windows XP anymore | |
176 if (MSVC_VERSION LESS 1800) | |
177 add_definitions( | |
178 -DWINVER=0x0501 | |
179 -D_WIN32_WINNT=0x0501 | |
180 ) | |
181 endif() | |
182 else() | |
183 add_definitions( | |
184 -DWINVER=0x0501 | |
185 -D_WIN32_WINNT=0x0501 | |
186 ) | |
187 endif() | |
188 | |
189 add_definitions( | |
190 -D_CRT_SECURE_NO_WARNINGS=1 | |
191 ) | |
192 link_libraries(rpcrt4 ws2_32 iphlpapi) # "iphlpapi" is for "SystemToolbox::GetMacAddresses()" | |
193 | |
194 if (CMAKE_COMPILER_IS_GNUCXX) | |
195 # Some additional C/C++ compiler flags for MinGW | |
196 SET(MINGW_NO_WARNINGS "-Wno-unused-function -Wno-unused-variable") | |
197 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MINGW_NO_WARNINGS} -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast") | |
198 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MINGW_NO_WARNINGS}") | |
199 | |
200 if (DYNAMIC_MINGW_STDLIB) | |
201 else() | |
202 # This is a patch for MinGW64 | |
203 SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-multiple-definition -static-libgcc -static-libstdc++") | |
204 SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--allow-multiple-definition -static-libgcc -static-libstdc++") | |
205 endif() | |
206 | |
207 CHECK_LIBRARY_EXISTS(winpthread pthread_create "" HAVE_WIN_PTHREAD) | |
208 if (HAVE_WIN_PTHREAD) | |
209 if (DYNAMIC_MINGW_STDLIB) | |
210 else() | |
211 # This line is necessary to compile with recent versions of MinGW, | |
212 # otherwise "libwinpthread-1.dll" is not statically linked. | |
213 SET(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic") | |
214 endif() | |
215 add_definitions(-DHAVE_WIN_PTHREAD=1) | |
216 else() | |
217 add_definitions(-DHAVE_WIN_PTHREAD=0) | |
218 endif() | |
219 endif() | |
220 | |
221 elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") | |
222 add_definitions( | |
223 -D_XOPEN_SOURCE=1 | |
224 ) | |
225 link_libraries(iconv) | |
226 | |
227 elseif (CMAKE_SYSTEM_NAME STREQUAL "Emscripten") | |
228 message("Building using Emscripten (for WebAssembly or asm.js targets)") | |
229 include(${CMAKE_CURRENT_LIST_DIR}/EmscriptenParameters.cmake) | |
230 | |
231 elseif (CMAKE_SYSTEM_NAME STREQUAL "Android") | |
232 | |
233 else() | |
234 message("Unknown target platform: ${CMAKE_SYSTEM_NAME}") | |
235 message(FATAL_ERROR "Support your platform here") | |
236 endif() | |
237 | |
238 | |
239 if (DEFINED ENABLE_PROFILING AND ENABLE_PROFILING) | |
240 if (CMAKE_COMPILER_IS_GNUCXX OR | |
241 CMAKE_CXX_COMPILER_ID STREQUAL "Clang") | |
242 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg") | |
243 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg") | |
244 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg") | |
245 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -pg") | |
246 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg") | |
247 else() | |
248 message(FATAL_ERROR "Don't know how to enable profiling on your configuration") | |
249 endif() | |
250 endif() | |
251 | |
252 | |
253 if (CMAKE_COMPILER_IS_GNUCXX) | |
254 # "When creating a static library using binutils (ar) and there | |
255 # exist a duplicate object name (e.g. a/Foo.cpp.o, b/Foo.cpp.o), the | |
256 # resulting static library can end up having only one of the | |
257 # duplicate objects. [...] This bug only happens if there are many | |
258 # objects." The trick consists in replacing the "r" argument | |
259 # ("replace") provided to "ar" (as used in CMake < 3.1) by the "q" | |
260 # argument ("quick append"). This is because of the fact that CMake | |
261 # will invoke "ar" several times with several batches of ".o" | |
262 # objects, and using "r" would overwrite symbols defined in | |
263 # preceding batches. https://cmake.org/Bug/view.php?id=14874 | |
264 set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> <LINK_FLAGS> q <TARGET> <OBJECTS>") | |
265 endif() | |
266 | |
267 | |
268 # This function defines macro "__ORTHANC_FILE__" as a replacement to | |
269 # macro "__FILE__", as the latter leaks the full path of the source | |
270 # files in the binaries | |
271 # https://stackoverflow.com/questions/8487986/file-macro-shows-full-path | |
272 # https://twitter.com/wget42/status/1676877802375634944?s=20 | |
273 function(DefineSourceBasenameForTarget targetname) | |
274 # Microsoft Visual Studio is extremely slow if using | |
275 # "set_property()", we only enable this feature for gcc and clang | |
276 if (CMAKE_COMPILER_IS_GNUCXX OR | |
277 CMAKE_CXX_COMPILER_ID STREQUAL "Clang") | |
278 get_target_property(source_files "${targetname}" SOURCES) | |
279 foreach(sourcefile ${source_files}) | |
280 get_filename_component(basename "${sourcefile}" NAME) | |
281 set_property( | |
282 SOURCE "${sourcefile}" APPEND | |
283 PROPERTY COMPILE_DEFINITIONS "__ORTHANC_FILE__=\"${basename}\"") | |
284 endforeach() | |
285 endif() | |
286 endfunction() |