Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 13df9cf

Browse files
Add CMake user input section
1 parent 28c6841 commit 13df9cf

File tree

1 file changed

+122
-19
lines changed

1 file changed

+122
-19
lines changed

‎CMake/01 CMake - Basics and Scripting.md‎

Lines changed: 122 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# CMake Crash Course - Basics and Scripting
22

33
Author: methylDragon
4-
Contains a syntax reference for CMake
4+
Contains a syntax reference for CMake. We'll be going through some concepts, the CLI, and scripting with CMake!
55

66
------
77

@@ -34,13 +34,14 @@ Contains a syntax reference for CMake
3434
3.3 [Comments](#3.3)
3535
3.4 [Printing](#3.4)
3636
3.5 [Variables](#3.5)
37-
3.6 [Variable Scope](#3.6)
38-
3.7 [Cache Variables](#3.7)
39-
3.8 [Prefixes](#3.8)
40-
3.9 [Conditionals](#3.9)
41-
3.10 [Loops](#3.10)
42-
3.11 [Functions](#3.11)
43-
3.12 [Macros](#3.12)
37+
3.6 [Variable Scope and Directories](#3.6)
38+
3.7 [User Input: Cache Variables](#3.7)
39+
3.8 [User Input: Options](#3.8)
40+
3.9 [Prefixes](#3.9)
41+
3.10 [Conditionals](#3.10)
42+
3.11 [Loops](#3.11)
43+
3.12 [Functions](#3.12)
44+
3.13 [Macros](#3.13)
4445

4546

4647

@@ -211,6 +212,8 @@ cmake -DCMAKE_BUILD_TYPE=Release # Build optimised code with no debug flags
211212
cmake -D CMAKE_BUILD_TYPE=Release # This also works!
212213
```
213214

215+
> Additionally, do note that this is actually the **best practice for invoking CMake**
216+
214217

215218

216219
#### **View Cached Variables**
@@ -322,7 +325,7 @@ The bare minimum CMakeLists that does meaningful work involves three things
322325
323326
- CMake requirements
324327
- Project name
325-
- Build targets
328+
- Build **targets**
326329
327330
> But actually, if you just want to test the basic programming stuff in CMake, you can have an empty `CMakeLists.txt` file with just the statements to test (in most cases!) It won't build anything, but it's good enough.
328331
>
@@ -471,6 +474,10 @@ set(EQUIVALENT_LIST "a;b") # These are the same!
471474
set(NUM_LIST 1 2)
472475
set(EQUIVALENT_NUM_LIST 1;2)
473476
477+
# You can even append to the lists!
478+
set(NUM_LIST ${NUM_LIST} 3 4) # So-so way
479+
list(APPEND NUM_LIST 5 6) # Better way
480+
474481
# Use variables
475482
message("WOW_NUM: ${WOW_NUM}")
476483
message("EQUIVALENT_NUM_LIST: ${EQUIVALENT_NUM_LIST}")
@@ -501,15 +508,21 @@ message($ENV{variable_name})
501508

502509

503510

504-
### 3.6 Variable Scope <a name="3.6"></a>
511+
### 3.6 Variable Scope and Directories <a name="3.6"></a>
505512
[go to top](#top)
506513

507514

508515
![img](assets/cmake-variable-scopes.png)
509516

510517
[Image Source](<https://preshing.com/20170522/learn-cmakes-scripting-language-in-15-minutes/>)
511518

512-
Variables are created in the scope they are set in. And scopes are defined by directories and functions. So just think of C++ scopes, and you should be fine.
519+
Variables are created in the scope they are set in. And scopes are defined by directories and functions.
520+
521+
> **Directories!?**
522+
>
523+
> Yes! Directories! Since each `CMakeLists.txt` file governs its directory, so treat them modularly like that
524+
525+
So just think of C++ scopes, and you should be fine.
513526

514527
Luckily, there is a way to state that a variable is set in its parent scope, instead of its current scope.
515528

@@ -520,13 +533,44 @@ set(PARENT_SCOPED_VAR 10 PARENT_SCOPE)
520533

521534

522535

523-
### 3.7 Cache Variables <a name="3.7"></a>
536+
### 3.7 User Input: Cache Variables <a name="3.7"></a>
524537
[go to top](#top)
525538

526539

527540
CMake uses a cache, which is really just a text file called `CMakeCache.txt` to remember settings so you don't have to restate them when running CMake.
528541

529-
Normal variables aren't placed in the cache unless you explicitly tell CMake to though! **Adding a variable as a cache variable also exposes them in the command line**.
542+
Normal variables aren't placed in the cache unless you explicitly tell CMake to though! **Adding a variable as a cache variable also exposes them in the command line**. If you don't set them in the command line, they'll use their default value, so it's a good way to create **settable variables with default values**!
543+
544+
545+
546+
#### **Listing Cache Variables**
547+
548+
You can list out all the available settable cache variables from the command line!
549+
550+
Just use these commands:
551+
552+
```shell
553+
cmake -L # List all non-advanced cache variables
554+
cmake -LA # List all cache varialbes (including advanced ones)
555+
cmake -LAH # List all cache variables, and also display help for them
556+
```
557+
558+
559+
560+
#### **Setting Cache Variables in the Command Line**
561+
562+
Same as setting variables in the command line.
563+
564+
```shell
565+
cmake -DCACHE_VAR_NAME=rawr
566+
567+
# Or with type hints
568+
cmake -DTYPED_CACHE_VAR_NAME:STRING=raa
569+
```
570+
571+
572+
573+
#### **Setting Cache Variables**
530574

531575
```cmake
532576
# General call
@@ -573,7 +617,56 @@ set(FORCED_VAR 10 CACHE STRING FORCE)
573617
574618
575619
576-
### 3.8 Prefixes <a name="3.8"></a>
620+
### 3.8 User Input: Options <a name="3.8"></a>
621+
622+
[go to top](#top)
623+
624+
These are just boolean cache variables that you can set ON or OFF in the command line.
625+
626+
627+
628+
#### **Listing Options**
629+
630+
If you want to use options, you can use the same cache variable interface.
631+
632+
> You can list out all the available settable cache variables from the command line!
633+
>
634+
> Just use these commands:
635+
>
636+
> ```shell
637+
> cmake -L # List all non-advanced cache variables
638+
> cmake -LA # List all cache varialbes (including advanced ones)
639+
> cmake -LAH # List all cache variables, and also display help for them
640+
> ```
641+
642+
643+
644+
#### **Setting Options in the Command Line**
645+
646+
Same as setting variables in the command line.
647+
648+
```shell
649+
cmake -DOPTION_NAME=ON
650+
```
651+
652+
653+
654+
#### **Setting Options**
655+
656+
```cmake
657+
option(OPTION_NAME "SOME_RANDOM_DOCSTRINGS" ON) # OPTION_NAME is default ON
658+
659+
# You can also set a dependent option!
660+
# This one defaults to ON
661+
# If and only if ON_ME_BEING_ON is ON and AND_ON_ME_BEING_OFF is OFF
662+
CMAKE_DEPENDENT_OPTION(DEP_OPTION "I'm dependent!!" ON
663+
"ON_ME_BEING_ON;NOT AND_ON_ME_BEING_OFF" OFF)
664+
```
665+
666+
667+
668+
### 3.9 Prefixes <a name="3.9"></a>
669+
577670
[go to top](#top)
578671
579672
@@ -594,7 +687,7 @@ set(${PREFIX}_C 3)
594687
595688
596689
597-
### 3.9 Conditionals <a name="3.9"></a>
690+
### 3.10 Conditionals <a name="3.10"></a>
598691
[go to top](#top)
599692
600693
@@ -608,7 +701,9 @@ else()
608701
endif()
609702
```
610703
611-
Cool! You can control program flow depending on whether stuff evaluates `TRUE` or `FALSE`
704+
Cool! You can control program flow depending on whether stuff evaluates `TRUE` or `FALSE`.
705+
706+
612707
613708
#### **Boolean Constants**
614709
@@ -626,6 +721,8 @@ Additionally, the following are evaluated to false:
626721
627722
`IGNORE`, `NOTFOUND`, an empty string, or strings that end with the suffix "-NOTFOUND"
628723
724+
725+
629726
#### **Example Usage**
630727
631728
```cmake
@@ -644,6 +741,8 @@ endif()
644741
>
645742
> The `if()` statement is a little weird in this regard, because it takes in both variables and constants! In this case, if we wrapped VAR and passed it in as `if(${VAR})`, the if statement evaluates it as the string "TRUE" as opposed to the boolean constant `TRUE`.
646743
744+
745+
647746
#### **Logic**
648747
649748
You can do logic with the expressions that go into `if()`!
@@ -673,7 +772,7 @@ if(<string> MATCHES regex) # Regex matching
673772
674773
675774
676-
### 3.10 Loops <a name="3.10"></a>
775+
### 3.11 Loops <a name="3.11"></a>
677776
[go to top](#top)
678777
679778
@@ -715,6 +814,8 @@ foreach(loop_var IN LISTS LIST_VAR)
715814
endforeach()
716815
```
717816
817+
818+
718819
#### **For Loop over a Range**
719820
720821
![1565773727787](assets/1565773727787.png)
@@ -739,6 +840,8 @@ foreach(loop_var RANGE 10 20 2) # Start, stop, step
739840
endforeach()
740841
```
741842
843+
844+
742845
#### **While Loop**
743846
744847
The while loop conditions are resolved in the same way the `if()` command resolves conditions.
@@ -751,7 +854,7 @@ endwhile()
751854
752855
753856
754-
### 3.11 Functions <a name="3.11"></a>
857+
### 3.12 Functions <a name="3.12"></a>
755858
[go to top](#top)
756859
757860
@@ -777,7 +880,7 @@ Also note that any variables `set()` within a function is local to its function'
777880
778881
779882
780-
### 3.12 Macros <a name="3.12"></a>
883+
### 3.13 Macros <a name="3.13"></a>
781884
[go to top](#top)
782885
783886

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /