TCM 1.1.0
A CMake module to reduce boilerplate
Loading...
Searching...
No Matches
TCM - A CMake Module

CMake Version MIT Documentation link

CI Windows CI Ubuntu CI MacOS

TCM is a opinionated CMake module to reduce boilerplate with common functionalities:

See documentation for a closer look.

Note

Defaults should give you good enough results. Most functions are customizable, but only to some extent. If you need full control, you are better off writing your own scripts.

Pitch: "Composition over inheritance"

When starting a new project, it is common to start from a template or a generator to reduce boilerplate. Overtime, they will update, fix bugs, bring new functionalists and so on. Your preferences and requirements will also change. These "inheritance" approaches make it hard to keep up with them after a project was started. For my projects, I thought that some functionalities should be better off as CMake modules for a more "compositional" approach. It is easier to update and mixmatch functionalities.

Note

To be clear, there is no "TCM over cmake-init" or any other templates, modules, etc. You can perfectly start a project with cmake-init and use some functionalities provided by TCM. TCM is an opinionated CMake module, use functionalities you like others without cluttering your code.

A Personal note

It was an interesting challenge, that started from the frustration of a template I wrote. I wanted it to be more configurable and adaptable to differents preferences / requirements. Also, it was cumbersome to update projects built from it. _I thought about writing a generator, but there is already cmake-init. _And there is still the problem of changing a project after it was generated.__

I thought it would be cool, if there was an easy-to-integrate library where I could pick functionalities as required. Cmake modules seemed like a great fit and so was born TCM. Overtime, I can work on new functionalities, fix bugs, etc. and easily share them with my projects, while still being able to use generators and templates.

Show me the code

set(TCM_TOOLS "CPM;CCACHE") # Defaults
# Option 1 - Update automatically
set(TCM_DOWNLOAD_VERSION "1.1.0")
include(cmake/get_tcm.cmake) # Download, cache and include tcm.cmake.
# Option 2 - Update manually
include(cmake/tcm.cmake)
# API overview
tcm_documentation(FILES docs/pages ASSETS assets)
tcm_benchmarks(FILES bench.cpp)
tcm_tests(FILES tests.cpp)
tcm_examples(FILES examples WITH_BENCHMARK INTERFACE ExampleInterface)
tcm_target_enable_optimisation_flags(a_target)
tcm_target_enable_warning_flags(a_target)
tcm_target_copy_assets(a_target FILES assets/)
# Static and shared library example
add_library(my_lib SHARED <same_source> ...)
target_include_directories(my_lib PUBLIC <same_include> ...)
tcm_target_export_header(my_lib)
add_library(my_lib_static SHARED <same_sources> ...)
target_include_directories(my_lib_static PUBLIC <same_include> ...)
tcm_target_export_header(my_lib_static BASE_NAME my_lib) # Needed as they both use same source filse.
add_executable(exe)
target_link_libraries(exe my_lib)
tcm_target_copy_dll(exe FROM my_lib)

Main Features

  • Easy to distribute / deploy
    • Inspired by STB, TCM is packed into a single CMake script.
    • Public domain licence.
    • External files are embedded in tcm.cmake:
      • custom shell file for emscripten,
      • custom header, footer, css and layout for doxygen.
  • Keep your CMake wokflows
    • TCM doesn't try to change how you write CMake files.
    • Functions are standalone, only use what you need.
    • Most CMake variables are set only if they were not already defined.
  • Modern CMake
Before After modifying some examples

  • Integrated documentation
    • When API is misused, error logs explain how to use it, e.g. with test_func(A test B test ARGS)
Before

Installation

You can either:

  • download or copy get_tcm.cmake, a script to easily download and update tcm.cmake through cmake.
set(TCM_DOWNLOAD_VERSION 1.1.0)
include(cmake/get_tcm.cmake)
  • or download tcm.cmake directly
include(cmake/tcm.cmake)

These two files are also available at these URLS (using wget):

wget -O cmake/tcm.cmake https://github.com/TBlauwe/tcm/releases/download/1.1.0/tcm.cmake
wget -O cmake/get_tcm.cmake https://github.com/TBlauwe/tcm/releases/download/1.1.0/get_tcm.cmake

Ideas / Backlog

  • Check if some functionalities, properties and variables should be added / reworked as a target.
  • Propose an optional checkup and warn for missing tools
  • Install
  • Packing
  • Bindings
  • Propose a single function to parametrize target according to current settings (optimization, warnings, emscripten)
  • For readme, generate credits (from cpm dependencies ?)
  • For readme, generate header (automatic badges from workflows ?, toc)
  • Generate a dummy doxygen page to include, so they can be showcased in the documentation.

Credits

This project was inspired by STB, FIPS and CPM.