1
0
mirror of synced 2024-11-09 22:48:53 +01:00
modern-cmake/README.md

107 lines
6.4 KiB
Markdown
Raw Normal View History

2017-10-18 14:48:28 +02:00
# An Introduction to Modern CMake
2016-04-09 07:39:36 +02:00
2017-10-17 21:41:41 +02:00
People love to hate build systems.
2017-10-18 14:48:28 +02:00
Just watch the talks from CppCon17 to see examples of developers making the state of build systems the brunt of jokes.
2017-10-17 21:41:41 +02:00
This raises the question: Why?
Certainly there are no shortage of problems when building.
2023-07-19 17:25:52 +02:00
But I think that, in 2023, we have a very good solution to quite a few of those problems.
2017-10-17 21:41:41 +02:00
It's CMake. Not CMake 2.8 though; that was released before C++11 even existed!
2017-10-18 16:20:39 +02:00
Nor the horrible examples out there for CMake (even those posted on KitWare's own tutorials list).
2024-01-06 00:32:24 +01:00
I'm talking about Modern CMake. CMake 3.5+, maybe even CMake 3.28+!
2017-10-18 16:20:39 +02:00
It's clean, powerful, and elegant, so you can spend most of your time coding, not adding lines to an unreadable, unmaintainable Make (Or CMake 2) file.
2018-07-19 11:54:08 +02:00
And CMake 3.11+ is supposed to be significantly faster, as well!
2018-03-28 20:50:38 +02:00
2021-11-02 14:18:48 +01:00
{% hint %}
2023-07-19 17:25:52 +02:00
Are you interested in using CMake to build Python packages? I'm working on scikit-build-core, [proposal described here][skprop]! Let me know if you have a use case!
2021-11-02 14:18:48 +01:00
[skprop]: https://iscinumpy.gitlab.io/post/scikit-build-proposal/
2022-06-02 20:30:14 +02:00
{% endhint %}
2016-05-14 07:39:23 +02:00
2017-10-18 14:48:28 +02:00
{% hint style='working' %}
2018-04-05 12:30:54 +02:00
This book is meant to be a living document. You can raise an issue or put in a merge request on [GitLab](https://gitlab.com/CLIUtils/modern-cmake).
2020-09-29 22:59:39 +02:00
You can also [download a copy as a PDF](https://CLIUtils.gitlab.io/modern-cmake/modern-cmake.pdf). Be sure to check the [HSF CMake Training][], as well!
2022-06-02 20:30:14 +02:00
[hsf cmake training]: https://hsf-training.github.io/hsf-training-cmake-webpage/01-intro/index.html
2017-10-18 14:48:28 +02:00
{% endhint %}
2017-10-17 21:41:41 +02:00
In short, here are the most likely questions in your mind if you are considering Modern CMake:
## Why do I need a good build system?
Do any of the following apply to you?
2022-06-02 20:30:14 +02:00
- You want to avoid hard-coding paths
- You need to build a package on more than one computer
- You want to use CI (continuous integration)
- You need to support different OSs (maybe even just flavors of Unix)
- You want to support multiple compilers
- You want to use an IDE, but maybe not all of the time
- You want to describe how your program is structured logically, not flags and commands
- You want to use a library
- You want to use tools, like Clang-Tidy, to help you code
- You want to use a debugger
2017-10-17 21:41:41 +02:00
2017-10-18 16:20:39 +02:00
If so, you'll benefit from a CMake-like build system.
2017-10-17 21:41:41 +02:00
## Why must the answer be CMake?
2021-11-16 16:27:00 +01:00
Build systems are a hot topic. Of course there are many options. But even a really good one, or one that re-uses a familiar syntax, can't come close to CMake.
2017-10-17 21:41:41 +02:00
Why?
Support.
Every IDE supports CMake (or CMake supports that IDE).
More packages use CMake than any other system.
2020-03-28 10:01:20 +01:00
So, if you use a library that is designed to be included in your code, you have a choice: Make your own build system, or use one of the provided ones, and that will almost always include CMake.
2017-10-17 21:41:41 +02:00
And that will quickly be the common denominator if you include multiple projects.
2017-10-18 16:20:39 +02:00
And, if you need a library that's preinstalled, the chances of it having a find CMake script or config CMake script are excellent.
2017-10-17 21:41:41 +02:00
## Why use a Modern CMake?
Around CMake 2.6-2.8, CMake started taking over. It was in most of the package managers for Linux OS's, and was being used in lots of packages.
Then Python 3 came out.
I know, this should have nothing whatsoever to do with CMake.
But it had a 3.
And it followed 2.
And it was a hard, ugly, transition that is still ongoing in some places, even today.
2018-03-30 14:26:33 +02:00
I believe that CMake 3 had the bad luck to follow Python 3.[^1]
2020-03-28 10:01:20 +01:00
Even though every version of CMake is insanely backward compatible, the 3 series was treated as if it were something new.
2021-03-23 21:10:07 +01:00
And so, you'll find OSs like CentOS7 with GCC 4.8, with almost-complete C++14 support, and CMake 2.8, which came out years before C++11.
2017-10-17 21:41:41 +02:00
2022-06-02 20:30:14 +02:00
You really should _at least_ use a version of CMake that came out after your compiler, since it needs to know compiler flags, etc, for that version.
2017-10-18 16:20:39 +02:00
And, since CMake will dumb itself down to the minimum required version in your CMake file, installing a new CMake, even system wide, is pretty safe.
2022-06-02 20:30:14 +02:00
You should _at least_ install it locally.
2019-07-19 20:02:28 +02:00
It's easy (1-2 lines in many cases), and you'll find that 5 minutes of work will save you hundreds of lines and hours of `CMakeLists.txt` writing, and will be much easier to maintain in the long run.
2017-10-17 21:41:41 +02:00
2018-04-06 11:40:00 +02:00
This book tries to solve the problem of the poor examples and best practices that you'll find proliferating the web.
2018-03-28 20:50:38 +02:00
2018-04-06 11:40:00 +02:00
## Other sources
2019-08-13 20:13:28 +02:00
Other material from the original author of this book:
2022-06-02 20:30:14 +02:00
- [HSF CMake Training][]
- [Interactive Modern CMake talks](https://gitlab.com/CLIUtils/modern-cmake-interactive-talk)
2019-08-13 20:13:28 +02:00
2018-04-06 11:40:00 +02:00
There are some other places to find good information on the web. Here are some of them:
2018-03-30 14:26:33 +02:00
2022-06-02 20:30:14 +02:00
- [The official help](https://cmake.org/cmake/help/latest/): Really amazing documentation. Nicely organized, great search, and you can toggle versions at the top. It just doesn't have a great "best practices tutorial", which is what this book tries to fill in.
- [Effective Modern CMake](https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1): A great list of do's and don'ts.
- [Embracing Modern CMake](https://steveire.wordpress.com/2017/11/05/embracing-modern-cmake/): A post with good description of the term
- [It's time to do CMake Right](https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/): A nice set of best practices for Modern CMake projects.
- [The Ultimate Guide to Modern CMake](https://rix0r.nl/blog/2015/08/13/cmake-guide/): A slightly dated post with similar intent.
- [More Modern CMake](https://youtu.be/y7ndUhdQuU8): A great presentation from Meeting C++ 2018 that recommends CMake 3.12+. This talk makes calls CMake 3.0+ "Modern CMake" and CMake 3.12+ "More Modern CMake".
- [Oh No! More Modern CMake](https://www.youtube.com/watch?v=y9kSr5enrSk): The sequel to More Modern CMake.
- [toeb/moderncmake](https://github.com/toeb/moderncmake): A nice presentation and examples about CMake 3.5+, with intro to syntax through project organization
2018-03-30 14:26:33 +02:00
2018-09-15 20:56:22 +02:00
## Credits
2020-06-09 16:59:07 +02:00
Modern CMake was originally written by [Henry Schreiner](https://iscinumpy.gitlab.io). Other contributors can be found [listed on GitLab](https://gitlab.com/CLIUtils/modern-cmake/-/network/master).
2018-09-15 20:56:22 +02:00
2022-06-02 20:30:14 +02:00
[hsf cmake training]: https://hsf-training.github.io/hsf-training-cmake-webpage/01-intro/index.html
2020-08-20 03:58:24 +02:00
2018-03-30 14:26:33 +02:00
[^1]: CMake 3.0 also removed several long deprecated features from very old versions of CMake and make one very tiny backwards incompatible change to syntax related to square brackets, so this is not entirely fair; there might be some very, very old CMake files that would stop working with 3. I've never seen one, though.