Skip to content

Commit 9427186

Browse files
committed
build: cleanup CMake
* build: inline a variable * build: use `LINKER_LANGUAGE` property for C++ runtime Rather than explicitly name the C++ runtime, use the `LINKER_LANGUAGE` property to use the driver to spell the C++ runtime appropriately. * build: use CMake to control C standard Rather than use compiler specific flags to control the language standard, indicate to CMake the desired standard. * build: use the correct variable These flags are being applied to the *C* compiler, check the C compiler, not the C++ compiler. * build: loosen the compiler check This loosens the compiler identifier check to enable matching AppleClang which is the identifier for the Xcode compiler. * build: hoist shared flags to top-level CMakeLists This hoists the common shared flags handling to the top-level CMakeLists from sub-layers. This prevents the duplication of the handling. * build: remove duplicated flags This is unnecessary, `/TP` is forced on all MSVC builds, no need to duplicate the flag for older versions. * build: loosen C compiler identifier check Loosen the check to a match rather than equality check, this allows it to match AppleClang which is the identifier for the Apple vended clang compiler part of Xcode. * build: use `add_compile_options` Use `add_compile_options` rather than modify `CMAKE_C_FLAGS`. The latter is meant to be only modified by the user, not the package developer. * build: hoist sanitizer flags to global state This moves the CMAKE_C_FLAGS handling to the top-level and uses `add_compile_options` rather than modifying the user controlled flags. * build: hoist `-fvisibilty` flags to top-level These are global settings, hoist them to the top level. * build: hoist the debug flag handling Use a generator expression and hoist the flag handling for the debug build. * build: hoist the profile flag handling This is a global flag, hoist it to the top level and use `add_compile_options` rather than modify the user controlled flags. * build: remove incorrect variable handling This seemed to be attempting to set the linker not the linker flags for the profile configuration. This variable is not used, do not set it. * build: remove unused CMake includes
1 parent 0c40e0c commit 9427186

4 files changed

Lines changed: 49 additions & 82 deletions

File tree

CMakeLists.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,50 @@ option(CMARK_SHARED "Build shared libcmark-gfm library" ON)
2929
option(CMARK_LIB_FUZZER "Build libFuzzer fuzzing harness" OFF)
3030
option(CMARK_FUZZ_QUADRATIC "Build quadratic fuzzing harness" OFF)
3131

32+
if(NOT MSVC)
33+
set(CMAKE_C_STANDARD 99)
34+
set(CMAKE_C_STANDARD_REQUIRED YES)
35+
set(CMAKE_C_EXTENSIONS NO)
36+
endif()
37+
38+
# -fvisibility=hidden
39+
set(CMAKE_C_VISIBILITY_PRESET hidden)
40+
set(CMAKE_VISIBILITY_INLINES_HIDDEN TRUE)
41+
42+
# Compiler flags
43+
if(MSVC)
44+
add_compile_options($<$<COMPILE_LANGUAGE:C>:/W4>)
45+
add_compile_options($<$<COMPILE_LANGUAGE:C>:/wd4706>)
46+
# Compile as C++ under MSVC older than 12.0
47+
add_compile_options($<$<COMPILE_LANGUAGE:C>:/TP>)
48+
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:_CRT_SECURE_NO_WARNINGS>)
49+
# BEGIN cmark-gfm
50+
add_compile_options($<$<COMPILE_LANGUAGE:C>:/wd4204>)
51+
add_compile_options($<$<COMPILE_LANGUAGE:C>:/wd4211>)
52+
add_compile_options($<$<COMPILE_LANGUAGE:C>:/wd4100>)
53+
add_compile_options($<$<COMPILE_LANGUAGE:C>:/WX>)
54+
# END cmark-gfm
55+
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES Clang)
56+
add_compile_options($<$<COMPILE_LANGUAGE:C>:-Wall>)
57+
add_compile_options($<$<COMPILE_LANGUAGE:C>:-Wextra>)
58+
add_compile_options($<$<COMPILE_LANGUAGE:C>:-pedantic>)
59+
# BEGIN cmark-gfm
60+
add_compile_options($<$<COMPILE_LANGUAGE:C>:-Wno-unused-parameter>)
61+
# END cmark-gfm
62+
endif()
63+
64+
# Check integrity of node structure when compiled as debug
65+
add_compile_definitions($<$<CONFIG:Debug>:CMARK_DEBUG_NODES>)
66+
67+
add_compile_options($<$<AND:$<CONFIG:PROFILE>,$<COMPILE_LANGUAGE:C>>:-pg>)
68+
69+
if(CMAKE_BUILD_TYPE STREQUAL Ubsan)
70+
add_compile_options($<$<COMPILE_LANGUAGE:C>:-fsanitize=undefined>)
71+
endif()
72+
if(CMARK_LIB_FUZZER)
73+
add_compile_options($<$<COMPILE_LANGUAGE:C>:-fsanitize-coverage=trace-pc-guard>)
74+
endif()
75+
3276
if(CMARK_FUZZ_QUADRATIC)
3377
set(FUZZER_FLAGS "-fsanitize=fuzzer-no-link,address -g")
3478
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FUZZER_FLAGS}")

