[cmake-developers] Adding an option for relative compiler invocations

Michael Enßlin michael at ensslin.cc
Thu Jan 22 04:46:40 EST 2015


Hi,

this issue has been discussed here in 2011, but not to my satisfaction
(http://www.cmake.org/pipermail/cmake/2011-December/thread.html#48266).

My logging code uses __FILE__ to print the name of the relevant source
file; cmake's absolute-path compiler invocations, combined with gcc's
implementation of __FILE__, cause two issues:

(0.0) The build path ends up being part of the binary
(0.1) The build path is printed for every log message

I've identified several solutions, all of which are unsatisfactory:

(1.0) At the top of every single source and header file, writing
      '#line 2 subdir/my_file.cpp'.
(1.1) Using run-time string manipulations to sanitize the filename.
      Doesn't solve (0.0) and comes with a run-time cost.
(1.2) Using compile-time string manipulations to sanitize the filename.
      Due to limitations of C++, this requires template metaprogramming,
      leading to unreasonable complexity and compile times.
(1.3) Storing CMAKE_SOURCE_DIR as an constexpr in configure.h,
      and using that to calculate constexpr offsets into the __FILE__
      literals. This does not solve (0.0), but it's the solution I've
      decided to use for now.
(1.4) Replacing gcc by a wrapper script that sanitizes all
      filename arguments and changes the working directory.

The following are not solutions at all:

(2.0) Using '-DFILENAME=subdir/my_file.cpp'. This fails for
      included headers.
(2.1) Ignoring the issue. I care about such minor details.

Over the last several decades, at least on the POSIX platform, it has
become common practice to invoke compilers with relative file paths, and
compilers have adopted to act accordingly. While the true culprit is the
C standard's lax definition of __FILE__, I'm blaming cmake's unusual,
absolute-path invocation behavior.

Even in the unlikely event that gcc (and clang) decided to introduce
flags that would modify the behavior of __FILE__, all they could do
would be stripping the entire basename, so 'project_dir/my_file.cpp' and
'project_dir/subdir/my_file.cpp' would have the same value for __FILE__.

I suggest adding an additional, optional parameter to add_executable
that would allow switching on relative gcc invocations from a directory
of the user's choosing.

Thanks for considering this proposal.

 ~ Michael

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://public.kitware.com/pipermail/cmake-developers/attachments/20150122/369d8805/attachment.sig>


More information about the cmake-developers mailing list