-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
219 lines (189 loc) · 6.98 KB
/
CMakeLists.txt
File metadata and controls
219 lines (189 loc) · 6.98 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# This file creates project 'Foo' with two library targets 'bar' and 'baz'.
# Target 'bar' depends on 'baz'. After installation this project can be found
# by 'find_package(... CONFIG)' command:
#
# find_package(Foo CONFIG REQUIRED)
# target_link_libraries(... Foo::bar)
#
# Note that requirements propagated automatically, for example:
# * Foo::baz linked automatically
# * <prefix>/include added to header search path
# * FOO_BAZ_DEBUG=1/FOO_BAR_DEBUG=1 added on Debug
# * FOO_BAZ_DEBUG=0/FOO_BAR_DEBUG=0 added on other configurations
####
# Set minimum version of CMake.
cmake_minimum_required(VERSION 3.22) # GENERATOR_IS_MULTI_CONFIG
####
# Set variables:
# * PROJECT_NAME
# * PROJECT_VERSION
project(ArrayBuilder VERSION 1.0.0 LANGUAGES C)
# Debug Information Format:
# * https://docs.microsoft.com/en-us/cpp/build/reference/z7-zi-zi-debug-information-format
#
# Notes:
#
# * /Z7 still produce PDB file for DLL and without the PDB file installed
# you can't debug DLL
#
# * /Z7 for static library doesn't produce PDB. It's the best option if you
# want debug library without changing internal CMake code.
# Toolchain example: https://github.com/ruslo/polly/blob/master/vs-15-2017-win64-z7.cmake
#
# * /Zi option is default (produce separate PDB files)
#
# * TARGET_PDB_FILE generator expression doesn't support static libraries.
# See https://gitlab.kitware.com/cmake/cmake/issues/16932
# (that's why it's not used here)
#
# * This code can be implemented as a 'PDB DESTINATION' feature.
# See https://gitlab.kitware.com/cmake/cmake/issues/16935#note_275180
#
# * By default only Debug/RelWithDebInfo produce debug information,
# Release/MinSizeRel do not.
#
# * Generated PDB for static libraries doesn't respect CMAKE_<CONFIG>_POSTFIX
# variable. It means if you specify Debug and RelWithDebInfo then generated
# PDB files for both will be "md5.pdb". When PDB files installed one will
# overwrite another making it unusable. Release + Debug configurations will
# work fine because Release doesn't produce PDB files.
#
# * All PDB files will be installed, including PDB for targets that will not
# be installed themselves.
if(MSVC)
set(pdb_output_dir "${CMAKE_CURRENT_BINARY_DIR}/pdb-files")
set(CMAKE_PDB_OUTPUT_DIRECTORY "${pdb_output_dir}")
set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY "${pdb_output_dir}")
get_cmake_property(is_multi GENERATOR_IS_MULTI_CONFIG)
if(is_multi)
set(config_suffix "$<CONFIG>")
else()
set(config_suffix "")
endif()
# Introduce variables:
# * CMAKE_INSTALL_LIBDIR
# * CMAKE_INSTALL_BINDIR
include(GNUInstallDirs)
if(BUILD_SHARED_LIBS)
set(pdb_dst ${CMAKE_INSTALL_BINDIR})
else()
set(pdb_dst ${CMAKE_INSTALL_LIBDIR})
endif()
install(
DIRECTORY "${pdb_output_dir}/${config_suffix}/"
DESTINATION ${pdb_dst}
)
endif()
####
# Create targets
add_library(array_builder "src/array_builder.c" "src/array_builder.h")
####
# Properties of targets
# Add definitions for targets
# Values:
# * Debug: -DFOO_BAR_DEBUG=1
# * Release: -DFOO_BAR_DEBUG=0
# * other: -DFOO_BAR_DEBUG=0
target_compile_definitions(array_builder PUBLIC "FOO_BAR_DEBUG=$<CONFIG:Debug>")
# Generate:
# * ${CMAKE_CURRENT_BINARY_DIR}/generated_headers/foo/BAR_EXPORT.h with BAR_EXPORT
# * ${CMAKE_CURRENT_BINARY_DIR}/generated_headers/foo/BAZ_EXPORT.h with BAZ_EXPORT
# Renaming because:
# * We need prefix 'foo' to fit OSX/iOS frameworks layout
# * File name match name of the macro
set(generated_headers "${CMAKE_CURRENT_BINARY_DIR}/generated_headers")
set(array_builder_export "${generated_headers}/array_builder/ARRAY_BUILDER_EXPORT.h")
# https://cmake.org/cmake/help/v3.9/module/GenerateExportHeader.html
include(GenerateExportHeader)
generate_export_header(array_builder EXPORT_FILE_NAME ${array_builder_export})
# Global includes. Used by all targets
# Note:
# * header location in project: Foo/Source/foo/Bar.hpp
# * header can be included by C++ code `#include <foo/Bar.hpp>`
# * header location in project: ${CMAKE_CURRENT_BINARY_DIR}/generated_headers/foo/BAR_EXPORT.h
# * header can be included by: `#include <BAR_EXPORT.h>`
#target_include_directories(
# baz PUBLIC
# "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/Source>"
# "$<BUILD_INTERFACE:${generated_headers}>"
#)
#
## Link required library
#target_link_libraries(bar PUBLIC baz)
# Installation (https://github.com/forexample/package-example) {
# Introduce variables:
# * CMAKE_INSTALL_LIBDIR
# * CMAKE_INSTALL_BINDIR
# * CMAKE_INSTALL_INCLUDEDIR
include(GNUInstallDirs)
# Layout. This works for all platforms:
# * <prefix>/lib*/cmake/<PROJECT-NAME>
# * <prefix>/lib*/
# * <prefix>/include/
set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
# Configuration
set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake")
set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")
set(TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
set(namespace "${PROJECT_NAME}::")
# Include module with fuction 'write_basic_package_version_file'
include(CMakePackageConfigHelpers)
# Configure '<PROJECT-NAME>ConfigVersion.cmake'
# Use:
# * PROJECT_VERSION
write_basic_package_version_file(
"${version_config}" COMPATIBILITY SameMajorVersion
)
# Configure '<PROJECT-NAME>Config.cmake'
# Use variables:
# * TARGETS_EXPORT_NAME
# * PROJECT_NAME
configure_package_config_file(
"cmake/Config.cmake.in"
"${project_config}"
INSTALL_DESTINATION "${config_install_dir}"
)
# Targets:
# * <prefix>/lib/libbar.a
# * <prefix>/lib/libbaz.a
# * header location after install: <prefix>/include/foo/Bar.hpp
# * headers can be included by C++ code `#include <foo/Bar.hpp>`
install(
TARGETS array_builder
EXPORT "${TARGETS_EXPORT_NAME}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
# Headers:
# * Source/foo/Bar.hpp -> <prefix>/include/foo/Bar.hpp
# * Source/foo/Baz.hpp -> <prefix>/include/foo/Baz.hpp
install(
DIRECTORY "src/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/array_builder"
FILES_MATCHING PATTERN "*.h"
)
# Export headers:
# * ${CMAKE_CURRENT_BINARY_DIR}/.../BAR_EXPORT.h -> <prefix>/include/foo/BAR_EXPORT.h
# * ${CMAKE_CURRENT_BINARY_DIR}/.../BAZ_EXPORT.h -> <prefix>/include/foo/BAZ_EXPORT.h
install(
FILES "${array_builder_export}"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/array_builder"
)
# Config
# * <prefix>/lib/cmake/Foo/FooConfig.cmake
# * <prefix>/lib/cmake/Foo/FooConfigVersion.cmake
install(
FILES "${project_config}" "${version_config}"
DESTINATION "${config_install_dir}"
)
# Config
# * <prefix>/lib/cmake/Foo/FooTargets.cmake
install(
EXPORT "${TARGETS_EXPORT_NAME}"
NAMESPACE "${namespace}"
DESTINATION "${config_install_dir}"
)
# }