Skip to content

Commit

Permalink
update tips-and-tricks episode
Browse files Browse the repository at this point in the history
  • Loading branch information
code4yonglei committed Sep 13, 2024
1 parent 4f59934 commit 378b6ab
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions content/tips-and-tricks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,68 @@ Functions and macros



Where to list sources and tests?
--------------------------------


Some projects collect all sources in one file, all tests in another file, and carry them across in variables:

.. code-block:: text
project/
├── CMakeLists.txt
├── cmake
| ├── sources.cmake
| ├── tests.cmake
| └── definitions.cmake
├── external
└── src
├── evolution
├── initial
├── io
└── parser
It is recommended to organize the sources like the format below, where sources, definitions, and tests are defined in the "closest" ``CMakeLists.txt`` files.

.. code-block:: text
project/
├── CMakeLists.txt
├── external
│ ├── CMakeLists.txt
└── src
├── CMakeLists.txt
├── evolution
│ ├── CMakeLists.txt
├── initial
│ ├── CMakeLists.txt
├── io
│ ├── CMakeLists.txt
└── parser
└── CMakeLists.txt
The reason is that this will minimize side-effects, ordering effects, and simplify maintenance for those who want to add or rename source files: they can do it in one place, close to where they are coding.



Order and side effects
----------------------


- When portioning your project into modules, design them in a way so that order does not matter (much).
- This is easier with functions than with macros, and easier with targets than with variables.
- Avoid variables with parent or global scope. Encapsulate and prefer separation of concerns.



Where to keep generated files
-----------------------------


CMake allows us to generate files at configure- or build-time. When generating files, we recommend to **always** generate into the build folder, never outside.








The reason is that you always want to maintain the possibility to configure different builds with the same source without having to copy the entire project to a different place.



Expand Down

0 comments on commit 378b6ab

Please sign in to comment.