1

If I'm using the clang compiler in CMake, I would like to prepend the option -cc1 to it for every possible invocation (better: only for a certain target)

I tried using

set(CMAKE_CXX_COMPILER "${CMAKE_CXX_COMPILER} -cc1")

But this wraps the invocation in quotes; consequently this doesn't get recognized as a valid command in my shell.

If I use

set(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER} -cc1)

then I get a semicolon between the clang invocation and the -cc1 option. This also doesn't work.

How do I get CMake to change /path/to/clang into /path/to/clang -cc1 ?

rwols
  • 2,968
  • 2
  • 19
  • 26
  • I think you can use something like [this](https://cmake.org/cmake/help/v3.0/command/target_compile_options.html) so `target_compile_options(target PUBLIC "-cc1")`. – Winestone Apr 21 '17 at 23:08
  • @Winestone yes that's possible, but I need to "prepend" the cc1 option after other `target_compile_options` are already set; maybe I'm trying too hard. – rwols Apr 21 '17 at 23:09
  • Try `target_compile_options(target BEFORE PUBLIC "-cc1")`. – Winestone Apr 21 '17 at 23:10
  • That puts it *after* the `target_include_directories`, not right after the compiler invocation. – rwols Apr 21 '17 at 23:11
  • One hacky way I can think of is to make a script which forwards it's arguments to clang, so like `/path/to/clang -cc1 "$@"` and then set `CMAKE_CXX_COMPILER` to that. – Winestone Apr 21 '17 at 23:23
  • Maybe try `string(CONCAT CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER} " -cc1")`? – MassPikeMike Apr 21 '17 at 23:30
  • @MassPikeMike I get `"/usr/bin/c++.exe -pedantic"` being run as a program, unfortunately. – Winestone Apr 21 '17 at 23:38
  • `CMAKE_CXX_FLAGS` doesn't put it at the right spot ? – luk32 Apr 21 '17 at 23:38
  • @luk32 I tried it and it seems to go after `target_include_directories`. – Winestone Apr 21 '17 at 23:39
  • Order seems to be `${CMAKE_CXX_COMPILER} target_include_directories ${CMAKE_CXX_FLAGS} target_compile_options -o output_file -c input_file`. – Winestone Apr 21 '17 at 23:45
  • OK, I found out about a workaround by using the `-Xclang` option for my clang-specific needs. Feel free to give this as an answer. – rwols Apr 22 '17 at 00:12
  • @rwols You can answer your own question. It's fine. You just need to wait before you can accept it. Or you need to wait before you can post it. Either way, eventually you should be able to do it. – luk32 Apr 22 '17 at 00:19

2 Answers2

1

One workaround for clang-specific needs is to use the -Xclang compiler option, which forces the clang driver to pass the option that follows it to clang -cc1.

For example:

target_compile_options(${target} PUBLIC "-Xclang -include-pch ${output}")
rwols
  • 2,968
  • 2
  • 19
  • 26
0

See: cmake CFLAGS CXXFLAGS modification

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -cc1")

For a single target:

target_compile_options(target -cc1)
Community
  • 1
  • 1
trollkill
  • 44
  • 3