From a1a5f3341f2e3052886fee79cffb5c6fcc80c1bd Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 3 Feb 2023 10:10:37 -0800 Subject: [PATCH] Add a `--build_target` option to singlejar If set, and build stamping is enabled, it is used as the `build.target` property instead of the output path. https://github.com/bazelbuild/bazel/issues/17316 PiperOrigin-RevId: 506932307 Change-Id: I06a710445ab79e98fc3794b7eb81a9abd61dce69 --- src/tools/singlejar/options.cc | 1 + src/tools/singlejar/options.h | 2 ++ src/tools/singlejar/output_jar.cc | 8 +++++--- src/tools/singlejar/output_jar_simple_test.cc | 7 ++++--- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/tools/singlejar/options.cc b/src/tools/singlejar/options.cc index 18d85376d076d0..2728af3f69302b 100644 --- a/src/tools/singlejar/options.cc +++ b/src/tools/singlejar/options.cc @@ -44,6 +44,7 @@ bool Options::ParseToken(ArgTokenStream *tokens) { tokens->MatchAndSet("--classpath_resources", &classpath_resources) || tokens->MatchAndSet("--include_prefixes", &include_prefixes) || tokens->MatchAndSet("--exclude_build_data", &exclude_build_data) || + tokens->MatchAndSet("--build_target", &build_target) || tokens->MatchAndSet("--compression", &force_compression) || tokens->MatchAndSet("--dont_change_compression", &preserve_compression) || tokens->MatchAndSet("--normalize", &normalize_timestamps) || diff --git a/src/tools/singlejar/options.h b/src/tools/singlejar/options.h index 298772deb26bd9..4527310075aeab 100644 --- a/src/tools/singlejar/options.h +++ b/src/tools/singlejar/options.h @@ -25,6 +25,7 @@ class Options { public: Options() : output_jar_creator("singlejar"), + build_target(""), exclude_build_data(false), force_compression(false), normalize_timestamps(false), @@ -44,6 +45,7 @@ class Options { std::string output_jar; std::string output_jar_creator; + std::string build_target; std::string main_class; std::string java_launcher; std::string cds_archive; diff --git a/src/tools/singlejar/output_jar.cc b/src/tools/singlejar/output_jar.cc index 70d445fc1e9225..d6b9785149d6c8 100644 --- a/src/tools/singlejar/output_jar.cc +++ b/src/tools/singlejar/output_jar.cc @@ -101,9 +101,11 @@ int OutputJar::Doit(Options *options) { manifest_.AppendLine("Manifest-Version: 1.0"); manifest_.AppendLine("Created-By: " + options_->output_jar_creator); - // TODO(b/28294322): do we need to resolve the path to be absolute or - // canonical? - build_properties_.AddProperty("build.target", options_->output_jar.c_str()); + // TODO(b/28294322): remove fallback to output_jar + build_properties_.AddProperty("build.target", + !options_->build_target.empty() + ? options_->build_target.c_str() + : options_->output_jar.c_str()); if (options_->verbose) { fprintf(stderr, "combined_file_name=%s\n", options_->output_jar.c_str()); if (!options_->main_class.empty()) { diff --git a/src/tools/singlejar/output_jar_simple_test.cc b/src/tools/singlejar/output_jar_simple_test.cc index 544f13692e1dee..c6b140b6699ec9 100644 --- a/src/tools/singlejar/output_jar_simple_test.cc +++ b/src/tools/singlejar/output_jar_simple_test.cc @@ -92,8 +92,9 @@ class OutputJarSimpleTest : public ::testing::Test { void SetUp() override { runfiles.reset(Runfiles::CreateForTest()); } void CreateOutput(const string &out_path, const std::vector &args) { - const char *option_list[100] = {"--output", out_path.c_str()}; - int nargs = 2; + const char *option_list[100] = {"--output", out_path.c_str(), + "--build_target", "//some/target"}; + int nargs = 4; for (auto &arg : args) { if (arg.empty()) { continue; @@ -215,7 +216,7 @@ TEST_F(OutputJarSimpleTest, Empty) { "\r\n", manifest); string build_properties = GetEntryContents(out_path, "build-data.properties"); - EXPECT_PRED2(HasSubstr, build_properties, "build.target="); + EXPECT_PRED2(HasSubstr, build_properties, "build.target=//some/target"); } // Source jars.