Search code examples
linuxbashcmakemakefilecmake-custom-command

CMake: how to use if condition in add_custom_command(...)


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!


Solution

  • 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".