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

Set runfiles env vars in libjvm_stub #57

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Changes from all 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
24 changes: 21 additions & 3 deletions jni/tools/libjvm_stub/bazel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
// limitations under the License.

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#include "jni/tools/libjvm_stub/current_java_runtime.h"
#include "rules_jni_internal.h"
Expand All @@ -25,8 +27,6 @@ using ::bazel::tools::cpp::runfiles::Runfiles;

static const char* rules_jni_arg0 = "";

void rules_jni_init(const char* argv0) { rules_jni_arg0 = argv0; }

static Runfiles* get_runfiles() {
Runfiles* runfiles = Runfiles::CreateForTest();
if (runfiles != nullptr) {
Expand All @@ -35,6 +35,24 @@ static Runfiles* get_runfiles() {
return Runfiles::Create(rules_jni_arg0);
}

void rules_jni_init(const char* argv0) {
rules_jni_arg0 = argv0;
Runfiles* runfiles = get_runfiles();
if (runfiles != nullptr) {
std::vector<std::pair<std::string, std::string>> envvars =
runfiles->EnvVars();
// Setting the runfiles variables for the current process is the only way
// to get them to the Java runfiles library.
for (const std::pair<std::string, std::string>& envvar : envvars) {
#ifdef _WIN32
_putenv_s(envvar.first.c_str(), envvar.second.c_str());
#else
setenv(envvar.first.c_str(), envvar.second.c_str(), 1);
#endif
}
}
}

static bool ends_with(const std::string& str, const std::string& suffix) {
if (str.size() < suffix.size()) {
return false;
Expand Down