utempl/CMakeLists.txt

233 lines
6.9 KiB
Text
Raw Normal View History

2024-07-31 02:19:28 +00:00
cmake_minimum_required(VERSION 3.30)
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD
"0e5b6991-d74f-4b3d-a41c-cf096e0b2508")
2024-03-04 17:39:09 +00:00
project(utempl
2024-07-31 02:19:28 +00:00
VERSION 0.1
LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
2024-02-27 18:54:37 +00:00
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2024-07-31 02:19:28 +00:00
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2024-07-06 00:32:33 +00:00
set(BOOST_INCLUDE_LIBRARIES "pfr")
2024-10-19 23:37:49 +00:00
set(NAMEOF_OPT_INSTALL ON)
2024-07-31 02:19:28 +00:00
2024-07-06 00:32:33 +00:00
option(CPM_USE_LOCAL_PACKAGES "Use local packages" ON)
option(UTEMPL_MODULE OFF)
2024-08-28 12:46:49 +00:00
if(UTEMPL_MODULE OR ENABLE_EXAMPLES OR ENABLE_TESTS)
set(CMAKE_CXX_MODULE_STD 1)
endif()
2024-07-31 02:19:28 +00:00
file(GLOB_RECURSE SOURCES "src/*.cpp")
2024-08-28 12:01:41 +00:00
if(TARGET __cmake_cxx23)
set_target_properties(__cmake_cxx23 PROPERTIES CXX_EXTENSIONS OFF)
endif()
2024-07-06 00:32:33 +00:00
file(
DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.40.0/CPM.cmake
${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake
EXPECTED_HASH
SHA256=7b354f3a5976c4626c876850c93944e52c83ec59a159ae5de5be7983f0e17a2a
)
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)
CPMAddPackage(
NAME Boost
VERSION 1.85.0
URL "https://github.com/boostorg/boost/releases/download/boost-1.85.0/boost-1.85.0-cmake.tar.xz"
EXCLUDE_FROM_ALL ON
OPTIONS "BOOST_SKIP_INSTALL_RULES OFF"
)
if(UTEMPL_MODULE)
2024-07-31 02:19:28 +00:00
set(TMP ${CPM_USE_LOCAL_PACKAGES})
set(CPM_USE_LOCAL_PACKAGES OFF)
CPMAddPackage(
2024-10-19 23:37:49 +00:00
NAME nameof
2024-07-31 02:19:28 +00:00
URL
2024-10-19 23:37:49 +00:00
"https://helicopter.myftp.org/git/sha512sum/nameof_module/archive/master.tar.gz"
EXCLUDE_FROM_ALL ON
2024-10-19 23:37:49 +00:00
OPTIONS "NAMEOF_MODULE ON"
2024-07-31 02:19:28 +00:00
)
set(CPM_USE_LOCAL_PACKAGES ${TMP})
2024-07-06 00:32:33 +00:00
else()
2024-10-19 23:37:49 +00:00
CPMAddPackage("gh:Neargye/nameof@0.10.4")
2024-07-06 00:32:33 +00:00
2024-07-31 02:19:28 +00:00
endif()
2024-10-19 23:37:49 +00:00
2024-08-27 16:34:14 +00:00
cmake_policy(SET CMP0079 NEW)
2024-07-31 02:19:28 +00:00
2024-08-27 16:34:14 +00:00
include(GNUInstallDirs)
2024-07-31 02:19:28 +00:00
file(MAKE_DIRECTORY "_headers")
file(MAKE_DIRECTORY "_headers/utempl")
file(MAKE_DIRECTORY "_headers/utempl/loopholes")
if(UTEMPL_MODULE)
add_library(utempl STATIC)
target_compile_definitions(utempl PUBLIC UTEMPL_MODULE)
target_compile_options(utempl PUBLIC "-Wno-pragma-once-outside-header")
target_sources(utempl PUBLIC FILE_SET utemplModules TYPE CXX_MODULES
FILES ${SOURCES})
file(GLOB_RECURSE HEADER_SOURCES "src/*.hpp")
set(EXISTING_HEADERS)
foreach(HPP_FILE ${HEADER_SOURCES})
2024-08-28 12:46:49 +00:00
get_filename_component(REL_PATH ${HPP_FILE} DIRECTORY)
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/src" "" REL_PATH ${REL_PATH})
get_filename_component(FILE_NAME ${HPP_FILE} NAME_WE)
set(FILE_NAME "${REL_PATH}/${FILE_NAME}")
set(HEADER_FILE "_headers/utempl/${FILE_NAME}.hpp")
file(READ ${HPP_FILE} FILE_CONTENT)
2024-08-28 12:46:49 +00:00
if(EXISTS ${HEADER_FILE})
file(READ ${HEADER_FILE} HEADER_CONTENT)
if(NOT FILE_CONTENT STREQUAL HEADER_CONTENT)
file(WRITE ${HEADER_FILE} "${FILE_CONTENT}")
endif()
else()
file(WRITE ${HEADER_FILE} "${FILE_CONTENT}")
endif()
list(APPEND EXISTING_HEADERS "${HEADER_FILE}")
endforeach()
file(GLOB HEADER_FILES "_headers/*.hpp")
foreach(HEADER_FILE ${HEADER_FILES})
if(NOT HEADER_FILE IN_LIST EXISTING_HEADERS)
file(REMOVE ${HEADER_FILE})
endif()
endforeach()
if(TARGET Boost::pfr)
2024-10-19 23:37:49 +00:00
target_link_libraries(utempl PUBLIC nameof::nameof
Boost::pfr)
else()
find_package(Boost 1.85.0 REQUIRED)
2024-10-19 23:37:49 +00:00
target_link_libraries(utempl PUBLIC nameof::nameof
${Boost_LIBRARIES})
endif()
2024-08-28 12:46:49 +00:00
target_include_directories(utempl PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/_headers>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
else()
add_library(utempl INTERFACE)
file(GLOB_RECURSE HEADER_SOURCES "src/*.hpp")
set(EXISTING_HEADERS)
foreach(CPP_FILE ${SOURCES} ${HEADER_SOURCES})
2024-08-28 12:46:49 +00:00
get_filename_component(REL_PATH ${CPP_FILE} DIRECTORY)
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/src" "" REL_PATH ${REL_PATH})
get_filename_component(FILE_NAME ${CPP_FILE} NAME_WE)
set(FILE_NAME "${REL_PATH}/${FILE_NAME}")
set(HEADER_FILE "_headers/utempl/${FILE_NAME}.hpp")
file(READ ${CPP_FILE} FILE_CONTENT)
2024-08-28 12:46:49 +00:00
if(EXISTS ${HEADER_FILE})
file(READ ${HEADER_FILE} HEADER_CONTENT)
if(NOT FILE_CONTENT STREQUAL HEADER_CONTENT)
file(WRITE ${HEADER_FILE} "${FILE_CONTENT}")
endif()
else()
file(WRITE ${HEADER_FILE} "${FILE_CONTENT}")
endif()
list(APPEND EXISTING_HEADERS "${HEADER_FILE}")
endforeach()
file(GLOB HEADER_FILES "_headers/*.hpp")
foreach(HEADER_FILE ${HEADER_FILES})
if(NOT HEADER_FILE IN_LIST EXISTING_HEADERS)
file(REMOVE ${HEADER_FILE})
endif()
endforeach()
if(TARGET Boost::pfr)
2024-10-19 23:37:49 +00:00
target_link_libraries(utempl INTERFACE nameof::nameof
Boost::pfr nameof::nameof)
else()
find_package(Boost 1.85.0 REQUIRED)
2024-10-19 23:37:49 +00:00
target_link_libraries(utempl INTERFACE nameof::nameof
${Boost_LIBRARIES} nameof::nameof)
endif()
2024-08-28 12:46:49 +00:00
target_include_directories(utempl INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/_headers>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
endif()
2024-08-27 16:34:14 +00:00
add_library(utempl::utempl ALIAS utempl)
target_compile_features(utempl INTERFACE cxx_std_23)
2024-07-06 00:32:33 +00:00
2024-08-28 12:46:49 +00:00
2024-07-06 00:32:33 +00:00
2024-03-04 17:39:09 +00:00
2024-07-06 02:03:58 +00:00
2024-08-27 16:34:14 +00:00
install(TARGETS utempl
EXPORT utemplTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
FILE_SET utemplModules DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
2024-03-04 17:39:09 +00:00
2024-08-27 16:34:14 +00:00
2024-03-04 17:39:09 +00:00
include(CMakePackageConfigHelpers)
write_basic_package_version_file("utemplConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/utemplConfig.cmake.in"
"${PROJECT_BINARY_DIR}/utemplConfig.cmake"
INSTALL_DESTINATION
${CMAKE_INSTALL_DATAROOTDIR}/utempl/cmake)
install(EXPORT utemplTargets
FILE utemplTargets.cmake
NAMESPACE utempl::
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/utempl/cmake)
install(FILES "${PROJECT_BINARY_DIR}/utemplConfig.cmake"
"${PROJECT_BINARY_DIR}/utemplConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/utempl/cmake)
2024-08-28 12:46:49 +00:00
install(DIRECTORY ${PROJECT_SOURCE_DIR}/_headers/utempl DESTINATION include)
2024-03-04 17:39:09 +00:00
if(ENABLE_TESTS)
2024-08-28 12:46:49 +00:00
CPMAddPackage(
NAME GTest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
enable_testing()
file(GLOB SOURCES tests/* tests/*/* tests/*/*/*)
2024-07-31 02:19:28 +00:00
add_executable(utempl_tests)
target_sources(utempl_tests PUBLIC ${SOURCES})
2024-07-31 02:19:28 +00:00
target_link_libraries(utempl_tests GTest::gtest_main utempl::utempl)
2024-07-15 13:54:41 +00:00
set_property(TARGET utempl_tests PROPERTY CXX_STANDARD 23)
include(GoogleTest)
gtest_discover_tests(utempl_tests)
endif()
2024-02-28 23:18:15 +00:00
if(ENABLE_EXAMPLES)
file(GLOB EXAMPLES_SRC "examples/src/*.cpp")
foreach(EXAMPLE_SRC ${EXAMPLES_SRC})
get_filename_component(EXAMPLE_NAME ${EXAMPLE_SRC} NAME_WE)
add_executable(${EXAMPLE_NAME} ${EXAMPLE_SRC})
2024-07-31 02:19:28 +00:00
target_link_libraries(${EXAMPLE_NAME} utempl::utempl)
2024-07-15 13:54:41 +00:00
set_property(TARGET ${EXAMPLE_NAME} PROPERTY CXX_STANDARD 23)
2024-02-28 23:18:15 +00:00
set_target_properties(${EXAMPLE_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/examples/output")
2024-02-28 23:18:15 +00:00
endforeach()
endif()