comparison Common/Resources/Orthanc/CMake/Compiler.cmake @ 59:f3c44d61e1e1

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