|
| 1 | +# |
| 2 | +# Library name and options |
| 3 | +# |
| 4 | + |
| 5 | +# Target name |
| 6 | +set(target backtrace) |
| 7 | + |
| 8 | +# Exit here if required dependencies are not met |
| 9 | +message(STATUS "Lib ${target}") |
| 10 | + |
| 11 | +# Set API export file and macro |
| 12 | +string(TOUPPER ${target} target_upper) |
| 13 | +set(feature_file "include/${target}/${target}_features.h") |
| 14 | +set(export_file "include/${target}/${target}_api.h") |
| 15 | +set(export_macro "${target_upper}_API") |
| 16 | + |
| 17 | +# |
| 18 | +# Compiler warnings |
| 19 | +# |
| 20 | + |
| 21 | +include(Warnings) |
| 22 | + |
| 23 | +# |
| 24 | +# Compiler security |
| 25 | +# |
| 26 | + |
| 27 | +include(SecurityFlags) |
| 28 | + |
| 29 | +# |
| 30 | +# Sources |
| 31 | +# |
| 32 | + |
| 33 | +set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}") |
| 34 | +set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source") |
| 35 | + |
| 36 | +set(headers |
| 37 | + ${include_path}/backtrace.h |
| 38 | +) |
| 39 | + |
| 40 | +if(UNIX) |
| 41 | + set(BACKTRACE_IMPL unix) |
| 42 | +elseif(WIN32) |
| 43 | + set(BACKTRACE_IMPL win32) |
| 44 | +else() |
| 45 | + message(ERROR "Backtrace library not supported in this platform") |
| 46 | +endif() |
| 47 | + |
| 48 | +set(sources |
| 49 | + ${source_path}/backtrace.c |
| 50 | + ${source_path}/backtrace_${BACKTRACE_IMPL}.c |
| 51 | +) |
| 52 | + |
| 53 | +# Group source files |
| 54 | +set(header_group "Header Files (API)") |
| 55 | +set(source_group "Source Files") |
| 56 | +source_group_by_path(${include_path} "\\\\.h$|\\\\.hpp$" |
| 57 | + ${header_group} ${headers}) |
| 58 | +source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$" |
| 59 | + ${source_group} ${sources}) |
| 60 | + |
| 61 | +# |
| 62 | +# Create library |
| 63 | +# |
| 64 | + |
| 65 | +# Build library |
| 66 | +add_library(${target} OBJECT |
| 67 | + ${sources} |
| 68 | + ${headers} |
| 69 | +) |
| 70 | + |
| 71 | +# Create namespaced alias |
| 72 | +add_library(${META_PROJECT_NAME}::${target} ALIAS ${target}) |
| 73 | + |
| 74 | +# Export library for downstream projects |
| 75 | +export(TARGETS ${target} NAMESPACE ${META_PROJECT_NAME}:: FILE ${PROJECT_BINARY_DIR}/cmake/${target}/${target}-export.cmake) |
| 76 | + |
| 77 | +# Create feature detection header |
| 78 | +# Compilers: https://cmake.org/cmake/help/v3.1/variable/CMAKE_LANG_COMPILER_ID.html#variable:CMAKE_%3CLANG%3E_COMPILER_ID |
| 79 | +# Feature: https://cmake.org/cmake/help/v3.1/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html |
| 80 | + |
| 81 | +# Check for availability of module; use pre-generated version if not found |
| 82 | +if (WriterCompilerDetectionHeaderFound) |
| 83 | + write_compiler_detection_header( |
| 84 | + FILE ${feature_file} |
| 85 | + PREFIX ${target_upper} |
| 86 | + COMPILERS AppleClang Clang GNU MSVC |
| 87 | + FEATURES cxx_alignas cxx_alignof cxx_constexpr cxx_final cxx_noexcept cxx_nullptr cxx_sizeof_member cxx_thread_local |
| 88 | + VERSION 3.2 |
| 89 | + ) |
| 90 | +else() |
| 91 | + file( |
| 92 | + COPY ${PROJECT_SOURCE_DIR}/codegeneration/${target}_features.h |
| 93 | + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/include/${target} |
| 94 | + USE_SOURCE_PERMISSIONS |
| 95 | + ) |
| 96 | +endif() |
| 97 | + |
| 98 | +# Create API export header |
| 99 | +generate_export_header(${target} |
| 100 | + EXPORT_FILE_NAME ${export_file} |
| 101 | + EXPORT_MACRO_NAME ${export_macro} |
| 102 | +) |
| 103 | + |
| 104 | +# |
| 105 | +# Project options |
| 106 | +# |
| 107 | + |
| 108 | +set_target_properties(${target} |
| 109 | + PROPERTIES |
| 110 | + ${DEFAULT_PROJECT_OPTIONS} |
| 111 | + FOLDER "${IDE_FOLDER}" |
| 112 | +) |
| 113 | + |
| 114 | +# |
| 115 | +# Include directories |
| 116 | +# |
| 117 | + |
| 118 | +target_include_directories(${target} |
| 119 | + PRIVATE |
| 120 | + ${PROJECT_BINARY_DIR}/source/include |
| 121 | + ${CMAKE_CURRENT_SOURCE_DIR}/include |
| 122 | + ${CMAKE_CURRENT_BINARY_DIR}/include |
| 123 | + |
| 124 | + PUBLIC |
| 125 | + ${DEFAULT_INCLUDE_DIRECTORIES} |
| 126 | + |
| 127 | + INTERFACE |
| 128 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 129 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include> |
| 130 | + $<INSTALL_INTERFACE:include> |
| 131 | +) |
| 132 | + |
| 133 | +# |
| 134 | +# Libraries |
| 135 | +# |
| 136 | + |
| 137 | +target_link_libraries(${target} |
| 138 | + PRIVATE |
| 139 | + ${META_PROJECT_NAME}::version |
| 140 | + |
| 141 | + PUBLIC |
| 142 | + ${DEFAULT_LIBRARIES} |
| 143 | + |
| 144 | + INTERFACE |
| 145 | +) |
| 146 | + |
| 147 | +# |
| 148 | +# Compile definitions |
| 149 | +# |
| 150 | + |
| 151 | +target_compile_definitions(${target} |
| 152 | + PRIVATE |
| 153 | + ${target_upper}_EXPORTS # Export API |
| 154 | + |
| 155 | + PUBLIC |
| 156 | + $<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:${target_upper}_STATIC_DEFINE> |
| 157 | + ${DEFAULT_COMPILE_DEFINITIONS} |
| 158 | + |
| 159 | + INTERFACE |
| 160 | +) |
| 161 | + |
| 162 | +# |
| 163 | +# Compile options |
| 164 | +# |
| 165 | + |
| 166 | +target_compile_options(${target} |
| 167 | + PRIVATE |
| 168 | + |
| 169 | + PUBLIC |
| 170 | + ${DEFAULT_COMPILE_OPTIONS} |
| 171 | + |
| 172 | + INTERFACE |
| 173 | +) |
| 174 | + |
| 175 | +# |
| 176 | +# Linker options |
| 177 | +# |
| 178 | + |
| 179 | +target_link_libraries(${target} |
| 180 | + PRIVATE |
| 181 | + |
| 182 | + PUBLIC |
| 183 | + ${DEFAULT_LINKER_OPTIONS} |
| 184 | + |
| 185 | + INTERFACE |
| 186 | +) |
| 187 | + |
| 188 | +# |
| 189 | +# Deployment |
| 190 | +# |
| 191 | + |
| 192 | +# Export |
| 193 | +install(TARGETS ${target} |
| 194 | + EXPORT "${target}-export" COMPONENT dev |
| 195 | +) |
0 commit comments