-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlibjvm_test.cpp
92 lines (81 loc) · 2.77 KB
/
libjvm_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2021 Fabian Meumertzheim
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <rules_jni.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include <cstdlib>
#include <iostream>
#include <string>
#include "hello_from_java.h"
#include "tools/cpp/runfiles/runfiles.h"
#define GREETING_NAME "rules_jni"
void clear_env(const char* name) {
#ifdef _WIN32
SetEnvironmentVariable(name, nullptr);
#else
unsetenv(name);
#endif
}
void set_env(const char* name, const char* value) {
#ifdef _WIN32
_putenv_s(name, value);
#else
setenv(name, value, 1);
#endif
}
int main(int argc, const char** argv) {
// Override PATH with PATH_OVERRIDE, if set.
if (getenv("PATH_OVERRIDE") != nullptr) {
set_env("PATH", getenv("PATH_OVERRIDE"));
}
// Set up the Bazel runfiles library to get the path to the JAR.
using ::bazel::tools::cpp::runfiles::Runfiles;
std::string runfiles_error;
Runfiles* runfiles = Runfiles::CreateForTest(&runfiles_error);
if (runfiles == nullptr) {
runfiles = Runfiles::Create(argv[0], &runfiles_error);
if (runfiles == nullptr) {
std::cerr << "Runfiles::CreateForTest and ::Create failed: "
<< runfiles_error << std::endl;
return EXIT_FAILURE;
}
}
// If we do not want to test finding the Bazel Java toolchain in the runfiles,
// we have to ensure that runfiles cannot be detected when calling into
// libjvm. Else, ensure that they are found.
if (getenv("HERMETIC") != nullptr) {
rules_jni_init(argv[0]);
} else {
clear_env("RUNFILES_MANIFEST_FILE");
clear_env("RUNFILES_DIR");
clear_env("TEST_SRCDIR");
// Let rules_jni_init find the coverage agent even though we intentionally
// cleared all runfiles variables.
set_env("RULES_JNI_COVERAGE_AGENT_JAR",
runfiles
->Rlocation(
"fmeum_rules_jni/jni/private/tools/libjvm_stub/coverage/"
"CoverageAgent_deploy.jar")
.c_str());
rules_jni_init("does_not_exist");
}
std::string greeting = get_java_greeting(*runfiles, GREETING_NAME);
if (greeting.find(GREETING_NAME) == std::string::npos ||
greeting.substr(0, 5) != "Good ") {
std::cerr << "Incorrect greeting: " << greeting << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}