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

Automatically formatting CMake #106

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ repos:
hooks:
- id: clang-format
args: [-i]
- repo: https://github.com/cheshirekow/cmake-format-precommit
rev: v0.6.10
hooks:
- id: cmake-format
- id: cmake-lint
- repo: https://github.com/tdegeus/conda_envfile
rev: v0.4.2
hooks:
Expand Down
261 changes: 136 additions & 125 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@ option(USE_DEBUG "${PROJECT_NAME}: Build in debug mode" OFF)
option(USE_SIMD "${PROJECT_NAME}: Build with hardware optimization" OFF)

if(SKBUILD)
set(BUILD_ALL 0)
set(BUILD_TESTS 0)
set(BUILD_PYTHON 1)
set(BUILD_DOCS 0)
set(BUILD_ALL 0)
set(BUILD_TESTS 0)
set(BUILD_PYTHON 1)
set(BUILD_DOCS 0)
endif()

# Read version
# ============

if (DEFINED ENV{SETUPTOOLS_SCM_PRETEND_VERSION})
set(PROJECT_VERSION $ENV{SETUPTOOLS_SCM_PRETEND_VERSION})
message(STATUS "Building ${PROJECT_NAME} ${PROJECT_VERSION} (read from SETUPTOOLS_SCM_PRETEND_VERSION)")
if(DEFINED ENV{SETUPTOOLS_SCM_PRETEND_VERSION})
set(PROJECT_VERSION $ENV{SETUPTOOLS_SCM_PRETEND_VERSION})
message(STATUS "Reading SETUPTOOLS_SCM_PRETEND_VERSION")
message(STATUS "Building ${PROJECT_NAME} ${PROJECT_VERSION}")
else()
execute_process(
COMMAND python -c "from setuptools_scm import get_version; print(get_version())"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE PROJECT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)

message(STATUS "Building ${PROJECT_NAME} ${PROJECT_VERSION}")
execute_process(
COMMAND python -c
"from setuptools_scm import get_version; print(get_version())"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE PROJECT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)

message(STATUS "Building ${PROJECT_NAME} ${PROJECT_VERSION}")
endif()

# Set target
Expand All @@ -48,16 +50,17 @@ find_package(xtensor REQUIRED)

add_library(${PROJECT_NAME} INTERFACE)

target_include_directories(${PROJECT_NAME} INTERFACE
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
target_include_directories(
${PROJECT_NAME}
INTERFACE $<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)

target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
target_link_libraries(${PROJECT_NAME} INTERFACE prrng)
target_link_libraries(${PROJECT_NAME} INTERFACE xtensor)

target_compile_definitions(${PROJECT_NAME} INTERFACE
${PROJECT_NAME_UPPER}_VERSION="${PROJECT_VERSION}")
target_compile_definitions(
${PROJECT_NAME} INTERFACE ${PROJECT_NAME_UPPER}_VERSION="${PROJECT_VERSION}")

# Libraries
# =========
Expand All @@ -72,41 +75,40 @@ include("${PROJECT_NAME}Config.cmake")

if(NOT SKBUILD)

install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/" DESTINATION include)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/" DESTINATION include)

configure_file("include/${PROJECT_NAME}/version.h"
"${CMAKE_CURRENT_BINARY_DIR}/version.h"
@ONLY)
configure_file("include/${PROJECT_NAME}/version.h"
"${CMAKE_CURRENT_BINARY_DIR}/version.h" @ONLY)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/version.h"
DESTINATION "include/${PROJECT_NAME}/")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/version.h"
DESTINATION "include/${PROJECT_NAME}/")

install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}-targets)
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}-targets)

install(
EXPORT ${PROJECT_NAME}-targets
FILE "${PROJECT_NAME}Targets.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
install(
EXPORT ${PROJECT_NAME}-targets
FILE "${PROJECT_NAME}Targets.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

set(${PROJECT_NAME}_TMP ${CMAKE_SIZEOF_VOID_P})
unset(CMAKE_SIZEOF_VOID_P)
set(${PROJECT_NAME}_TMP ${CMAKE_SIZEOF_VOID_P})
unset(CMAKE_SIZEOF_VOID_P)

write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION "${PROJECT_VERSION}"
COMPATIBILITY AnyNewerVersion)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION "${PROJECT_VERSION}"
COMPATIBILITY AnyNewerVersion)

set(CMAKE_SIZEOF_VOID_P ${${PROJECT_NAME}_TMP})
set(CMAKE_SIZEOF_VOID_P ${${PROJECT_NAME}_TMP})

