bagage
bagage

Reputation: 1114

Calling cmake with bash variable argument

I am struggling into an odd issue. I am trying to run cmake command line with a shell variable as an argument but it fails. This is what I've done:

#1. Basic. works fine
cmake -G 'Sublime Text 2 - Ninja'

#2. Argument into variable. error
CMAKE_CONFIG="-G 'Sublime Text 2 - Ninja'"
cmake $CMAKE_CONFIG ../..
> CMake Error: Could not create named generator  'Sublime Text 2 - Ninja'

#3. Adding -v before variable. 'compile' but ignore the argument (generate a Makefile). Hacky and senseless?
CMAKE_CONFIG="-G 'Sublime Text 2 - Ninja'"
cmake -v$CMAKE_CONFIG ../..

#4. Quoting argument. error (same as #2)
CMAKE_CONFIG="-G 'Sublime Text 2 - Ninja'"
cmake "$CMAKE_CONFIG" ../..

Playing with --trace and --debug-output variables gives the following:

#5. Working command
cmake ../.. --trace --debug-output -G "Sublime Text 2 - Ninja"

#6. Non existing generator. 
#Expected result (witness purpose only)
cmake ../.. --trace --debug-output -G 'random test'     
[...]
CMake Error: Could not create named generator random test

#7. Testing with variable. 
#Output error quotes the generator's name and there is an extra space before it
cmake ../.. --trace --debug-output $CMAKE_CONFIG     
[...]
CMake Error: Could not create named generator  'Sublime Text 2 - Ninja'

#8. Removing the quote within the variable. 
#Still error, but the only difference with #6 is the extra space after 'generator'
CMAKE_CONFIG="-G Sublime Text 2 - Ninja"
cmake ../.. --trace --debug-output $CMAKE_CONFIG     
[...]
CMake Error: Could not create named generator  Sublime Text 2 - Ninja

I tried to change the IFS variable too but didn't succeed to achieve my goal.

Any hints?

Upvotes: 4

Views: 1734

Answers (1)

Martin Vidner
Martin Vidner

Reputation: 2337

In this case, you need to debug the shell, not cmake. The trick is to replace "cmake" by printf '%q\n' in your commands, to let bash show you how it interprets your arguments.

I think using arrays like this will work:

CMAKE_CONFIG=(-G 'Sublime Text 2 - Ninja')
cmake "${CMAKE_CONFIG[@]}" ../..

Upvotes: 5

Related Questions