Aside of the ways that have already been mentioned in the question and the previous answer, another way to go is something akin to what @LinuxDisciple mentioned in their answer; have Sublime internally fire up the batch file once at Sublime start up to do what you need to do.
An example of this in action is the following plugin. To start with, add the following settings to your user preferences (Preferences > Settings
or Preferences > Settings - User
). Here I'm using the setup for my own local machine; update the path to suit for your version of Visual Studio:
"vc_vars_cmd": "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\vcvarsall.bat",
"vc_vars_arch": "amd64",
The vc_vars_cmd
setting is required by the following plugin, but the vc_vars_arch
setting is optional; if it is not given, it defaults to amd64
.
Next, select Tools > Developer > New Plugin
from the menu and replace the stub plugin code with the following and save it in the default location that Sublime defaults to as something like set_vc_vars.py
:
import sublime
import sublime_plugin
from threading import Thread
from subprocess import Popen, PIPE
from os import environ
SENTINEL="SUBL_VC_VARS"
def _get_vc_env():
"""
Run the batch file specified in the vc_vars_cmd setting (with an
optional architecture type) and return back a dictionary of the
environment that the batch file sets up.
Returns None if the preference is missing or the batch file fails.
"""
settings = sublime.load_settings("Preferences.sublime-settings")
vars_cmd = settings.get("vc_vars_cmd")
vars_arch = settings.get("vc_vars_arch", "amd64")
if vars_cmd is None:
print("set_vc_vars: Cannot set Visual Studio Environment")
print("set_vc_vars: Add 'vc_vars_cmd' setting to settings and restart")
return None
try:
# Run the batch, outputting a sentinel value so we can separate out
# any error messages the batch might generate.
shell_cmd = "\"{0}\" {1} && echo {2} && set".format(
vars_cmd, vars_arch, SENTINEL)
output = Popen(shell_cmd, stdout=PIPE, shell=True).stdout.read()
lines = [line.strip() for line in output.decode("utf-8").splitlines()]
env_lines = lines[lines.index(SENTINEL) + 1:]
except:
return None
# Convert from var=value to dictionary key/value pairs. We upper case the
# keys, since Python does that to the mapping it stores in environ.
env = {}
for env_var in env_lines:
parts = env_var.split("=", maxsplit=1)
env[parts[0].upper()] = parts[1]
return env
def install_vc_env():
"""
Try to collect the appropriate Visual Studio environment variables and
set them into the current environment.
"""
vc_env = _get_vc_env()
if vc_env is None:
print("set_vc_vars: Unable to fetch the Visual Studio Environment")
return sublime.status_message("Error fetching VS Environment")
# Add newly set environment variables
for key in vc_env.keys():
if key not in environ:
environ[key] = vc_env[key]
# Update existing variables whose values changed.
for key in environ:
if key in vc_env and environ[key] != vc_env[key]:
environ[key] = vc_env[key]
# Set a sentinel variable so we know not to try setting up the path again.
environ[SENTINEL] = "BOOTSTRAPPED"
sublime.status_message("VS Environment enabled")
def plugin_loaded():
if sublime.platform() != "windows":
return sublime.status_message("VS is not supported on this platform")
# To reload the environment if it changes, restart Sublime.
if SENTINEL in environ:
return sublime.status_message("VS Environment already enabled")
# Update in the background so we don't block the UI
Thread(target=install_vc_env).start()
Once you save the plugin, Sublime will load it and execute the plugin_loaded
function, which will do all of the work.You should see the status bar say VS Environment Enabled
if it worked.
If you see Error Fetching VS Environment
double check that the path to the batch file is correct (note that you need to double all path separators to make them JSON compliant).
This operates very similarly to (and is vaguely based on) the Fix Mac Path package, which does something similar to update the path in Sublime on MacOS, where the vagaries of the OS cause GUI applications to have their environment set in a way different than terminal applications.
The general idea is that the batch file is executed in a sub-process, and then before returning we also execute the set
command to get the command interpreter to dump its entire environment to the console, which the plugin captures.
Once we have that data, we can easily compare it with the current environment to add in any environment variables that the batch file set up but which didn't already exist, and update the values of any environment variables that were changed (e.g. the PATH
).
As a part of this process we also set a new environment variable that tells the plugin that the environment has already been set up, so that if the plugin gets reloaded it doesn't try to run the operation again.
As such, if you need to change the path to the batch file or the architecture that you want to be set up for, you need to change the preference and restart Sublime.
This could be made more robust by having it do something like store the current environment before modifying it and then watching the preferences to see if that setting is changed and act accordingly, but presumably it's not often that you would need to modify these settings.
For testing purposes, I created the following simple C program:
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Hello VC World!\n");
return 0;
}
I also created the following sublime-build
file, based loosely off of the C++ Single File.sublime-build
that ships with Sublime:
{
"shell_cmd": "cl \"${file}\" /Fe\"${file_base_name}\"",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "cl \"${file}\" /Fe\"${file_base_name}\" && \"${file_base_name}\""
}
]
}
If you attempt to build the sample C file without the plugin, an error is generated about not being able to find cl
(unless you have started Sublime from a developer prompt or otherwise already set up the path). With the plugin in place, you get output similar to the following:
