Skip to content

Commit 9f37457

Browse files
committed
cmake: move cflag defaults into a separate module
1 parent 6c00fcb commit 9f37457

File tree

2 files changed

+171
-171
lines changed

2 files changed

+171
-171
lines changed

CMakeLists.txt

Lines changed: 18 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,6 @@ project(libgit2 VERSION "1.3.0" LANGUAGES C)
88
# Add find modules to the path
99
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${libgit2_SOURCE_DIR}/cmake")
1010

11-
# Modules
12-
13-
include(CheckLibraryExists)
14-
include(CheckFunctionExists)
15-
include(CheckSymbolExists)
16-
include(CheckStructHasMember)
17-
include(CheckPrototypeDefinition)
18-
include(AddCFlagIfSupported)
19-
include(FindPkgLibraries)
20-
include(FindThreads)
21-
include(FindStatNsec)
22-
include(Findfutimens)
23-
include(GNUInstallDirs)
24-
include(IdeSplitSources)
25-
include(FeatureSummary)
26-
include(EnableWarnings)
27-
2811
#
2912
# Build options
3013
#
@@ -98,6 +81,24 @@ if(WIN32)
9881
endif()
9982

10083

84+
# Modules
85+
86+
include(CheckLibraryExists)
87+
include(CheckFunctionExists)
88+
include(CheckSymbolExists)
89+
include(CheckStructHasMember)
90+
include(CheckPrototypeDefinition)
91+
include(AddCFlagIfSupported)
92+
include(FindPkgLibraries)
93+
include(FindThreads)
94+
include(FindStatNsec)
95+
include(Findfutimens)
96+
include(GNUInstallDirs)
97+
include(IdeSplitSources)
98+
include(FeatureSummary)
99+
include(EnableWarnings)
100+
include(DefaultCFlags)
101+
101102
#
102103
# Compiler / linker flags
103104
#
@@ -106,160 +107,6 @@ if(DEPRECATE_HARD)
106107
add_definitions(-DGIT_DEPRECATE_HARD)
107108
endif()
108109

109-
# Platform specific compilation flags
110-
if(MSVC)
111-
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
112-
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
113-
add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
114-
115-
string(REPLACE "/Zm1000" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
116-
117-
# /GF - String pooling
118-
# /MP - Parallel build
119-
set(CMAKE_C_FLAGS "/GF /MP /nologo ${CMAKE_C_FLAGS}")
120-
121-
# /Gd - explicitly set cdecl calling convention
122-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd")
123-
124-
if(NOT (MSVC_VERSION LESS 1900))
125-
# /guard:cf - Enable Control Flow Guard
126-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
127-
endif()
128-
129-
if(STATIC_CRT)
130-
set(CRT_FLAG_DEBUG "/MTd")
131-
set(CRT_FLAG_RELEASE "/MT")
132-
else()
133-
set(CRT_FLAG_DEBUG "/MDd")
134-
set(CRT_FLAG_RELEASE "/MD")
135-
endif()
136-
137-
if(WIN32_LEAKCHECK)
138-
set(GIT_WIN32_LEAKCHECK 1)
139-
set(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
140-
set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
141-
endif()
142-
143-
# /Zi - Create debugging information
144-
# /Od - Disable optimization
145-
# /D_DEBUG - #define _DEBUG
146-
# /MTd - Statically link the multithreaded debug version of the CRT
147-
# /MDd - Dynamically link the multithreaded debug version of the CRT
148-
# /RTC1 - Run time checks
149-
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Zi /Od /D_DEBUG /RTC1 ${CRT_FLAG_DEBUG}")
150-
151-
# /DNDEBUG - Disables asserts
152-
# /MT - Statically link the multithreaded release version of the CRT
153-
# /MD - Dynamically link the multithreaded release version of the CRT
154-
# /O2 - Optimize for speed
155-
# /Oy - Enable frame pointer omission (FPO) (otherwise CMake will automatically turn it off)
156-
# /GL - Link time code generation (whole program optimization)
157-
# /Gy - Function-level linking
158-
set(CMAKE_C_FLAGS_RELEASE "/DNDEBUG /O2 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
159-
160-
# /Oy- - Disable frame pointer omission (FPO)
161-
set(CMAKE_C_FLAGS_RELWITHDEBINFO "/DNDEBUG /Zi /O2 /Oy- /GL /Gy ${CRT_FLAG_RELEASE}")
162-
163-
# /O1 - Optimize for size
164-
set(CMAKE_C_FLAGS_MINSIZEREL "/DNDEBUG /O1 /Oy /GL /Gy ${CRT_FLAG_RELEASE}")
165-
166-
# /IGNORE:4221 - Ignore empty compilation units
167-
set(CMAKE_STATIC_LINKER_FLAGS "/IGNORE:4221")
168-
169-
# /DYNAMICBASE - Address space load randomization (ASLR)
170-
# /NXCOMPAT - Data execution prevention (DEP)
171-
# /LARGEADDRESSAWARE - >2GB user address space on x86
172-
# /VERSION - Embed version information in PE header
173-
set(CMAKE_EXE_LINKER_FLAGS "/DYNAMICBASE /NXCOMPAT /LARGEADDRESSAWARE /VERSION:${libgit2_VERSION_MAJOR}.${libgit2_VERSION_MINOR}")
174-
175-
if(NOT (MSVC_VERSION LESS 1900))
176-
# /GUARD:CF - Enable Control Flow Guard
177-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
178-
endif()
179-
180-
# /DEBUG - Create a PDB
181-
# /LTCG - Link time code generation (whole program optimization)
182-
# /OPT:REF /OPT:ICF - Fold out duplicate code at link step
183-
# /INCREMENTAL:NO - Required to use /LTCG
184-
# /DEBUGTYPE:cv,fixup - Additional data embedded in the PDB (requires /INCREMENTAL:NO, so not on for Debug)
185-
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/DEBUG")
186-
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
187-
set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "/DEBUG /RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO /DEBUGTYPE:cv,fixup")
188-
set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "/RELEASE /LTCG /OPT:REF /OPT:ICF /INCREMENTAL:NO")
189-
190-
# Same linker settings for DLL as EXE
191-
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
192-
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
193-
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}")
194-
set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
195-
set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
196-
else()
197-
if(ENABLE_REPRODUCIBLE_BUILDS)
198-
set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
199-
set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>")
200-
set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
201-
endif()
202-
203-
if(NOT BUILD_SHARED_LIBS)
204-
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
205-
endif()
206-
207-
set(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
208-
209-
enable_warnings(all)
210-
enable_warnings(extra)
211-
212-
if(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
213-
set(CMAKE_C_FLAGS "-D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
214-
endif()
215-
216-
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
217-
218-
if(MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
219-
string(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
220-
elseif(BUILD_SHARED_LIBS)
221-
add_c_flag_IF_SUPPORTED(-fvisibility=hidden)
222-
223-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
224-
endif()
225-
226-
if(MINGW)
227-
# MinGW >= 3.14 uses the C99-style stdio functions
228-
# automatically, but forks like mingw-w64 still want
229-
# us to define this in order to use them
230-
add_definitions(-D__USE_MINGW_ANSI_STDIO=1)
231-
endif()
232-
233-
enable_warnings(documentation)
234-
disable_warnings(documentation-deprecated-sync)
235-
disable_warnings(missing-field-initializers)
236-
enable_warnings(strict-aliasing)
237-
enable_warnings(strict-prototypes)
238-
enable_warnings(declaration-after-statement)
239-
enable_warnings(shift-count-overflow)
240-
enable_warnings(unused-const-variable)
241-
enable_warnings(unused-function)
242-
enable_warnings(int-conversion)
243-
enable_warnings(c11-extensions)
244-
enable_warnings(c99-c11-compat)
245-
246-
# MinGW uses gcc, which expects POSIX formatting for printf, but
247-
# uses the Windows C library, which uses its own format specifiers.
248-
# Disable format specifier warnings.
249-
if(MINGW)
250-
disable_warnings(format)
251-
disable_warnings(format-security)
252-
else()
253-
enable_warnings(format)
254-
enable_warnings(format-security)
255-
endif()
256-
endif()
257-
258-
# Ensure that MinGW provides the correct header files.
259-
if(WIN32 AND NOT CYGWIN)
260-
add_definitions(-DWIN32 -D_WIN32_WINNT=0x0600)
261-
endif()
262-
263110
if(NOT CMAKE_CONFIGURATION_TYPES)
264111
# Build Debug by default
265112
if(NOT CMAKE_BUILD_TYPE)

cmake/DefaultCFlags.cmake

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

0 commit comments

Comments
 (0)