Skip to content

Commit

Permalink
Wrap the dotnet executable to set default environment variables (#102)
Browse files Browse the repository at this point in the history
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
jrbeverly authored Dec 3, 2019
1 parent 5939e95 commit 15ed431
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 13 deletions.
5 changes: 5 additions & 0 deletions csharp/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ load(
"//csharp/private:rules/binary.bzl",
_csharp_binary = "csharp_binary",
)
load(
"//csharp/private:rules/wrapper.bzl",
_dotnet_wrapper = "dotnet_wrapper",
)
load(
"//csharp/private:rules/library.bzl",
_csharp_library = "csharp_library",
Expand Down Expand Up @@ -34,6 +38,7 @@ load(
_setup_basic_nuget_package = "setup_basic_nuget_package",
)

dotnet_wrapper = _dotnet_wrapper
csharp_binary = _csharp_binary
csharp_library = _csharp_library
csharp_library_set = _csharp_library_set
Expand Down
7 changes: 2 additions & 5 deletions csharp/private/BUILD
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
load(":toolchains.bzl", "configure_toolchain")

exports_files(["nunit/shim.cs"])
exports_files(["nunit/shim.cs", "wrappers/dotnet.cc"])

toolchain_type(name = "toolchain_type")

configure_toolchain(
"windows",
exe = "dotnet.exe",
)
configure_toolchain("windows")

configure_toolchain("linux")

Expand Down
4 changes: 3 additions & 1 deletion csharp/private/actions/assembly.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ def AssemblyAction(
# spill to a "response file" when the argument list gets too big (Bazel
# makes that call based on limitations of the OS).
args.set_param_file_format("multiline")
args.use_param_file("@%s")
# Our wrapper uses _spawnv to launch dotnet, and that has a command line limit
# of 1024 bytes, so always use a param file.
args.use_param_file("@%s", use_always=True)

direct_inputs = srcs + resources + analyzer_assemblies + additionalfiles + [toolchain.compiler]
direct_inputs += [keyfile] if keyfile else []
Expand Down
39 changes: 39 additions & 0 deletions csharp/private/rules/wrapper.bzl
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,
),
},
)
40 changes: 35 additions & 5 deletions csharp/private/runtime.BUILD
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@d2l_rules_csharp//csharp:defs.bzl", "dotnet_wrapper")

exports_files(
glob([
"dotnet",
"dotnet.exe", # windows, yeesh
], allow_empty = True) +
glob([
glob(
[
"dotnet",
"dotnet.exe", # windows, yeesh
],
allow_empty = True,
) + glob([
"host/**/*",
"shared/**/*",
]) +
Expand All @@ -13,3 +18,28 @@ exports_files(
]),
visibility = ["//visibility:public"],
)

cc_binary(
name = "dotnetw",
srcs = [":main-cc"],
data = glob(
[
"dotnet",
"dotnet.exe",
],
allow_empty = True,
),
visibility = ["//visibility:public"],
deps = ["@bazel_tools//tools/cpp/runfiles"],
)

dotnet_wrapper(
name = "main-cc",
src = glob(
[
"dotnet",
"dotnet.exe",
],
allow_empty = True,
),
)
4 changes: 2 additions & 2 deletions csharp/private/toolchains.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def _csharp_toolchain_impl(ctx):
return [
platform_common.ToolchainInfo(
runtime = ctx.file.runtime,
runtime = ctx.attr.runtime.files_to_run,
compiler = ctx.file.compiler,
),
]
Expand All @@ -25,7 +25,7 @@ csharp_toolchain = rule(
)

# This is called in BUILD
def configure_toolchain(os, exe = "dotnet"):
def configure_toolchain(os, exe = "dotnetw"):
csharp_toolchain(
name = "csharp_x86_64-" + os,
runtime = "@netcore-sdk-%s//:%s" % (os, exe),
Expand Down
14 changes: 14 additions & 0 deletions csharp/private/wrappers/.clang-format
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
93 changes: 93 additions & 0 deletions csharp/private/wrappers/dotnet.cc
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;
}

0 comments on commit 15ed431

Please sign in to comment.