comparison Resources/Orthanc/CMake/Compiler.cmake @ 24:065bc476bcdc

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