Opinionated CMake project initializer to generate CMake projects that are FetchContent ready, separate consumer and developer targets, provide install rules with proper relocatable CMake packages and use modern CMake (3.14+)
See the gif in the assets folder for an example of using
cmake-init
.
Here are some example outputs of the script as of 0.6.1:
- https://github.com/friendlyanon/cmake-init-shared-static
- https://github.com/friendlyanon/cmake-init-executable
- https://github.com/friendlyanon/cmake-init-header-only
- Be simple to use
The script allows you to just mash enter to get you a correctly set up project for a library, which can be built as either static or shared. You want a project with an executable? Just choosee
when prompted. Header-only library? Just chooseh
when prompted. Simple and correct! - Create
FetchContent
ready projects
This is important, because in the near feature this might allow CMake to consume other projects in a trivial fashion similar to other languages, e.g. in JavaScript's case (npm). - Cleanly separate developer and consumer targets
This ties into the previous point as well, but developers and consumers of a project have different needs, and separating targets achieves that goal. A developer should be able to run tests, add warning flags, run benchmarks, etc., while a consumer, such as a package maintainer, generally only wants to build the library or the executable itself, without having to patch around in the CMake scripts. Show some love to your package maintainers! - Use modern CMake (3.14+)
There are too many outdated and plain wrong examples on the internet, it's time to change that.
- Cover every possible project structure
Doing this is pointless as an init script, because there are far too many ways people have been building software, and if you have special needs, you ought to already know CMake and you can set the project up yourself. - Generate files and show tips for websites other than GitHub
While I understand the people who are against GitHub (and by proxy Microsoft), it's by far the most used website of its kind, the files and messages specific to it are small in number, and they are easily adapted for any other service.
Make sure you have these programs installed:
- Python 3.8 or newer
- CMake 3.19 or newer
- git
- clang-tidy (optional, should be available in PATH as
clang-tidy
)
cmake-init
consists of a single file that can be run using Python. Python was
chosen for this, because it is cross-platform, convenient for this use-case and
you likely already have it installed or the need for it will come up later
anyway.
You have to just download the cmake-init.pyz
script from the releases,
place it somewhere in your PATH
and rename it to cmake-init
. On Windows,
you have to create a cmake-init.bat
file to proxy to the script:
@echo off
python %~dp0\cmake-init.pyz %*
clang-tidy is a static analysis tool that helps you spot logical errors in
your code before it is compiled. This script gives you the option to inherit
the clang-tidy
preset in your dev
preset, enabling the CMake integration
for this tool.
CI will always run clang-tidy for you, so it is entirely optional to install and use it locally, but it is recommended.
For Windows users, if you wish to use clang-tidy, then you must install
Ninja and set the generator
field in your dev
preset to Ninja
. The
reason for this is that only Makefiles and Ninja are supported with CMake
for use with clang-tidy. For other generators, this feature is a no-op.
cmake-init <path>
This command will create a CMake project at the provided location and according to the answers given to the prompts. You may pass the-s
,-e
or-h
flags after to quickly create a shared library, executable or a header only library respectively.cmake-init --help
Shows the help screen for more flags and switches.
cmake-init
is Free Software: You can use, study, share and improve it at your
will. Specifically you can redistribute and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
Files generated by cmake-init
are placed under Public Domain. Everyone is
free to use, modify, republish, sell or give away these files without prior
consent from anybody. These files are provided on an "as is" basis, without
warranty of any kind. Use at your own risk! Under no circumstances shall the
author(s) or contributor(s) be liable for damages resulting directly or
indirectly from the use or non-use of these files.