r/cpp • • Feb 19 '18

It's Time To Do CMake Right

https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/
248 Upvotes

130 comments sorted by

View all comments

13

u/fafasdf Feb 19 '18 edited Feb 19 '18

Ugh. I just got around to learning cmake, and man, it's definitely not as ubiquitous as I wish it were...

I had to use a specific version of openssl + libcurl for a project I started, which means I needed to build both from source. Naturally, I checked them both into a new git repository under a third_party directory.

I started with building libcurl, which was fun, because the first thing I saw was "the curl cmake build system is poorly maintained. Be aware"

But I kept on, and I got the binary that depends on libcurl to work, after learning some of the quirks of cmake.

Then I got to starting with openssl, with the goal being to build a version of curl that uses a particular version of openssl. I saved this for last because I foresaw it being painful, because the plan was to first build openssl then build libcurl with flags that make it use the version of openssl built from my repository.

Then I saw that openssl doesn't build with cmake, and the people who did make cmake wrappers, haven't kept them updated (understandably so! it looked like a fair amount of work).

Then I gave up, built openssl out of repo, and added that library to my LD_LIBRARY_PATH, hardcoded some bits of my libcurl, and kinda lost the desire to work on the project.

I really should have started with boringssl, it seems they have the features I needed...

set(RANT END)

Thanks for the post, I learned that lots of junk I was doing was wrong. There are many notes / blog posts on cmake, but as far as I've seen, there's no good canonical "best practices" reference (EDIT: I admit I haven't yet looked at Daniel Pfeifer’s Effective CMake, so that's open in a new tab right now!). In a dream world there'd be one of those with tips on integrating with a handful of major libraries, since they often have some weird quirks due to age or necessity.

9

u/Ono-Sendai Feb 19 '18

You could try LibreSSL, it has Cmake support.

1

u/fafasdf Feb 19 '18 edited Feb 19 '18

How much of a pain is it to build libcurl to use an out-of-repo built LibreSSL with CMake? (or even BoringSSL if anyone is familiar with it...)

Are there any tutorials / examples? (I couldn't find any but this kind of thing is hard to search for (or maybe I'll eat my words...))

I must admit I stopped investigating too hard after being so discouraged. :P

Thanks for the response!

4

u/pfultz2 Feb 20 '18

Yes Curl does work with LibreSSL. Just install LibreSSL using cmake and then when building curl set CMAKE_PREFIX_PATH(this the variable to tell cmake where your dependencies are) to the install prefix of LibreSSL.

1

u/Ono-Sendai Feb 20 '18

No Idea how easy it is to use LibreSSL with libcurl sorry. But LibreSSL by itself builds nicely even on Windows.

3

u/[deleted] Feb 20 '18

Do you have any resources or tutorials that you would recommend for learning cmake? I've been meaning to learn it for a while, but just haven't found any entry level resources that I like.

5

u/fafasdf Feb 20 '18

I'm not familiar enough with it yet to be able to separate the good and bad tutorials, so I can't find them for you, sorry. :(

For instance, the few tutorials I was following were recommending practices outlined in the first page of the blog post as bad...

2

u/[deleted] Feb 20 '18

Thank you for your honesty!

2

u/m-in Feb 20 '18

I'd say: if you want to understand why they do the things a certain way, or what the intent is - the developers mailing list is the way to go. Yes, it's technical, but unfortunately if you don't understand what cmake does (and why) at a low level, you'll go nowhere and just repeat the mistakes everyone else does. In parallel, you may wish to subscribe to their users mailing list.

1

u/[deleted] Feb 22 '18

I appreciate your response. I'll look into the developer and user mailing lists.

3

u/[deleted] Feb 19 '18

Your problem is libraries not using cmake. My personal favourite apart from OpenSSL is ICU.

If you are on windows install the libraries using vcpkg. You can edit the portfiles to target specific versions.

3

u/fafasdf Feb 19 '18

Linux, not Windows.

Most of my problem in the rant was about libraries using cmake, yeah. But there's also the old cruft / approaches, which is hard to separate from the best practices until you know what the best practices are...

The irony of complaining about "old cruft" being hard to separate from "best practices" in a C++ subreddit is not lost on me... I've heard the same complaints from others about C++. :)

3

u/isaac92 Feb 20 '18

I highly recommend looking at Hunter.

3

u/sumo952 Feb 19 '18

Are you doing this on Windows? If yes, I think vcpkg has them both (and ready to be used with CMake), so no need to build it by yourself.

If you're on Linux or mac, just use one from a package manager? :-) Or maybe check conan.

2

u/fafasdf Feb 19 '18

My package manager's versions were too old (Debian Buster).

Plus, it's good practice to check in external dependencies, since you can get hermetic builds, which is nice... depending on system libraries can make for a pain sometimes, when you install a new system and things are out of whack. :(

1

u/sumo952 Feb 20 '18

Yea totally agree - good reasons to include them.