From f23e5b39f1349429cbc876a41010c84e2190ba06 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 6 Jul 2021 10:39:57 -0700 Subject: [PATCH] build: allow building with CMake[<3.3] The `COMPILE_LANGUAGE` generator expression was introduced in CMake 3.3. Debian Jessie has an older CMake version bundled, which prevents building on that platform without manually building CMake. This uses a more error-prone mechanism of requiring the commonmark developer to explicitly invoke `cmark_add_compile_options` on the target to ensure that the flags are propagated for the targets without using the generator expression. Adding the options unconditionally prevents using CMark in a build with a non-C programming language. Fixes: #384 --- CMakeLists.txt | 49 +++++++++++++++++++++-------------------- api_test/CMakeLists.txt | 1 + src/CMakeLists.txt | 4 ++++ 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 538ba3586..791832818 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,33 +28,34 @@ if(CMARK_STATIC AND "${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS) endif() -# Compiler flags -if(MSVC) - # Force to always compile with W4 - add_compile_options($<$:/W4>) - add_compile_options($<$:/wd4706>) - # Compile as C++ under MSVC older than 12.0 - if(MSVC_VERSION LESS 1800) - add_compile_options($<$:/TP>) - endif() - add_compile_options($<$:/D_CRT_SECURE_NO_WARNINGS>) -elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES Clang) - add_compile_options($<$:-Wall>) - add_compile_options($<$:-Wextra>) - add_compile_options($<$:-pedantic>) -endif() - # Check integrity of node structure when compiled as debug add_compile_options($<$:-DCMARK_DEBUG_NODES>) -add_compile_options($<$,$>:-pg>) - -if(CMAKE_BUILD_TYPE STREQUAL Ubsan) - add_compile_options($<$:-fsanitize=undefined>) -endif() -if(CMARK_LIB_FUZZER) - add_compile_options($<$:-fsanitize-coverage=trace-pc-guard>) -endif() +# In order to maintain compatibility with older platforms which may not have a +# recent version of CMake (i.e. are running CMake <3.3), we cannot simply use +# the `add_compile_options` with a generator expression. This uses the +# `target_compile_options` with `PRIVATE` to add the flags only to the targets +# so that CMark may be used in projects with non-C languages. +function(cmark_add_compile_options target) + if(MSVC) + target_compile_options(${target} PRIVATE /W4 /wd4706) + if(MSVC_VERSION LESS 1800) + target_compile_options(${target} PRIVATE /TP) + endif() + target_compile_definitions(${target} PRIVATE _CRT_SECURE_NO_WARNINGS) + else() + target_compile_options(${target} PRIVATE -Wall -Wextra -pedantic) + endif() + if(CMAKE_BUILD_TYPE MATCHES profile) + target_compile_options(${target} PRIVATE -pg) + endif() + if(CMAKE_BUILD_TYPE MATCHES ubsan) + target_compile_options(${target} PRIVATE -fsanitize=undefined) + endif() + if(CMARK_LIB_FUZZER) + target_compile_options(${target} PRIVATE -fsanitize-coverage=trace-pc-guard) + endif() +endfunction() add_subdirectory(src) if(CMARK_TESTS AND (CMARK_SHARED OR CMARK_STATIC)) diff --git a/api_test/CMakeLists.txt b/api_test/CMakeLists.txt index b8b135b33..8f19ff70c 100644 --- a/api_test/CMakeLists.txt +++ b/api_test/CMakeLists.txt @@ -4,6 +4,7 @@ add_executable(api_test harness.h main.c ) +cmark_add_compile_options(api_test) if(CMARK_SHARED) target_link_libraries(api_test cmark) else() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 026fd7913..33fd578d8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -53,6 +53,7 @@ include(GNUInstallDirs) include (GenerateExportHeader) add_executable(${PROGRAM} ${PROGRAM_SOURCES}) +cmark_add_compile_options(${PROGRAM}) set_target_properties(${PROGRAM} PROPERTIES OUTPUT_NAME "cmark") @@ -71,6 +72,7 @@ set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) if (CMARK_SHARED) add_library(${LIBRARY} SHARED ${LIBRARY_SOURCES}) + cmark_add_compile_options(${LIBRARY}) set_target_properties(${LIBRARY} PROPERTIES MACOSX_RPATH TRUE OUTPUT_NAME "cmark" @@ -93,6 +95,7 @@ endif() if (CMARK_STATIC) add_library(${STATICLIBRARY} STATIC ${LIBRARY_SOURCES}) + cmark_add_compile_options(${STATICLIBRARY}) set_target_properties(${STATICLIBRARY} PROPERTIES COMPILE_FLAGS -DCMARK_STATIC_DEFINE OUTPUT_NAME "cmark$<$:_static>" @@ -188,6 +191,7 @@ CONFIGURE_FILE( if(CMARK_LIB_FUZZER) add_executable(cmark-fuzz ../test/cmark-fuzz.c ${LIBRARY_SOURCES}) + cmark_add_compile_options(cmark-fuzz) target_link_libraries(cmark-fuzz "${CMAKE_LIB_FUZZER_PATH}") # cmark is written in C but the libFuzzer runtime is written in C++ which