From d3b0ede8c87f257451f9dfa8d9f0ea73798d6dc0 Mon Sep 17 00:00:00 2001 From: Sasha Smundak Date: Wed, 20 Jul 2016 09:11:34 +0000 Subject: Command line options processing. -- MOS_MIGRATED_REVID=127924233 --- src/tools/singlejar/BUILD | 23 ++++++ src/tools/singlejar/options.cc | 61 ++++++++++++++++ src/tools/singlejar/options.h | 55 ++++++++++++++ src/tools/singlejar/options_test.cc | 122 +++++++++++++++++++++++++++++++ src/tools/singlejar/token_stream_test.cc | 4 +- 5 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 src/tools/singlejar/options.cc create mode 100644 src/tools/singlejar/options.h create mode 100644 src/tools/singlejar/options_test.cc (limited to 'src/tools/singlejar') diff --git a/src/tools/singlejar/BUILD b/src/tools/singlejar/BUILD index de0e0d65c5..a2c8855864 100644 --- a/src/tools/singlejar/BUILD +++ b/src/tools/singlejar/BUILD @@ -47,6 +47,19 @@ cc_test( deps = ["//third_party:gtest"], ) +cc_test( + name = "options_test", + srcs = [ + "options.h", + "options_test.cc", + ":token_stream", + ], + deps = [ + ":options", + "//third_party:gtest", + ], +) + cc_test( name = "token_stream_test", srcs = [ @@ -84,6 +97,16 @@ cc_test( ], ) +cc_library( + name = "options", + srcs = [ + "diag.h", + "options.cc", + "options.h", + ":token_stream", + ], +) + filegroup( name = "input_jar", srcs = [ diff --git a/src/tools/singlejar/options.cc b/src/tools/singlejar/options.cc new file mode 100644 index 0000000000..2e33151ba8 --- /dev/null +++ b/src/tools/singlejar/options.cc @@ -0,0 +1,61 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// 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 "src/tools/singlejar/options.h" + +#include "src/tools/singlejar/diag.h" +#include "src/tools/singlejar/token_stream.h" + +void Options::ParseCommandLine(int argc, const char *argv[]) { + ArgTokenStream tokens(argc, argv); + std::string optarg; + while (!tokens.AtEnd()) { + if (tokens.MatchAndSet("--output", &output_jar) || + tokens.MatchAndSet("--main_class", &main_class) || + tokens.MatchAndSet("--java_launcher", &java_launcher) || + tokens.MatchAndSet("--deploy_manifest_lines", &manifest_lines) || + tokens.MatchAndSet("--sources", &input_jars) || + tokens.MatchAndSet("--resources", &resources) || + tokens.MatchAndSet("--classpath_resources", &classpath_resources) || + tokens.MatchAndSet("--include_prefixes", &include_prefixes) || + tokens.MatchAndSet("--exclude_build_data", &exclude_build_data) || + tokens.MatchAndSet("--compression", &force_compression) || + tokens.MatchAndSet("--dont_change_compression", + &preserve_compression) || + tokens.MatchAndSet("--normalize", &normalize_timestamps) || + tokens.MatchAndSet("--no_duplicates", &no_duplicates) || + tokens.MatchAndSet("--verbose", &verbose) || + tokens.MatchAndSet("--warn_duplicate_resources", + &warn_duplicate_resources)) { + continue; + } else if (tokens.MatchAndSet("--build_info_file", &optarg)) { + build_info_files.push_back(optarg); + continue; + } else if (tokens.MatchAndSet("--extra_build_info", &optarg)) { + build_info_lines.push_back(optarg); + continue; + } else { + diag_errx(1, "Bad command line argument %s", tokens.token().c_str()); + } + } + + if (output_jar.empty()) { + diag_errx(1, "Use --output to specify the output file name"); + } + if (force_compression && preserve_compression) { + diag_errx( + 1, + "--compression and --dont_change_compression are mutually exclusive"); + } +} diff --git a/src/tools/singlejar/options.h b/src/tools/singlejar/options.h new file mode 100644 index 0000000000..74de3a4790 --- /dev/null +++ b/src/tools/singlejar/options.h @@ -0,0 +1,55 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// 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. + +#ifndef THIRD_PARTY_BAZEL_SRC_TOOLS_SINGLEJAR_OPTIONS_H_ +#define THIRD_PARTY_BAZEL_SRC_TOOLS_SINGLEJAR_OPTIONS_H_ + +#include +#include + +/* Command line options. */ +class Options { + public: + Options() + : exclude_build_data(false), + force_compression(false), + normalize_timestamps(false), + no_duplicates(false), + preserve_compression(false), + verbose(false), + warn_duplicate_resources(false) {} + + // Parses command line arguments into the fields of this instance. + void ParseCommandLine(int argc, const char *argv[]); + + std::string output_jar; + std::string main_class; + std::string java_launcher; + std::vector manifest_lines; + std::vector input_jars; + std::vector resources; + std::vector classpath_resources; + std::vector build_info_files; + std::vector build_info_lines; + std::vector include_prefixes; + bool exclude_build_data; + bool force_compression; + bool normalize_timestamps; + bool no_duplicates; + bool preserve_compression; + bool verbose; + bool warn_duplicate_resources; +}; + +#endif // THIRD_PARTY_BAZEL_SRC_TOOLS_SINGLEJAR_OPTIONS_H_ diff --git a/src/tools/singlejar/options_test.cc b/src/tools/singlejar/options_test.cc new file mode 100644 index 0000000000..9744d6c7d3 --- /dev/null +++ b/src/tools/singlejar/options_test.cc @@ -0,0 +1,122 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// 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 + +#include "src/tools/singlejar/options.h" + +#include "gtest/gtest.h" + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +class OptionsTest : public testing::Test { + protected: + void SetUp() override { options_.reset(new Options); } + std::unique_ptr options_; +}; + +TEST_F(OptionsTest, Flags1) { + const char *args[] = {"--exclude_build_data", + "--compression", + "--normalize", + "--no_duplicates", + "--output", "output_jar"}; + options_->ParseCommandLine(ARRAY_SIZE(args), args); + + EXPECT_TRUE(options_->exclude_build_data); + EXPECT_TRUE(options_->force_compression); + EXPECT_TRUE(options_->normalize_timestamps); + EXPECT_TRUE(options_->no_duplicates); + EXPECT_FALSE(options_->preserve_compression); + EXPECT_FALSE(options_->verbose); + EXPECT_FALSE(options_->warn_duplicate_resources); + EXPECT_EQ("output_jar", options_->output_jar); +} + +TEST_F(OptionsTest, Flags2) { + const char *args[] = {"--dont_change_compression", + "--verbose", + "--warn_duplicate_resources", + "--output", "output_jar"}; + options_->ParseCommandLine(ARRAY_SIZE(args), args); + + ASSERT_FALSE(options_->exclude_build_data); + ASSERT_FALSE(options_->force_compression); + ASSERT_FALSE(options_->normalize_timestamps); + ASSERT_FALSE(options_->no_duplicates); + ASSERT_TRUE(options_->preserve_compression); + ASSERT_TRUE(options_->verbose); + ASSERT_TRUE(options_->warn_duplicate_resources); +} + +TEST_F(OptionsTest, SingleOptargs) { + const char *args[] = {"--output", "output_jar", + "--main_class", "com.google.Main", + "--java_launcher", "//tools:mylauncher", + "--build_info_file", "build_file1", + "--extra_build_info", "extra_build_line1", + "--build_info_file", "build_file2", + "--extra_build_info", "extra_build_line2"}; + options_->ParseCommandLine(ARRAY_SIZE(args), args); + + EXPECT_EQ("output_jar", options_->output_jar); + EXPECT_EQ("com.google.Main", options_->main_class); + EXPECT_EQ("//tools:mylauncher", options_->java_launcher); + ASSERT_EQ(2, options_->build_info_files.size()); + EXPECT_EQ("build_file1", options_->build_info_files[0]); + EXPECT_EQ("build_file2", options_->build_info_files[1]); + ASSERT_EQ(2, options_->build_info_lines.size()); + EXPECT_EQ("extra_build_line1", options_->build_info_lines[0]); + EXPECT_EQ("extra_build_line2", options_->build_info_lines[1]); +} + +TEST_F(OptionsTest, MultiOptargs) { + const char *args[] = {"--output", "output_file", + "--sources", "jar1", "jar2", + "--resources", "res1", "res2", + "--classpath_resources", "cpres1", "cpres2", + "--sources", "jar3", + "--include_prefixes", "prefix1", "prefix2"}; + options_->ParseCommandLine(ARRAY_SIZE(args), args); + + ASSERT_EQ(3, options_->input_jars.size()); + EXPECT_EQ("jar1", options_->input_jars[0]); + EXPECT_EQ("jar2", options_->input_jars[1]); + EXPECT_EQ("jar3", options_->input_jars[2]); + ASSERT_EQ(2, options_->resources.size()); + EXPECT_EQ("res1", options_->resources[0]); + EXPECT_EQ("res2", options_->resources[1]); + ASSERT_EQ(2, options_->classpath_resources.size()); + EXPECT_EQ("cpres1", options_->classpath_resources[0]); + EXPECT_EQ("cpres2", options_->classpath_resources[1]); + ASSERT_EQ(2, options_->include_prefixes.size()); + EXPECT_EQ("prefix1", options_->include_prefixes[0]); + EXPECT_EQ("prefix2", options_->include_prefixes[1]); +} + +TEST_F(OptionsTest, EmptyMultiOptargs) { + const char *args[] = {"--output", "output_file", + "--sources", + "--resources", + "--classpath_resources", + "--sources", + "--include_prefixes", "prefix1", + "--resources"}; + options_->ParseCommandLine(ARRAY_SIZE(args), args); + + EXPECT_EQ(0, options_->input_jars.size()); + EXPECT_EQ(0, options_->resources.size()); + EXPECT_EQ(0, options_->classpath_resources.size()); + EXPECT_EQ(1, options_->include_prefixes.size()); +} diff --git a/src/tools/singlejar/token_stream_test.cc b/src/tools/singlejar/token_stream_test.cc index f4b2a4c8cd..8a0742ce6f 100644 --- a/src/tools/singlejar/token_stream_test.cc +++ b/src/tools/singlejar/token_stream_test.cc @@ -59,7 +59,7 @@ TEST_F(TokenStreamTest, CommandFile) { std::string command_file_path_ = std::string(tempdir) + "/tokens"; FILE *fp = fopen(command_file_path_.c_str(), "w"); ASSERT_NE(nullptr, fp); - for (int i = 0; i < ARRAY_SIZE(lines); ++i) { + for (size_t i = 0; i < ARRAY_SIZE(lines); ++i) { fprintf(fp, "%s\n", lines[i]); } fclose(fp); @@ -71,7 +71,7 @@ TEST_F(TokenStreamTest, CommandFile) { bool flag = false; ASSERT_TRUE(token_stream.MatchAndSet("-before_file", &flag)); EXPECT_TRUE(flag); - for (int i = 0; i < ARRAY_SIZE(expected_tokens); ++i) { + for (size_t i = 0; i < ARRAY_SIZE(expected_tokens); ++i) { flag = false; ASSERT_TRUE(token_stream.MatchAndSet(expected_tokens[i], &flag)); EXPECT_TRUE(flag); -- cgit v1.2.3