Skip to content

Commit 341d7f3

Browse files
cushoncopybara-github
authored andcommitted
Add a --hermetic_java_home= flag to singlejar
PiperOrigin-RevId: 445519501
1 parent db7ff21 commit 341d7f3

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

src/tools/singlejar/options.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ bool Options::ParseToken(ArgTokenStream *tokens) {
5555
&warn_duplicate_resources) ||
5656
tokens->MatchAndSet("--nocompress_suffixes", &nocompress_suffixes) ||
5757
tokens->MatchAndSet("--check_desugar_deps", &check_desugar_deps) ||
58-
tokens->MatchAndSet("--multi_release", &multi_release)) {
58+
tokens->MatchAndSet("--multi_release", &multi_release) ||
59+
tokens->MatchAndSet("--hermetic_java_home", &hermetic_java_home)) {
5960
return true;
6061
} else if (tokens->MatchAndSet("--build_info_file", &optarg)) {
6162
build_info_files.push_back(optarg);

src/tools/singlejar/options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Options {
6565
bool warn_duplicate_resources;
6666
bool check_desugar_deps;
6767
bool multi_release;
68+
std::string hermetic_java_home;
6869

6970
protected:
7071
/*

src/tools/singlejar/options_test.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
#include "googletest/include/gtest/gtest.h"
2121

2222
TEST(OptionsTest, Flags1) {
23-
const char *args[] = {"--exclude_build_data",
24-
"--compression",
25-
"--normalize",
26-
"--no_duplicates",
27-
"--output", "output_jar"};
23+
const char *args[] = {
24+
"--exclude_build_data", "--compression", "--normalize",
25+
"--no_duplicates", "--output", "output_jar",
26+
"--hermetic_java_home", "hermetic_java_home"};
2827
Options options;
2928
options.ParseCommandLine(arraysize(args), args);
3029

@@ -38,6 +37,7 @@ TEST(OptionsTest, Flags1) {
3837
EXPECT_FALSE(options.check_desugar_deps);
3938
EXPECT_FALSE(options.multi_release);
4039
EXPECT_EQ("output_jar", options.output_jar);
40+
EXPECT_EQ("hermetic_java_home", options.hermetic_java_home);
4141
}
4242

4343
TEST(OptionsTest, Flags2) {
@@ -60,6 +60,7 @@ TEST(OptionsTest, Flags2) {
6060
ASSERT_TRUE(options.warn_duplicate_resources);
6161
ASSERT_TRUE(options.check_desugar_deps);
6262
ASSERT_TRUE(options.multi_release);
63+
EXPECT_EQ("", options.hermetic_java_home);
6364
}
6465

6566
TEST(OptionsTest, SingleOptargs) {

src/tools/singlejar/output_jar.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ int OutputJar::Doit(Options *options) {
145145
if (options_->multi_release) {
146146
manifest_.EnableMultiRelease();
147147
}
148+
if (!options_->hermetic_java_home.empty()) {
149+
manifest_.AppendLine("Hermetic-Java-Home: " + options_->hermetic_java_home);
150+
}
148151
for (auto &manifest_line : options_->manifest_lines) {
149152
if (!manifest_line.empty()) {
150153
manifest_.AppendLine(manifest_line);

src/tools/singlejar/output_jar_simple_test.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,4 +989,17 @@ TEST_F(OutputJarSimpleTest, MultiReleaseManifestLines) {
989989
manifest);
990990
}
991991

992+
// --hermetic_java_home
993+
TEST_F(OutputJarSimpleTest, HermeticJavaHome) {
994+
string out_path = OutputFilePath("out.jar");
995+
CreateOutput(out_path, {"--hermetic_java_home", "foo/bar/java_home"});
996+
string manifest = GetEntryContents(out_path, "META-INF/MANIFEST.MF");
997+
EXPECT_EQ(
998+
"Manifest-Version: 1.0\r\n"
999+
"Created-By: singlejar\r\n"
1000+
"Hermetic-Java-Home: foo/bar/java_home\r\n"
1001+
"\r\n",
1002+
manifest);
1003+
}
1004+
9921005
} // namespace

0 commit comments

Comments
 (0)