|
| 1 | +# This is the "physical" (i.e. real) name of the library. |
| 2 | +# The actual file name of the library built is constructed based on conventions |
| 3 | +# of the native platform, such as lib<LIBRARY_TARGET_NAME>.a, |
| 4 | +# lib<LIBRARY_TARGET_NAME>.dylib or <LIBRARY_TARGET_NAME>.lib. |
| 5 | +set(LIBRARY_TARGET_NAME ${PROJECT_NAME}HeaderOnly) |
| 6 | + |
| 7 | +# List of HPP (header) library files. |
| 8 | +set(${LIBRARY_TARGET_NAME}_HDR |
| 9 | + include/LibHeaderOnlyTemplateCMake/LibHeaderOnlyTemplateCMake.h |
| 10 | +) |
| 11 | + |
| 12 | +# You can add an external dependency using the find_package() function call |
| 13 | +# See: https://cmake.org/cmake/help/latest/command/find_package.html |
| 14 | +# Note that the imported objects resulting from the find_package() depends upon |
| 15 | +# the configuration files generated by the developer of the library. |
| 16 | +# These imported objects can be either: |
| 17 | +# - variables that must be used to include directories and/or link libraries |
| 18 | +# with function calls like target_include_directories() and/or |
| 19 | +# target_link_libraries(); |
| 20 | +# - a CMake target that must only be linked using target_link_libraries() and |
| 21 | +# CMake will take care of including and/or linking the appropriate |
| 22 | +# directories and/or libraries. |
| 23 | +# See: https://cmake.org/cmake/help/latest/command/target_include_directories.html |
| 24 | +# See: https://cmake.org/cmake/help/latest/command/target_link_libraries.html |
| 25 | +# For example: |
| 26 | +# find_package(FooPackage) may import either |
| 27 | +# - variables: FooPackage_INCLUDE_DIRS and FooPackage_LIBRARIES |
| 28 | +# - target: FooPackage::FooPackage |
| 29 | + |
| 30 | +# Adds a library target called ${LIBRARY_TARGET_NAME} to be built from the |
| 31 | +# source and header files listed in the command invocation. |
| 32 | +add_library(${LIBRARY_TARGET_NAME} INTERFACE) |
| 33 | + |
| 34 | +# Within this project, you can link to this library by just specifing the name |
| 35 | +# of the target, i.e. ${LIBRARY_TARGET_NAME} = LibTemplateCMakeHeaderOnly. It is |
| 36 | +# useful, however, to define an alias of this library with the scope of the |
| 37 | +# exported library itself because (1) you will link against it with the exact |
| 38 | +# same syntax of an imported library and (2) because names having a double-colon |
| 39 | +# (::) are always treated as the name of either an alias or imported target. Any |
| 40 | +# attempt to use such a name for a different target type will result in an error. |
| 41 | +add_library(${PROJECT_NAME}::${LIBRARY_TARGET_NAME} ALIAS ${LIBRARY_TARGET_NAME}) |
| 42 | + |
| 43 | +# Specify include directories for both compilation and installation process. |
| 44 | +# The $<INSTALL_PREFIX> generator expression is useful to ensure to create |
| 45 | +# relocatable configuration files, see https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#creating-relocatable-packages |
| 46 | +target_include_directories(${LIBRARY_TARGET_NAME} INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>" |
| 47 | + "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>") |
| 48 | + |
| 49 | +# If you used find_package() you need to use target_include_directories() and/or |
| 50 | +# target_link_libraries(). As explained previously, depending on the imported |
| 51 | +# objects, you may need to call either or both: |
| 52 | +# - with imported variable: |
| 53 | +# target_include_directories(${LIBRARY_TARGET_NAME} ${FooPackage_INCLUDE_DIRS}) |
| 54 | +# target_link_libraries(${LIBRARY_TARGET_NAME} ${FooPackage_LIBRARIES}) |
| 55 | +# - with imported target: |
| 56 | +# target_link_libraries(${LIBRARY_TARGET_NAME} FooPackage_LIBRARIES::FooPackage_LIBRARIES) |
| 57 | + |
| 58 | +# Specify installation targets, typology and destination folders. |
| 59 | +install(TARGETS ${LIBRARY_TARGET_NAME} |
| 60 | + EXPORT ${PROJECT_NAME}) |
| 61 | + |
| 62 | +install(FILES ${${LIBRARY_TARGET_NAME}_HDR} |
| 63 | + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${LIBRARY_TARGET_NAME}") |
| 64 | + |
| 65 | + |
| 66 | +message(STATUS "Created target ${LIBRARY_TARGET_NAME} for export ${PROJECT_NAME}.") |
0 commit comments