Skip to content

Commit

Permalink
build: remove CMARK_STATIC and CMARK_SHARED options
Browse files Browse the repository at this point in the history
Remove the `CMARK_STATIC` and `CMARK_SHARED` options as one of the two
must be enabled always as the cmark executable depends on the library.
Instead of having a custom flag to discern between the library type, use
the native CMake option `BUILD_SHARED_LIBS` allowing the user to control
which library to build [1]. This matches recommendations to only build a
single copy of the library [2].

[1] https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html
[2] https://alexreinking.com/blog/building-a-dual-shared-and-static-library-with-cmake.html
  • Loading branch information
compnerd committed Jan 8, 2024
1 parent f3e5832 commit 5abf7b4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 110 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
fail-fast: false
matrix:
cmake_opts:
- '-DCMARK_SHARED=ON'
- '-DBUILD_SHARED_LIBS=YES'
- ''
compiler:
- c: 'clang'
Expand Down Expand Up @@ -63,7 +63,7 @@ jobs:
fail-fast: false
matrix:
cmake_opts:
- '-DCMARK_SHARED=ON'
- '-DBUILD_SHARED_LIBS=YES'
- ''
compiler:
- c: 'clang'
Expand All @@ -79,7 +79,7 @@ jobs:
- uses: actions/checkout@v4
- name: Build and test
env:
CMAKE_OPTIONS: -DCMARK_SHARED=OFF
CMAKE_OPTIONS: -DBUILD_SHARED_LIBS=NO
run: |
make
make test
Expand All @@ -91,7 +91,7 @@ jobs:
fail-fast: false
matrix:
cmake_opts:
- '-DCMARK_SHARED=ON'
- '-DBUILD_SHARED_LIBS=YES'
- ''
env:
CMAKE_OPTIONS: ${{ matrix.cmake_opts }}
Expand Down
13 changes: 1 addition & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ 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 library" ON)
option(CMARK_SHARED "Build shared libcmark library" ON)
option(CMARK_LIB_FUZZER "Build libFuzzer fuzzing harness" OFF)

if(NOT MSVC)
Expand All @@ -49,13 +47,6 @@ set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

