Skip to content

Commit

Permalink
update(cmake-patterns): basic modern cmake patterns
Browse files Browse the repository at this point in the history
issue #104
  • Loading branch information
sabertazimi committed Dec 23, 2018
1 parent 53ab4f3 commit 5d2742d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions programming/tools/buildTools/CmakeBasicNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [Cmake Basic Notes](#cmake-basic-notes)
- [Basic Build System](#basic-build-system)
- [Basic Options](#basic-options)
- [Flow Control](#flow-control)
- [if control](#if-control)
- [foreach control](#foreach-control)
Expand All @@ -19,6 +20,9 @@
- [Install Command](#install-command)
- [find packages](#find-packages)
- [Useful Tools](#useful-tools)
- [CMake Patterns](#cmake-patterns)
- [Nice Patterns](#nice-patterns)
- [Anti Patterns](#anti-patterns)
- [Reference](#reference)

<!-- /TOC -->
Expand Down Expand Up @@ -151,6 +155,19 @@ add_executable(minisat-simp minisat/simp/Main.cc)
target_link_libraries(minisat-simp libminisat)
```

### Basic Options

```bash
make VERBOSE=1
```

Standard options:

- `-DCMAKE_BUILD_TYPE=` Pick from Release, RelWithDebInfo, Debug, or sometimes more
- `-DCMAKE_INSTALL_PREFIX=` /usr/local (the default), ~/.local
- `-D BUILD_SHARED_LIBS=`
- `--trace` print every line of CMake

## Flow Control

### if control
Expand Down Expand Up @@ -514,6 +531,27 @@ ifeq ($(findstring clean,$(MAKECMDGOALS)),)
endif
```
## CMake Patterns
### Nice Patterns
- Think in targets (Object-Oriented)
- Export your interface: You should be able to run from build or install
- Write a Config.cmake file: This is what a library author should do to support clients
- Make ALIAS targets to keep usage consistent
- Combine common functionality into clearly documented functions
- Use lowercase function names
- Upper case is for variables
- Use cmake_policy and/or range of versions
### Anti Patterns
- Do not use global functions: e.g `link_directories`, `include_libraries`
- Don't add unneeded PUBLIC requirements e.g `-Wall`
- Don't GLOB files
- Link to built files directly: Always link to targets if available
- Never skip PUBLIC/PRIVATE when linking
## Reference
- [Offical Reference](https://cmake.org/cmake/help/latest)
Expand Down

0 comments on commit 5d2742d

Please sign in to comment.