-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap the dotnet executable to set default environment variables (#102)
To run `dotnet` cross-platform we need to ensure some environment are set. The problem though is that we must: - Use an absolute path - Have different paths for each OS (windows vs unix) This commit implements a wrapper for dotnet (dotnetw) that ensures the environment variables are configured based on the operating system. Additionally it lays the groundwork for other rules to call dotnet build during execution. This is necessary for the work to support ResX files. See also: #10, #98
- Loading branch information
Showing
8 changed files
with
193 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
_TEMPLATE = "@d2l_rules_csharp//csharp/private:wrappers/dotnet.cc" | ||
|
||
def _dotnet_wrapper_impl(ctx): | ||
cc_file = ctx.actions.declare_file("%s.cc" % (ctx.attr.name)) | ||
if len(ctx.files.src) < 1: | ||
fail("No dotnet executable found in the SDK") | ||
|
||
ctx.actions.expand_template( | ||
template = ctx.file.template, | ||
output = cc_file, | ||
substitutions = { | ||
"{DotnetExe}": ctx.files.src[0].short_path[3:], | ||
}, | ||
) | ||
|
||
files = depset(direct = [cc_file]) | ||
return [ | ||
DefaultInfo( | ||
files = files, | ||
), | ||
] | ||
|
||
dotnet_wrapper = rule( | ||
implementation = _dotnet_wrapper_impl, | ||
attrs = { | ||
"template": attr.label( | ||
doc = """Path to the program that will wrap the dotnet executable. | ||
This program will be compiled and used instead of directly calling the dotnet executable.""", | ||
default = Label(_TEMPLATE), | ||
allow_single_file = True | ||
), | ||
"src": attr.label_list( | ||
doc = """The name of the dotnet executable. | ||
On windows this should be 'dotnet.exe', and 'dotnet' on linux/macOS.""", | ||
mandatory = True, | ||
allow_files = True, | ||
), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Use the Google style in this project. | ||
BasedOnStyle: Google | ||
|
||
DerivePointerAlignment: false | ||
PointerAlignment: Left | ||
|
||
IncludeBlocks: Merge | ||
IncludeCategories: | ||
- Regex: '^\"' | ||
Priority: 1000 | ||
- Regex: '^<.*/.*' | ||
Priority: 4000 | ||
- Regex: '^<[^/]*>' | ||
Priority: 5000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include <iostream> | ||
#include <sstream> | ||
#include <string> | ||
|
||
#ifdef _WIN32 | ||
#include <errno.h> | ||
#include <process.h> | ||
#include <windows.h> | ||
#else // not _WIN32 | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#endif // _WIN32 | ||
|
||
#include "tools/cpp/runfiles/runfiles.h" | ||
|
||
using bazel::tools::cpp::runfiles::Runfiles; | ||
|
||
std::string evprintf(std::string name, std::string path) { | ||
std::stringstream ss; | ||
ss << name << "=" << path; | ||
return ss.str(); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
std::string error; | ||
|
||
auto runfiles = Runfiles::Create(argv[0], &error); | ||
|
||
if (runfiles == nullptr) { | ||
std::cerr << "Couldn't load runfiles: " << error << std::endl; | ||
return 101; | ||
} | ||
|
||
auto dotnet = runfiles->Rlocation("{DotnetExe}"); | ||
if (dotnet.empty()) { | ||
std::cerr << "Couldn't find the .NET runtime" << std::endl; | ||
return 404; | ||
} | ||
|
||
// Get the name of the directory containing dotnet.exe | ||
auto dotnetDir = dotnet.substr(0, dotnet.find_last_of("/\\")); | ||
|
||
/* | ||
dotnet requires these environment variables to be set. | ||
*/ | ||
std::vector<std::string> envvars = { | ||
evprintf("HOME", dotnetDir), | ||
evprintf("DOTNET_CLI_HOME", dotnetDir), | ||
evprintf("APPDATA", dotnetDir), | ||
evprintf("PROGRAMFILES", dotnetDir), | ||
evprintf("USERPROFILE", dotnetDir), | ||
evprintf("DOTNET_CLI_TELEMETRY_OPTOUT", "1"), // disable telemetry | ||
}; | ||
|
||
// dotnet wants this to either be dotnet or dotnet.exe but doesn't have a | ||
// preference otherwise. | ||
auto dotnet_argv = new char*[argc]; | ||
dotnet_argv[0] = (char*)"dotnet"; | ||
for (int i = 1; i < argc; i++) { | ||
dotnet_argv[i] = argv[i]; | ||
} | ||
dotnet_argv[argc] = nullptr; | ||
|
||
#ifdef _WIN32 | ||
// _spawnve has a limit on the size of the environment variables | ||
// passed to the process. So here we will set the environment | ||
// variables for this process, and the spawned instance will inherit them | ||
for (int i = 1; i < envvars.size(); i++) { | ||
putenv(envvars[i].c_str()); | ||
} | ||
|
||
// run `dotnet.exe` and wait for it to complete | ||
// the output from this cmd will be emitted to stdout | ||
auto result = _spawnv(_P_WAIT, dotnet.c_str(), dotnet_argv); | ||
#else | ||
auto envc = envvars.size(); | ||
auto envp = new char*[envc + 1]; | ||
for (uint i = 0; i < envc; i++) { | ||
envp[i] = const_cast<char*>(&envvars[i][0]); | ||
} | ||
envp[envc] = nullptr; | ||
|
||
// run `dotnet.exe` and wait for it to complete | ||
// the output from this cmd will be emitted to stdout | ||
auto result = execve(dotnet.c_str(), const_cast<char**>(dotnet_argv), envp); | ||
#endif // _WIN32 | ||
if (result != 0) { | ||
std::cout << "dotnet failed: " << errno << std::endl; | ||
return -1; | ||
} | ||
|
||
return result; | ||
} |