api_test/CMakeLists.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,3 @@ if(WIN32)
2222
ENVIRONMENT "PATH=$<TARGET_FILE_DIR:libcmark-gfm-extensions>;$<TARGET_FILE_DIR:libcmark-gfm>;$ENV{PATH}")
2323
endif()
2424
endif()
25-
26-
# Compiler flags
27-
if(MSVC)
28-
# Force to always compile with W4
29-
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
30-
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
31-
else()
32-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
33-
endif()
34-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4706 /D_CRT_SECURE_NO_WARNINGS")
35-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /TP")
36-
elseif(CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
37-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=c99 -pedantic")
38-
endif()

extensions/CMakeLists.txt

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ include_directories(
1919

2020
include_directories(. ${CMAKE_CURRENT_BINARY_DIR})
2121

22-
set(CMAKE_C_FLAGS_PROFILE "${CMAKE_C_FLAGS_RELEASE} -pg")
23-
set(CMAKE_LINKER_PROFILE "${CMAKE_LINKER_FLAGS_RELEASE} -pg")
24-
2522
if (CMARK_SHARED)
2623
add_library(${LIBRARY} SHARED ${LIBRARY_SOURCES})
2724

@@ -85,31 +82,7 @@ endif()
8582

8683
# Feature tests
8784
include(CheckCSourceCompiles)
88-
include(CheckCSourceRuns)
89-
include(CheckSymbolExists)
9085

9186
CHECK_C_SOURCE_COMPILES(
9287
"int main() { __builtin_expect(0,0); return 0; }"
9388
HAVE___BUILTIN_EXPECT)
94-
95-
# Always compile with warnings
96-
if(MSVC)
97-
# Force to always compile with W4
98-
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
99-
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
100-
else()
101-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
102-
endif()
103-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX /wd4706 /wd4204 /wd4221 /wd4100 /D_CRT_SECURE_NO_WARNINGS")
104-
elseif(CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
105-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter -std=c99 -pedantic")
106-
endif()
107-
108-
# Compile as C++ under MSVC older than 12.0
109-
if(MSVC AND MSVC_VERSION LESS 1800)
110-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /TP")
111-
endif()
112-
113-
if(CMAKE_BUILD_TYPE STREQUAL "Ubsan")
114-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
115-
endif()

src/CMakeLists.txt

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,6 @@ elseif(CMARK_STATIC)
7777
target_link_libraries(${PROGRAM} libcmark-gfm-extensions_static libcmark-gfm_static)
7878
endif()
7979

80-
# Check integrity of node structure when compiled as debug:
81-
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DCMARK_DEBUG_NODES -DDEBUG")
82-
set(CMAKE_LINKER_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG}")
83-
84-
set(CMAKE_C_FLAGS_PROFILE "${CMAKE_C_FLAGS_RELEASE} -pg")
85-
set(CMAKE_LINKER_PROFILE "${CMAKE_LINKER_FLAGS_RELEASE} -pg")
86-
87-
# -fvisibility=hidden
88-
set(CMAKE_C_VISIBILITY_PRESET hidden)
89-
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
90-
9180
if (CMARK_SHARED)
9281
add_library(${LIBRARY} SHARED ${LIBRARY_SOURCES})
9382
# Include minor version and patch level in soname for now.
@@ -172,8 +161,6 @@ endif()
172161

173162
# Feature tests
174163
include(CheckCSourceCompiles)
175-
include(CheckCSourceRuns)
176-
include(CheckSymbolExists)
177164

178165
CHECK_C_SOURCE_COMPILES(
179166
"int main() { __builtin_expect(0,0); return 0; }"
@@ -183,35 +170,12 @@ CONFIGURE_FILE(
183170
${CMAKE_CURRENT_SOURCE_DIR}/config.h.in
184171
${CMAKE_CURRENT_BINARY_DIR}/config.h)
185172

186-
# Always compile with warnings
187-
if(MSVC)
188-
# Force to always compile with W4
189-
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
190-
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
191-
else()
192-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
193-
endif()
194-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX /wd4706 /wd4204 /wd4221 /wd4100 /D_CRT_SECURE_NO_WARNINGS")
195-
elseif(CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
196-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter -std=c99 -pedantic")
197-
endif()
198-
199-
# Compile as C++ under MSVC older than 12.0
200-
if(MSVC AND MSVC_VERSION LESS 1800)
201-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /TP")
202-
endif()
203-
204-
if(CMAKE_BUILD_TYPE STREQUAL "Ubsan")
205-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
206-
endif()
207-
208173
if(CMARK_LIB_FUZZER)
209-
set(FUZZ_HARNESS "cmark-fuzz")
210-
add_executable(${FUZZ_HARNESS} ../test/cmark-fuzz.c ${LIBRARY_SOURCES})
211-
target_link_libraries(${FUZZ_HARNESS} "${CMAKE_LIB_FUZZER_PATH}")
212-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-coverage=trace-pc-guard")
174+
add_executable(cmark-fuzz ../test/cmark-fuzz.c ${LIBRARY_SOURCES})
175+
target_link_libraries(cmark-fuzz "${CMAKE_LIB_FUZZER_PATH}")
213176

214177
# cmark is written in C but the libFuzzer runtime is written in C++ which
215-
# needs to link against the C++ runtime. Explicitly link it into cmark-fuzz
216-
set_target_properties(${FUZZ_HARNESS} PROPERTIES LINK_FLAGS "-lstdc++")
178+
# needs to link against the C++ runtime.
179+
set_target_properties(${FUZZ_HARNESS} PROPERTIES
180+
LINKER_LANGUAGE CXX)
217181
endif()

0 commit comments

Comments
 (0)