escaping - cmake: How to include literal double-quote in custom command? -
i'm trying create custom command runs environment variables, such ldflags, value needs quoted if contains spaces:
ldflags="-lmydir -lmyotherdir"
i cannot find way include argument in cmake custom command, due cmake's escaping rules. here's i've tried far:
command ldflags="-ldir -ldir2" echo blah verbatim)
yields "ldflags=\"-ldir -ldir2\"" echo blah
command ldflags=\"-ldir -ldir2\" echo blah verbatim)
yields ldflags=\"-ldir -ldir2\" echo blah
it seems either whole string quoted, or escaped quotes don't resolve when used part of command.
would appreciate either way include literal double-quote or alternative better way set environement variables command. please note i'm still on cmake 2.8, don't have new "env" command available in 3.2. in advance!
note not duplicate of cmake: when quote variables? none of quoting methods work particular case.
the obvious choice - recommended when hitting boundaries of command
older versions of cmake - use external script.
i wanted add simple command
variations work , won't need shell, - have admit - still partly platform dependent.
one example put quoted part variable:
set(vars_as_string "-ldir -ldir2") add_custom_target( quotedenvvar command env ld_flags=${vars_as_string} | grep ld_flags )
which escape space , not quotes.
another example add escaped quotes "launcher" rule:
add_custom_target( launcherenvvar command env | grep ld_flags ) set_target_properties( launcherenvvar properties rule_launch_custom "env ld_flags=\"-ldir -ldir2\"" )
edit: added examples multiple quoted arguments without need of escaping quotes
another example "hide of complexity" in function , - if want add custom command calls - use global/directory
rule_launch_custom
property:function(set_env) get_property(_env global property rule_launch_custom) if (not _env) set_property(global property rule_launch_custom "env") endif() foreach(_arg in lists argn) set_property(global append_string property rule_launch_custom " ${_arg}") endforeach() endfunction(set_env) set_env(ldflags="-ldir1 -ldir2" cflags="-idira -idirb") add_custom_target( multipleenvvar command env | grep -e 'ldflags|cflags' )
alternative (for cmake >= 3.0)
i think looking here (besides
cmake -e env ...
) named bracket argument , allow character without need of adding backslashes:set_property( global property rule_launch_custom [=[env ldflags="-ldir1 -ldir2" cflags="-idira -idirb"]=] ) add_custom_target( multipleenvvarnew command env | grep -e 'ldflags|cflags' )
references
Comments
Post a Comment