Sublime Forum

Build System: Executing a command only on the first build

#1

Is it possible to have a build system execute a command only once, on the first build so to speak?

I want to compile my C++ code using the sublime text build system. I currently call a bat file, that does the compilation. However, this .bat file relies on some environment variables to be set, that are needed by the Visual C++ compiler. There is another .bat file provided by the compiler, that sets up those environment variables, but also takes several seconds to execute, so I don’t want to call it every time I want to build my program.

Is there a way to specify a command that is only executed on the first build, or maybe pass environment variables to the build system? (Maybe creating a custom command in python? It would need to remember if the command was executed however, which would require it to keep some data beyond the lifetime of the command execution. Is that possible?)

0 Likes

#2

You can indeed pass environment to a build system by using env in the build system (see here), presuming you knew the complete list of variables and what they’re set to.

You could create a custom bat file that checks the environment and invokes the setup batch if one of the known variables is missing, followed by taking the rest of the build action. Also, if you wrote a custom target command for the build system (which requires a plugin), it would be able to track whether the build had been executed or not and act accordingly.

With this said, I believe that since Sublime invokes a sub-process to run the build, any changes made to the environment from within that subprocess aren’t “permanent”; they would only be in effect while the build is running, and then go away.

If that’s the case, the first solution would find that the variables are always missing and the second one would only build correctly once.

The plugin referenced below is something that I wrote a ways back in response to an SO question similar to this one. In use, when Sublime starts it invokes the batch file and modifies Sublime’s environment directly based on what the batch file sets, which sound like it might be helpful in your case.

1 Like

#3

I used your plugin and it works flawlessly. Thanks! I am very much looking forward to enjoy the goodness of seeing my build errors in editor (and jumping to them)!

I am just gonna link the original Stackoverflow-Question here, since you explain how to setup the plugin there, in detail (even if its linked in the plugin itsself), in case anyone else stumbles upon this post: https://stackoverflow.com/questions/39881091/how-to-run-sublimetext-with-visual-studio-environment-enabled/

1 Like