aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2017-08-03 10:50:29 +0200
committerGravatar Dmitry Lomov <dslomov@google.com>2017-08-03 12:10:09 +0200
commit837e1b3d4859140d29aaa6bbab8fbb008e6d701e (patch)
treea4e26afbd16151ae314010ca962877484ee11cf7
parentd4fa181f8607c35230b7efa1ce94188b51508962 (diff)
Windows, sh_bin. launcher: export runfiles envvars
Fix the exe launcher of sh_binary rules to export the RUNFILES_MANIFEST_ONLY and RUNFILES_MANIFEST_FILE environment variables. Fixes https://github.com/bazelbuild/bazel/issues/3492 Change-Id: I8507565f44c8b59f8218570306375cc083a41e03 PiperOrigin-RevId: 164095286
-rw-r--r--src/test/py/bazel/launcher_test.py13
-rw-r--r--src/tools/launcher/bash_launcher.cc2
-rw-r--r--src/tools/launcher/launcher.cc17
-rw-r--r--src/tools/launcher/launcher.h7
-rw-r--r--src/tools/launcher/util/launcher_util.cc4
-rw-r--r--src/tools/launcher/util/launcher_util.h2
-rw-r--r--src/tools/launcher/util/launcher_util_test.cc4
7 files changed, 34 insertions, 15 deletions
diff --git a/src/test/py/bazel/launcher_test.py b/src/test/py/bazel/launcher_test.py
index a093b26868..1baf5d29c6 100644
--- a/src/test/py/bazel/launcher_test.py
+++ b/src/test/py/bazel/launcher_test.py
@@ -112,6 +112,8 @@ class LauncherTest(test_base.TestBase):
foo_sh = self.ScratchFile('foo/foo.sh', [
'#!/bin/bash',
'echo hello shell',
+ 'echo runfiles_manifest_only=${RUNFILES_MANIFEST_ONLY:-}',
+ 'echo runfiles_manifest_file=${RUNFILES_MANIFEST_FILE:-}',
])
foo_cmd = self.ScratchFile('foo/foo.cmd', ['@echo hello batch'])
self.ScratchFile('bar/BUILD', ['exports_files(["bar.txt"])'])
@@ -182,7 +184,18 @@ class LauncherTest(test_base.TestBase):
exit_code, stdout, stderr = self.RunProgram([bin1])
self.AssertExitCode(exit_code, 0, stderr)
+ self.assertEqual(len(stdout), 3)
self.assertEqual(stdout[0], 'hello shell')
+ if self.IsWindows():
+ self.assertEqual(stdout[1], 'runfiles_manifest_only=1')
+ self.assertRegexpMatches(stdout[2], r'^runfiles_manifest_file.*MANIFEST$')
+ else:
+ # TODO(laszlocsomor): Find out whether the runfiles-related envvars should
+ # be set on Linux (e.g. $RUNFILES, $RUNFILES_MANIFEST_FILE). Currently
+ # they aren't, and that may be a bug. If it's indeed a bug, fix that bug
+ # and update this test.
+ self.assertEqual(stdout[1], 'runfiles_manifest_only=')
+ self.assertEqual(stdout[2], 'runfiles_manifest_file=')
if self.IsWindows():
exit_code, stdout, stderr = self.RunProgram([bin2])
diff --git a/src/tools/launcher/bash_launcher.cc b/src/tools/launcher/bash_launcher.cc
index e1203a3f58..af0047d45a 100644
--- a/src/tools/launcher/bash_launcher.cc
+++ b/src/tools/launcher/bash_launcher.cc
@@ -30,7 +30,7 @@ ExitCode BashBinaryLauncher::Launch() {
string bash_binary = this->GetLaunchInfoByKey(BASH_BIN_PATH);
// If specified bash binary path doesn't exist, then fall back to
// bash.exe and hope it's in PATH.
- if (!DoesFilePathExist(bash_binary)) {
+ if (!DoesFilePathExist(bash_binary.c_str())) {
bash_binary = "bash.exe";
}
diff --git a/src/tools/launcher/launcher.cc b/src/tools/launcher/launcher.cc
index 86e9d8d473..d3a023eb8b 100644
--- a/src/tools/launcher/launcher.cc
+++ b/src/tools/launcher/launcher.cc
@@ -34,27 +34,28 @@ using std::vector;
BinaryLauncherBase::BinaryLauncherBase(
const LaunchDataParser::LaunchInfo& _launch_info, int argc, char* argv[])
- : launch_info(_launch_info) {
- this->workspace_name = GetLaunchInfoByKey(WORKSPACE_NAME);
+ : launch_info(_launch_info),
+ manifest_file(FindManifestFile(argv[0])),
+ workspace_name(GetLaunchInfoByKey(WORKSPACE_NAME)) {
for (int i = 0; i < argc; i++) {
this->commandline_arguments.push_back(argv[i]);
}
- ParseManifestFile(&this->manifest_file_map, FindManifestFile());
+ ParseManifestFile(&this->manifest_file_map, this->manifest_file);
}
-string BinaryLauncherBase::FindManifestFile() const {
+string BinaryLauncherBase::FindManifestFile(const char* argv0) {
// Get the name of the binary
- string binary = GetBinaryPathWithoutExtension(this->commandline_arguments[0]);
+ string binary = GetBinaryPathWithoutExtension(argv0);
// Try to find <path to binary>.runfiles/MANIFEST
string manifest_file = binary + ".runfiles\\MANIFEST";
- if (DoesFilePathExist(manifest_file)) {
+ if (DoesFilePathExist(manifest_file.c_str())) {
return manifest_file;
}
// Also try to check if <path to binary>.runfiles_manifest exists
manifest_file = binary + ".runfiles_manifest";
- if (DoesFilePathExist(manifest_file)) {
+ if (DoesFilePathExist(manifest_file.c_str())) {
return manifest_file;
}
@@ -125,6 +126,8 @@ void BinaryLauncherBase::CreateCommandLine(
ExitCode BinaryLauncherBase::LaunchProcess(
const string& executable, const vector<string>& arguments) const {
+ SetEnvironmentVariableA("RUNFILES_MANIFEST_ONLY", "1");
+ SetEnvironmentVariableA("RUNFILES_MANIFEST_FILE", manifest_file.c_str());
CmdLine cmdline;
CreateCommandLine(&cmdline, executable, arguments);
PROCESS_INFORMATION processInfo = {0};
diff --git a/src/tools/launcher/launcher.h b/src/tools/launcher/launcher.h
index e9f461b324..3ba8ca4723 100644
--- a/src/tools/launcher/launcher.h
+++ b/src/tools/launcher/launcher.h
@@ -66,12 +66,15 @@ class BinaryLauncherBase {
// A map to store all the launch information.
const LaunchDataParser::LaunchInfo& launch_info;
+ // Absolute path to the runfiles manifest file.
+ const std::string manifest_file;
+
// The commandline arguments recieved.
// The first argument is the path of this launcher itself.
std::vector<std::string> commandline_arguments;
// The workspace name of the repository this target belongs to.
- std::string workspace_name;
+ const std::string workspace_name;
// A map to store all entries of the manifest file.
std::unordered_map<std::string, std::string> manifest_file_map;
@@ -89,7 +92,7 @@ class BinaryLauncherBase {
// Expect the manifest file to be at
// 1. <path>/<to>/<binary>/<target_name>.runfiles/MANIFEST
// or 2. <path>/<to>/<binary>/<target_name>.runfiles_manifest
- std::string FindManifestFile() const;
+ static std::string FindManifestFile(const char* argv0);
// Parse manifest file into a map
static void ParseManifestFile(ManifestFileMap* manifest_file_map,
diff --git a/src/tools/launcher/util/launcher_util.cc b/src/tools/launcher/util/launcher_util.cc
index 4ef64eaa70..610516bcc3 100644
--- a/src/tools/launcher/util/launcher_util.cc
+++ b/src/tools/launcher/util/launcher_util.cc
@@ -67,8 +67,8 @@ void PrintError(const char* format, ...) {
fputc('\n', stderr);
}
-bool DoesFilePathExist(const string& path) {
- DWORD dwAttrib = GetFileAttributes(path.c_str());
+bool DoesFilePathExist(const char* path) {
+ DWORD dwAttrib = GetFileAttributes(path);
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
diff --git a/src/tools/launcher/util/launcher_util.h b/src/tools/launcher/util/launcher_util.h
index 10f5f53000..669ea1dbfc 100644
--- a/src/tools/launcher/util/launcher_util.h
+++ b/src/tools/launcher/util/launcher_util.h
@@ -48,7 +48,7 @@ std::string GetBinaryPathWithExtension(const std::string& binary);
std::string GetEscapedArgument(const std::string& argument);
// Check if a file exists at a given path.
-bool DoesFilePathExist(const std::string& path);
+bool DoesFilePathExist(const char* path);
} // namespace launcher
} // namespace bazel
diff --git a/src/tools/launcher/util/launcher_util_test.cc b/src/tools/launcher/util/launcher_util_test.cc
index 2e528a88a3..b895b65c9c 100644
--- a/src/tools/launcher/util/launcher_util_test.cc
+++ b/src/tools/launcher/util/launcher_util_test.cc
@@ -86,8 +86,8 @@ TEST_F(LaunchUtilTest, DoesFilePathExistTest) {
string file1 = GetTmpDir() + "/foo";
string file2 = GetTmpDir() + "/bar";
CreateEmptyFile(file1);
- ASSERT_TRUE(DoesFilePathExist(file1));
- ASSERT_FALSE(DoesFilePathExist(file2));
+ ASSERT_TRUE(DoesFilePathExist(file1.c_str()));
+ ASSERT_FALSE(DoesFilePathExist(file2.c_str()));
}
} // namespace launcher