Hi there,
I’d like to be able to define my own custom environment variables and refer to them from within the same sublime-build file. Currently I think the latter is not possible, or I am improperly using the ${variable}. Please see below:
{
"env": { "TESTGCC":"gcc --version"},
"shell_cmd" : "${TESTGCC}"
}
My point of defining custom variables is twofold:
- split long build command into sub sections to make them more manageable. I’m aware of Make file but what I really like in this solution is that it is instantly available when prototyping.
- echoing the final shell_cmd requires me to type the full command twice
At the moment I have a quasi solution to this problem which relays on heavy use of grep/sed/regex Surprisingly it works pretty good for me so I thought I would share it here. All elements of the build command are stored in custom env variables which later are concatenated inside this massive regex expression on the shell_cmd line. The good side of it is that once set there is not need to change the shell_cmd again. Same approach is applied to Run command, the only difference is tat -L have to be removed from the subLIB variable.
{
"env": {
"subFLAGS": "-std=c++0x",
"subLINK": "-ltbb -lboost_regex",
"subINCLUDE": "-I/usr/local/include -I/usr/local/boost-1_51_0/",
"subLIB": "-L/usr/lib -L/usr/local/lib",
},
"shell_cmd" : "echo g++ \"${file}\" `env | grep -oEi 'subFLAGS.+' | sed 's/^subFLAGS=\\(.*\\)/\\1/'` `env | grep -oEi 'subINCLUDE.+' | sed 's/^subINCLUDE=\\(.*\\)/\\1/'` `env | grep -oEi 'subLIB.+' | sed 's/^subLIB=\\(.*\\)/\\1/'` `env | grep -oEi 'subLINK.+' | sed 's/^subLINK=\\(.*\\)/\\1/'` -o \"${file_path}/${file_base_name}\"; g++ \"${file}\" `env | grep -oEi 'subFLAGS.+' | sed 's/^subFLAGS=\\(.*\\)/\\1/'` `env | grep -oEi 'subINCLUDE.+' | sed 's/^subINCLUDE=\\(.*\\)/\\1/'` `env | grep -oEi 'subLIB.+' | sed 's/^subLIB=\\(.*\\)/\\1/'` `env | grep -oEi 'subLINK.+' | sed 's/^subLINK=\\(.*\\)/\\1/'` -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
{ // This is quite complex grep/sed command, it is composed of 4 following steps:
// 1. grep env and capture $subLIB
// 2. (sed no.1) removes the env variable name "subLIB=" and keep the trailing characters
// 3. (sed no.2) replace "-L" with ""
// 4. (sed no.3) replcae " " with ":"
"name": "Run",
"shell_cmd": "echo export LD_LIBRARY_PATH=`env | grep -oEi 'subLIB.+' | sed 's/^subLIB=\\(.*\\)/\\1/' | sed -e 's/-L//g' | sed -e 's/ /:/g'`; export LD_LIBRARY_PATH=`env | grep -oEi 'subLIB.+' | sed 's/^subLIB=\\(.*\\)/\\1/' | sed -e 's/-L//g' | sed -e 's/ /:/g'`; ${file_path}/${file_base_name}"
}
]
}
Cheers
kuba