cmake: move cflag defaults into a separate module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 040881f..f7f1946 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,23 +8,6 @@ project(libgit2 VERSION "1.3.0" LANGUAGES C)
# Add find modules to the path
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${libgit2_SOURCE_DIR}/cmake")
-# Modules
-
-include(CheckLibraryExists)
-include(CheckFunctionExists)
-include(CheckSymbolExists)
-include(CheckStructHasMember)
-include(CheckPrototypeDefinition)
-include(AddCFlagIfSupported)
-include(FindPkgLibraries)
-include(FindThreads)
-include(FindStatNsec)
-include(Findfutimens)
-include(GNUInstallDirs)
-include(IdeSplitSources)
-include(FeatureSummary)
-include(EnableWarnings)
-
#
# Build options
#
@@ -98,6 +81,24 @@ if(WIN32)
endif()
+# Modules
+
+include(CheckLibraryExists)
+include(CheckFunctionExists)
+include(CheckSymbolExists)
+include(CheckStructHasMember)
+include(CheckPrototypeDefinition)
+include(AddCFlagIfSupported)
+include(FindPkgLibraries)
+include(FindThreads)
+include(FindStatNsec)
+include(Findfutimens)
+include(GNUInstallDirs)
+include(IdeSplitSources)
+include(FeatureSummary)
+include(EnableWarnings)
+include(DefaultCFlags)
+
#
# Compiler / linker flags
#
@@ -106,160 +107,6 @@ if(DEPRECATE_HARD)
add_definitions(-DGIT_DEPRECATE_HARD)
endif()
-# Platform specific compilation flags
-if(MSVC)
- add_definitions(-D_SCL_SECURE_NO_WARNINGS)
- add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
- add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
-
- string(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
-
- # /GF - String pooling
- # /MP - Parallel build
- set(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}")
-
- # /Gd - explicitly set cdecl calling convention
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd")
-
- if(NOT (MSVC_VERSION LESS 1900))
- # /guard:cf - Enable Control Flow Guard
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
- endif()
-
- if(STATIC_CRT)
- set(CRT_FLAG_DEBUG "/MTd")
- set(CRT_FLAG_RELEASE "/MT")
- else()
- set(CRT_FLAG_DEBUG "/MDd")
- set(CRT_FLAG_RELEASE "/MD")
- endif()
-
- if(WIN32_LEAKCHECK)
- set(GIT_WIN32_LEAKCHECK 1)
- set(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
- set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
- endif()
-
- # /Zi - Create debugging information
- # /Od - Disable optimization
- # /D_DEBUG - #define _DEBUG
- # /MTd - Statically link the multithreaded debug version of the CRT
- # /MDd - Dynamically link the multithreaded debug version of the CRT
- # /RTC1 - Run time checks
- set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}")
-
- # /DNDEBUG - Disables asserts
- # /MT - Statically link the multithreaded release version of the CRT
- # /MD - Dynamically link the multithreaded release version of the CRT
- # /O2 - Optimize for speed
- # /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off)
- # /GL - Link time code generation (whole program optimization)
- # /Gy - Function-level linking
- set(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
-
- # /Oy- - Disable frame pointer omission (FPO)
- set(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}")
-
- # /O1 - Optimize for size
- set(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
-
- # /IGNORE:4221 - Ignore empty compilation units
- set(CMAKE_STATIC_LINKER_FLAGS "/IGNORE:4221")
-
- # /DYNAMICBASE - Address space load randomization (ASLR)
- # /NXCOMPAT - Data execution prevention (DEP)
- # /LARGEADDRESSAWARE - >2GB user address space on x86
- # /VERSION - Embed version information in PE header
- set(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${libgit2_VERSION_MAJOR}.${libgit2_VERSION_MINOR}")
-
- if(NOT (MSVC_VERSION LESS 1900))
- # /GUARD:CF - Enable Control Flow Guard
- set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
- endif()
-
- # /DEBUG - Create a PDB
- # /LTCG - Link time code generation (whole program optimization)
- # /OPT:REF /OPT:ICF - Fold out duplicate code at link step
- # /INCREMENTAL:NO - Required to use /LTCG
- # /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug)
- set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG")
- set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
- set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup")
- set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
-
- # Same linker settings for DLL as EXE
- set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
- set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
- set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
- set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
- set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
-else()
- if(ENABLE_REPRODUCIBLE_BUILDS)
- set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
- set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>")
- set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
- endif()
-
- if(NOT BUILD_SHARED_LIBS)
- set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
- endif()
-
- set(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
-
- enable_warnings(all)
- enable_warnings(extra)
-
- if(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
- set(CMAKE_C_FLAGS "-D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
- endif()
-
- set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
-
- if(MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
- string(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
- elseif(BUILD_SHARED_LIBS)
- add_c_flag_IF_SUPPORTED(-fvisibility=hidden)
-
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
- endif()
-
- if(MINGW)
- # MinGW >= 3.14 uses the C99-style stdio functions
- # automatically, but forks like mingw-w64 still want
- # us to define this in order to use them
- add_definitions(-D__USE_MINGW_ANSI_STDIO=1)
- endif()
-
- enable_warnings(documentation)
- disable_warnings(documentation-deprecated-sync)
- disable_warnings(missing-field-initializers)
- enable_warnings(strict-aliasing)
- enable_warnings(strict-prototypes)
- enable_warnings(declaration-after-statement)
- enable_warnings(shift-count-overflow)
- enable_warnings(unused-const-variable)
- enable_warnings(unused-function)
- enable_warnings(int-conversion)
- enable_warnings(c11-extensions)
- enable_warnings(c99-c11-compat)
-
- # MinGW uses gcc, which expects POSIX formatting for printf, but
- # uses the Windows C library, which uses its own format specifiers.
- # Disable format specifier warnings.
- if(MINGW)
- disable_warnings(format)
- disable_warnings(format-security)
- else()
- enable_warnings(format)
- enable_warnings(format-security)
- endif()
-endif()
-
-# Ensure that MinGW provides the correct header files.
-if(WIN32 AND NOT CYGWIN)
- add_definitions(-DWIN32 -D_WIN32_WINNT=0x0600)
-endif()
-
if(NOT CMAKE_CONFIGURATION_TYPES)
# Build Debug by default
if(NOT CMAKE_BUILD_TYPE)
diff --git a/cmake/DefaultCFlags.cmake b/cmake/DefaultCFlags.cmake
new file mode 100644
index 0000000..d122f6c
--- /dev/null
+++ b/cmake/DefaultCFlags.cmake
@@ -0,0 +1,153 @@
+# Platform specific compilation flags
+if(MSVC)
+ add_definitions(-D_SCL_SECURE_NO_WARNINGS)
+ add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
+ add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
+
+ string(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
+
+ # /GF - String pooling
+ # /MP - Parallel build
+ set(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}")
+
+ # /Gd - explicitly set cdecl calling convention
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd")
+
+ if(NOT (MSVC_VERSION LESS 1900))
+ # /guard:cf - Enable Control Flow Guard
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
+ endif()
+
+ if(STATIC_CRT)
+ set(CRT_FLAG_DEBUG "/MTd")
+ set(CRT_FLAG_RELEASE "/MT")
+ else()
+ set(CRT_FLAG_DEBUG "/MDd")
+ set(CRT_FLAG_RELEASE "/MD")
+ endif()
+
+ if(WIN32_LEAKCHECK)
+ set(GIT_WIN32_LEAKCHECK 1)
+ set(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
+ set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
+ endif()
+
+ # /Zi - Create debugging information
+ # /Od - Disable optimization
+ # /D_DEBUG - #define _DEBUG
+ # /MTd - Statically link the multithreaded debug version of the CRT
+ # /MDd - Dynamically link the multithreaded debug version of the CRT
+ # /RTC1 - Run time checks
+ set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}")
+
+ # /DNDEBUG - Disables asserts
+ # /MT - Statically link the multithreaded release version of the CRT
+ # /MD - Dynamically link the multithreaded release version of the CRT
+ # /O2 - Optimize for speed
+ # /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off)
+ # /GL - Link time code generation (whole program optimization)
+ # /Gy - Function-level linking
+ set(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
+
+ # /Oy- - Disable frame pointer omission (FPO)
+ set(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}")
+
+ # /O1 - Optimize for size
+ set(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
+
+ # /IGNORE:4221 - Ignore empty compilation units
+ set(CMAKE_STATIC_LINKER_FLAGS "/IGNORE:4221")
+
+ # /DYNAMICBASE - Address space load randomization (ASLR)
+ # /NXCOMPAT - Data execution prevention (DEP)
+ # /LARGEADDRESSAWARE - >2GB user address space on x86
+ # /VERSION - Embed version information in PE header
+ set(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${libgit2_VERSION_MAJOR}.${libgit2_VERSION_MINOR}")
+
+ if(NOT (MSVC_VERSION LESS 1900))
+ # /GUARD:CF - Enable Control Flow Guard
+ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
+ endif()
+
+ # /DEBUG - Create a PDB
+ # /LTCG - Link time code generation (whole program optimization)
+ # /OPT:REF /OPT:ICF - Fold out duplicate code at link step
+ # /INCREMENTAL:NO - Required to use /LTCG
+ # /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug)
+ set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG")
+ set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
+ set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup")
+ set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
+
+ # Same linker settings for DLL as EXE
+ set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
+ set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
+ set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
+ set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
+ set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
+else()
+ if(ENABLE_REPRODUCIBLE_BUILDS)
+ set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
+ set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>")
+ set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
+ endif()
+
+ if(NOT BUILD_SHARED_LIBS)
+ set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
+ endif()
+
+ set(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
+
+ enable_warnings(all)
+ enable_warnings(extra)
+
+ if(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
+ set(CMAKE_C_FLAGS "-D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
+ endif()
+
+ set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
+
+ if(MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
+ string(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
+ elseif(BUILD_SHARED_LIBS)
+ add_c_flag_IF_SUPPORTED(-fvisibility=hidden)
+
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
+ endif()
+
+ if(MINGW)
+ # MinGW >= 3.14 uses the C99-style stdio functions
+ # automatically, but forks like mingw-w64 still want
+ # us to define this in order to use them
+ add_definitions(-D__USE_MINGW_ANSI_STDIO=1)
+ endif()
+
+ enable_warnings(documentation)
+ disable_warnings(documentation-deprecated-sync)
+ disable_warnings(missing-field-initializers)
+ enable_warnings(strict-aliasing)
+ enable_warnings(strict-prototypes)
+ enable_warnings(declaration-after-statement)
+ enable_warnings(shift-count-overflow)
+ enable_warnings(unused-const-variable)
+ enable_warnings(unused-function)
+ enable_warnings(int-conversion)
+ enable_warnings(c11-extensions)
+ enable_warnings(c99-c11-compat)
+
+ # MinGW uses gcc, which expects POSIX formatting for printf, but
+ # uses the Windows C library, which uses its own format specifiers.
+ # Disable format specifier warnings.
+ if(MINGW)
+ disable_warnings(format)
+ disable_warnings(format-security)
+ else()
+ enable_warnings(format)
+ enable_warnings(format-security)
+ endif()
+endif()
+
+# Ensure that MinGW provides the correct header files.
+if(WIN32 AND NOT CYGWIN)
+ add_definitions(-DWIN32 -D_WIN32_WINNT=0x0600)
+endif()