Debian Codegraph @

cmake/DefaultCFlags.cmake

raw
1 # Platform specific compilation flags
2 if(MSVC)
3 add_definitions(-D_SCL_SECURE_NO_WARNINGS)
4 add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
5 add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
6
7 string(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
8
9 # /GF - String pooling
10 # /MP - Parallel build
11 set(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}")
12
13 # /Gd - explicitly set cdecl calling convention
14 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd")
15
16 # Remove warnings about operands that use different enum types.
17 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd5287")
18
19 if(NOT (MSVC_VERSION LESS 1900))
20 # /guard:cf - Enable Control Flow Guard
21 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
22 endif()
23
24 if(STATIC_CRT)
25 set(CRT_FLAG_DEBUG "/MTd")
26 set(CRT_FLAG_RELEASE "/MT")
27 else()
28 set(CRT_FLAG_DEBUG "/MDd")
29 set(CRT_FLAG_RELEASE "/MD")
30 endif()
31
32 if(WIN32_LEAKCHECK)
33 set(GIT_WIN32_LEAKCHECK 1)
34 set(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
35 set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
36 endif()
37
38 # /Zi - Create debugging information
39 # /Od - Disable optimization
40 # /D_DEBUG - #define _DEBUG
41 # /MTd - Statically link the multithreaded debug version of the CRT
42 # /MDd - Dynamically link the multithreaded debug version of the CRT
43 # /RTC1 - Run time checks
44 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}")
45
46 # /DNDEBUG - Disables asserts
47 # /MT - Statically link the multithreaded release version of the CRT
48 # /MD - Dynamically link the multithreaded release version of the CRT
49 # /O2 - Optimize for speed
50 # /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off)
51 # /GL - Link time code generation (whole program optimization)
52 # /Gy - Function-level linking
53 set(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
54
55 # /Oy- - Disable frame pointer omission (FPO)
56 set(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}")
57
58 # /O1 - Optimize for size
59 set(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
60
61 # /IGNORE:4221 - Ignore empty compilation units
62 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
63
64 # /DYNAMICBASE - Address space load randomization (ASLR)
65 # /NXCOMPAT - Data execution prevention (DEP)
66 # /LARGEADDRESSAWARE - >2GB user address space on x86
67 # /VERSION - Embed version information in PE header
68 set(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${libgit2_VERSION_MAJOR}.${libgit2_VERSION_MINOR}")
69
70 if(NOT (MSVC_VERSION LESS 1900))
71 # /GUARD:CF - Enable Control Flow Guard
72 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
73 endif()
74
75 # /DEBUG - Create a PDB
76 # /LTCG - Link time code generation (whole program optimization)
77 # /OPT:REF /OPT:ICF - Fold out duplicate code at link step
78 # /INCREMENTAL:NO - Required to use /LTCG
79 # /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug)
80 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG")
81 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
82 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup")
83 set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
84
85 # Same linker settings for DLL as EXE
86 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
87 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
88 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
89 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
90 set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
91 else()
92 if(ENABLE_REPRODUCIBLE_BUILDS)
93 set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
94 set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>")
95 set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
96 endif()
97
98 if(NOT BUILD_SHARED_LIBS AND LINK_WITH_STATIC_LIBRARIES)
99 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
100 endif()
101
102 set(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
103
104 enable_warnings(all)
105 enable_warnings(extra)
106
107 if(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
108 set(CMAKE_C_FLAGS "-D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
109 endif()
110
111 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
112
113 if(MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
114 string(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
115 elseif(BUILD_SHARED_LIBS)
116 add_c_flag_IF_SUPPORTED(-fvisibility=hidden)
117
118 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
119 endif()
120
121 if(MINGW)
122 # MinGW >= 3.14 uses the C99-style stdio functions
123 # automatically, but forks like mingw-w64 still want
124 # us to define this in order to use them
125 add_definitions(-D__USE_MINGW_ANSI_STDIO=1)
126 endif()
127
128 enable_warnings(documentation)
129 disable_warnings(documentation-deprecated-sync)
130 disable_warnings(missing-field-initializers)
131 enable_warnings(missing-declarations)
132 enable_warnings(strict-aliasing)
133 enable_warnings(strict-prototypes)
134 enable_warnings(declaration-after-statement)
135 enable_warnings(shift-count-overflow)
136 enable_warnings(unused-const-variable)
137 enable_warnings(unused-function)
138 enable_warnings(int-conversion)
139 enable_warnings(c11-extensions)
140 enable_warnings(c99-c11-compat)
141
142 # MinGW uses gcc, which expects POSIX formatting for printf, but
143 # uses the Windows C library, which uses its own format specifiers.
144 # Disable format specifier warnings.
145 if(MINGW)
146 disable_warnings(format)
147 disable_warnings(format-security)
148 else()
149 enable_warnings(format)
150 enable_warnings(format-security)
151 endif()
152 endif()
153
154 # Ensure that MinGW provides the correct header files.
155 if(WIN32 AND NOT CYGWIN)
156 add_definitions(-DWIN32 -D_WIN32_WINNT=0x0600)
157 endif()