Skip to content

Commit

Permalink
assorted fixes to the build system
Browse files Browse the repository at this point in the history
Remove the use of `CMARK_INLINE` (as is being considered upstream) as
this workaround is no longer needed. It was to support VS2013 which has
been obsoleted for some time now.

Restructure the CMake modules to match expectations for CMake.

Avoid defining `likely` and `unlikely` in global scope. While doing so,
prefer the modern `__has_builtin` for the check (with fallbacks for
pre-GCC 4.10, clang 3.4, and MSVC). This largely shouldn't matter in
practice as modern branch predictors do well without the hint. Sink the
declaration into source files as the macros are not namespaced.

Remove the now unnecessary `config.h`.

Clean up the builds to only support a single build type which is
dependent on the standard `BUILD_SHARED_LIBS`.

Simplify and normalise the install rules to use the `GNUInstallDirs`
parameters.
  • Loading branch information
compnerd committed Jan 4, 2024
1 parent 1dc6da9 commit fe3902c
Show file tree
Hide file tree
Showing 32 changed files with 217 additions and 393 deletions.
49 changes: 18 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,43 @@ if(POLICY CMP0092)
cmake_policy(SET CMP0092 NEW)
endif()

project(cmark-gfm)

set(PROJECT_VERSION_MAJOR 0)
set(PROJECT_VERSION_MINOR 29)
set(PROJECT_VERSION_PATCH 0)
set(PROJECT_VERSION_GFM 13)
set(PROJECT_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}.gfm.${PROJECT_VERSION_GFM})

include("FindAsan.cmake")
include("CheckFileOffsetBits.cmake")
include(CTest)
include(GNUInstallDirs)
project(cmark-gfm
LANGUAGES C CXX)
set(PROJECT_VERSION 0.29.0.gfm.13)

if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
message(FATAL_ERROR "Do not build in-source.\nPlease remove CMakeCache.txt and the CMakeFiles/ directory.\nThen: mkdir build ; cd build ; cmake .. ; make")
endif()

option(CMARK_STATIC "Build static libcmark-gfm library" ON)
option(CMARK_SHARED "Build shared libcmark-gfm library" ON)
option(CMARK_LIB_FUZZER "Build libFuzzer fuzzing harness" OFF)
option(CMARK_FUZZ_QUADRATIC "Build quadratic fuzzing harness" OFF)

if(NOT MSVC)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED YES)
set(CMAKE_C_EXTENSIONS NO)
endif()

# -fvisibility=hidden
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED YES)
set(CMAKE_C_EXTENSIONS NO)
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_VISIBILITY_INLINES_HIDDEN TRUE)

if(NOT MSVC OR CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS ON)
include(InstallRequiredSystemLibraries)
endif()

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)

include(CheckFileOffsetBits)
include(CTest)
include(FindAsan)
include(GenerateExportHeader)
include(GNUInstallDirs)

check_file_offset_bits()

# Compiler flags
if(MSVC)
add_compile_options($<$<COMPILE_LANGUAGE:C>:/W4>)
add_compile_options($<$<COMPILE_LANGUAGE:C>:/wd4706>)
# Compile as C++ under MSVC older than 12.0
add_compile_options($<$<COMPILE_LANGUAGE:C>:/TP>)
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:_CRT_SECURE_NO_WARNINGS>)
# BEGIN cmark-gfm
add_compile_options($<$<COMPILE_LANGUAGE:C>:/wd4204>)
Expand Down Expand Up @@ -80,11 +73,7 @@ if(CMARK_LIB_FUZZER)
endif()

if(CMARK_FUZZ_QUADRATIC)
set(FUZZER_FLAGS "-fsanitize=fuzzer-no-link,address -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FUZZER_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FUZZER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FUZZER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${FUZZER_FLAGS}")
add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-fsanitize=fuzzer-no-link,address>)
endif()

add_subdirectory(src)
Expand All @@ -96,9 +85,7 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
endif()
if(BUILD_TESTING)
add_subdirectory(test testdir)
if(CMARK_STATIC OR CMARK_SHARED)
add_subdirectory(api_test)
endif()
add_subdirectory(api_test)
endif()
if(CMARK_FUZZ_QUADRATIC)
add_subdirectory(fuzz)
Expand Down
17 changes: 4 additions & 13 deletions api_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
add_executable(api_test
cplusplus.cpp
harness.c
harness.h
main.c
)
include_directories(
${PROJECT_SOURCE_DIR}/src
${PROJECT_BINARY_DIR}/src
${PROJECT_BINARY_DIR}/extensions
)
if(CMARK_SHARED)
target_link_libraries(api_test libcmark-gfm-extensions libcmark-gfm)
else()
target_link_libraries(api_test libcmark-gfm-extensions_static libcmark-gfm_static)
endif()
main.c)
target_link_libraries(api_test PRIVATE
libcmark-gfm
libcmark-gfm-extensions)