install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" @ONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" @ONLY)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig/")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig/")

endif()

Expand All @@ -115,8 +117,8 @@ endif()

if(BUILD_TESTS OR BUILD_ALL)

enable_testing()
add_subdirectory(tests)
enable_testing()
add_subdirectory(tests)

endif()

Expand All @@ -125,8 +127,8 @@ endif()

if(BUILD_EXAMPLES OR BUILD_ALL)

enable_testing()
add_subdirectory(docs/examples)
enable_testing()
add_subdirectory(docs/examples)

endif()

Expand All @@ -135,65 +137,74 @@ endif()

if(BUILD_PYTHON OR BUILD_ALL)

# The C++ functions are build to a library with name "_${PROJECT_NAME}"
# The Python library simply loads all functions
set(PYPROJECT_NAME "_${PROJECT_NAME}")

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

find_package(pybind11 REQUIRED CONFIG)
find_package(xtensor-python REQUIRED)

if (SKBUILD)
find_package(NumPy REQUIRED)
else()
find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy)
endif()

pybind11_add_module(${PYPROJECT_NAME} python/main.cpp)

target_compile_definitions(${PYPROJECT_NAME} PUBLIC VERSION_INFO=${PROJECT_VERSION})
target_link_libraries(${PYPROJECT_NAME} PUBLIC ${PROJECT_NAME} xtensor-python)
target_link_libraries(${PYPROJECT_NAME} PUBLIC ${PROJECT_NAME}::compiler_warnings)

if (SKBUILD)
target_include_directories(${PYPROJECT_NAME} PUBLIC ${NumPy_INCLUDE_DIRS})
# The C++ functions are build to a library with name "_${PROJECT_NAME}" The
# Python library simply loads all functions
set(PYPROJECT_NAME "_${PROJECT_NAME}")

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

find_package(pybind11 REQUIRED CONFIG)
find_package(xtensor-python REQUIRED)

if(SKBUILD)
find_package(NumPy REQUIRED)
else()
find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy)
endif()

pybind11_add_module(${PYPROJECT_NAME} python/main.cpp)

target_compile_definitions(${PYPROJECT_NAME}
PUBLIC VERSION_INFO=${PROJECT_VERSION})
target_link_libraries(${PYPROJECT_NAME} PUBLIC ${PROJECT_NAME} xtensor-python)
target_link_libraries(${PYPROJECT_NAME}
PUBLIC ${PROJECT_NAME}::compiler_warnings)

if(SKBUILD)
target_include_directories(${PYPROJECT_NAME} PUBLIC ${NumPy_INCLUDE_DIRS})
else()
target_link_libraries(${PYPROJECT_NAME}
PUBLIC ${PROJECT_NAME} pybind11::module Python::NumPy)
endif()

if(USE_WARNINGS)
target_link_libraries(${PYPROJECT_NAME} PUBLIC ${PROJECT_NAME}::warnings)
message(STATUS "Compiling ${PROJECT_NAME}-Python with run-time warnings")
endif()

if(USE_ASSERT)
target_link_libraries(${PYPROJECT_NAME} PUBLIC ${PROJECT_NAME}::assert)
message(STATUS "Compiling ${PROJECT_NAME}-Python with assertions")
endif()

if(USE_DEBUG)
target_link_libraries(${PYPROJECT_NAME} PUBLIC ${PROJECT_NAME}::debug)
message(STATUS "Compiling ${PROJECT_NAME}-Python in debug mode")
endif()

if(USE_SIMD)
find_package(xtensor REQUIRED)
find_package(xsimd REQUIRED)
target_link_libraries(${PYPROJECT_NAME} PUBLIC xtensor::optimize
xtensor::use_xsimd)
message(
STATUS "Compiling ${PROJECT_NAME}-Python with hardware optimization")
endif()

if(SKBUILD)
if(APPLE)
set_target_properties(
${PYPROJECT_NAME} PROPERTIES INSTALL_RPATH
"@loader_path/${CMAKE_INSTALL_LIBDIR}")
else()
target_link_libraries(${PYPROJECT_NAME} PUBLIC ${PROJECT_NAME} pybind11::module Python::NumPy)
endif()

if (USE_WARNINGS)
target_link_libraries(${PYPROJECT_NAME} PUBLIC ${PROJECT_NAME}::warnings)
message(STATUS "Compiling ${PROJECT_NAME}-Python with run-time warnings")
endif()

