Skip to content

Commit

Permalink
[cmake] Overhaul of cmake files and project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Feb 22, 2024
1 parent 231d1ff commit 49e88c3
Show file tree
Hide file tree
Showing 68 changed files with 225 additions and 212 deletions.
177 changes: 80 additions & 97 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,82 @@ if(POLICY CMP0144)
cmake_policy(SET CMP0144 NEW)
endif()

#
# ############
# Define Project
project(c2py VERSION 0.1.0 LANGUAGES C CXX)
get_directory_property(IS_SUBPROJECT PARENT_DIRECTORY)
include(CheckIncludeFile)
include(CheckIncludeFileCXX)

# Default to Release build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Type of build" FORCE)
endif()
message(STATUS "-------- BUILD-TYPE: ${CMAKE_BUILD_TYPE} -------------")

# CXX options
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Global compiler options
option(BUILD_SHARED_LIBS "Enable compilation of shared libraries" OFF)
add_compile_options($<$<CONFIG:Debug>:-ggdb3>)
message(STATUS "-------- c2py version and git hash detection -------------")
find_package(Git)
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE PROJECT_GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
message(STATUS "c2py version : ${PROJECT_VERSION}")
message(STATUS "c2py Git hash: ${PROJECT_GIT_HASH}")

# Check CMAKE_INSTALL_PREFIX : must be provided and absolute
# Assert that install directory is provided and is absolute path.
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT OR (NOT IS_ABSOLUTE ${CMAKE_INSTALL_PREFIX}))
message(FATAL_ERROR "CMAKE_INSTALL_PREFIX must be specified and must be an absolute path.\n There is no default.\n Current value : ${CMAKE_INSTALL_PREFIX}\n. e.g. ... -DCMAKE_INSTALL_PREFIX=$HOME/clair_install")
endif()
if(NOT IS_SUBPROJECT)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT OR (NOT IS_ABSOLUTE ${CMAKE_INSTALL_PREFIX}))
message(FATAL_ERROR "CMAKE_INSTALL_PREFIX must be specified and must be an absolute path.\n There is no default.\n Current value : ${CMAKE_INSTALL_PREFIX}\n. e.g. ... -DCMAKE_INSTALL_PREFIX=$HOME/c2py_install")
endif()
message(STATUS "Installation directory will be ${CMAKE_INSTALL_PREFIX}")
message(STATUS "-------- CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX} --------")
endif()
set(C2PY_BINARY_DIR ${PROJECT_BINARY_DIR} CACHE STRING "Binary directory of the Clair Project")
set(c2py_BINARY_DIR ${PROJECT_BINARY_DIR} CACHE STRING "Binary directory of the c2py Project")

# add ./cmake to the path for module
# Make additional Find Modules available
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/share/cmake/Modules)

# log info
message( STATUS "-------- c2py version and git hash detection -------------")

find_package(Git)
# ############
# CMake Options

# Get the Git Hash
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
OUTPUT_VARIABLE PROJECT_GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)

message(STATUS "${PROJECT_NAME} version : ${PROJECT_VERSION}")
message(STATUS "${PROJECT_NAME} Git hash: ${PROJECT_GIT_HASH}")
# Default to Release build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Type of build" FORCE)
endif()
if(NOT IS_SUBPROJECT)
message(STATUS "-------- BUILD-TYPE: ${CMAKE_BUILD_TYPE} --------")
endif()

# --- Python ---
find_package(Python COMPONENTS Interpreter Development NumPy)
set(c2py_all_python_flags " -I${Python_INCLUDE_DIRS} -I${Python_NumPy_INCLUDE_DIRS} -L${Python_LIBRARY_DIRS} -lpython${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}")
configure_file(c2py_flags.py.in c2py_flags @ONLY)
# Testing
option(Build_Tests "Build tests" ON)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/c2py_flags DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
# ############
# Global Compilation Settings

# Set binary dir
set(${PROJECT_NAME}_BINARY_DIR ${PROJECT_BINARY_DIR} CACHE STRING "Binary directory of the ${PROJECT_NAME} Project")
# Build static libraries by default
option(BUILD_SHARED_LIBS "Enable compilation of shared libraries" OFF)

# Export the list of compile-commands into compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Disable compiler extensions
set(CMAKE_CXX_EXTENSIONS OFF)

# Provide additional debugging information for Debug builds
add_compile_options($<$<CONFIG:Debug>:-ggdb3>)

