5

I want to use Linux if condition in CMakeLists.txt by add_custom_command(...) for i need run these if condition and do some judgement in makefile. Like this:

cmake_minimum_required(VERSION 2.8)
add_custom_target(temp_target ALL)
add_custom_command(TARGET temp_target
                   PRE_BUILD
                   COMMAND if ["a" != "b"]; then echo 1; fi;
                   VERBATIM )

What should i do if i want to use

if ["a" != "b"]; then echo 1; fi;

when make a makefile? Thanks a lot for you help!

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Chao Zhang
  • 190
  • 2
  • 15

2 Answers2

4

You may specify one-line shell code with using /bin/sh -c as COMMAND argument:

COMMAND /bin/sh -c "if [ 'a' != 'b' ]; then echo 1; fi;"

Note, that [ is an extension of the bash, it may be unknown for simple shells like "dash".

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
1

You may need to preceed semicolons with backslash

COMMAND if [ 'a' != 'b' ] \; then echo 1\; fi\;
Master Yoda
  • 176
  • 1
  • 8