add_test(NAME api_test COMMAND api_test)
if(WIN32)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
111 changes: 28 additions & 83 deletions extensions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,85 +1,30 @@
set(LIBRARY "libcmark-gfm-extensions")
set(STATICLIBRARY "libcmark-gfm-extensions_static")
set(LIBRARY_SOURCES
core-extensions.c
table.c
strikethrough.c
autolink.c
tagfilter.c
ext_scanners.c
ext_scanners.re
ext_scanners.h
tasklist.c
)

include_directories(
${PROJECT_SOURCE_DIR}/src
${PROJECT_BINARY_DIR}/src
)

include_directories(. ${CMAKE_CURRENT_BINARY_DIR})

if (CMARK_SHARED)
add_library(${LIBRARY} SHARED ${LIBRARY_SOURCES})

set_target_properties(${LIBRARY} PROPERTIES
OUTPUT_NAME "cmark-gfm-extensions"
DEFINE_SYMBOL "cmark-gfm"
SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}.gfm.${PROJECT_VERSION_GFM}
VERSION ${PROJECT_VERSION})

set_property(TARGET ${LIBRARY}
APPEND PROPERTY MACOSX_RPATH true)

# Avoid name clash between PROGRAM and LIBRARY pdb files.
set_target_properties(${LIBRARY} PROPERTIES PDB_NAME cmark-gfm-extensions_dll)

list(APPEND CMARK_INSTALL ${LIBRARY})
target_link_libraries(${LIBRARY} libcmark-gfm)

endif()

if (CMARK_STATIC)
add_library(${STATICLIBRARY} STATIC ${LIBRARY_SOURCES})
target_compile_definitions(${STATICLIBRARY} PUBLIC
CMARK_GFM_STATIC_DEFINE
CMARK_GFM_EXTENSIONS_STATIC_DEFINE)
set_target_properties(${STATICLIBRARY} PROPERTIES
DEFINE_SYMBOL "cmark-gfm"
POSITION_INDEPENDENT_CODE ON)

if (MSVC)
set_target_properties(${STATICLIBRARY} PROPERTIES
OUTPUT_NAME "cmark-gfm-extensions_static"
VERSION ${PROJECT_VERSION})
else()
set_target_properties(${STATICLIBRARY} PROPERTIES
OUTPUT_NAME "cmark-gfm-extensions"
VERSION ${PROJECT_VERSION})
endif(MSVC)

list(APPEND CMARK_INSTALL ${STATICLIBRARY})
endif()

install(TARGETS ${CMARK_INSTALL}
add_library(libcmark-gfm-extensions
autolink.c
core-extensions.c
ext_scanners.c
ext_scanners.h
ext_scanners.re
strikethrough.c
table.c
tagfilter.c
tasklist.c)
set_target_properties(libcmark-gfm-extensions PROPERTIES
DEFINE_SYMBOL cmark-gfm
MACOSX_RPATH TRUE
OUTPUT_NAME cmark-gfm-extensions
PDB_NAME libcmark-gfm-extensions
SOVERSION ${PROJECT_VERSION}
VERSION ${PROJECT_VERSION})
target_link_libraries(libcmark-gfm-extensions PRIVATE
libcmark-gfm)


install(TARGETS libcmark-gfm-extensions
EXPORT cmark-gfm-extensions
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib${LIB_SUFFIX}
ARCHIVE DESTINATION lib${LIB_SUFFIX}
)

if (CMARK_SHARED OR CMARK_STATIC)
install(FILES
cmark-gfm-core-extensions.h
DESTINATION include
)

install(EXPORT cmark-gfm-extensions DESTINATION lib${LIB_SUFFIX}/cmake-gfm-extensions)
endif()

# Feature tests
include(CheckCSourceCompiles)

CHECK_C_SOURCE_COMPILES(
"int main() { __builtin_expect(0,0); return 0; }"
HAVE___BUILTIN_EXPECT)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES cmark-gfm-core-extensions.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT cmark-gfm-extensions
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake-gfm-extensions)
Loading

0 comments on commit fe3902c

Please sign in to comment.