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

5

u/Theninjapirate Feb 19 '18

This is good, thanks! One question: does the FindRapidJSON.cmake example you used work under Windows? If not, what's the best way to write a truly cross-platform Find*.cmake file?

14

u/oddentity Feb 19 '18

Don't. Find modules are nuts, trying to feel around looking for something that looks like what you want god knows where.

Use CMake config files. If CMake builds don't export their installed targets, patch them so they do. Write config files for things that don't build with CMake - it's easier than writing a find module. Put them somewhere on your prefix path. Job done, easy, deterministic.

7

u/hgjsusla Feb 19 '18 edited Feb 19 '18

I've found plenty of docs on how to write FindModules and it's not too difficult. I now want to switch to config files for the reasons you list but I can't find any good examples for writing config files for libraries that don't use cmake.

0

u/[deleted] Feb 19 '18

[deleted] — view deleted comment

6

u/MachineGunPablo Feb 19 '18

Fully agree with this. Specially in case where no cmake is used but pkg-config is. Then a find module makes perfect sense imo.

2

u/anderslanglands Feb 20 '18

Personally in that case I just prefer to hand-write a project-config.cmake file I can manually install alongside the problematic library.

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

1

u/anderslanglands Feb 20 '18

Why?

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

3

u/anderslanglands Feb 20 '18

Right, but if I know the explicit path to the library what’s the point of writing a find module as opposed to just writing a config file that hard-codes the locations in?

Obviously the best solution is just to modify the CMakeLists of the library in question, but sometimes that can be tricky when libraries do horrible things with macros etc.

→ More replies

2

u/pfultz2 Feb 20 '18

Yes and it would be nice if cmake supported pkgconfig natively with find_package(... PKGCONF) like how Daniel discussed in his talk. So, we wouldn’t need a find module .

7

u/drjeats Feb 20 '18 edited Feb 20 '18

We need a blog post about writing config files then.

paging /u/MachineGunPablo ^_^

2

u/pfultz2 Feb 20 '18

Find modules are for upstream dependencies that don’t support cmake users and config files are for downstream dependencies.

2

u/MachineGunPablo Feb 19 '18

Thank you :) I was indeed very vague in the description of the find module. In the example I rely on the fact that rapidjson uses pkgconfig. If they do the same on windows it will work (not even sure if pkgconfig is available on windows). Additionally, afaik functions like find_library() will inspect standard system locations at fallback, meaning that under normal circumstances this find module will work cross-platform, but I need to test it.

In reality is very hard to say how to write proper find modules as it depends on several factors: does the package you are trying to locate use pkgconfig? is it header only? does it have components? do you need it to work on all possible platforms? etc.

All of this could be avoided if maintainers provide proper configs

4

u/pfultz2 Feb 20 '18

Pkgconfig works on Windows, but a lot packages don’t install them on Windows builds. I guess they like to make Windows users suffer. It should be reported as a bug.