-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
749 lines (563 loc) · 23.4 KB
/
CMakeLists.txt
File metadata and controls
749 lines (563 loc) · 23.4 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#############################################
# It's Time To Do CMake Right
#
# https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/
#############################################
# /JSONUtils/CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(libjsonutils VERSION 1.0.0 LANGUAGES CXX)
add_library(JSONUtils src/json_utils.cpp)
target_include_directories(JSONUtils
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
$<{CMAKE_CURRENT_SOURCE_DIR}/src
)
target_compile_options(JSONUtils PRIVATE -Werror)
target_compile_features(JSONUtils PRIVATE cxx_std_11)
find_package(Boost 1.55 REQUIRED COMPONENTS regex)
find_package(RapidJSON 1.0 REQUIRED MODULE)
target_link_libraries(JSONUtils
PUBLIC
Boost::boost RapidJSON::RapidJSON
PRIVATE
Boost::regex
)
include(GNUInstallDirs)
install(TARGETS JSONUtils
EXPORT jsonutils-export
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(EXPORT jsonutil-targets
FILE
JSONUtilsTArgets.cmake
NAMESPACE
JSONUtils::
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/JSONUtils
)
# /Example/CMakeLists.txt
cmake_minimum_required(CERSION 3.5)
project(Example VERSION 1.0 LANGUAGES CXX)
add_executable(example src/example.cpp)
target_link_libraries(exampe JSONUtils::JSONUtils)
#############################################
# CMake Tutorial
#
# https://cmake.org/cmake/help/git-stage/guide/tutorial/index.html
#############################################
# A basic starting point
# An executable built from source code files.
# /CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(Tutorial VERSION 1.0)
option(USE_MYMATH "Use tutorial provided math implementation" ON)
include(CheckSymbolExists)
set(CMAKE_REQUIRED_LIBRARIES "m")
check_symbol_exists(log "math.h" HAVE_LOG)
check_symbol_exists(exp "math.h" HAVE_EXP)
add_library(compiler_flags INTERFACE)
target_compile_features(compiler_flags INTERFACE cxx_std_11)
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU>")
set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
target_compile_options(compiler_flags INTERFACE
"$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall;-Wshadow;-Wformat=2;-Wunused>>"
"$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>")
configure_file(TutorialConfig.h.in TutorialConfig.h)
if (USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
endif()
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
target_include_directories(Tutorial
PUBLIC
"${PROJECT_BINARY_DIR}")
install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" DESTINATION include)
enable_testing()
add_test(NAME Runs COMMAND Tutorial 25)
add_test(NAME Usage COMMAND Tutorial)
set_test_properties(Usage PROPERTIES
PASS_REUALAR_EXPRESSION "Usage:.*number")
function(do_test target arg result)
add_test(NAME Comp${arg} COMMAND ${target} ${arg})
set_tests_properties(Comp${arg} PROPERTIES
PASS_REULAR_EXPRESSION ${result})
endfunction()
do_test(Tutorial 4 "4 is 2")
do_test(Tutorial 9 "9 is 3")
do_test(Tutorial 5 "5 is 2.236")
do_test(Tutorial -25 "-25 is [-nan|nan|0]")
include(InstallRequiredSystemLibraries)
set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/License.txt)
set(CPACK_PACKAGE_VERSION_MAJOR ${Tutorial_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${Tutorial_VERSION_MINOR})
include(CPack)
install(EXPORT MathFunctionsTargets
FILE MathFunctionsTargets.cmake
DESTINATION lib/cmake/MathFunctions)
include(CMakePAckageConfigHelpers)
configure_package_config_files(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake"
INSTALL_DESTINATION "lib/cmake/example"
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfigVersion.cmake"
VERSION "${Tutorial_VERSION_MAJOR}.${Tutorial_VERSION_MINOR}"
COMPATIBILITY AnyNewerVersion)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsConfig.cmake
DESTINATION lib/cmake/MathFunctions)
export(EXPORT MathFunctionsTarget
FILE "${CMAKE_CURRENT_BINARY_DIR}/MathFunctionsTargets.cmake")
# /TutorialConfig.h
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
# /MathFunctions/CMakeLists.txt
add_executable(MakeTable MakeTable.cxx)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h
DEPENDS MakeTable)
add_library(MathFunctions mysqrt.cxx ${CMAKE_BINARY_DIR}/Table.h)
target_include_directories(MathFunctions
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>)
install(TARGETS MathFunctions DESTINATION lib EXPORT MathFunctionsTargets)
install(FILES MathFunctions.h DESTINATION include)
# /Config.cmake.in
@PACKAGE_INIT@
include ( "${CMAKE_CURRENT_LIST_DIR}/MathFunctionsTargets.cmake" )
#############################################
# An Introduction to Modern CMake
#
# https://cliutils.gitlab.io/modern-cmake/
#############################################
###
# Introduction to the basics.
###
# Set version of CMake to behave as.
cmake_minimum_required(
VERSION 3.1..3.15) # The minimum required version ... Highest version tested.
# Something about policies as well. Not sure on the details.
# Some information about the project.
project(
MyProject # Name of the project.
VERSION 1.0 # Version of the project.
DESCRIPTION "Very nice project" # Description of the project.
LANGUAGES CXX) # List of languages used. Defaults to "C CXX".
# Create an executable from source.
add_executable(
exe1 # Name of executable file generated and name of the CMake target.
two.cpp three.h) # List of files to include. Header files will not be compiled but included in IDEs.
# Create a library from source.
add_library(
lib1 # Name of the library file generated and name of the CMake target.
STATIC # STATIC, SHARED or MODULE. Defaults to either STATIC or SHARED based on BUILD_SHARED_LIBS.
two.cpp three.h) # List of files to include.
# STATIC = Static library. .a on Linux, .lib without .dll on Windows.
# SHARED = Dynamicly linked library. .so on Linux, .lib/.dll on Windows.
# MODULE = Dynamicly linkede library that is not linked at compile time but with dlopen. Plugins.
# There is also INTERFACE which means that nothing needs to be compiled, for
# example a header-only library.
# There is also ALIAS which is a new name for an existing target.
# Tell the given target about a directory where header files are stored.
target_include_directories(
exe1 # Target to modify.
PUBLIC # PUBLIC ⇒ Both this target and users need the directory.
# PRIVATE ⇒ Only this target needs the directory.
# INTERFACE ⇒ Only users needs the directory.
include) # The directory that should be passed to the compiler as a -I<directory>.
# Link exe1 to lib1.
target_link_libraries(
exe1 # Target that should get the dependency.
PUBLIC # Or PRIVATE or INTERFACE. Same as for target_include_directories.
lib1) # Library to link to. Can be either a target, a library on disk, or a linker flag.
# Targets can have
# - include directories
# - linked libraries
# - compile options
# - compile features
# - and more.
# This project requires CMake version 3.8.
cmake_minimum_required(VERSION 3.8)
# This is a C++ project named Calculator.
project(Calculator LANGUAGES CXX)
# The project contains a static library called calclib built from calclib.cpp.
add_library(calclib STATIC src/calclib.cpp include/calc/lib.hpp)
# To build calclib and anything that uses it one need header files from the
# include directory.
target_include_directories(calclib PUBLIC include)
# To build calclib and anything that uses it one need a compiler that supports
# C++11.
target_compile_features(calclib PUBLIC cxx_std_11)
# The project contains an executable called calc built from calc.cpp.
add_executable(calc apps/calc.cpp)
# The executable need to use calclib, which means that it incorporates all the
# PUBLIC and INTERFACE requirements of calclib. In this case that means the
# include include directory and a C++11 compiler.
target_link_libraries(calc PUBLIC calclib)
###
# Variables and the cache
###
set(
MY_VARIABLE # Name of the variable. Usually all caps.
"value") # The value to give to the variable.
# Variables are accessable until the end of a function or file. The lifetime
# can be expanded one scope step by adding PARENT_SCOPE at the end.
set(
MY_LIST # Name of the variable holding the list.
"one" "two") # The elements of the list.
set(MY_LIST "one;two") # This is an equivalent statement. That is, CMAKE doesn't
# really have lists but it can split strings at ';'.
# An unquoted value between whitespace is a string.
set(MY_LIST one two) # Equivalent to set(MY_LIST "one" "two")
# The same is true for expanded variables.
set(MY_ELEMENTS "one two") # I'm actually not sure what these produces.
set(MY_LIST ${MY_ELEMENTS}) # More testing is required.
# Printing a list prints the elements without separator.
# A string with spaces is converted to a list with separate_arguments.
set(MY_ELEMENTS "one two")
message(${MY_ELEMENTS})
# > one two
# Prints the string as it was passed to set. No surprices here.
set(MY_ELEMENTS one two)
message(${MY_ELEMENTS})
# > onetwo
# We gave two arguments to set, "one" and "two", so CMake interpreted this to
# mean a list of two elements. Elements are printed without separator, so the
# two words are concatenated together in the print.
set(MY_ELEMENTS "one;two")
message(${MY_ELEMENTS})
# > onetwo
# Here we only pass a single argument to set, but that parameter included the
# list separation token so CMake interpreted this to mean a list of two elements.
set(MY_ELEMENTS "one two")
set(MY_LIST ${MY_ELEMENTS})
message(${MY_LIST})
# > one two
# Creating a list from a string with spaces creates a list with one element
# containing the space. MY_LIST isn't really a list, it's just another STRING
# variables.
set(MY_ELEMENTS one two)
set(MY_LIST ${MY_ELEMENTS})
message(${MY_LIST})
# > onetwo
# Creating a list from another list. No surprices here.
set(MY_ELEMENTS "one;two")
set(MY_LIST ${MY_ELEMENTS})
message(${MY_LIST})
# > onetwo
# Basically the same. MY_ELEMENTS is a list and so is MY_LIST.
# The tutorial state that file system search path variables should always be
# quoted. The above testing indicate that this isn't necessary.
set(MY_PATH "/opt/my company/my application/my application")
message("Spaces are preserved here despite expanding without quotes:")
message(${MY_PATH})
# > Spaces are preserved here despite expanding without quotes:
# > /opt/my company/my application/my application
# Additionally, using quotes at the expand site doesn't help against missing
# quotes at the definition site:
set(MY_PATH /opt/my company/my application/my application)
message("Spaces are NOT preserved here despite the inclusion of quotes:")
message("${MY_PATH}")
# > Spaces are NOT preserved here despite the inclusing of quotes:
# > /opt/my;company/my;application/my;application
# This is because the ${MY_PATH} part of the code never sees the spaces. They
# have already been replaced with ';' when MY_PATH was first set.
# So when, exactly, are the quotes around ${MY_PATH} required?
set(
MY_CACHE_VARIABLE # The name of the variable. The same as regular set.
"VALUE" # The default value of the variable. Ignored if the variable already exists.
CACHE # Marker to indicate that the variable should be cached.
STRING # The type of the variable. Options are STRING, PATH, BOOL, ...
"Description") # Description to show in cmake-gui, ccmake, etc.
# One can add FORCE to the end to force the variable to take the new value even
# if it already exist in the cache. Not sure if the cache is updated with the
# new value, of it is set for this run only.
mark_as_advanced(MY_CACHE_VARIABLE) # Hide the variable from cmake-gui, ccmake, etc.
# Passing INTERNAL as the type has has the same effect as if the type was STRING,
# FORCE was given, and mark_as_advanced called.
set(MY_CACHE_VARIABLE "VALUE" CACHE INTERNAL "Description")
# is the same as
set(MY_CACHE_VARIABLE "VALUE" CACHE STRING "Description" FORCE)
mark_as_advanced(MY_CACHE_VARIABLE)
# Booleans are often used so they have a short form.
option(MY_OPTION "Description" OFF)
# is the same as
set(MY_OPTION "OFF" CACHE BOOL "Description")
# ON and OFF are not the only valid boolean literals. Also TRUE and FALSE,
# System environment variables can be set with
set(ENV{variable_name} value)
# and read with
$ENV{variable_name}
# The recommendation is to avoid the usage of environment variables.
# The cache is stored in a text file named CMakeCache.txt in the build directory.
# Variables can be set not only in the current scope or in the cache, but also
# to items such as directories and targets. They are then call properties. Some
# are automatically created and initialized from a corrensponding global
# variable prefix with CMAKE_. Setting CMAKE_CXX_STANDARD causes new targets to
# be created with the CXX_STANDARD property set to that value.
set_property(
TARGET target_name # The target to set the property on.
PROPERTY property_name # The property that should be set.
"value") # The value the the property should be given.
set_target_properties(
target_name # The name to set the property on.
PROPERTIES # Keyworld to signal that the list of properties start.
property_name # The name of the property to set.
"value") # The value that the property should be give.
# The last two entries can be repeated with additional properties and values.
get_property(result_variable TARGET target_name PROPERTY property_name)
# After this ${result_variable} will be the value of the property.
###
# Programming in CMake
###
if (VARIABLE)
# If VARIABLE is "ON", "YES", "TRUE", "Y", or non-zero number.
else()
# If VARIABLES ins "OFF", "NO", "FALSE", "N", "0", "NOTFOUND", "",
# or ends in "-NOTFOUND".
endif()
# If variables does not expand to one of the above, CMake will expand it then
# try again.
# I don't believe the last statment above, the one about expand and try again.
# In the below the if first sees the variable B and it is expanded to "A", which
# is neither in the TRUE list nor in the FALSE list. So the if should expand it
# and try again. By exanding "A" we get "OFF", which is in the FALSE list and
# the code should print "FALSE". It does not. Instead it prints TRUE. It seems
# the if interpreted the non-empty string "A" as TRUE and took the first block
# immediately.
set(A "OFF")
set(B A)
if (B)
message("TRUE")
else ()
message("FALSE")
endif()
# > TRUE
# Chained variable expansion is disabled when the variable is quoted, perhaps
# that has been expanded to unquoted variables as well since the tutorial was
# written.
# In addition to the list of TRUE/FALSE values there is a list of operator
# keywords: NOT, TARGET, EXISTS, DEFINED, STREQUAL, AND, OR, MATCHES,
# VERSION_LESS, VERSION_LESS_EQUAL, etc.
# Parenthesis can be used to group subexpressions.
# Generator-expressions
# Informational generator expressions are of the form `$<KEYWORD>`. They
# evaluate to a piece of information releveant for the current configuration.
# Another variant is `$<KEYWORD:value>`. The expression becomes `value` if
# KEYWORD is 1 and nothing if KEYWORD is 0.
# The inner generator-expression is $<CONFIG:Debug>
$<$<CONFIG:Debug>:--my-flag>
# I dont understand this at all. The description makes no sense.
# Macros and functions
# Macros set all it's variables in the parent scope while a function does not.
# Declare a new function named `my_function` that that two required arguments.
function(my_fuction arg1 arg2)
message(${arg1}) # We expand parameters like any other variables.
message(${arg2})
# A common idiom is to let the caller pass in a variable name to which we
# write the return value.
set(${arg1} ${arg2} PARENT_SCOPE)
endfunction()
my_function(my_var "My string")
message("Variable set to ${my_var}.")
# Any arguments passed in addition to the once listed in the function signature
# are stored in the list ${ARGN}. All arguments, both listed and unlisted, are
# stored in ${ARGV}.
# Arguments
function(complex)
# Generate a bunch of variables based on the arguments.
cmake_parse_arguments(
COMPLEX_PREFIX # Prefix to give to all variable names.
"SINGLE;ANOTHER" # List of true/false flags.
"ONE_VALUE;ALSO_ONE_VALUE" # List of single-word parameters.
"MULTI_VALUES" # List of list parameters.
${ARGN})
message("${COMPLEX_PREFIX_SINGLE}") # TRUE
message("${COMPLEX_PREFIX_ANOTHER}") # FALSE
message("${COMPLEX_PREFIX_ONE_VALUE}") # value
message("${COMPLEX_PREFIX_ALSO_ONE_VALUE}") #
message("${COMPLEX_PREFIX_MULTI_VALUES}") # some;other;values
endfunction()
complex(
SINGLE # By passing single we set it to TRUE.
# By not passing ANOTHER we set it to FALSE
ONE_VALUE value # ONE_VALUE set to 'value'.
MULTI_VALUES some other values) # MULTI_VALUES set to the list "some;other;values".
###
# Communication with your code
###
# Configure files
# Used to write CMake variables into code by letting CMake generate the code.
# This is done by copying a .in file to non-.in file and doing variable
# substitution on its contents, where @ is used to mark variables.
# The copy-and-substitute is invoked usign the `configur_file` CMake command.
# Version.h.in:
#pragma once
#define MY_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define MY_VERSION_MINOR @PROJECT_VERSION_MINOR@
#define MY_VERSION_PATCH @PROJECT_VERSION_PATCH@
#define MY_VERSION_TWEAK @PROJECT_VERSION_TWEAK@
#define MY_VERSION "@PROJECT_VERSION@"
# CMake:
configure_file (
"${PROJECT_SOURCE_DIR}/include/MyLib/Version.h.in"
"${PROJECT_BINARY_DIR}/include/MyLib/Version.h")
# Reading files
# If we wish to do this the other way around, having Version.h being the source
# of truth and CMake reading its variables from that, we would do the following:
set(VERSION_REGEX "#define MY_VERSION[ \t]+\"(.+)\"")
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/incldue/MyLib/Version.hpp"
VERSION_STRING REGEX ${VERSION_REGEX})
string(REGEX REPLACE ${VERSION_REGEX} "\\1" VERSION_STRING "${VERSION_STRING}")
project(MyLib LANGUAGES CXX VERSION ${VERSION_STRING})
###
# How to structure you project
###
# Project/
# .gitignore
# README.md
# LICENCE.md
# CMakeLists.txt
# cmake # Add these with set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
# FindSomeLib.cmake
# something_else.cmake
# include
# project
# lib.hpp
# src
# CMakeLists.txt
# lib.cpp
# apps
# CMakeLists.txt
# app.cpp
# tests
# CMakeLists.txt
# testlib.cpp
# docs
# CMakeLists.txt
# extern
# googletest
# scripts
# helper.py
# We can make sure we are doing a out-of-source build using
file(TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOC_PATH)
if(EXISTS "${LOC_PATH}")
message(FATAL_ERROR "You cannot build in a source directory (or any directory with a CMakeLists.txt file). Please make a build subdirectory. Feel free to remove CMakeCache.txt and CMakeFiles.")
endif()
###
# Running other programs
###
# Running a command at configure time
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
endif()
endif()
# Running a command at build time
find_package(PythonInterp REQUIRED)
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include/Generated.hpp"
COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/scripts/GenerateHeader.py" --argument
DEPENDS some_target)
add_custom_target(generate_header ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/include/Generated.hpp")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/Generated.hpp DESTINATION include)
###
# C++11 and beyond
###
# Two ways to select the C++ version to use.
# The first is
target_compile_features(myTarget PUBLIC cxx_std_11)
set_target_properties(myTarget PROPERTIES CXX_EXTENSIONS OFF)
# And the second is
set_target_properties(myTarget PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO)
###
# Adding features
###
# Position independent code
# i.e., `-fPIC`. Usually automatically enabled for `SHARED` and `MODULE`
# libraries. Can be explictly enabled using
set_target_properties(lib1 PROPERTIES POSITION_INDEPENDENT_CODE ON)
# Not sure when one would need to do that though.
# Little libraries
# The `ld` library, which includes `ldopen` and `ldclose`, is included using
target_link_libraries(myTarget PUBLIC ${CMAKE_LD_LIBS})
# The `m` math library is included using
find_library(MATH_LIBRARY m)
if (MATH_LIBRARY)
target_link_libraries(myTarget PUBLIC ${MATH_LIBRARY})
endif()
# Interprocedural optimization
# Also called link time optimization or `-flto`.
set_target_properties(myTarget PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
###
# CCache and utilities
###
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
endif()
# Utilities
# Set the following properties or CMAKE_* initializer variables to the command
# line for the tools. Most of them are limited to C or CXX with make or ninja
# generators.
#
# <LANG>_CLANG_TIDY: CMake 3.6+
# <LANG>_CPPCHECK
# <LANG>_CPPLINT
# <LANG>_INCLUDE_WHAT_YOU_USE
# Clang-tidy example:
if(CMAKE_VERSION VERSION_GREATER 3.6)
# Add clang-tidy if available
option(CLANG_TIDY_FIX "Perform fixes for Clang-Tidy" OFF)
find_program(
CLANG_TIDY_EXE
NAMES "clang-tidy"
DOC "Path to clang-tidy executable")
if(CLANG_TIDY_EXE)
if(CLANG_TIDY_FIX)
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}" "-fix")
else()
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
endif()
endif()
endif()
###
# Exporting and installing
###
# Different ways of making a library usable by other projects.
# ... This entire chapter is a mess. I don't understand what he's trying to say.
# Find module (the bad way)
# Write a Find<MyPackage>.cmake script. These are intended as a work-around
# for projects that don't support CMake.
# Add subproject
# This is when the source of a project is included as a directory in another
# project. The parent project does `add_directory`, optionally passing in
# `EXCLUDE_FROM_ALL`. When writing your `CMakeLists.txt` file, take note to use
# `CMAKE_CURRENT_SOURCE_DIR` instead of `PROJECT_SOURCE_DIR` since the latter
# will reference the root project. Does it really? I need to experiment with
# this. One can test `CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME` to differentiate
# between being built stand-alone or part of another project.
#
# Use aliases to setup namespaced names for your targets.
add_library(MyLib::MyLib ALIAS MyLib)
# Exporting
# Exporting changed in CMake 3.15. Prior to this it wrote stuff to the user's
# home directory.
# And then stuff I don't really get. Time to disembark from this tutorial.