Skip to content

Commit

Permalink
cmake updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nessan committed Aug 28, 2024
1 parent 9277a67 commit 32c2c23
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
33 changes: 16 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 <bit/bit.h>`).
target_include_directories(bit INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/>
$<INSTALL_INTERFACE:include/bit>)
# Where to find the headers (e.g., how to resolve `#include <bit/bit.h>`).
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)
Expand All @@ -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 $<$<CONFIG:Debug>: -DBIT_VERIFY>)
target_compile_definitions(${PROJECT_NAME} INTERFACE $<$<CONFIG:Debug>: -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()
6 changes: 3 additions & 3 deletions cmake/add_executables.cmake
Original file line number Diff line number Diff line change
@@ -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 <nzznfitz+gh@icloud.com>
# SPDX-FileCopyrightText: 2023 Nessan Fitzmaurice <nessan.fitzmaurice@me.com>
# SPDX-License-Identifier: MIT
# -----------------------------------------------------------------------------

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions cmake/disable_in_source_builds.cmake
Original file line number Diff line number Diff line change
@@ -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 <nzznfitz+gh@icloud.com>
# SPDX-FileCopyrightText: 2023 Nessan Fitzmaurice <nessan.fitzmaurice@me.com>
# SPDX-License-Identifier: MIT
# -----------------------------------------------------------------------------
function(disable_in_source_builds)
Expand All @@ -18,4 +18,7 @@ function(disable_in_source_builds)
of those or cmake will refuse to work.")
endif()

endfunction()
endfunction()

# Immediately run the function -- hust including this file should run it.
disable_in_source_builds()
18 changes: 18 additions & 0 deletions cmake/fetch_content.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -----------------------------------------------------------------------------
# @brief Wrapper around FetchContent.
# @link https://nessan.github.io/cmake/
#
# SPDX-FileCopyrightText: 2023 Nessan Fitzmaurice <[email protected]>
# 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()

0 comments on commit 32c2c23

Please sign in to comment.