Update CMakeLists.txt to use IOCore library framewokr

This commit is contained in:
S David 2024-03-30 14:32:13 -04:00
parent 0fc2c38572
commit f2d009dd3e
5 changed files with 116 additions and 183 deletions

2
.gitignore vendored
View File

@ -60,3 +60,5 @@ release/*
docs/*
*.bak
.cmake/
.vscode/

View File

@ -1,6 +1,9 @@
cmake_minimum_required(VERSION 3.26)
set(mdml_CMAKE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(mdml_INCLUDE_DIR ${mdml_CMAKE_SOURCE_DIR}/include)
# Additional paths to search for custom and third-party CMake modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
@ -16,7 +19,7 @@ disable_deprecated_features()
disable_tests_if_subproject()
project(mdml
VERSION 0.2
VERSION 0.3.0
LANGUAGES C CXX
# HOMEPAGE_URL
DESCRIPTION "A CGI script written in C++ that converts markdown to HTML"
@ -30,10 +33,6 @@ else()
option(USE_BOOST_STACKTRACE "Use Boost for stacktraces, instead of execinfo.h" OFF)
endif()
# Define an option that can be overridden by the user
# Example usage
IF( NOT CMAKE_BUILD_TYPE )
SET( CMAKE_BUILD_TYPE Debug )
ENDIF()
@ -64,8 +63,37 @@ endif()
include(FetchContent)
include(CheckIncludeFile)
CPMFindPackage(NAME cmark
GITHUB_REPOSITORY commonmark/cmark
GIT_TAG 0.31.0
OPTIONS
"BUILD_SHARED_LIBS OFF"
"BUILD_TESTING OFF"
)
if (cmark_ADDED AND TARGET cmark_exe)
set_target_properties(cmark_exe PROPERTIES EXCLUDE_FROM_ALL 1)
endif()
if (ENABLE_TESTS)
if(cmark_FOUND)
list(APPEND CMAKE_MODULE_PATH ${cmark_DIR})
endif()
CPMFindPackage(NAME nlohmann_json
GITHUB_REPOSITORY nlohmann/json
GIT_TAG 3.11.2
OPTIONS
"BUILD_SHARED_LIBS OFF"
"BUILD_TESTING OFF"
)
CPMAddPackage(NAME IOCore
GIT_REPOSITORY "https://gitea.beniquez.me/sdaveb/IOCore.git"
GIT_TAG v0.2.7
OPTIONS
"BUILD_SHARED_LIBS OFF"
"BUILD_TESTING OFF"
)
if (BUILD_TESTING)
CPMFindPackage(NAME Catch2
GITHUB_REPOSITORY catchorg/Catch2
VERSION 3.4.0
@ -79,158 +107,28 @@ if (ENABLE_TESTS)
OPTIONS
"ENABLE_TESTING OFF"
)
if (Catch2_ADDED)
if (TARGET Catch2)
set_target_properties(Catch2 PROPERTIES
CXX_STANDARD 20
)
elseif (TARGET Catch2WithMain)
endif()
if (TARGET Catch2WithMain)
set_target_properties(Catch2WithMain PROPERTIES
CXX_STANDARD 20
)
endif()
if (Catch2_ADDED STREQUAL "YES")
list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
else()
list(APPEND CMAKE_MODULE_PATH ${Catch2_DIR})
message("Using internal cmake")
endif()
endif()
find_package(PkgConfig REQUIRED)
pkg_check_modules(cmark libcmark)
pkg_check_modules(json "nlohmann_json >= 3.11.2" REQUIRED)
CPMFindPackage(NAME cmark
GITHUB_REPOSITORY commonmark/cmark
GIT_TAG 0.31.0
OPTIONS
"BUILD_SHARED_LIBS OFF"
"BUILD_TESTING OFF"
)
#if (cmark_FOUND)
# set(USE_SYSTEM_CMARK True)
#else()
# set(USE_SYSTEM_CMARK False)
#
# CPMFindPackage(NAME cmark
# GITHUB_REPOSITORY commonmark/cmark
# GIT_TAG 0.31.0
# OPTIONS
# "BUILD_SHARED_LIBS OFF"
# "BUILD_TESTING OFF"
# )
#
# if(TARGET cmark)
# set_target_properties(
# cmark PROPERTIES
# C_STANDARD 17
# C_EXTENSIONS ON
# )
# endif(TARGET cmark)
# if(TARGET cmark_static)
# set_target_properties(
# cmark_static PROPERTIES
# C_STANDARD 17
# C_EXTENSIONS ON
# )
# endif(TARGET cmark_static)
#endif(NOT cmark_FOUND)
set(STACKTRACE_DEP_LIBS, "")
# If using the system cmark libraries, we need to import the include and library
# directories
if (cmark_FOUND OR cmark_ADDED)
include_directories(${cmark_INCLUDE_DIRS})
link_directories(${cmark_LIBRARY_DIRS})
endif()
if (USE_EXECINFO_STACKTRACE)
CHECK_INCLUDE_FILE("execinfo.h" HAVE_EXECINFO_H)
if (HAVE_EXECINFO_H)
add_definitions(-DHAVE_EXECINFO_H=1)
endif()
find_library(LIB_EXEC_INFO
NAMES execinfo # Specify the library name without the 'lib' prefix or file extension
HINTS /usr/lib /usr/local/lib # Optional hint for the library location
)
if (LIB_EXEC_INFO)
message(STATUS "Found libexecinfo: ${LIB_EXEC_INFO}")
list(APPEND STACKTRACE_DEP_LIBS ${LIB_EXEC_INFO})
endif()
endif()
# Defining and handling specific project directories
include_directories(
${CMAKE_SOURCE_DIR}/include
)
set(ARTIFACT_DIR ${CMAKE_BINARY_DIR}/out)
set(CMAKE_ARCHIVE_ARTIFACT_DIRECTORY ${ARTIFACT_DIR}/lib)
set(CMAKE_LIBRARY_ARTIFACT_DIRECTORY ${ARTIFACT_DIR}/lib)
set(CMAKE_RUNTIME_ARTIFACT_DIRECTORY ${ARTIFACT_DIR}/bin)
add_custom_target(copy_assets
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/data ${CMAKE_BINARY_DIR}/out/share/data
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/data ${CMAKE_BINARY_DIR}/tests/data
)
# Specifying configuration for Debug vs Release
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
#set(custom_dbg_flags "-O0 -ggdb")
add_compile_definitions("-DDEBUG=1")
# Custom flags for Debug configuration
#set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${custom_dbg_flags}")
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${custom_dbg_flags}")
#elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
#set(custom_rel_flags "-Os")
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${custom_rel_flags}")
#set(
# CMAKE_CXX_FLAGS_RELEASE
# "${CMAKE_CXX_FLAGS_RELEASE} ${custom_rel_flags}"
#)
endif()
if(USE_BOOST_STACKTRACE)
CPMFindPackage(
NAME Boost
URL https://github.com/boostorg/boost/releases/download/boost-1.82.0/boost-1.82.0.tar.gz
URL_HASH SHA256=b62bd839ea6c28265af9a1f68393eda37fab3611425d3b28882d8e424535ec9d
OPTIONS
"BOOST_ENABLE_CMAKE ON"
"BOOST_INCLUDE_LIBRARIES system\\\;stacktrace"
"BOOST_USE_STATIC_LIBS OFF"
"BOOST_USE_STATIC_RUNTIME OFF"
"BOOST_USE_MULTITHREADED ON"
"GIT_SUBMODULES stacktrace"
)
include_directories(SYSTEM ${Boost_INCLUDE_DIR})
#set(Boost_USE_STATIC_LIBS OFF)
#set(Boost_USE_STATIC_RUNTIME OFF) # Do not require static C++ runtime
#set(Boost_USE_MULTITHREADED ON)
#find_package(Boost 1.82.0 COMPONENTS system filesystem REQUIRED)
list(APPEND STACKTRACE_DEP_LIBS Boost::system)
add_definitions(-DBOOST_STACKTRACE_USE_ADDR2LINE=1)
endif()
add_subdirectory(src)
if (ENABLE_TESTS)
if (BUILD_TESTING)
add_subdirectory(tests)
endif()
#add_subdirectory(apps)
# vim: ft=cmake sw=4 ts=4 noet sts=4 foldmethod=indent :

View File

@ -21,11 +21,38 @@ endfunction()
function(disable_tests_if_subproject)
if (NOT DEFINED PROJECT_NAME)
option(ENABLE_TESTS "Build and run unit tests" ON)
option(BUILD_TESTING "Build and run unit tests" ON)
else()
option(ENABLE_TESTS "Build and run unit tests" OFF)
option(BUILD_TESTING "Build and run unit tests" OFF)
endif()
endfunction()
option(LOCAL_INCLUDES "Automatically add /usr/local/include to include search paths" ON)
option(LOCAL_LIBS "Automatically add /usr/local/lib to linker search paths" ON)
function(include_usr_local)
if (LOCAL_INCLUDES)
if(APPLE)
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
include_directories(BEFORE SYSTEM /usr/local/include)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
include_directories(BEFORE SYSTEM /opt/homebrew/include)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
include_directories(BEFORE SYSTEM /usr/local/include)
endif()
endif()
if (LOCAL_LIBS)
if(APPLE)
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
link_directories(BEFORE /usr/local/lib)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
link_directories(BEFORE /opt/homebrew/lib)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
link_directories(BEFORE /usr/local/lib)
endif()
endif()
endfunction()
# vim: ts=4 sts=4 sw=4 noet :

View File

@ -1,44 +1,51 @@
# Copyright (c) 2024 Saul D. Beniquez
# License: MIT
set(MDML_SOURCES
libmdml.cpp
Exception.cpp
debuginfo.cpp
Application.cpp
CgiApplication.cpp
MarkdownRouteHandler.cpp
)
add_library(mdml OBJECT
${MDML_SOURCES}
libmdml.cpp
MarkdownRouteHandler.cpp
CgiApplication.cpp
)
set(LIBMDML_DEP_LIBS ${LIBMDML_DEP_LIBS} ${STACKTRACE_DEP_LIBS} )
if(json_FOUND)
target_include_directories(mdml PUBLIC ${json_INCLUDE_DIRS})
set(LIBMDML_DEP_LIBS ${LIBMDML_DEP_LIBS} ${json_LIBRARIES} )
endif()
if (USE_SYSTEM_CMARK)
set(LIBMDML_DEP_LIBS ${LIBMDML_DEP_LIBS} ${cmark_LIBRARIES} )
else()
if (NOT CMARK_STATIC_LINKAGE)
set(LIBMDML_DEP_LIBS cmark)
else()
set(LIBMDML_DEP_LIBS cmark_static)
endif()
endif()
set_target_properties(mdml PROPERTIES
CMAKE_POSITION_INDEPENDENT_CODE OFF
POSITION_INDEPENDENT_CODE ON
)
# Check if the system is Linux
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
# Add the -rdynamic linker option
target_link_options(mdml PUBLIC -rdynamic)
set(mdml_DEP_LIBS)
set(cmark_INCLUDE_DIRS)
if(cmark_ADDED)
if (TARGET cmark)
list(APPEND mdml_DEP_LIBS cmark)
endif()
get_target_property(cmark_INCLUDE_DIRS cmark INTERFACE_INCLUDE_DIRECTORIES)
elseif(cmark_FOUND OR cmark_DIR)
list(APPEND mdml_DEP_LIBS cmark::cmark)
get_target_property(cmark_INCLUDE_DIRS cmark::cmark INTERFACE_INCLUDE_DIRECTORIES)
endif()
target_link_libraries(mdml PUBLIC ${LIBMDML_DEP_LIBS})
if(nlohmann_json_ADDED OR nlohmann_json_FOUND)
set(mdml_DEP_LIBS ${mdml_DEP_LIBS} nlohmann_json::nlohmann_json} )
endif()
if (IOCore_ADDED)
list(APPEND mdml_DEP_LIBS IOCore::IOCoreStatic)
endif()
target_include_directories(mdml PUBLIC ${mdml_INCLUDE_DIR})
add_library(mdml_shared SHARED )
add_library(mdml::mdml_shared ALIAS mdml_shared)
set_target_properties(mdml_shared PROPERTIES
OUTPUT_NAME "mdml"
POSITION_INDEPENDENT_CODE ON
)
add_library(mdml_static STATIC)
add_library(mdml::mdml_static ALIAS mdml_static)
set_target_properties(mdml_static PROPERTIES OUTPUT_NAME "mdml")
target_link_libraries(mdml PUBLIC ${mdml_DEP_LIBS})
target_link_libraries(mdml_shared PRIVATE mdml)
target_link_libraries(mdml_static PRIVATE mdml)

View File

@ -26,7 +26,6 @@ PRIVATE
# Extras: Catch2 Ctest integration. @{
# add catch2's ctest CMake module and register the tests defined by test-runner
# using catch_discover_tests
list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
catch_discover_tests(test-runner)