diff --git a/CMakeLists.txt b/CMakeLists.txt index a42f5c8..adb8e40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,20 +1,21 @@ -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.23) # Our C++ project ... project(bit DESCRIPTION "C++ classes for working in GF(2)" LANGUAGES CXX) -# Add a target for the "library" we are building (bit is header only -- hence INTERFACE). +# Add a target for the "library" we are building (${PROJECT_NAME} is header only -- hence INTERFACE). # Also add an alias that prepends a "namespace" -- if clients use that to link to, they get better error messages. -add_library(bit INTERFACE) -add_library(bit::bit ALIAS bit) +add_library(${PROJECT_NAME} INTERFACE) +add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) # We use some C++20 features. -target_compile_features(bit INTERFACE cxx_std_20) +target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_20) -# Where to find the bit headers (e.g., how to resolve `#include `). -target_include_directories(bit INTERFACE - $ - $) +# Where to find the headers (e.g., how to resolve `#include `). +target_sources(${PROJECT_NAME} INTERFACE + FILE_SET library_headers + TYPE HEADERS + BASE_DIRS include/) # That's it unless we are developing the library instead of just using it. if (PROJECT_IS_TOP_LEVEL) @@ -24,27 +25,25 @@ if (PROJECT_IS_TOP_LEVEL) # Prevent in-source builds for the example programs. include(disable_in_source_builds) - disable_in_source_builds() # Make the compiler issue warnings for "bad" code, etc. include(compiler_init) - compiler_init(bit) + compiler_init(${PROJECT_NAME}) # Debug builds get the BIT_VERIFY flag set. - target_compile_definitions(bit INTERFACE $<$: -DBIT_VERIFY>) + target_compile_definitions(${PROJECT_NAME} INTERFACE $<$: -DBIT_VERIFY>) # For neatness, we put the example executables in build/bin/. include(GNUInstallDirs) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}") - # Our example programs use the header-only `utilities` library. - include(FetchContent) - FetchContent_Declare(utilities URL https://github.com/nessan/utilities/releases/download/current/utilities.zip) - FetchContent_MakeAvailable(utilities) + # Our example programs use the header-only `utilities` library -- grab the current version from GitHub. + include(fetch_content) + fetch_content(utilities URL https://github.com/nessan/utilities/releases/download/current/utilities.zip) # Walk through the examples/ directory and build a target for each .cpp file with appropriate linkage. # We have a CMake module that makes that traversal straightforward. include(add_executables) - add_executables(examples bit::bit utilities::utilities) + add_executables(examples ${PROJECT_NAME}::${PROJECT_NAME} utilities::utilities) endif() diff --git a/cmake/add_executables.cmake b/cmake/add_executables.cmake index 1353f67..8f0628b 100644 --- a/cmake/add_executables.cmake +++ b/cmake/add_executables.cmake @@ -1,8 +1,8 @@ # ----------------------------------------------------------------------------- # @brief Add targets for lots of small executables. -# @link https://nessan.github.io/cmake +# @link https://nessan.github.io/cmake/ # -# SPDX-FileCopyrightText: 2023 Nessan Fitzmaurice +# SPDX-FileCopyrightText: 2023 Nessan Fitzmaurice # SPDX-License-Identifier: MIT # ----------------------------------------------------------------------------- @@ -69,7 +69,7 @@ function(add_executables dir) foreach(source_file ${sources}) # File "Foo03.cpp" is expected to yield target "Foo03" in the target variable - get_filename_component(target ${source_file} NAME_WLE) + cmake_path(GET source_file STEM target) # Might have a target naming conflict (test program Foo linking to library Foo) check_target(${target} clean_target) diff --git a/cmake/disable_in_source_builds.cmake b/cmake/disable_in_source_builds.cmake index 694c241..05314fc 100644 --- a/cmake/disable_in_source_builds.cmake +++ b/cmake/disable_in_source_builds.cmake @@ -1,8 +1,8 @@ # ----------------------------------------------------------------------------- # @brief Dsable in-source builds. A classic! -# @link https://nessan.github.io/cmake +# @link https://nessan.github.io/cmake/ # -# SPDX-FileCopyrightText: 2023 Nessan Fitzmaurice +# SPDX-FileCopyrightText: 2023 Nessan Fitzmaurice # SPDX-License-Identifier: MIT # ----------------------------------------------------------------------------- function(disable_in_source_builds) @@ -18,4 +18,7 @@ function(disable_in_source_builds) of those or cmake will refuse to work.") endif() -endfunction() \ No newline at end of file +endfunction() + +# Immediately run the function -- hust including this file should run it. +disable_in_source_builds() \ No newline at end of file diff --git a/cmake/fetch_content.cmake b/cmake/fetch_content.cmake new file mode 100644 index 0000000..1feecc2 --- /dev/null +++ b/cmake/fetch_content.cmake @@ -0,0 +1,18 @@ +# ----------------------------------------------------------------------------- +# @brief Wrapper around FetchContent. +# @link https://nessan.github.io/cmake/ +# +# SPDX-FileCopyrightText: 2023 Nessan Fitzmaurice +# SPDX-License-Identifier: MIT +# ----------------------------------------------------------------------------- +function(fetch_content) + include(FetchContent) + set(FETCHCONTENT_QUIET FALSE) + set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE ALWAYS) + FetchContent_Declare(${ARGV} + GIT_SHALLOW 1 + GIT_PROGRESS TRUE + SYSTEM + ) + FetchContent_MakeAvailable(${ARGV0}) +endfunction()