MantisBT - CMake
View Issue Details
0008362CMakeCMakepublic2009-01-11 07:042011-01-18 11:52
Jelle Geerts 
Brad King 
normalmajoralways
closedduplicate 
CMake-2-6 
 
0008362: cmake fails executing the command in link.txt with continuation characters in CMAKE_C_FLAGS
For example:
set( CMAKE_C_FLAGS "-ansi \
    -pedantic" )

This would generate:
path_to_gcc.exe -ansi \
    -pedantic CMakeFiles\test.dir\src\main.c.obj -o _build\bin\test.exe -Wl,--out-implib,_build\lib\libtest.dll.a -Wl,--major-image-version,0,--minor-image-version,0

Now, the command 'cmake.exe -E cmake_link_script CMakeFiles\pkeep.dir\link.txt'
fails, it only executes the first line, 'path_to_gcc.exe -ansi \'.

Tested on WinXP SP3.
No tags attached.
duplicate of 0006493closed Stephen Kelly configuration dependent COMPILE_FLAGS for SET_TARGET_PROPERTIES 
Issue History
2009-01-11 07:04Jelle GeertsNew Issue
2009-01-11 20:29Bill HoffmanStatusnew => assigned
2009-01-11 20:29Bill HoffmanAssigned To => Brad King
2009-01-12 08:59Brad KingNote Added: 0014530
2009-01-12 08:59Brad KingStatusassigned => closed
2009-01-12 08:59Brad KingResolutionopen => won't fix
2009-01-17 08:43Jelle GeertsNote Added: 0014596
2009-01-17 08:43Jelle GeertsStatusclosed => feedback
2009-01-17 08:43Jelle GeertsResolutionwon't fix => reopened
2009-01-17 09:02Brad KingNote Added: 0014597
2009-01-17 09:04Jelle GeertsNote Added: 0014598
2009-01-17 11:07Brad KingNote Added: 0014599
2010-07-01 07:40Göran UddeborgNote Added: 0021205
2011-01-18 11:17David ColeNote Added: 0024860
2011-01-18 11:51Brad KingRelationship addedduplicate of 0006493
2011-01-18 11:52Brad KingNote Added: 0024875
2011-01-18 11:52Brad KingStatusfeedback => closed
2011-01-18 11:52Brad KingResolutionreopened => duplicate

Notes
(0014530)
Brad King   
2009-01-12 08:59   
Continuation characters in flags is not a supported use case. If the flags are generated by something else, use string(REGEX REPLACE) to remove them.
(0014596)
Jelle Geerts   
2009-01-17 08:43   
It's not about the continuation characters, but it's about the newline characters. The command in link.txt may not have newline characters because cmake only executes the first line.

Might this be an issue to look into?
(0014597)
Brad King   
2009-01-17 09:02   
What is your use case for this? Why does the _link_ command line ever need a newline in it?
(0014598)
Jelle Geerts   
2009-01-17 09:04   
Because having newlines inside the 'set( CMAKE_C_FLAGS' part improves the maintainability of the CMakeLists.txt file.

Example:
set( CMAKE_C_FLAGS "-ansi -Wall -Wextra -Werror -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Wstrict-prototypes -Wbad-function-cast -Wformat=2 -pedantic -Wa,--fatal-warnings" )
Versus:
set( CMAKE_C_FLAGS "-ansi -Wall -Wextra -Werror -Wshadow
-Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes
-Wmissing-declarations -Wredundant-decls -Wnested-externs
-Wstrict-prototypes -Wbad-function-cast -Wformat=2 -pedantic
-Wa,--fatal-warnings" )
(0014599)
Brad King   
2009-01-17 11:07   
I see. Unfortunately the flags were introduced _really_ early in CMake, before we had the convention of semi-colon separated lists. Otherwise you could just write the above code without the quotes. A while back I started working on a COMPILE_OPTIONS feature which is like flags but uses modern CMake conventions. I don't know when I'll get back to it.

Meanwhile, you can use the string(REPLACE) command to remove the newlines:

string(REPLACE "\n" " " CMAKE_C_FLAGS "
-ansi -Wall -Wextra -Werror -Wshadow
-Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes
-Wmissing-declarations -Wredundant-decls -Wnested-externs
-Wstrict-prototypes -Wbad-function-cast -Wformat=2 -pedantic
-Wa,--fatal-warnings
")

or create a macro to help you

macro(add_flags var)
  foreach(flag ${ARGN})
    set(${var} "${${var}} ${flag}")
  endforeach(flag)
endmacro(add_flags)

add_flags(CMAKE_C_FLAGS
  -ansi -Wall -Wextra -Werror -Wshadow
  -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes
  -Wmissing-declarations -Wredundant-decls -Wnested-externs
  -Wstrict-prototypes -Wbad-function-cast -Wformat=2 -pedantic
  -Wa,--fatal-warnings
  )

The macro can be more efficient like this:

macro(add_flags var)
  string(REPLACE ";" " " _flags "${ARGN}")
  set(${var} "${${var}} ${_flags}")
endmacro(add_flags)
(0021205)
Göran Uddeborg   
2010-07-01 07:40   
It would certainly be very nice if this could be fixed without extra local code like add_flags. Coming from Make previously, we hit this almost immediately. In our makefiles we always put the values of variables on several lines when there were many of them.
(0024860)
David Cole   
2011-01-18 11:17   
Looking at "older" "feedback" status bugs today... is this still an issue that we want to address moving forward? If so, please remove the "feedback" status and just set it to "assigned"... If not, please resolve it.

Thanks.
(0024875)
Brad King   
2011-01-18 11:52   
Closing as duplicate of 0006493 because the solution is the same: a future COMPILE_OPTIONS property. Please visit that issue and click the "Monitor" button to get further updates.