Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct CMake issues #68

Merged
merged 13 commits into from
Nov 13, 2023
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
-DCMAKE_BUILD_TYPE=${{matrix.target}} \
-DCMAKE_CXX_COMPILER=${{matrix.compiler}} \
-DCPPTRACE_BACKTRACE_PATH=/usr/lib/gcc/x86_64-linux-gnu/11/include/backtrace.h \
-DASSERT_BUILD_TESTS=On
-DASSERT_BUILD_TESTING=On
make -j
- name: test
working-directory: build
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
-DCMAKE_BUILD_TYPE=${{matrix.target}} \
-DCMAKE_CXX_COMPILER=${{matrix.compiler}} \
-DCPPTRACE_BACKTRACE_PATH=/usr/lib/gcc/x86_64-linux-gnu/11/include/backtrace.h \
-DASSERT_BUILD_TESTS=On
-DASSERT_BUILD_TESTING=On
make -j
- name: test
working-directory: build
Expand All @@ -82,7 +82,7 @@ jobs:
cmake .. `
-DCMAKE_BUILD_TYPE=${{matrix.target}} `
-DCMAKE_CXX_COMPILER=${{matrix.compiler}} `
-DASSERT_BUILD_TESTS=On
-DASSERT_BUILD_TESTING=On
msbuild .\libassert.sln
- name: test
working-directory: build
Expand All @@ -106,7 +106,7 @@ jobs:
cmake .. `
-DCMAKE_BUILD_TYPE=${{matrix.target}} `
-DCMAKE_CXX_COMPILER=${{matrix.compiler}} `
-DASSERT_BUILD_TESTS=On `
-DASSERT_BUILD_TESTING=On `
"-GUnix Makefiles"
make -j
- name: test
Expand Down
318 changes: 126 additions & 192 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,230 +1,164 @@
cmake_minimum_required(VERSION 3.8...3.23)
cmake_minimum_required(VERSION 3.14)

if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()
include(cmake/PreventInSourceBuilds.cmake)

project(
libassert
VERSION 1.2.1
LANGUAGES CXX
)

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
# ---- Initialize Project ----

### libassert uses relocs, -fpic has to be used to allow shared libs to link libassert as a static library.
##set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# used to support find_package
set(package_name "assert")

include(GNUInstallDirs)
include(FetchContent)
# create base project
project(
libassert
VERSION 1.2.1
DESCRIPTION "The most over-engineered C++ assertion library"
HOMEPAGE_URL "https://github.com/jeremy-rifkin/libassert"
LANGUAGES CXX
)

option(ASSERT_DECOMPOSE_BINARY_LOGICAL "Enables expression decomposition of && and || (this prevents short circuiting)" OFF)
option(ASSERT_LOWERCASE "Enables assert alias for ASSERT" OFF)
option(ASSERT_USE_MAGIC_ENUM "Use the MagicEnum library to print better diagnostics for enum classes" ON)
option(ASSERT_USE_EXTERNAL_CPPTRACE "Controls whether cpptrace is gathered with FetchContent or fina_package" OFF)
option(ASSERT_STATIC "Specifies libassert as a static library" OFF)
set(ASSERT_FAIL "" CACHE STRING "ASSERT_FAIL")
# don't change include order, OptionVariables checks if project is top level
include(cmake/ProjectIsTopLevel.cmake)
include(cmake/OptionVariables.cmake)

option(ASSERT_BUILD_TESTS "Build tests" OFF)
option(ASSERT_BUILD_BASIC_TEST "Build basic tests" OFF)
mark_as_advanced(
ASSERT_BUILD_TESTS
ASSERT_BUILD_BASIC_TEST
)

if(ASSERT_STATIC)
add_library(assert STATIC src/assert.cpp include/assert.hpp)
# ---- Project Dependencies ----

# obtain cpptrace
if(ASSERT_USE_EXTERNAL_CPPTRACE)
find_package(cpptrace REQUIRED)
else()
add_library(assert SHARED src/assert.cpp include/assert.hpp)
include(FetchContent)
FetchContent_Declare(
cpptrace
GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git
GIT_TAG v0.2.1
)
FetchContent_MakeAvailable(cpptrace)
endif()