if (USE_ASSERT)
target_link_libraries(${PYPROJECT_NAME} PUBLIC ${PROJECT_NAME}::assert)
message(STATUS "Compiling ${PROJECT_NAME}-Python with assertions")
endif()

if (USE_DEBUG)
target_link_libraries(${PYPROJECT_NAME} PUBLIC ${PROJECT_NAME}::debug)
message(STATUS "Compiling ${PROJECT_NAME}-Python in debug mode")
endif()

if (USE_SIMD)
find_package(xtensor REQUIRED)
find_package(xsimd REQUIRED)
target_link_libraries(${PYPROJECT_NAME} PUBLIC xtensor::optimize xtensor::use_xsimd)
message(STATUS "Compiling ${PROJECT_NAME}-Python with hardware optimization")
endif()

if (SKBUILD)
if(APPLE)
set_target_properties(${PYPROJECT_NAME} PROPERTIES INSTALL_RPATH "@loader_path/${CMAKE_INSTALL_LIBDIR}")
else()
set_target_properties(${PYPROJECT_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/${CMAKE_INSTALL_LIBDIR}")
endif()
install(TARGETS ${PYPROJECT_NAME} DESTINATION .)
set_target_properties(
${PYPROJECT_NAME} PROPERTIES INSTALL_RPATH
"$ORIGIN/${CMAKE_INSTALL_LIBDIR}")
endif()
install(TARGETS ${PYPROJECT_NAME} DESTINATION .)
endif()

endif()

Expand All @@ -202,25 +213,25 @@ endif()

if(BUILD_DOCS OR BUILD_ALL)

find_package(Doxygen REQUIRED)

set(DOXYGEN_EXCLUDE_SYMBOLS detail)
set(DOXYGEN_CASE_SENSE_NAMES YES)
set(DOXYGEN_USE_MATHJAX YES)
set(DOXYGEN_GENERATE_TREEVIEW YES)
set(DOXYGEN_JAVADOC_AUTOBRIEF YES)
set(DOXYGEN_INLINE_INHERITED_MEMB YES)
set(DOXYGEN_MACRO_EXPANSION YES)
set(DOXYGEN_SOURCE_BROWSER YES)
set(DOXYGEN_GENERATE_XML YES)
set(DOXYGEN_QUIET YES)
set(DOXYGEN_WARN_IF_UNDOCUMENTED YES)
set(DOXYGEN_WARN_AS_ERROR YES)
set(DOXYGEN_HTML_COLORSTYLE TOGGLE)
set(DOXYGEN_ALIASES "license=@par License:")
set(DOXYGEN_USE_MDFILE_AS_MAINPAGE "README.md")
set(DOXYGEN_STRIP_FROM_INC_PATH "${CMAKE_CURRENT_SOURCE_DIR}/include")
set(DOXYGEN_STRIP_FROM_PATH "${CMAKE_CURRENT_SOURCE_DIR}/include")
doxygen_add_docs(html "${CMAKE_CURRENT_SOURCE_DIR}/include" "README.md")
find_package(Doxygen REQUIRED)

set(DOXYGEN_EXCLUDE_SYMBOLS detail)
set(DOXYGEN_CASE_SENSE_NAMES YES)
set(DOXYGEN_USE_MATHJAX YES)
set(DOXYGEN_GENERATE_TREEVIEW YES)
set(DOXYGEN_JAVADOC_AUTOBRIEF YES)
set(DOXYGEN_INLINE_INHERITED_MEMB YES)
set(DOXYGEN_MACRO_EXPANSION YES)
set(DOXYGEN_SOURCE_BROWSER YES)
set(DOXYGEN_GENERATE_XML YES)
set(DOXYGEN_QUIET YES)
set(DOXYGEN_WARN_IF_UNDOCUMENTED YES)
set(DOXYGEN_WARN_AS_ERROR YES)
set(DOXYGEN_HTML_COLORSTYLE TOGGLE)
set(DOXYGEN_ALIASES "license=@par License:")
set(DOXYGEN_USE_MDFILE_AS_MAINPAGE "README.md")
set(DOXYGEN_STRIP_FROM_INC_PATH "${CMAKE_CURRENT_SOURCE_DIR}/include")
set(DOXYGEN_STRIP_FROM_PATH "${CMAKE_CURRENT_SOURCE_DIR}/include")
doxygen_add_docs(html "${CMAKE_CURRENT_SOURCE_DIR}/include" "README.md")

endif()
Loading
Loading