Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap the dotnet executable to set default environment variables #102

Merged
merged 21 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
j3parker marked this conversation as resolved.
Show resolved Hide resolved
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"])
jrbeverly marked this conversation as resolved.
Show resolved Hide resolved

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)
jrbeverly marked this conversation as resolved.
Show resolved Hide resolved

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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this file isn't really used at compile time so it's a tiny bit weird to call it src, but that's fine. This is an internal rule.

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([
jrbeverly marked this conversation as resolved.
Show resolved Hide resolved
"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",
],
jrbeverly marked this conversation as resolved.
Show resolved Hide resolved
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
jrbeverly marked this conversation as resolved.
Show resolved Hide resolved
PointerAlignment: Left

IncludeBlocks: Merge
IncludeCategories:
- Regex: '^\"'
Priority: 1000
- Regex: '^<.*/.*'
Priority: 4000
- Regex: '^<[^/]*>'
Priority: 5000
97 changes: 97 additions & 0 deletions csharp/private/wrappers/dotnet.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#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 and nuget require these environment variables to be set
without them we cannot build/run anything with dotnet.

dotnet: HOME, DOTNET_CLI_HOME, APPDATA, PROGRAMFILES
nuget: TMP, TEMP, USERPROFILE
jrbeverly marked this conversation as resolved.
Show resolved Hide resolved
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: inconsistent comment style in this file

std::vector<std::string> envvars;
envvars.push_back(evprintf("HOME", dotnetDir));
jrbeverly marked this conversation as resolved.
Show resolved Hide resolved
envvars.push_back(evprintf("DOTNET_CLI_HOME", dotnetDir));
envvars.push_back(evprintf("APPDATA", dotnetDir));
envvars.push_back(evprintf("PROGRAMFILES", dotnetDir));
envvars.push_back(evprintf("USERPROFILE", dotnetDir));
envvars.push_back(
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
jrbeverly marked this conversation as resolved.
Show resolved Hide resolved
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] = &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;
}