Skip to content

Commit

Permalink
fix: modernize CMake usage
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Nov 29, 2023
1 parent a30f5b2 commit f4a5020
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
17 changes: 17 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,23 @@ cmake -S . -B build-iwyu -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=$(which include-what-y
cmake --build build
```

## Timing steps

Make time/memory taken can be set
`CMAKE_CXX_COMPILER_LAUNCHER`/`CMAKE_CXX_LINKER_LANCHER`. Some examples:

```
# Linux:
# "time"
# "time;-v"
# "time;-f;'%U user %S system %E elapsed %P CPU %M KB'"
# macOS:
# "time"
# macOS with brew install gnu-time:
# "gtime;-f;'%U user %S system %E elapsed %P CPU %M KB'"
#
```

## Common tasks

<details><summary>Updating dependencies (click to expand)</summary>
Expand Down
52 changes: 11 additions & 41 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.15...3.26)
cmake_minimum_required(VERSION 3.15...3.27)

project(BOOST_HISTOGRAM LANGUAGES CXX)
# Version is added later
Expand All @@ -13,31 +13,6 @@ set(CMAKE_CXX_EXTENSIONS OFF)
# Adding folders to keep the structure a bit nicer in IDE's
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Make time/memory taken to compile/link available as a setting
#
# Useful settings:
# Linux:
# "time"
# "time -v"
# "time -f '%U user %S system %E elapsed %P CPU %M KB'"
# macOS:
# "time -l"
# macOS with brew install gnu-time:
# "gtime -f '%U user %S system %E elapsed %P CPU %M KB'"
#
set(BH_RULE_LAUNCH_COMPILE
""
CACHE STRING [=[Use a command, like "time" to wrap the compile]=])
if(NOT BH_RULE_LAUNCH_COMPILE STREQUAL "")
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${BH_RULE_LAUNCH_COMPILE}")
endif()
set(BH_RULE_LAUNCH_LINK
""
CACHE STRING [=[Use a command, like "time" to wrap the link]=])
if(NOT BH_RULE_LAUNCH_LINK STREQUAL "")
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${BH_RULE_LAUNCH_LINK}")
endif()

# This will force color output at build time if this env variable is set _at
# configure time_. This is useful for CI.
if($ENV{FORCE_COLOR})
Expand All @@ -49,11 +24,11 @@ if($ENV{FORCE_COLOR})
endif()

# This is a standard recipe for setting a default build type
set(default_build_type "Debug")
set(_default_build_type "Debug")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
message(STATUS "Setting build type to '${_default_build_type}' as none was specified.")
set(CMAKE_BUILD_TYPE
"${default_build_type}"
"${_default_build_type}"
CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel"
Expand All @@ -65,9 +40,11 @@ message(STATUS "CMake ${CMAKE_VERSION}")

# Adding pybind11 and setting up Python
# Will display pybind11 version
set(PYBIND11_FINDPYTHON TRUE)
set(Python_ARTIFACTS_INTERACTIVE TRUE)
add_subdirectory(extern/pybind11)

message(STATUS "Python ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
message(STATUS "Python ${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}")

# This is completely optional and just adds hints to IDEs - no affect on build at all.
file(GLOB_RECURSE BOOST_HIST_FILES "extern/histogram/include/*.hpp")
Expand Down Expand Up @@ -151,8 +128,7 @@ endif()
# which confuses Python.
set_property(TARGET _core PROPERTY LIBRARY_OUTPUT_DIRECTORY "$<1:boost_histogram>")

# Collect all the python files and symlink them (3.14+) or copy them (3.12-3.13)
# into the build directory
# Collect all the python files and symlink them into the build directory
# Protects from in-source builds (don't do this, please)
if(NOT "${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}" AND NOT DEFINED
SKBUILD)
Expand All @@ -162,15 +138,9 @@ if(NOT "${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}" AND
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/src"
CONFIGURE_DEPENDS "src/boost_histogram/*.py")
foreach(F IN LISTS BOOST_HIST_PY_FILES)
if(CMAKE_VERSION VERSION_LESS 3.14)
get_filename_component(FDIR "${F}" DIRECTORY)
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/src/${F}"
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/${FDIR}")
else()
file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/${F}")
file(CREATE_LINK "${CMAKE_CURRENT_SOURCE_DIR}/src/${F}" "${CMAKE_CURRENT_BINARY_DIR}/${F}"
COPY_ON_ERROR SYMBOLIC)
endif()
file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/${F}")
file(CREATE_LINK "${CMAKE_CURRENT_SOURCE_DIR}/src/${F}" "${CMAKE_CURRENT_BINARY_DIR}/${F}"
COPY_ON_ERROR SYMBOLIC)
endforeach()
endif()

Expand Down
3 changes: 0 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
pybind11_find_import(numpy)
pybind11_find_import(pytest)
pybind11_find_import(pytest-benchmark)
if(PYTHON_VERSION VERSION_LESS 3.5)
pybind11_find_import(typing)
endif()

# Support for running from build directory
file(WRITE "${PROJECT_BINARY_DIR}/pytest.ini" "[pytest]\n" "addopts = --benchmark-disable\n"
Expand Down

0 comments on commit f4a5020

Please sign in to comment.