简体   繁体   中英

CMake run custom command with mixed shell scripts

Let's say we want to run following command in CMake add_custom_command :

echo "#define FOO \"$(sh tools/foo.sh)\"" > foo.h

The foo.sh is a script maintained by others so we could not easily change it here.

The problem is, CMake will not evaluate \\"$(sh tools/foo.sh)\\"' in add_costom_command`, instead it just copy them into foo.h directly, which is obvious not what we want.

In the generated build.make I found something like:

echo "#define FOO \"\$$(sh tools/foo.sh)\"" > .../foo.h

What're the escape rules for CMake add_custom_command COMMAND ?

How should I write the command in add_custom_command COMMAND ?

CMake syntax is crazy and it's not easy to make this kind of escaping (involves , and ) right. To work it around I suggest you put the shell command in a separate file (eg helper.sh ) in your project dir and then call it like this --

add_custom_command(
  ...
  COMMAND bash ${PROJECT_SOURCE_DIR}/helper.sh > foo.h
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM