[CMake] [PATCH] Support multiple arguments in CC,CXX,FC values

Brad King brad.king at kitware.com
Mon Mar 15 09:07:17 EDT 2010


Teach compiler identification to support values such as

  export CC='gcc -g -O2'

by separating the arguments on spaces.  We already do this for the
values of CFLAGS, CXXFLAGS, and FFLAGS.
---

Hi Alan,

The standard way to do the "set and forget" approach with environment
variables is

  export CC=gcc
  export CXX=g++
  export FC=gfortran
  export CFLAGS='-g -fvisibility=hidden'
  export CXXFLAGS='-g -fvisibility=hidden'
  export FFLAGS='-g -fvisibility=hidden'

An argument in the "CC" value is meant for wrappers like ccache:

  export CC="ccache gcc"
  export CXX="ccache g++"
  export FC="ccache gfortran"

which take only *one* argument.

Alan W. Irwin wrote:
> I expect the dropped -fPIC problem will occur whenever there are two flags
> of any kind.

It's not just the -fPIC that gets dropped.  The whole compiler
identification fails:

  -- The C compiler identification is unknown
  -- The CXX compiler identification is unknown
  -- The Fortran compiler identification is unknown

Once that happens then the flag lookup fails for all kinds of things
including -fPIC.  An error is reported in CMakeFiles/CMakeError.log:

  Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
  Compiler: /usr/bin/gcc -g -fvisibility=hidden
  ...
  cc1: error: unrecognised debug output level " -fvisibility=hidden"

I can reproduce this error message with

  $ touch foo.c
  $ gcc '-g -fvisibility=hidden' foo.c
  cc1: error: unrecognised debug output level " -fvisibility=hidden"

During compiler identification all arguments get treated as a single
argument with spaces.  AFAIK this never worked, but in earlier CMake
versions we used a different scheme to add -fPIC and other platform
flags that did not depend on the compiler identification (but which
failed in other cases).

Anyway, this patch should fix it.  Please test.

Thanks,
-Brad


 Modules/CMakeDetermineCompilerId.cmake |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/Modules/CMakeDetermineCompilerId.cmake b/Modules/CMakeDetermineCompilerId.cmake
index bddd6a1..4a800a8 100644
--- a/Modules/CMakeDetermineCompilerId.cmake
+++ b/Modules/CMakeDetermineCompilerId.cmake
@@ -19,6 +19,7 @@
 FUNCTION(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
   # Make sure the compiler arguments are clean.
   STRING(STRIP "${CMAKE_${lang}_COMPILER_ARG1}" CMAKE_${lang}_COMPILER_ID_ARG1)
+  STRING(REGEX REPLACE " +" ";" CMAKE_${lang}_COMPILER_ID_ARG1 "${CMAKE_${lang}_COMPILER_ID_ARG1}")

   # Make sure user-specified compiler flags are used.
   IF(CMAKE_${lang}_FLAGS)
-- 
1.6.6.1



More information about the CMake mailing list