Skip to content
Snippets Groups Projects
Commit 6d7131c1 authored by Maxence Naud's avatar Maxence Naud
Browse files

UPD: CMakeLists.txt enforce C++14 and try to reorder sections

parent 63cd594e
No related branches found
No related tags found
1 merge request!39UPD: version 0.3.0 -> 0.3.1
# CMake >= 3.18 is required for good support of FindCUDAToolkit
cmake_minimum_required(VERSION 3.18) # XXX 3.18
set(CXX_STANDARD 14)
cmake_minimum_required(VERSION 3.18)
file(STRINGS "${CMAKE_SOURCE_DIR}/version.txt" version)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Read project metadata
file(STRINGS "${CMAKE_SOURCE_DIR}/project_name.txt" project)
message(STATUS "Project name: ${project}")
file(STRINGS "${CMAKE_SOURCE_DIR}/version.txt" version)
# Parse version.txt to retrieve Major, Minor and Path
string(REGEX MATCH "([0-9]+)\\.([0-9]+)\\.([0-9]+)" _ MATCHES ${version})
set(PROJECT_VERSION_MAJOR ${CMAKE_MATCH_1})
set(PROJECT_VERSION_MINOR ${CMAKE_MATCH_2})
set(PROJECT_VERSION_PATCH ${CMAKE_MATCH_3})
message(STATUS "Project version: ${version}")
# Retrieve latest git commit
execute_process(
......@@ -19,17 +26,25 @@ execute_process(
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
message(STATUS "Project name: ${project}")
message(STATUS "Project version: ${version}")
message(STATUS "Latest git commit: ${GIT_COMMIT_HASH}")
message(STATUS "Creating ${CMAKE_CURRENT_SOURCE_DIR}/include/aidge/quantization_version.h")
project(${project}
VERSION ${version}
DESCRIPTION "Quantization methods for the Aidge framework."
LANGUAGES CXX)
if(NOT $ENV{AIDGE_INSTALL} STREQUAL "")
set(CMAKE_INSTALL_PREFIX $ENV{AIDGE_INSTALL})
list(APPEND CMAKE_PREFIX_PATH $ENV{AIDGE_INSTALL})
message(WARNING "Env var AIDGE_INSTALL detected : $ENV{AIDGE_INSTALL}. Set CMAKE_INSTALL_PREFIX to AIDGE_INSTALL & added to CMAKE_PREFIX_PATH"
"\n\tCMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}"
"\n\tCMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH}")
endif()
message(STATUS "Creating ${CMAKE_CURRENT_SOURCE_DIR}/include/aidge/quantization_version.h")
# Note: Using configure_file later in the code make so that version variables are lost...
# I tried to set in internal cache but it failed.
# Current code is working, but there might be a scope issue.
......@@ -39,21 +54,12 @@ configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/include/aidge/quantization_version.h"
)
# Note : project name is {project} and python module name is also {project}
set(module_name _${project}) # target name
set(pybind_module_name ${CMAKE_PROJECT_NAME}) # name of submodule for python bindings
set(CXX_STANDARD 14)
##############################################
# Import utils CMakeLists
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
##############################################
# Define options
option(PYBIND "python binding" ON)
option(PYBIND "python binding" OFF)
option(WERROR "Warning as error" OFF)
option(TEST "Enable tests" ON)
option(TEST "Enable tests" OFF)
option(COVERAGE "Enable coverage" OFF)
option(CUDA "Enable CUDA backend" OFF) # XXX OFF
option(ENABLE_ASAN "Enable ASan (AddressSanitizer) for runtime analysis of memory use (over/underflow, memory leak, ...)" OFF)
......@@ -61,74 +67,55 @@ option(ENABLE_ASAN "Enable ASan (AddressSanitizer) for runtime analysis of memor
##############################################
# Import utils CMakeLists
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
include(PybindModuleCreation)
if(CMAKE_COMPILER_IS_GNUCXX AND COVERAGE)
Include(CodeCoverage)
endif()
# Set variables
if(CUDA)
enable_language(CUDA)
message(STATUS "Cuda compiler version = ${CMAKE_CUDA_COMPILER_VERSION}")
# Define a preprocessor macro with the Cuda compiler version
add_definitions(-DCUDA_COMPILER_VERSION="${CMAKE_CUDA_COMPILER_VERSION}")
endif()
if(NOT $ENV{AIDGE_INSTALL} STREQUAL "")
set(CMAKE_INSTALL_PREFIX $ENV{AIDGE_INSTALL})
list(APPEND CMAKE_PREFIX_PATH $ENV{AIDGE_INSTALL})
message(WARNING "Env var AIDGE_INSTALL detected : $ENV{AIDGE_INSTALL}. Set CMAKE_INSTALL_PREFIX to AIDGE_INSTALL & added to CMAKE_PREFIX_PATH"
"\n\tCMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}"
"\n\tCMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH}")
# Source files
if(CUDA)
file(GLOB_RECURSE src_files "src/*.cpp" "src/*.cu")
else()
file(GLOB_RECURSE src_files "src/*.cpp")
endif()
# ##############################################
# Find system dependencies
# Header files
file(GLOB_RECURSE inc_files "include/*.hpp")
if(CUDA)
find_package(CUDAToolkit REQUIRED)
endif()
# Note: cxx project name is {CMAKE_PROJECT_NAME} and python module name is also {CMAKE_PROJECT_NAME}
set(module_name _${CMAKE_PROJECT_NAME}) # target name
add_library(${module_name} ${src_files} ${inc_files})
set(pybind_module_name ${CMAKE_PROJECT_NAME}) # name of submodule for python bindings
##############################################
# Find system dependencies
# Dependencies and linking
find_package(aidge_core REQUIRED)
find_package(aidge_backend_cpu REQUIRED)
target_link_libraries(${module_name}
PUBLIC
_aidge_core
_aidge_backend_cpu
)
if(CUDA)
find_package(CUDAToolkit REQUIRED)
find_package(aidge_backend_cuda REQUIRED)
endif()
##############################################
# Create target and set properties
if(CUDA)
file(GLOB_RECURSE src_files "src/*.cpp" "src/*.cu")
file(GLOB_RECURSE inc_files "include/*.hpp")
add_library(${module_name} ${src_files} ${inc_files})
target_link_libraries(${module_name}
PUBLIC
_aidge_core # _ is added because we link the target not the project
_aidge_backend_cpu
# _aidge_backend_cuda # XXX
CUDA::cudart
CUDA::cublas
cudnn
)
else()
file(GLOB_RECURSE src_files "src/*.cpp")
file(GLOB_RECURSE inc_files "include/*.hpp")
add_library(${module_name} ${src_files} ${inc_files})
target_link_libraries(${module_name}
PUBLIC
_aidge_core # _ is added because we link the target not the project
_aidge_backend_cpu
)
endif()
#Set target properties
# Include directories
target_include_directories(${module_name}
PUBLIC
$<INSTALL_INTERFACE:include>
......@@ -137,6 +124,7 @@ target_include_directories(${module_name}
${CMAKE_CURRENT_SOURCE_DIR}/src
)
# Compilation settings
if(CUDA)
if(NOT DEFINED CMAKE_CUDA_STANDARD)
set(CMAKE_CUDA_STANDARD 14)
......@@ -157,23 +145,44 @@ if (PYBIND)
generate_python_binding(${pybind_module_name} ${module_name})
endif()
# XXX HERE !!!
target_link_libraries(${module_name} PRIVATE fmt::fmt)
target_compile_features(${module_name} PRIVATE cxx_std_14)
target_link_libraries(${module_name} PRIVATE fmt::fmt)
####################################
# Compilation options and warnings
target_compile_options(${module_name} PRIVATE
# Options for Clang, AppleClang, and GCC compilers
$<$<COMPILE_LANGUAGE:CPP>:$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall -Wextra -Wold-style-cast -Winline -pedantic -Werror=narrowing -Wshadow $<$<BOOL:${WERROR}>:-Werror>>>)
-Wall # Enable all warnings
-Wextra # Enable extra warnings
-Wold-style-cast # Warn about C-style casts
-Winline # Warn if inline expansion fails
-pedantic # Enforce strict ISO C++ standards
-Werror=narrowing # Treat narrowing conversions as errors
-Wshadow # Warn about variable shadowing
$<$<BOOL:${WERROR}>:-Werror> # Optionally treat warnings as errors
>>
)
# Additional MSVC-specific warning level
target_compile_options(${module_name} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:
/W4 # Warning level 4 (highest for MSVC)
>
)
# CUDA-specific compile options
if(CUDA)
target_compile_options(${module_name} PRIVATE
$<$<COMPILE_LANGUAGE:CUDA>:
-Wall>)
-Wall # Enable all warnings for CUDA
>
)
endif()
target_compile_options(${module_name} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:
/W4>)
# Coverage flags for GCC
if(CMAKE_COMPILER_IS_GNUCXX AND COVERAGE)
include(CodeCoverage)
append_coverage_compiler_flags()
endif()
......@@ -183,29 +192,31 @@ endif()
include(GNUInstallDirs)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${project})
# Install the library target
install(TARGETS ${module_name} EXPORT ${project}-targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
# Install header files
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
#Export the targets to a script
# Export targets for other projects to use
install(EXPORT ${project}-targets
FILE "${project}-targets.cmake"
DESTINATION ${INSTALL_CONFIGDIR}
COMPONENT ${module_name}
FILE "${project}-targets.cmake"
DESTINATION ${INSTALL_CONFIGDIR}
COMPONENT ${module_name}
)
if (PYBIND)
# Python binding installation
if(PYBIND)
install(TARGETS ${pybind_module_name}
DESTINATION ${PYBIND_INSTALL_PREFIX}
)
endif()
#Create a ConfigVersion.cmake file
# Create and install CMake configuration files
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${project}-config-version.cmake"
......@@ -218,15 +229,14 @@ configure_package_config_file("${project}-config.cmake.in"
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
)
#Install the config, configversion and custom find modules
# Install CMake configuration files
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${project}-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${project}-config-version.cmake"
DESTINATION ${INSTALL_CONFIGDIR}
)
##############################################
## Exporting from the build tree
# Export from build tree
export(EXPORT ${project}-targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${project}-targets.cmake")
......@@ -234,10 +244,6 @@ export(EXPORT ${project}-targets
##############################################
## Add test
if(TEST)
if (AIDGE_REQUIRES_PYTHON AND NOT AIDGE_PYTHON_HAS_EMBED)
message(WARNING "Skipping compilation of tests: missing Python embedded interpreter")
else()
enable_testing()
add_subdirectory(unit_tests)
endif()
enable_testing()
add_subdirectory(unit_tests)
endif()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment