4

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?

1 Answer 1

5

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[@]}" ../..
Sign up to request clarification or add additional context in comments.

2 Comments

@Martin Vidner you saved my day! Thanks a lot! I didn't know trivial things can be complex some times

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.