| 1 |
cmake_minimum_required(VERSION 3.12) |
| 2 |
|
| 3 |
include(cmake/ProjectVersion.cmake) |
| 4 |
|
| 5 |
project(LibBGCode VERSION ${LibBGCode_VERSION}) |
| 6 |
|
| •7 |
# Turn on position independent code |
| 8 |
set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 9 |
|
| 10 |
# Set C++ standard |
| 11 |
set(CMAKE_CXX_STANDARD 17) |
| 12 |
set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 13 |
|
| 14 |
set(_libname ${PROJECT_NAME}) |
| 15 |
string(TOLOWER ${_libname} _libname) |
| 16 |
string(REPLACE "lib" "" _libname ${_libname}) |
| 17 |
|
| 18 |
option(${PROJECT_NAME}_BUILD_TESTS "Build unit tests" ON) |
| 19 |
option(${PROJECT_NAME}_BUILD_COMPONENT_Binarize "Include Binarize component in the library" ON) |
| 20 |
option(${PROJECT_NAME}_BUILD_SANITIZERS "Turn on sanitizers" OFF) |
| 21 |
|
| 22 |
# Dependency build management |
| 23 |
option(${PROJECT_NAME}_BUILD_DEPS "Build dependencies before the project" OFF) |
| 24 |
option(${PROJECT_NAME}_DEPS_OUTPUT_QUIET "Don't print build output for dependencies" ON) |
| 25 |
option(${PROJECT_NAME}_DEPS_SHARED "Build dependencies as shared libraries" OFF) |
| 26 |
set(${PROJECT_NAME}_DEPS_PRESET "default" CACHE STRING "Preset of the dependencies when ${PROJECT_NAME}_BUILD_DEPS is ON") |
| 27 |
set(${PROJECT_NAME}_DEPS_BUILD_DIR "" CACHE PATH "Binary dir of the dependencies build when ${PROJECT_NAME}_BUILD_DEPS is ON") |
| 28 |
|
| 29 |
if (${PROJECT_NAME}_BUILD_DEPS) |
| 30 |
include(deps/autobuild.cmake) |
| 31 |
endif () |
| 32 |
|
| 33 |
if (NOT CMAKE_DEBUG_POSTFIX) |
| 34 |
set(CMAKE_DEBUG_POSTFIX "d") |
| 35 |
endif () |
| 36 |
|
| 37 |
include(CMakeDependentOption) |
| 38 |
cmake_dependent_option(${PROJECT_NAME}_BUILD_COMPONENT_Convert "Include Convert component in the library" ON |
| 39 |
"${PROJECT_NAME}_BUILD_COMPONENT_Binarize" OFF) |
| 40 |
cmake_dependent_option(${PROJECT_NAME}_BUILD_CMD_TOOL "Include bgcode command line tool in the library" ON |
| 41 |
"${PROJECT_NAME}_BUILD_COMPONENT_Convert" OFF) |
| 42 |
|
| 43 |
if (EMSCRIPTEN) |
| 44 |
cmake_dependent_option(${PROJECT_NAME}_BUILD_WASM "Include bgcode wasm module in the build" ON |
| 45 |
"${PROJECT_NAME}_BUILD_COMPONENT_Convert" OFF) |
| 46 |
endif () |
| 47 |
|
| 48 |
set(_selected_components "Core") |
| 49 |
set(_selected_libs "${_libname}_core") |
| 50 |
set(_highest_comp "Core") |
| 51 |
|
| 52 |
# Set this if downstream would need to link additional boost |
| 53 |
# components |
| 54 |
set(Boost_DOWNSTREAM_COMPONENTS "") |
| 55 |
|
| 56 |
set(namespace "${PROJECT_NAME}::") |
| 57 |
|
| 58 |
# Create an export header |
| 59 |
include(GenerateExportHeader) |
| 60 |
|
| 61 |
set(_srcloc src/${PROJECT_NAME}) |
| 62 |
|
| 63 |
configure_file( |
| 64 |
${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.rc.in |
| 65 |
${PROJECT_BINARY_DIR}/version.rc |
| 66 |
@ONLY) |
| 67 |
|
| 68 |
if (CMAKE_COMPILER_IS_GNUCXX) |
| 69 |
add_compile_options(-fvisibility=hidden) |
| 70 |
endif () |
| 71 |
|
| 72 |
add_subdirectory(${_srcloc}/core) |
| 73 |
|
| 74 |
if (${PROJECT_NAME}_BUILD_COMPONENT_Binarize) |
| 75 |
add_subdirectory(${_srcloc}/binarize) |
| 76 |
|
| 77 |
list(APPEND _selected_components Binarize) |
| 78 |
list(APPEND _selected_libs ${_libname}_binarize) |
| 79 |
set(_highest_comp "Binarize") |
| 80 |
endif () |
| 81 |
|
| 82 |
if (${PROJECT_NAME}_BUILD_COMPONENT_Convert) |
| 83 |
add_subdirectory(${_srcloc}/convert) |
| 84 |
|
| 85 |
list(APPEND _selected_components Convert) |
| 86 |
list(APPEND _selected_libs ${_libname}_convert) |
| 87 |
set(_highest_comp "Convert") |
| 88 |
endif () |
| 89 |
|
| 90 |
add_library(${_libname} INTERFACE) |
| 91 |
target_link_libraries(${_libname} INTERFACE ${_selected_libs}) |
| 92 |
|
| 93 |
if (${PROJECT_NAME}_BUILD_CMD_TOOL) |
| 94 |
add_subdirectory(${_srcloc}/cmd) |
| 95 |
endif () |
| 96 |
|
| 97 |
if (${PROJECT_NAME}_BUILD_WASM) |
| 98 |
add_subdirectory(${_srcloc}/wasm) |
| 99 |
endif() |
| 100 |
|
| 101 |
if (${PROJECT_NAME}_BUILD_PYTHON_BINDING) |
| 102 |
set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 103 |
endif () |
| 104 |
|
| 105 |
if(${PROJECT_NAME}_BUILD_TESTS) |
| 106 |
enable_testing () |
| 107 |
add_subdirectory(tests) |
| 108 |
endif() |
| 109 |
|
| 110 |
# Create and install the CMake config script |
| 111 |
include(CMakePackageConfigHelpers) |
| 112 |
include(GNUInstallDirs) |
| 113 |
set(CONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") |
| 114 |
|
| 115 |
set(version_config "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake") |
| 116 |
set(project_config "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake") |
| 117 |
|
| 118 |
install(TARGETS ${_libname} |
| 119 |
EXPORT ${PROJECT_NAME}{_highest_comp}Targets |
| 120 |
INCLUDES DESTINATION include/${PROJECT_NAME} |
| 121 |
) |
| 122 |
|
| 123 |
foreach(_comp ${_selected_components}) |
| 124 |
# Install targets and headers |
| 125 |
string(TOLOWER ${_comp} _comp_lower) |
| 126 |
set(_export_targets ${_libname}_${_comp_lower}) |
| 127 |
|
| 128 |
set_target_properties(${_libname}_${_comp_lower} |
| 129 |
PROPERTIES |
| 130 |
VERSION ${PROJECT_VERSION} |
| 131 |
SOVERSION ${PROJECT_VERSION}) |
| 132 |
|
| 133 |
install(TARGETS ${_export_targets} |
| 134 |
EXPORT ${PROJECT_NAME}${_comp}Targets |
| 135 |
INCLUDES DESTINATION include/${PROJECT_NAME} |
| 136 |
) |
| 137 |
|
| 138 |
install(FILES ${_srcloc}/${_comp_lower}/${_comp_lower}.hpp DESTINATION include/${PROJECT_NAME}/${_comp_lower}) |
| 139 |
install(FILES |
| 140 |
${PROJECT_BINARY_DIR}/${_comp_lower}/export.h DESTINATION include/${PROJECT_NAME}/${_comp_lower}/ |
| 141 |
) |
| 142 |
install( |
| 143 |
EXPORT "${PROJECT_NAME}${_comp}Targets" |
| 144 |
NAMESPACE "${namespace}" |
| 145 |
DESTINATION "${CONFIG_INSTALL_DIR}" |
| 146 |
) |
| 147 |
endforeach() |
| 148 |
|
| 149 |
write_basic_package_version_file( |
| 150 |
"${version_config}" |
| 151 |
VERSION ${PROJECT_VERSION} |
| 152 |
COMPATIBILITY SameMajorVersion |
| 153 |
) |
| 154 |
|
| 155 |
configure_package_config_file( |
| 156 |
"cmake/Config.cmake.in" |
| 157 |
"${project_config}" |
| 158 |
INSTALL_DESTINATION "${CONFIG_INSTALL_DIR}" |
| 159 |
NO_CHECK_REQUIRED_COMPONENTS_MACRO |
| 160 |
) |
| 161 |
|
| 162 |
install( |
| 163 |
FILES "${project_config}" "${version_config}" |
| 164 |
DESTINATION "${CONFIG_INSTALL_DIR}" |
| 165 |
) |