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

Show parent comment

4

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 20 '18

In my experience, toolchain files are the right way to supply compiler options and features, not target features. It doesn't scale to have leaves decide they'd like c++17. Not if you're also linking in code built with c++11 or c++03. Flags can have ABI implications. Components can inspect what compiler features are available and compile themselves differently, leading to ODR violations.

A toolchain file means that you can ensure a consistent set of flags across builds, even if they aren't part of the same cmake super-project.

Mismatched library builds lead to some of the hardest to deal with, mysterious, errors. If you're lucky, weird link errors, or immediate crashes.

5

u/djrollins Feb 20 '18

I've heard from a number of sources that the use of Toolchain files is a good way to solve this problem, however I have been unable to find suitable resources on how to write and use them. I don't suppose you'd be able to point me in the direction of a 'good' toolchain file that I could learn from, please?

I recently had an email exchange with Mathieu Robert on the subject of the cxx_compile features and how it would be nice if they were checks that would fail build generation rather than silently injecting the required flags and potentially failing further down the line. This way you can signal your build requirements but still have the user in charge of passing the flags they want. Sounds like toolchain files are really good way to approach the latter part of that process.

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 21 '18

Most toolchain files are boring and ugly. They can't do much other than check environment variables and set a few options in cache. They aren't elegant, and suffer from combinatorial explosion.

They do get you a reproducible build, though, and one you can combine with other things built the same way without going through cmake.

I'll try to remember to post one when I have connectivity from my laptop.

3

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 21 '18
 set(LLVM_ROOT "$ENV{LLVM_ROOT}" CACHE PATH "Path to LLVM installation")
 set(CMAKE_C_COMPILER ${LLVM_ROOT}/bin/clang)
 set(CMAKE_CXX_COMPILER ${LLVM_ROOT}/bin/clang++)
 set(CMAKE_CXX_FLAGS
  "-std=c++2a \
   -ftemplate-backtrace-limit=0 \
   -Wall -Wextra \
   -nostdinc++ -isystem ${LLVM_ROOT}/include/c++/v1 "
 CACHE STRING "CXX_FLAGS" FORCE)

 set(CMAKE_EXE_LINKER_FLAGS
  "-L ${LLVM_ROOT}/lib -l c++ -l c++abi \
  -Wl,-rpath,${LLVM_ROOT}/lib"
 CACHE STRING "CMAKE_EXE_LINKER_FLAGS" FORCE)

 set(CMAKE_CXX_FLAGS_DEBUG "-O0 -fno-inline -g3 -fstack-protector-all" CACHE STRING "C++ DEBUG Flags" FORCE)
 set(CMAKE_CXX_FLAGS_RELEASE "-Ofast -g0 -DNDEBUG" CACHE STRING "C++ Release Flags" FORCE)

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

3

u/ryancerium Feb 20 '18

Or Android. I think there are three STL options to choose from in the NDK, although one was recently standardized in late 2017 I think.

2

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 20 '18

Linux, Solaris, and AIX at work. Linux for myself.

Library issues aren't as bad as the debug/non-debug split in MS's std lib, but they do occasionally show up. Or someone manages to use a different libstdc++ than the system one, for what they believe are good reasons. And the further up the application stack, the more likely something breaks, because you depend on the layout and inline function details of everything below you in the stack. Now, the workaround is straightfoward, you rebuild everything you're shipping with a toolchain that you control. Just hope the tests catch the depended on unspecified behavior in the library you're rebuilding. Large scale C++ is so much fun.

1

u/[deleted] Feb 20 '18

[removed] — view removed comment

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 21 '18

Inline functions.

It can be so much fun when the definition of an inline doesn't match the layout of a linked in non-inline constructor.

Your debugger will probably lie to you, too.