-
Notifications
You must be signed in to change notification settings - Fork 54
/
CMakeLists.txt
94 lines (83 loc) · 5.48 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# This is the "physical" (i.e. real) name of the library.
# The actual file name of the library built is constructed based on conventions
# of the native platform, such as lib<LIBRARY_TARGET_NAME>.a,
# lib<LIBRARY_TARGET_NAME>.dylib or <LIBRARY_TARGET_NAME>.lib.
#
# In order to use a library name LIBRARY_TARGET_NAME that is not the same as the
# `EXPORT` used in the `install` command (see the end of this file), it is
# required to use `FIRST_TARGET` option of the `install_basic_package_files`
# command in the main CMakeLists.txt file with the name of one of the exported
# targets. `FIRST_TARGET` option defaults to LIBRARY_TARGET_NAME.
# By using setting LIBRARY_TARGET_NAME to ${PROJECT_NAME} it is not necessary to
# use `FIRST_TARGET` option in the main CMakeLists.txt.
# In general, in a C++ project, there is at least one exported target whose
# name is always know. Consequently, `FIRST_TARGET` option in the main
# CMakeLists.txt can be statically set to such target name. In the context of
# projects where users may choose what target to export, thus the name of a
# target may change at configure time, you can use propagate back to parent
# CMakeLists.txt, up to the main one, a variable containing the name of a
# target. In order to do it, you can use `set` command with `PARENT_SCOPE`
# option, see https://cmake.org/cmake/help/latest/command/set.html.
set(LIBRARY_TARGET_NAME ${PROJECT_NAME})
# List of CPP (source) library files.
set(${LIBRARY_TARGET_NAME}_SRC
src/LibTemplateCMake.cpp
)
# List of HPP (header) library files.
set(${LIBRARY_TARGET_NAME}_HDR
include/LibTemplateCMake/LibTemplateCMake.h
)
# You can add an external dependency using the find_package() function call
# See: https://cmake.org/cmake/help/latest/command/find_package.html
# Note that the imported objects resulting from the find_package() depends upon
# the configuration files generated by the developer of the library.
# These imported objects can be either:
# - variables that must be used to include directories and/or link libraries
# with function calls like target_include_directories() and/or
# target_link_libraries();
# - a CMake target that must only be linked using target_link_libraries() and
# CMake will take care of including and/or linking the appropriate
# directories and/or libraries.
# See: https://cmake.org/cmake/help/latest/command/target_include_directories.html
# See: https://cmake.org/cmake/help/latest/command/target_link_libraries.html
# For example:
# find_package(FooPackage) may import either
# - variables: FooPackage_INCLUDE_DIRS and FooPackage_LIBRARIES
# - target: FooPackage::FooPackage
# Adds a library target called ${LIBRARY_TARGET_NAME} to be built from the
# source and header files listed in the command invocation.
add_library(${LIBRARY_TARGET_NAME} ${${LIBRARY_TARGET_NAME}_SRC} ${${LIBRARY_TARGET_NAME}_HDR})
# Within this project, you can link to this library by just specifing the name
# of the target, i.e. ${LIBRARY_TARGET_NAME} = LibTemplateCMake. It is useful,
# however, to define an alias of this library with the scope of the exported
# library itself because (1) you will link against it with the exact same syntax
# of an imported library and (2) because names having a double-colon (::) are
# always treated as the name of either an alias or imported target. Any attempt
# to use such a name for a different target type will result in an error.
add_library(${PROJECT_NAME}::${LIBRARY_TARGET_NAME} ALIAS ${LIBRARY_TARGET_NAME})
# Set two minimum target properties for the library.
# See https://cmake.org/cmake/help/latest/command/set_target_properties.html
# Properties are: 1) Library version number 2) list of corresponding public headers
set_target_properties(${LIBRARY_TARGET_NAME} PROPERTIES VERSION ${${PROJECT_NAME}_VERSION}
PUBLIC_HEADER "${${LIBRARY_TARGET_NAME}_HDR}")
# Specify include directories for both compilation and installation process.
# The $<INSTALL_PREFIX> generator expression is useful to ensure to create
# relocatable configuration files, see https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#creating-relocatable-packages
target_include_directories(${LIBRARY_TARGET_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
# If you used find_package() you need to use target_include_directories() and/or
# target_link_libraries(). As explained previously, depending on the imported
# objects, you may need to call either or both:
# - with imported variable:
# target_include_directories(${LIBRARY_TARGET_NAME} ${FooPackage_INCLUDE_DIRS})
# target_link_libraries(${LIBRARY_TARGET_NAME} ${FooPackage_LIBRARIES})
# - with imported target:
# target_link_libraries(${LIBRARY_TARGET_NAME} FooPackage_LIBRARIES::FooPackage_LIBRARIES)
# Specify installation targets, typology and destination folders.
install(TARGETS ${LIBRARY_TARGET_NAME}
EXPORT ${PROJECT_NAME}
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT shlib
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT lib
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${LIBRARY_TARGET_NAME}" COMPONENT dev)
message(STATUS "Created target ${LIBRARY_TARGET_NAME} for export ${PROJECT_NAME}.")