This repository provides utilities for automatically detecting and configuring different target types in a CMake
project based on marker .cmake
files. It works on Linux and Windows, scanning your source tree for these marker files:
- executable.cmake
- header.cmake
- shared.cmake
- static.cmake
- tests.cmake
The main script:
- Scans your source tree for these marker
.cmake
files. - Collects source/header files by looking in specific subdirectories (
src
,source
,inc
,include
). - Creates corresponding targets (
add_executable
,add_library
) automatically. - Optionally creates test executables if
BUILD_TESTING
is turned ON.
No test framework is used—tests are simple executables run via CTest.
Purpose: Defines a directory containing the source of an executable.
Scanned Directories:
src/
source/
inc/
(headers only)
All .c
, .cc
, .cp
, .cpp
, .cxx
files found are added as source files for the resulting executable. Any headers
in inc/
are also included as part of the target.
Purpose: Defines a directory containing a header-only library (an INTERFACE
library in CMake).
Scanned Directories:
include/
(headers only)
Because this creates an INTERFACE library, no source files are compiled; everything in include/
is installed and
exposed to consumers.
Purpose: Defines a directory containing the source of a shared library.
Scanned Directories:
src/
source/
inc/
(headers only)include/
(headers only)
Export Logic:
- An export header file is automatically generated in
<binary-dir>/<target-name>/include/<target-name>_export.h
. - The library is compiled with
add_library(<target> SHARED)
. - An appropriate preprocessor definition (e.g.,
MY_LIBRARY_EXPORTS
) is added to support symbol exports on different platforms.
Purpose: Defines a directory containing the source of a static library.
Scanned Directories:
src/
source/
inc/
(headers only)include/
(headers only)
A normal static library is compiled with add_library(<target> STATIC)
.
Purpose: Defines a directory containing test sources.
Behavior:
- Each
.cpp
(or other source extension) in this folder is built as a separate test executable. - No test framework is provided; each test is a simple executable.
- When
BUILD_TESTING=ON
, CTest is enabled, and these executables are registered as tests (viaadd_test
).
my_project/
.cmake_utilities/
all.cmake
...
src_shared/
shared.cmake
src/
lib.cpp
include/
lib.hpp
src_static/
static.cmake
src/
lib_impl.cpp
src_exec/
executable.cmake
src/
main.cpp
headers_only/
header.cmake
include/
utility.hpp
tests/
tests.cmake
test_something.cpp
CMakeLists.txt
Upon configuring CMake:
- Shared, static, header, executable targets are generated automatically.
- Test executables are created and registered if
BUILD_TESTING
is ON.
- Download
bootstrap.cmake
from this repository (preferably a pinned version), or copy the.cmake_utilities
folder locally. - In your top-level
CMakeLists.txt
, include both:include("${CMAKE_BINARY_DIR}/bootstrap.cmake") include(".cmake_utilities/all.cmake")
- Any directory that has
executable.cmake
,header.cmake
,shared.cmake
,static.cmake
, ortests.cmake
will be automatically picked up and turned into the relevant target.
For more detailed information, see the inline documentation within the scripts in this repository.