# The Linux modules distributed with CMake add "-rdynamic" to the build flags
# which is incompatible with static linking under certain configurations.
# Unsetting CMAKE_SHARED_LIBRARY_LINK_C_FLAGS ensures this does not happen.
if(CMARK_STATIC AND "${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS)
endif()

# Check integrity of node structure when compiled as debug
add_compile_options($<$<CONFIG:Debug>:-DCMARK_DEBUG_NODES>)

Expand Down Expand Up @@ -90,9 +81,7 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
add_subdirectory(man)
endif()
if(BUILD_TESTING)
if(CMARK_SHARED OR CMARK_STATIC)
add_subdirectory(api_test)
endif()
add_subdirectory(api_test)
add_subdirectory(test testdir)
endif()

Expand Down
7 changes: 2 additions & 5 deletions api_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ add_executable(api_test
main.c
)
cmark_add_compile_options(api_test)
if(CMARK_SHARED)
target_link_libraries(api_test cmark)
else()
target_link_libraries(api_test cmark_static)
endif()
target_link_libraries(api_test PRIVATE
cmark)

add_test(NAME api_test COMMAND api_test)
if(WIN32)
Expand Down
143 changes: 55 additions & 88 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,64 +49,34 @@ add_executable(${PROGRAM} ${PROGRAM_SOURCES})
cmark_add_compile_options(${PROGRAM})
set_target_properties(${PROGRAM} PROPERTIES
OUTPUT_NAME "cmark")

if (CMARK_STATIC)
target_link_libraries(${PROGRAM} ${STATICLIBRARY})
elseif (CMARK_SHARED)
target_link_libraries(${PROGRAM} ${LIBRARY})
endif()

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"
# Avoid name clash between PROGRAM and LIBRARY pdb files.
PDB_NAME cmark_dll
# Include minor version and patch level in soname for now.
SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}
VERSION ${PROJECT_VERSION})
target_include_directories(${LIBRARY} INTERFACE
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
add_library(cmark::cmark ALIAS ${LIBRARY})

generate_export_header(${LIBRARY}
BASE_NAME ${PROJECT_NAME})

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

if (CMARK_STATIC)
add_library(${STATICLIBRARY} STATIC ${LIBRARY_SOURCES})
cmark_add_compile_options(${STATICLIBRARY})
target_compile_definitions(${STATICLIBRARY} PUBLIC
target_link_libraries(${PROGRAM} PRIVATE
cmark)

add_library(${LIBRARY}
${LIBRARY_SOURCES})
cmark_add_compile_options(${LIBRARY})
set_target_properties(${LIBRARY} PROPERTIES
MACOSX_RPATH TRUE
OUTPUT_NAME "cmark"
# Avoid name clash between PROGRAM and LIBRARY pdb files.
PDB_NAME libcmark
POSITION_INDEPENDENT_CODE $<BOOL:${BUILD_SHARED_LIBS}>
# Include minor version and patch level in soname for now.
SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}
VERSION ${PROJECT_VERSION})
if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(${LIBRARY} PUBLIC
CMARK_STATIC_DEFINE)
set_target_properties(${STATICLIBRARY} PROPERTIES
POSITION_INDEPENDENT_CODE ON
VERSION ${PROJECT_VERSION})
if(MSVC)
set_target_properties(${STATICLIBRARY} PROPERTIES
OUTPUT_NAME cmark_static)
else()
set_target_properties(${STATICLIBRARY} PROPERTIES
OUTPUT_NAME cmark)
endif()
target_include_directories(${STATICLIBRARY} INTERFACE
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
add_library(cmark::cmark_static ALIAS ${STATICLIBRARY})
endif()
target_include_directories(${LIBRARY} INTERFACE
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)

if (NOT CMARK_SHARED)
generate_export_header(${STATICLIBRARY}
BASE_NAME ${PROJECT_NAME})
endif()
generate_export_header(${LIBRARY}
BASE_NAME ${PROJECT_NAME})

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

install(TARGETS ${PROGRAM} ${CMARK_INSTALL}
EXPORT cmark-targets
Expand All @@ -115,42 +85,39 @@ install(TARGETS ${PROGRAM} ${CMARK_INSTALL}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

if(CMARK_SHARED OR CMARK_STATIC)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libcmark.pc.in
${CMAKE_CURRENT_BINARY_DIR}/libcmark.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libcmark.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

install(FILES
cmark.h
${CMAKE_CURRENT_BINARY_DIR}/cmark_export.h
${CMAKE_CURRENT_BINARY_DIR}/cmark_version.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libcmark.pc.in
${CMAKE_CURRENT_BINARY_DIR}/libcmark.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libcmark.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

# generate cmark-config.cmake and cmark-config-version.cmake files
configure_package_config_file(
"cmarkConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/generated/cmark-config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cmark")
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/generated/cmark-config-version.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
# install config and version file
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/generated/cmark-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/generated/cmark-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cmark"
)
# install targets file
install(
EXPORT "cmark-targets"
NAMESPACE "cmark::"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cmark"
install(FILES
cmark.h
${CMAKE_CURRENT_BINARY_DIR}/cmark_export.h
${CMAKE_CURRENT_BINARY_DIR}/cmark_version.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

endif()
# generate cmark-config.cmake and cmark-config-version.cmake files
configure_package_config_file(
"cmarkConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/generated/cmark-config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cmark")
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/generated/cmark-config-version.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
# install config and version file
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/generated/cmark-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/generated/cmark-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cmark"
)
# install targets file
install(
EXPORT "cmark-targets"
NAMESPACE "cmark::"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cmark"
)

if(CMARK_LIB_FUZZER)
add_executable(cmark-fuzz ../test/cmark-fuzz.c ${LIBRARY_SOURCES})
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ IF (Python3_Interpreter_FOUND)
add_test(NAME html_normalization
COMMAND "$<TARGET_FILE:Python3::Interpreter>" -m doctest "${CMAKE_CURRENT_SOURCE_DIR}/normalize.py")

if (CMARK_SHARED)
if(BUILD_SHARED_LIBS)
add_test(NAME spectest_library
COMMAND "$<TARGET_FILE:Python3::Interpreter>" "${CMAKE_CURRENT_SOURCE_DIR}/spec_tests.py"
--no-normalize
Expand Down

0 comments on commit 5abf7b4

Please sign in to comment.