MantisBT - CMake
View Issue Details
0016056CMakeCMakepublic2016-04-08 13:182016-06-10 14:21
johnnyzz 
 
normalminoralways
closedno change required 
CMake 3.5.1 
 
0016056: file(GENERATE) not processing variables from INPUT file
There is a bug when generating a file with:

file(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/output1.txt"
      INPUT ${CMAKE_SOURCE_DIR}/input.txt
)

file(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/output2.txt"
      CONTENT "SOURCE DIR = '${CMAKE_SOURCE_DIR}'"
)

- Using "INPUT filename", variables are not processed, but "CONTENT text" with the same content works properly
1) cmake .
2) cat output1.txt
3) cat output2.txt

-> The variable in output1.txt is not replaced by its value
No tags attached.
txt CMakeLists.txt (270) 2016-04-08 13:18
https://public.kitware.com/Bug/file/5671/CMakeLists.txt
txt input.txt (35) 2016-04-08 13:19
https://public.kitware.com/Bug/file/5672/input.txt
Issue History
2016-04-08 13:18johnnyzzNew Issue
2016-04-08 13:18johnnyzzFile Added: CMakeLists.txt
2016-04-08 13:19johnnyzzFile Added: input.txt
2016-04-08 13:27Brad KingNote Added: 0040830
2016-04-08 13:27Brad KingStatusnew => resolved
2016-04-08 13:27Brad KingResolutionopen => no change required
2016-04-08 13:30Brad KingNote Added: 0040831
2016-04-08 14:01johnnyzzNote Added: 0040832
2016-06-10 14:21Kitware RobotNote Added: 0041198
2016-06-10 14:21Kitware RobotStatusresolved => closed

Notes
(0040830)
Brad King   
2016-04-08 13:27   
file(GENERATE) does not document that it expands variables. It doesn't expand them in either case. In fact it is not well-defined to do so because the generation does not occur until after configuration is complete when the variable definitions may no longer be available.

The difference you're seeing is the CMake language processing that expands variables in the argument to CONTENT before the file(GENERATE) command even sees it.
(0040831)
Brad King   
2016-04-08 13:30   
> CMake language processing that expands variables in the argument to CONTENT

One can see this by using a bracket argument instead:

    file(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/output2.txt"
         CONTENT [[SOURCE DIR = '${CMAKE_SOURCE_DIR}']]
    )

Bracket-enclosed arguments have no substitutions done by the language processing.

You could consider using configure_file first to replace variables and then use its output as the INPUT to file(GENERATE).
(0040832)
johnnyzz   
2016-04-08 14:01   
Well, I see it now, thanks, my intention was to store the input template separately.
The configure_file looks what I'm looking for.
Anyway, documentation states that it can expand "generator expressions", so I thought it can/could do the same with variables
(0041198)
Kitware Robot   
2016-06-10 14:21   
This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.