# Create an Interface target for compiler warnings
add_library(${PROJECT_NAME}_warnings INTERFACE)
target_compile_options(${PROJECT_NAME}_warnings
add_library(c2py_warnings INTERFACE)
target_compile_options(c2py_warnings
INTERFACE
-Wall
-Wextra
-Wfloat-conversion
-Wpedantic
-Wno-sign-compare
$<$<CXX_COMPILER_ID:GNU>:-Wno-comma-subscript>
$<$<CXX_COMPILER_ID:GNU>:-Wshadow=local>
$<$<CXX_COMPILER_ID:GNU>:-Wno-attributes>
$<$<CXX_COMPILER_ID:Clang>:-Wshadow>
$<$<CXX_COMPILER_ID:Clang>:-Wno-gcc-compat>
$<$<CXX_COMPILER_ID:Clang>:-Wno-unused-parameter>
$<$<CXX_COMPILER_ID:Clang>:-Wno-unused-const-variable>
$<$<CXX_COMPILER_ID:Clang>:-Wno-unused-variable>
$<$<CXX_COMPILER_ID:AppleClang>:-Wshadow>
$<$<CXX_COMPILER_ID:AppleClang>:-Wno-gcc-compat>
$<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations>
$<$<CXX_COMPILER_ID:AppleClang,Clang,IntelLLVM>:-Wno-deprecated-comma-subscript>
$<$<CXX_COMPILER_ID:AppleClang,Clang,IntelLLVM>:-Wno-unknown-warning-option>
$<$<CXX_COMPILER_ID:AppleClang,Clang,IntelLLVM>:-Wshadow>
$<$<CXX_COMPILER_ID:AppleClang,Clang,IntelLLVM>:-Wno-gcc-compat>
$<$<CXX_COMPILER_ID:AppleClang,Clang,IntelLLVM>:-Wno-c++20-extensions>
$<$<CXX_COMPILER_ID:AppleClang,Clang,IntelLLVM>:-Wno-c++20-compat>
$<$<CXX_COMPILER_ID:IntelLLVM>:-Wno-tautological-constant-compare>
)

# ---------------------------------
Expand All @@ -92,8 +90,9 @@ target_compile_options(${PROJECT_NAME}_warnings
# The clang linker however ignores LIBRARY_PATH.
# We thus explicitly add the content of LIBRARY_PATH to the LDFLAGS
# FIXME For future cmake versions we should populate the
# INTERFACE_LINK_DIRECTORIES of the target
# ---------------------------------
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND DEFINED ENV{LIBRARY_PATH})
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND DEFINED ENV{LIBRARY_PATH})
string(REPLACE ":" ";" LINK_DIRS $ENV{LIBRARY_PATH})
foreach(dir ${LINK_DIRS})
string(APPEND CMAKE_SHARED_LINKER_FLAGS " -L${dir}")
Expand All @@ -102,16 +101,32 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND DEFINED ENV{LIBRARY_PATH})
endforeach()
endif()

add_subdirectory(src)
add_subdirectory(share) # Exported Clair-Config
# Provide GNU Installation directories
include(GNUInstallDirs)