target_include_directories(
assert
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/assert/assert>
)
# cpptrace potentially does not have an alias target
if(NOT TARGET cpptrace::cpptrace)
add_library(cpptrace::cpptrace ALIAS cpptrace)
endif()

#set(CMAKE_CXX_FLAGS_REL_WITH_ASSERTS "-O3")

# TODO
target_compile_features(
assert
PUBLIC
cxx_std_17
# ---- Declare Library ----

# target that we can modify (can't modify ALIAS targets)
# target name should not be the same as ${PROJECT_NAME}, causes add_subdirectory issues
set(target_name "libassert-lib")
add_library(${target_name} ${build_type})

# alias to cause error at configuration time instead of link time if target is missing
add_library(assert::assert ALIAS ${target_name})

# add /include files to target
# this is solely for IDE benefit, doesn't affect building, so unconditionally list magic_enum
target_sources(
${target_name} PRIVATE
# include
include/assert.hpp
# third_party
include/assert/third_party/magic_enum.hpp
)

set_target_properties(
assert
PROPERTIES
CXX_STANDARD_REQUIRED TRUE
CXX_EXTENSIONS OFF
# add /src files to target
target_sources(
${target_name} PRIVATE
# src
src/assert.cpp
)

target_compile_options(
assert
PRIVATE
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -Werror=return-type -Wshadow -Wundef>
$<$<CXX_COMPILER_ID:GNU>:-Wuseless-cast -Wnonnull-compare>
$<$<CXX_COMPILER_ID:MSVC>:/W4 /WX /permissive->
# link dependencies
Comment on lines -64 to -69
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am likely going to re-add these warnings, they're important for development

target_link_libraries(
${target_name} PRIVATE
cpptrace::cpptrace
)

if(ASSERT_USE_EXTERNAL_CPPTRACE)
find_package(cpptrace REQUIRED)
set(ASSERT_CPPTRACE_TARGET_NAME cpptrace::cpptrace)
else()
FetchContent_Declare(
cpptrace
GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git
GIT_TAG v0.2.1
)
FetchContent_MakeAvailable(cpptrace)
set(ASSERT_CPPTRACE_TARGET_NAME cpptrace)
endif()
target_link_libraries(assert PRIVATE ${ASSERT_CPPTRACE_TARGET_NAME})

if(ASSERT_DECOMPOSE_BINARY_LOGICAL)
target_compile_definitions(assert PUBLIC ASSERT_DECOMPOSE_BINARY_LOGICAL)
endif()
# ---- Generate Build Info Headers ----

if(ASSERT_LOWERCASE)
target_compile_definitions(assert PUBLIC ASSERT_LOWERCASE)
# used in export header generated below
if(NOT ASSERT_BUILD_SHARED)
target_compile_definitions(${target_name} PUBLIC ASSERT_STATIC_DEFINE)
endif()

if(ASSERT_USE_MAGIC_ENUM)
target_compile_definitions(assert PUBLIC ASSERT_USE_MAGIC_ENUM)
endif()
# generate header file with export macros prefixed with BASE_NAME
include(GenerateExportHeader)
generate_export_header(
${target_name}
BASE_NAME assert
EXPORT_FILE_NAME include/assert/assert_export.hpp
)

if(NOT "${ASSERT_FAIL}" STREQUAL "")
target_compile_definitions(assert PUBLIC ASSERT_FAIL=${ASSERT_FAIL})
endif()

if(CMAKE_BUILD_TYPE STREQUAL "")
message(FATAL_ERROR "Setting CMAKE_BUILD_TYPE is required")
endif()
# ---- Library Properties ----

if(NOT CMAKE_SKIP_INSTALL_RULES)
include(CMakePackageConfigHelpers)

install(
TARGETS assert
EXPORT assert_targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
# hide all symbols by default
# use SameMajorVersion versioning for shared library runtime linker lookup
set_target_properties(
${target_name} PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES
VERSION "${PROJECT_VERSION}"
SOVERSION "${PROJECT_VERSION_MAJOR}"
EXPORT_NAME "assert"
OUTPUT_NAME "assert"
)

install(
FILES
include/assert.hpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/assert/assert
)
# header files generated by CMake
target_include_directories(
${target_name} SYSTEM PUBLIC
"$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>"
)

install(
FILES
third_party/magic_enum.hpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/assert/third_party
)
# header files from /include
target_include_directories(
${target_name} ${warning_guard} PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
)

export(
EXPORT assert_targets
FILE ${CMAKE_CURRENT_BINARY_DIR}/assert/assert_targets.cmake
NAMESPACE assert::
)
# require C++17 support
target_compile_features(
${target_name}
PUBLIC cxx_std_17
)

configure_package_config_file(
cmake/assert-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/assert/assert-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/assert
)
# add pre-processor definitions corresponding to CMake options
# fixme: supposedly generator expressions should work for this
if(ASSERT_USE_MAGIC_ENUM)
target_compile_definitions(
${target_name} PUBLIC
# $<$<BOOL:"${ASSERT_USE_MAGIC_ENUM}">:ASSERT_USE_MAGIC_ENUM>
ASSERT_USE_MAGIC_ENUM
)
endif()

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/assert/assert-config-version.cmake
VERSION ${PACKAGE_VERSION}
COMPATIBILITY SameMajorVersion
)

install(
EXPORT assert_targets
FILE assert_targets.cmake
NAMESPACE assert::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/assert
)
# ---- Install Rules ----

install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/assert/assert-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/assert/assert-config-version.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/assert
)
if(NOT CMAKE_SKIP_INSTALL_RULES)
include(cmake/InstallRules.cmake)
endif()

# Don't run tests when library is used with add_subdirectory
if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
if(WIN32)
add_custom_command(
TARGET assert POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:${ASSERT_CPPTRACE_TARGET_NAME}>
$<TARGET_FILE_DIR:assert>
)
endif()
if(ASSERT_BUILD_TESTS)
include(CTest)

set(
demo_sources
tests/demo/bar.cpp
tests/demo/baz/demo.cpp
tests/demo/demo.cpp
tests/demo/foo.cpp
)
add_executable(demo ${demo_sources})
target_link_libraries(demo PRIVATE assert)
target_compile_options(
demo
PRIVATE
"-DASSERT_USE_MAGIC_ENUM"
"-DASSERT_FAIL=custom_fail"
"-DASSERT_LOWERCASE"
)

add_executable(integration tests/integration/integration.cpp tests/integration/a.cpp tests/integration/x/a.cpp)
target_link_libraries(integration PRIVATE assert)
target_compile_options(
integration
PRIVATE
"-DASSERT_USE_MAGIC_ENUM"
"-DASSERT_FAIL=custom_fail"
"-DASSERT_LOWERCASE"
)
add_test(
NAME integration
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests
COMMAND
python3 run-tests.py $<TARGET_FILE:integration> ${CMAKE_BUILD_TYPE} ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_STANDARD}
)
# ---- Setup Tests ----

if(ASSERT_BUILD_TESTING)

jeremy-rifkin marked this conversation as resolved.
Show resolved Hide resolved
# need to enable testing in case BUILD_TESTING is disabled
# ctest expects that the top level project enables testing
if(PROJECT_IS_TOP_LEVEL)
enable_testing()
endif()

# tell unit tests where our files are
set(ASSERT_BINARY_DIR "${PROJECT_BINARY_DIR}")
set(ASSERT_SOURCE_DIR "${PROJECT_SOURCE_DIR}")

# include test project
# add_subdirectory(tests)
include(tests/CMakeLists.txt)

set(
unit_test_sources
tests/unit/constexpr_contexts.cpp
tests/unit/disambiguation.cpp
tests/unit/literals.cpp
tests/unit/test_public_utilities.cpp
tests/unit/test_type_prettier.cpp
tests/unit/tokens_and_highlighting.cpp
tests/unit/type_handling.cpp
)
foreach(test_file ${unit_test_sources})
get_filename_component(test_name ${test_file} NAME_WE)
add_executable(${test_name} ${test_file})
target_link_libraries(${test_name} assert)
add_test(NAME ${test_name} COMMAND ${test_name})
endforeach(test_file ${unit_test_sources})
endif()
if(ASSERT_BUILD_BASIC_TEST)
add_executable(basic tests/basic/basic_test.cpp)
target_link_libraries(basic PRIVATE assert)
endif()
endif()
Loading
Loading