From 0a69659039de07cf3ed2cc4338d53fa8a95f5bc1 Mon Sep 17 00:00:00 2001 From: Pablo Hernandez-Cerdan Date: Tue, 8 Oct 2019 19:45:54 +0200 Subject: [PATCH 1/2] Add CMake support - Creates aabbcc library - Creates Targets and Config file for easy usage by third party libraries using find_package(aabbcc) - Add option to build the demo/hard_disc Missing/TODO: - Add option to build python wrapping - Add target to create the header-only version --- CMakeLists.txt | 75 +++++++++++++++++++++++++++++++++++++ cmake/aabbccConfig.cmake.in | 5 +++ demos/CMakeLists.txt | 2 + 3 files changed, 82 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 cmake/aabbccConfig.cmake.in create mode 100644 demos/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..059461f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,75 @@ +cmake_minimum_required(VERSION 3.9) +project(aabbcc + VERSION 0.1.0 + LANGUAGES CXX) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") +set(CMAKE_INCLUDE_CURRENT_DIR ON) +include(GNUInstallDirs) # Define CMAKE_INSTALL_xxx: LIBDIR, INCLUDEDIR + +option(AABBCC_BUILD_DEMOS "Build demos" OFF) +mark_as_advanced(AABBCC_BUILD_DEMOS) + +add_library(aabbcc src/AABB.cc) +set(aabbcc_include_dir src) +set(aabbcc_export_file "${PROJECT_BINARY_DIR}/aabbccTargets.cmake") + +target_include_directories(aabbcc PUBLIC + $ + $) + +install(TARGETS aabbcc + EXPORT aabbccTargets + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + ) + +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${aabbcc_include_dir}/AABB.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/aabbcc) + +# export to the build tree +export( TARGETS aabbcc + NAMESPACE aabbcc:: + APPEND FILE ${aabbcc_export_file}) + +if(AABBCC_BUILD_DEMOS) + add_subdirectory(demos) +endif() + +# Install targets and config files +set(install_cmake_dir "${CMAKE_INSTALL_LIBDIR}/cmake/aabbcc") +install (EXPORT aabbccTargets + NAMESPACE aabbcc:: + DESTINATION ${install_cmake_dir} ) + +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cmake/aabbccConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/aabbccConfigVersion.cmake + DESTINATION ${install_cmake_dir} ) + +include(CMakePackageConfigHelpers) +write_basic_package_version_file(aabbccConfigVersion.cmake + VERSION ${aabbcc_VERSION} + COMPATIBILITY SameMajorVersion) + +# Build tree +set(aabbcc_TARGETS_FILE ${aabbcc_export_file}) +configure_package_config_file( + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/aabbccConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/aabbccConfig.cmake + INSTALL_DESTINATION ${install_cmake_dir} + PATH_VARS aabbcc_TARGETS_FILE + NO_CHECK_REQUIRED_COMPONENTS_MACRO # aabbcc does not provide components + INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR} + ) + +# Install tree +set(aabbcc_TARGETS_FILE ${CMAKE_INSTALL_PREFIX}/${install_cmake_dir}/aabbccTargets.cmake) +configure_package_config_file( + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/aabbccConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/cmake/aabbccConfig.cmake + INSTALL_DESTINATION ${install_cmake_dir} + PATH_VARS aabbcc_TARGETS_FILE + NO_CHECK_REQUIRED_COMPONENTS_MACRO # aabbcc does not provide components + ) + diff --git a/cmake/aabbccConfig.cmake.in b/cmake/aabbccConfig.cmake.in new file mode 100644 index 0000000..1dd3a8c --- /dev/null +++ b/cmake/aabbccConfig.cmake.in @@ -0,0 +1,5 @@ +@PACKAGE_INIT@ +get_filename_component(aabbcc_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) +if(NOT TARGET aabbcc) + include ("${aabbcc_CMAKE_DIR}/aabbccTargets.cmake") +endif() diff --git a/demos/CMakeLists.txt b/demos/CMakeLists.txt new file mode 100644 index 0000000..ca33a09 --- /dev/null +++ b/demos/CMakeLists.txt @@ -0,0 +1,2 @@ +add_executable(hard_disc hard_disc.cc) +target_link_libraries(hard_disc PUBLIC aabbcc) From 3d49df9015bfbe3127eddf44dfa086b87d15c6c6 Mon Sep 17 00:00:00 2001 From: Pablo Hernandez-Cerdan Date: Wed, 9 Oct 2019 16:24:16 +0200 Subject: [PATCH 2/2] Add custom_target to generate header only file This includes some string replacements in header and source files --- CMakeLists.txt | 21 ++++++++++++++++++++- cmake/generate_header_only.cmake | 16 ++++++++++++++++ cmake/header-only.h.in | 4 ++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 cmake/generate_header_only.cmake create mode 100644 cmake/header-only.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 059461f..8137d29 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,14 @@ include(GNUInstallDirs) # Define CMAKE_INSTALL_xxx: LIBDIR, INCLUDEDIR option(AABBCC_BUILD_DEMOS "Build demos" OFF) mark_as_advanced(AABBCC_BUILD_DEMOS) -add_library(aabbcc src/AABB.cc) +option(AABBCC_GENERATE_HEADER_ONLY "Generate a header-only file" OFF) +mark_as_advanced(AABBCC_GENERATE_HEADER_ONLY) + set(aabbcc_include_dir src) +set(header_file_ ${CMAKE_CURRENT_SOURCE_DIR}/src/AABB.h) +set(source_file_ ${CMAKE_CURRENT_SOURCE_DIR}/src/AABB.cc) + +add_library(aabbcc ${source_file_}) set(aabbcc_export_file "${PROJECT_BINARY_DIR}/aabbccTargets.cmake") target_include_directories(aabbcc PUBLIC @@ -37,6 +43,19 @@ if(AABBCC_BUILD_DEMOS) add_subdirectory(demos) endif() +if(AABBCC_GENERATE_HEADER_ONLY) + set(header_only_configure_file_input_ ${CMAKE_CURRENT_SOURCE_DIR}/cmake/header-only.h.in) + set(header_only_configure_file_output_ AABB.h) + add_custom_target(header-only + COMMAND ${CMAKE_COMMAND} + -Dheader_file_=${header_file_} + -Dsource_file_=${source_file_} + -Dconfigure_file_input_=${header-only_configure_file_input} + -Dheader_only_configure_file_input_=${header_only_configure_file_input_} + -Dheader_only_configure_file_output_=${header_only_configure_file_output_} + -P ${CMAKE_CURRENT_LIST_DIR}/cmake/generate_header_only.cmake) +endif() + # Install targets and config files set(install_cmake_dir "${CMAKE_INSTALL_LIBDIR}/cmake/aabbcc") install (EXPORT aabbccTargets diff --git a/cmake/generate_header_only.cmake b/cmake/generate_header_only.cmake new file mode 100644 index 0000000..d05ff4e --- /dev/null +++ b/cmake/generate_header_only.cmake @@ -0,0 +1,16 @@ +# Example of usage from any other CMakeLists. +# Use absolute paths for variables refering to files. +# add_custom_target(header-only +# COMMAND ${CMAKE_COMMAND} +# -Dheader_file_=${header_file_} +# -Dsource_file_=${source_file_} +# -Dconfigure_file_input_=${header-only_configure_file_input} +# -Dheader_only_configure_file_input_=${header_only_configure_file_input_} +# -Dheader_only_configure_file_output_=${header_only_configure_file_output_} +# -P ${CMAKE_CURRENT_LIST_DIR}/cmake/generate_header_only.cmake) +# +file(READ "${header_file_}" HEADER_FILE) +STRING(REPLACE "#endif /* _AABB_H */" " " HEADER_FILE_HEAD ${HEADER_FILE}) +file(READ "${source_file_}" SOURCE_FILE) +STRING(REPLACE "#include \"AABB.h\"" " " SOURCE_FILE_TAIL ${SOURCE_FILE}) +configure_file(${header_only_configure_file_input_} ${header_only_configure_file_output_}) diff --git a/cmake/header-only.h.in b/cmake/header-only.h.in new file mode 100644 index 0000000..1745193 --- /dev/null +++ b/cmake/header-only.h.in @@ -0,0 +1,4 @@ +@HEADER_FILE_HEAD@ +// -- SOURCE FILE -- +@SOURCE_FILE_TAIL@ +#endif /* AUTOGENERATED_AABB_H */