Home Download Buy Blog Forum Support

Custom env varaibles in sublime-build

Custom env varaibles in sublime-build

Postby kuba on Wed May 14, 2014 6:52 pm

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:
Code: Select all
{
"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.

Code: Select all
{
    "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
kuba
 
Posts: 4
Joined: Thu Mar 20, 2014 6:00 pm

Re: Custom env varaibles in sublime-build

Postby FichteFoll on Fri May 16, 2014 2:11 pm

You are right, custom environment variables that you want the build system to define using the "env" key are not expanded anywhere else in the file. They are in fact only set for the subprocess and not even for ST itself.

I like your suggestion because it is useful with long commands with many parameters. Luckily ST allows us to specify a "target" command (which is a Python WindowCommand) where you can do this thing programmatically. (You could also modify `exec.py`, but this should only be looked at as an option when you plan to release that.)
For when you want to run multiple commands in a single build system there are quite a few custom build systems or build system enhancers on Package Control which you could check out. https://sublime.wbond.net/search/build
FichteFoll
 
Posts: 404
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany


Return to Ideas and Feature Requests

Who is online

Users browsing this forum: No registered users and 11 guests