Rename Options to JavaOptions
The aidl command line interface exposes a lot of knobs that
are not used in practice and complicate the internal logic to support.
For C++ generation we have an opportunity to reduce the exposed surface.
Begin by renaming Options to a more specific JavaOptions.
Test: unittests pass
Bug: 23599341
Change-Id: I76478ab2fb12612bf7120a1af28c8db1da9ed22f
diff --git a/Android.mk b/Android.mk
index 2dc5acd..f4dd07a 100644
--- a/Android.mk
+++ b/Android.mk
@@ -51,7 +51,7 @@
LOCAL_MODULE_HOST_OS := darwin linux windows
LOCAL_CFLAGS := -Wall -Werror
LOCAL_C_INCLUDES := external/gtest/include
-LOCAL_SRC_FILES := main.cpp
+LOCAL_SRC_FILES := main_java.cpp
LOCAL_STATIC_LIBRARIES := libaidl-common $(aidl_static_libraries)
include $(BUILD_HOST_EXECUTABLE)
diff --git a/aidl.cpp b/aidl.cpp
index acc09bd..8f893bf 100644
--- a/aidl.cpp
+++ b/aidl.cpp
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2015, The Android Open Source Project
+ *
+ * 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 "aidl_language.h"
#include "options.h"
@@ -544,7 +559,7 @@
// ==========================================================
static int
-exactly_one_interface(const char* filename, const document_item_type* items, const Options& options,
+exactly_one_interface(const char* filename, const document_item_type* items, const JavaOptions& options,
bool* onlyParcelable)
{
if (items == NULL) {
@@ -585,7 +600,7 @@
// ==========================================================
void
-generate_dep_file(const Options& options, const document_item_type* items)
+generate_dep_file(const JavaOptions& options, const document_item_type* items)
{
/* we open the file in binary mode to ensure that the same output is
* generated on all platforms !!
@@ -647,7 +662,7 @@
// ==========================================================
static string
-generate_outputFileName2(const Options& options, const buffer_type& name, const char* package)
+generate_outputFileName2(const JavaOptions& options, const buffer_type& name, const char* package)
{
string result;
@@ -679,7 +694,7 @@
// ==========================================================
static string
-generate_outputFileName(const Options& options, const document_item_type* items)
+generate_outputFileName(const JavaOptions& options, const document_item_type* items)
{
// items has already been checked to have only one interface.
if (items->item_type == INTERFACE_TYPE_BINDER) {
@@ -876,7 +891,7 @@
// ==========================================================
int
-compile_aidl(const Options& options)
+compile_aidl(const JavaOptions& options)
{
int err = 0, N;
@@ -1006,7 +1021,7 @@
}
int
-preprocess_aidl(const Options& options)
+preprocess_aidl(const JavaOptions& options)
{
vector<string> lines;
int err;
diff --git a/aidl.h b/aidl.h
index f830e3b..5700a0c 100644
--- a/aidl.h
+++ b/aidl.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2015, The Android Open Source Project
+ *
+ * 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 AIDL_AIDL_H_
#define AIDL_AIDL_H_
@@ -13,8 +29,8 @@
int convert_direction(const char* direction);
-int compile_aidl(const Options& options);
-int preprocess_aidl(const Options& options);
+int compile_aidl(const JavaOptions& options);
+int preprocess_aidl(const JavaOptions& options);
} // namespace android
} // namespace aidl
diff --git a/main.cpp b/main_java.cpp
similarity index 84%
rename from main.cpp
rename to main_java.cpp
index f4c1911..7c90145 100644
--- a/main.cpp
+++ b/main_java.cpp
@@ -21,20 +21,20 @@
#include "logging.h"
#include "options.h"
-using android::aidl::Options;
+using android::aidl::JavaOptions;
int main(int argc, char** argv) {
android::base::InitLogging(argv);
LOG(DEBUG) << "aidl starting";
- std::unique_ptr<Options> options = Options::ParseOptions(argc, argv);
+ std::unique_ptr<JavaOptions> options = JavaOptions::Parse(argc, argv);
if (!options) {
return 1;
}
switch (options->task) {
- case Options::COMPILE_AIDL_TO_JAVA:
+ case JavaOptions::COMPILE_AIDL_TO_JAVA:
return android::aidl::compile_aidl(*options);
- case Options::PREPROCESS_AIDL:
+ case JavaOptions::PREPROCESS_AIDL:
return android::aidl::preprocess_aidl(*options);
}
std::cerr << "aidl: internal error" << std::endl;
diff --git a/options.cpp b/options.cpp
index d5aa21d..26a29d2 100644
--- a/options.cpp
+++ b/options.cpp
@@ -25,7 +25,7 @@
namespace aidl {
namespace {
-unique_ptr<Options> usage() {
+unique_ptr<JavaOptions> java_usage() {
fprintf(stderr,
"usage: aidl OPTIONS INPUT [OUTPUT]\n"
" aidl --preprocess OUTPUT INPUT...\n"
@@ -48,18 +48,18 @@
"used, with the .aidl extension changed to a .java extension.\n"
" If the -o option is used, the generated files will be placed in "
"the base output folder, under their package folder\n");
- return unique_ptr<Options>(nullptr);
+ return unique_ptr<JavaOptions>(nullptr);
}
} // namespace
-unique_ptr<Options> Options::ParseOptions(int argc, const char* const* argv) {
- unique_ptr<Options> options(new Options());
+unique_ptr<JavaOptions> JavaOptions::Parse(int argc, const char* const* argv) {
+ unique_ptr<JavaOptions> options(new JavaOptions());
int i = 1;
if (argc >= 2 && 0 == strcmp(argv[1], "--preprocess")) {
if (argc < 4) {
- return usage();
+ return java_usage();
}
options->output_file_name_ = argv[2];
for (int i = 3; i < argc; i++) {
@@ -79,7 +79,7 @@
}
if (len <= 1) {
fprintf(stderr, "unknown option (%d): %s\n", i, s);
- return usage();
+ return java_usage();
}
// -I<system-import-path>
if (s[1] == 'I') {
@@ -87,14 +87,14 @@
options->import_paths_.push_back(s + 2);
} else {
fprintf(stderr, "-I option (%d) requires a path.\n", i);
- return usage();
+ return java_usage();
}
} else if (s[1] == 'd') {
if (len > 2) {
options->dep_file_name_ = s + 2;
} else {
fprintf(stderr, "-d option (%d) requires a file.\n", i);
- return usage();
+ return java_usage();
}
} else if (strcmp(s, "-a") == 0) {
options->auto_dep_file_ = true;
@@ -103,32 +103,31 @@
options->preprocessed_files_.push_back(s + 2);
} else {
fprintf(stderr, "-p option (%d) requires a file.\n", i);
- return usage();
+ return java_usage();
}
} else if (s[1] == 'o') {
if (len > 2) {
options->output_base_folder_= s + 2;
} else {
fprintf(stderr, "-o option (%d) requires a path.\n", i);
- return usage();
+ return java_usage();
}
} else if (strcmp(s, "-b") == 0) {
options->fail_on_parcelable_ = true;
} else {
// s[1] is not known
fprintf(stderr, "unknown option (%d): %s\n", i, s);
- return usage();
+ return java_usage();
}
i++;
}
-
// INPUT
if (i < argc) {
options->input_file_name_ = argv[i];
i++;
} else {
fprintf(stderr, "INPUT required\n");
- return usage();
+ return java_usage();
}
// OUTPUT
@@ -144,7 +143,7 @@
options->output_file_name_.replace(pos, suffix_len, ".java");
} else {
fprintf(stderr, "INPUT is not an .aidl file.\n");
- return usage();
+ return java_usage();
}
}
@@ -156,7 +155,7 @@
fprintf(stderr, " %s", argv[i]);
}
fprintf(stderr, "\n");
- return usage();
+ return java_usage();
}
return options;
diff --git a/options.h b/options.h
index c8ac8a8..0763ff5 100644
--- a/options.h
+++ b/options.h
@@ -27,21 +27,20 @@
namespace android {
namespace aidl {
-// This struct is the parsed version of the command line options
-class Options {
+// This object represents the parsed options to the Java generating aidl.
+class JavaOptions final {
public:
enum {
COMPILE_AIDL_TO_JAVA,
PREPROCESS_AIDL,
};
- ~Options() = default;
+ ~JavaOptions() = default;
- // Takes the inputs from the command line and returns a pointer to an
- // Options object on success.
- // Also prints the usage statement on failure.
- static std::unique_ptr<Options> ParseOptions(int argc,
- const char* const* argv);
+ // Parses the command line and returns a non-null pointer to an JavaOptions
+ // object on success.
+ // Prints the usage statement on failure.
+ static std::unique_ptr<JavaOptions> Parse(int argc, const char* const* argv);
int task{COMPILE_AIDL_TO_JAVA};
bool fail_on_parcelable_{false};
@@ -55,10 +54,10 @@
std::vector<std::string> files_to_preprocess_;
private:
- Options() = default;
+ JavaOptions() = default;
FRIEND_TEST(EndToEndTest, IExampleInterface);
- DISALLOW_COPY_AND_ASSIGN(Options);
+ DISALLOW_COPY_AND_ASSIGN(JavaOptions);
};
} // namespace android
diff --git a/options_unittest.cpp b/options_unittest.cpp
index 31c4ced..0c5102d 100644
--- a/options_unittest.cpp
+++ b/options_unittest.cpp
@@ -56,11 +56,12 @@
};
const char kCompileCommandOutput[] = "input.java";
-unique_ptr<Options> GetOptions(const char* command[]) {
+template <typename T>
+unique_ptr<T> GetOptions(const char* command[]) {
int argc = 0;
const char** command_part = command;
for (; *command_part; ++argc, ++command_part) {}
- unique_ptr<Options> options(Options::ParseOptions(argc, command));
+ unique_ptr<T> options(T::Parse(argc, command));
if (!options) {
cerr << "Failed to parse command line:";
for (int i = 0; i < argc; ++i) {
@@ -74,9 +75,9 @@
} // namespace
-TEST(OptionsTests, ParsesPreprocess) {
- unique_ptr<Options> options = GetOptions(kPreprocessCommand);
- EXPECT_EQ(Options::PREPROCESS_AIDL, options->task);
+TEST(JavaOptionsTests, ParsesPreprocess) {
+ unique_ptr<JavaOptions> options = GetOptions<JavaOptions>(kPreprocessCommand);
+ EXPECT_EQ(JavaOptions::PREPROCESS_AIDL, options->task);
EXPECT_EQ(false, options->fail_on_parcelable_);
EXPECT_EQ(0u, options->import_paths_.size());
EXPECT_EQ(0u, options->preprocessed_files_.size());
@@ -89,9 +90,10 @@
EXPECT_EQ(expected_input, options->files_to_preprocess_);
}
-TEST(OptionsTests, ParsesCompileJava) {
- unique_ptr<Options> options = GetOptions(kCompileJavaCommand);
- EXPECT_EQ(Options::COMPILE_AIDL_TO_JAVA, options->task);
+TEST(JavaOptionsTests, ParsesCompileJava) {
+ unique_ptr<JavaOptions> options =
+ GetOptions<JavaOptions>(kCompileJavaCommand);
+ EXPECT_EQ(JavaOptions::COMPILE_AIDL_TO_JAVA, options->task);
EXPECT_EQ(true, options->fail_on_parcelable_);
EXPECT_EQ(1u, options->import_paths_.size());
EXPECT_EQ(0u, options->preprocessed_files_.size());
diff --git a/tests/end_to_end_tests.cpp b/tests/end_to_end_tests.cpp
index 12e73d0..8eb721a 100644
--- a/tests/end_to_end_tests.cpp
+++ b/tests/end_to_end_tests.cpp
@@ -150,7 +150,7 @@
};
TEST_F(EndToEndTest, IExampleInterface) {
- Options options;
+ JavaOptions options;
options.fail_on_parcelable_ = true;
options.import_paths_.push_back(inputDir_.value());
options.input_file_name_ =