option(Build_Tests "Build tests" ON)
if (Build_Tests AND (NOT IS_SUBPROJECT OR IS_CLAIR_BUILD))
set(H5_INCLUDE_DIR ${CMAKE_BINARY_DIR}/deps/h5_src/c++/)
enable_testing()
add_subdirectory(test)
# Python
message(STATUS "-------- Python detection -------------")
find_package(Python COMPONENTS Interpreter Development NumPy)
set(c2py_all_python_flags " -I${Python_INCLUDE_DIRS} -I${Python_NumPy_INCLUDE_DIRS} -L${Python_LIBRARY_DIRS} -lpython${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}")
configure_file(c2py_flags.py.in c2py_flags @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/c2py_flags DESTINATION ${CMAKE_INSTALL_BINDIR} PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

# #############
# Build Project

# Build and install the library
add_subdirectory(src/c2py)

# Tests
if(Build_Tests AND NOT IS_SUBPROJECT)
set(H5_INCLUDE_DIR ${PROJECT_BINARY_DIR}/deps/h5_src/c++/)
enable_testing()
add_subdirectory(test)
endif()

# Additional configuration files
add_subdirectory(share)

#--------------------------------------------------------
# Sanitizers
#--------------------------------------------------------
Expand All @@ -130,50 +145,18 @@ if(UBSAN)
endif()
endif()

#--------------------------------------------------------
# Remind the user how to set up his/her variables
#--------------------------------------------------------
# #############
# Debian Package

if(NOT IS_SUBPROJECT
AND NOT CMAKE_INSTALL_PREFIX STREQUAL "/usr"
AND NOT CMAKE_INSTALL_PREFIX STREQUAL "/usr/local"
)
# Configure and install the file to source to setup the environment variables
message(STATUS "***************************************************************")
message(STATUS "* Use : ")
message(STATUS "* source ${CMAKE_INSTALL_PREFIX}/share/c2py/c2pyvars.sh ")
message(STATUS "* to set up the environment variables ")
if(DEFINED ENV{MODULEPATH})
message(STATUS "* ")
message(STATUS "* Consider copying ${CMAKE_CURRENT_BINARY_DIR}/c2py.modulefile ")
message(STATUS "* into your environment module directories ")
endif()
message(STATUS "***************************************************************")
endif()

# uninstall target
#if(NOT TARGET uninstall)
#configure_file(
#"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
#"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
#IMMEDIATE @ONLY)

#add_custom_target(uninstall
#COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
#endif()

#--------------------------------------------------------
# Packaging
#--------------------------------------------------------
option(BUILD_DEBIAN_PACKAGE "Build a deb package" OFF)
if(BUILD_DEBIAN_PACKAGE)
if(BUILD_DEBIAN_PACKAGE AND NOT IS_SUBPROJECT)
if(NOT CMAKE_INSTALL_PREFIX STREQUAL "/usr")
message(FATAL_ERROR "CMAKE_INSTALL_PREFIX must be /usr for packaging")
endif()
set(CPACK_PACKAGE_NAME c2py)
set(CPACK_GENERATOR "DEB")
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
set(CPACK_PACKAGE_CONTACT "https://github.com/TRIQS/c2py")
set(CPACK_PACKAGE_CONTACT "https://github.com/flatironinstitute/c2py")
execute_process(COMMAND dpkg --print-architecture OUTPUT_VARIABLE CPACK_DEBIAN_PACKAGE_ARCHITECTURE OUTPUT_STRIP_TRAILING_WHITESPACE)
set(CPACK_DEBIAN_PACKAGE_DEPENDS "python")
SET(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
Expand Down
3 changes: 0 additions & 3 deletions requirements.txt

This file was deleted.

26 changes: 21 additions & 5 deletions share/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
add_subdirectory(cmake)

if(NOT CMAKE_INSTALL_PREFIX STREQUAL TRIQS_ROOT AND NOT IS_SUBPROJECT)
if(NOT IS_SUBPROJECT
AND NOT CMAKE_INSTALL_PREFIX STREQUAL "/usr"
AND NOT CMAKE_INSTALL_PREFIX STREQUAL "/usr/local"
)

configure_file(${PROJECT_NAME}.modulefile.in ${PROJECT_NAME}.modulefile @ONLY)
configure_file(${PROJECT_NAME}vars.sh.in ${PROJECT_NAME}vars.sh @ONLY)
configure_file(c2py.modulefile.in c2py.modulefile @ONLY)
configure_file(c2pyvars.sh.in c2pyvars.sh @ONLY)

install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.modulefile
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}vars.sh
${CMAKE_CURRENT_BINARY_DIR}/c2py.modulefile
${CMAKE_CURRENT_BINARY_DIR}/c2pyvars.sh
DESTINATION share/c2py
)

message(STATUS "*********************************************************************************")
message(STATUS "* Custom install Location. Use: ")
message(STATUS "* ")
message(STATUS "* source ${CMAKE_INSTALL_PREFIX}/share/c2py/c2pyvars.sh ")
message(STATUS "* ")
message(STATUS "* to set up the environment variables ")
if(DEFINED ENV{MODULEPATH})
message(STATUS "* ")
message(STATUS "* Consider copying ${CMAKE_CURRENT_BINARY_DIR}/c2py.modulefile ")
message(STATUS "* into your environment module directories ")
endif()
message(STATUS "*********************************************************************************")

endif()
4 changes: 2 additions & 2 deletions share/c2py.modulefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ set version @C2PY_VERSION@
set root @CMAKE_INSTALL_PREFIX@
set git_hash @C2PY_GIT_HASH@

set url "https://github.com/TRIQS/c2py"
set description "Cpp2Py is the Python/C++ interfacing tool of the TRIQS project"
set url "https://github.com/flatironinstitute/c2py"
set description "Cpp2Py is a Python/C++ interfacing library"

module-whatis "$description"

Expand Down
12 changes: 6 additions & 6 deletions share/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
configure_file(${PROJECT_NAME}-config.cmake.in ${PROJECT_NAME}-config.cmake @ONLY)
configure_file(${PROJECT_NAME}-config-version.cmake.in ${PROJECT_NAME}-config-version.cmake @ONLY)
configure_file(c2py-config.cmake.in c2py-config.cmake @ONLY)
configure_file(c2py-config-version.cmake.in c2py-config-version.cmake @ONLY)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
DESTINATION lib/cmake/${PROJECT_NAME}
${CMAKE_CURRENT_BINARY_DIR}/c2py-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/c2py-config-version.cmake
DESTINATION lib/cmake/c2py
)

#install(EXPORT ${PROJECT_NAME}-targets NAMESPACE ${PROJECT_NAME}:: DESTINATION lib/cmake/${PROJECT_NAME})
install(EXPORT c2py-targets NAMESPACE c2py:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/c2py)
3 changes: 0 additions & 3 deletions src/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit 49e88c3

Please sign in to comment.