Skip to content
Snippets Groups Projects
CMakeLists.txt 4.28 KiB
Newer Older
cmake_minimum_required(VERSION 3.15)
Cyril Moineau's avatar
Cyril Moineau committed

file(READ "${CMAKE_SOURCE_DIR}/version.txt" version)
file(READ "${CMAKE_SOURCE_DIR}/project_name.txt" project)

message(STATUS "Project name: ${project}")
message(STATUS "Project version: ${version}")

# Note : project name is {project} and python module name is also {project} 
set(module_name _${project}) # target name

project(${project})
Cyril Moineau's avatar
Cyril Moineau committed

##############################################
# Define options
option(PYBIND "python binding" ON)
option(WERROR "Warning as error" OFF)
Olivier BICHLER's avatar
Olivier BICHLER committed
option(TEST "Enable tests" ON)
option(COVERAGE "Enable coverage" OFF)
Grégoire Kubler's avatar
Grégoire Kubler committed
option(ENABLE_ASAN "Enable ASan (adress sanitizer) for runtime analysis of memory use (over/underflow, memory leak, ...)" OFF)
Olivier BICHLER's avatar
Olivier BICHLER committed

##############################################
# 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()
Cyril Moineau's avatar
Cyril Moineau committed

##############################################
# Find system dependencies
Cyril Moineau's avatar
Cyril Moineau committed

##############################################
# Create target and set properties
file(GLOB_RECURSE src_files "src/*.cpp")
file(GLOB_RECURSE inc_files "include/*.hpp")
Cyril Moineau's avatar
Cyril Moineau committed

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
)
#Set target properties
set_property(TARGET ${module_name} PROPERTY POSITION_INDEPENDENT_CODE ON)
Cyril Moineau's avatar
Cyril Moineau committed

Grégoire Kubler's avatar
Grégoire Kubler committed
if( ${ENABLE_ASAN} )
    message("Building ${module_name} with ASAN.")
Grégoire Kubler's avatar
Grégoire Kubler committed
    set(SANITIZE_FLAGS -fsanitize=address -fno-omit-frame-pointer)
    target_link_libraries(${module_name}
        PUBLIC
            -fsanitize=address
    )
    target_compile_options(${module_name}
        PRIVATE
            ${SANITIZE_FLAGS}
    )
endif()

    PUBLIC
        $<INSTALL_INTERFACE:include>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/src
)

Cyril Moineau's avatar
Cyril Moineau committed
if (PYBIND)
Olivier BICHLER's avatar
Olivier BICHLER committed
    generate_python_binding(${project} ${module_name})

    # Handles Python + pybind11 headers dependencies
        PUBLIC 
            pybind11::pybind11
            Python::Python
        )
Cyril Moineau's avatar
Cyril Moineau committed
endif()

target_compile_features(${module_name} PRIVATE cxx_std_14)
Cyril Moineau's avatar
Cyril Moineau committed

Olivier BICHLER's avatar
Olivier BICHLER committed
target_compile_options(${module_name} PRIVATE
    $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
Olivier BICHLER's avatar
Olivier BICHLER committed
    -Wall -Wextra -Wold-style-cast -Winline -pedantic -Werror=narrowing -Wshadow $<$<BOOL:${WERROR}>:-Werror>>)
target_compile_options(${module_name} PRIVATE
    $<$<CXX_COMPILER_ID:MSVC>:
    /W4>)

Olivier BICHLER's avatar
Olivier BICHLER committed
if(CMAKE_COMPILER_IS_GNUCXX AND COVERAGE)
    append_coverage_compiler_flags()
endif()

##############################################
# Installation instructions

include(GNUInstallDirs)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${project})

install(TARGETS ${module_name} EXPORT ${project}-targets
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

#Export the targets to a script

install(EXPORT ${project}-targets
 FILE "${project}-targets.cmake"
 DESTINATION ${INSTALL_CONFIGDIR}
 COMPONENT ${module_name} 
)  

#Create a ConfigVersion.cmake file
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    "${CMAKE_CURRENT_BINARY_DIR}/${project}-config-version.cmake"
    VERSION ${version}
    COMPATIBILITY AnyNewerVersion
)

configure_package_config_file("${project}-config.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/${project}-config.cmake"
    INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
)

#Install the config, configversion and custom find modules
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(EXPORT ${project}-targets
    FILE "${CMAKE_CURRENT_BINARY_DIR}/${project}-targets.cmake")


##############################################
## Add test
Olivier BICHLER's avatar
Olivier BICHLER committed
if(TEST)
    enable_testing()
    add_subdirectory(unit_tests)
endif()