blob: 27cfb9d5f4285b9275383301beb89525c88b6043 [file] [log] [blame]
Christopher Wiley3616d132015-09-01 11:07:48 -07001/*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jiyong Park74595c12018-07-23 15:22:50 +090017#include "options.h"
18
Christopher Wileyd93c5b72015-09-14 13:21:37 -070019#include <iostream>
Jiyong Park74595c12018-07-23 15:22:50 +090020#include <memory>
Christopher Wiley3616d132015-09-01 11:07:48 -070021#include <string>
22#include <vector>
23
24#include <gtest/gtest.h>
25
Christopher Wileyd93c5b72015-09-14 13:21:37 -070026using std::cerr;
27using std::endl;
Christopher Wiley3616d132015-09-01 11:07:48 -070028using std::string;
Christopher Wiley4427d862015-09-14 11:07:39 -070029using std::unique_ptr;
30using std::vector;
31
32namespace android {
33namespace aidl {
34namespace {
Christopher Wiley3616d132015-09-01 11:07:48 -070035
36const char kPreprocessCommandOutputFile[] = "output_file_name";
Jiyong Park74595c12018-07-23 15:22:50 +090037const char kPreprocessCommandInput1[] = "input1.aidl";
38const char kPreprocessCommandInput2[] = "input2.aidl";
39const char kPreprocessCommandInput3[] = "input3.aidl";
Christopher Wiley3616d132015-09-01 11:07:48 -070040const char* kPreprocessCommand[] = {
41 "aidl", "--preprocess",
42 kPreprocessCommandOutputFile,
43 kPreprocessCommandInput1,
44 kPreprocessCommandInput2,
45 kPreprocessCommandInput3,
Christopher Wileyd93c5b72015-09-14 13:21:37 -070046 nullptr,
Christopher Wiley3616d132015-09-01 11:07:48 -070047};
48
Christopher Wiley4432ccf2015-09-18 18:32:08 -070049const char kCompileCommandInput[] = "directory/ITool.aidl";
Christopher Wileyd93c5b72015-09-14 13:21:37 -070050const char kCompileCommandIncludePath[] = "-Iinclude_path";
51const char* kCompileJavaCommand[] = {
52 "aidl",
53 "-b",
54 kCompileCommandIncludePath,
55 kCompileCommandInput,
56 nullptr,
57};
Christopher Wiley4432ccf2015-09-18 18:32:08 -070058const char kCompileCommandJavaOutput[] = "directory/ITool.java";
Christopher Wileyd93c5b72015-09-14 13:21:37 -070059
Steven Morelandb436cb72018-07-06 11:33:47 -070060const char kCompileDepFileNinja[] = "--ninja";
Dan Willemsen93298ee2016-11-10 23:55:55 -080061const char* kCompileJavaCommandNinja[] = {
62 "aidl",
63 "-b",
64 kCompileDepFileNinja,
65 kCompileCommandIncludePath,
66 kCompileCommandInput,
67 nullptr,
68};
69
Christopher Wileya590de82015-09-15 15:46:28 -070070const char kCompileDepFile[] = "-doutput.deps";
Jiyong Park05463732018-08-09 16:03:02 +090071const char kCompileCommandHeaderDir[] = "output/dir/";
Christopher Wiley054afbd2015-10-16 17:08:43 -070072const char kCompileCommandCppOutput[] = "some/file.cpp";
Christopher Wileya590de82015-09-15 15:46:28 -070073const char* kCompileCppCommand[] = {
74 "aidl-cpp",
75 kCompileCommandIncludePath,
76 kCompileDepFile,
77 kCompileCommandInput,
Christopher Wiley054afbd2015-10-16 17:08:43 -070078 kCompileCommandHeaderDir,
79 kCompileCommandCppOutput,
Christopher Wileya590de82015-09-15 15:46:28 -070080 nullptr,
81};
Dan Willemsen93298ee2016-11-10 23:55:55 -080082const char* kCompileCppCommandNinja[] = {
83 "aidl-cpp",
84 kCompileCommandIncludePath,
85 kCompileDepFile,
86 kCompileDepFileNinja,
87 kCompileCommandInput,
88 kCompileCommandHeaderDir,
89 kCompileCommandCppOutput,
90 nullptr,
91};
Christopher Wileya590de82015-09-15 15:46:28 -070092
Jiyong Park74595c12018-07-23 15:22:50 +090093unique_ptr<Options> GetOptions(const char* command[],
94 Options::Language default_lang = Options::Language::JAVA) {
Christopher Wileyd93c5b72015-09-14 13:21:37 -070095 int argc = 0;
96 const char** command_part = command;
97 for (; *command_part; ++argc, ++command_part) {}
Jiyong Park74595c12018-07-23 15:22:50 +090098 unique_ptr<Options> ret(new Options(argc, command, default_lang));
99 if (!ret->Ok()) {
100 cerr << ret->GetErrorMessage();
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700101 cerr << "Failed to parse command line:";
102 for (int i = 0; i < argc; ++i) {
103 cerr << " " << command[i];
104 cerr << endl;
105 }
106 }
Jiyong Park74595c12018-07-23 15:22:50 +0900107 EXPECT_NE(ret, nullptr) << "Failed to parse options!";
108 return ret;
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700109}
110
Christopher Wiley4427d862015-09-14 11:07:39 -0700111} // namespace
112
Jiyong Park74595c12018-07-23 15:22:50 +0900113TEST(OptionsTests, ParsesPreprocess) {
114 unique_ptr<Options> options = GetOptions(kPreprocessCommand);
115 EXPECT_EQ(Options::Task::PREPROCESS, options->GetTask());
116 EXPECT_EQ(false, options->FailOnParcelable());
Jiyong Park3c35e392018-08-30 13:10:30 +0900117 EXPECT_EQ(0u, options->ImportDirs().size());
Jiyong Park74595c12018-07-23 15:22:50 +0900118 EXPECT_EQ(0u, options->PreprocessedFiles().size());
119 EXPECT_EQ(string{kPreprocessCommandOutputFile}, options->OutputFile());
120 EXPECT_EQ(false, options->AutoDepFile());
Christopher Wiley3616d132015-09-01 11:07:48 -0700121 const vector<string> expected_input{kPreprocessCommandInput1,
122 kPreprocessCommandInput2,
123 kPreprocessCommandInput3};
Jiyong Park74595c12018-07-23 15:22:50 +0900124 EXPECT_EQ(expected_input, options->InputFiles());
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700125}
126
Jiyong Park74595c12018-07-23 15:22:50 +0900127TEST(OptionsTests, ParsesCompileJava) {
128 unique_ptr<Options> options = GetOptions(kCompileJavaCommand);
129 EXPECT_EQ(Options::Task::COMPILE, options->GetTask());
130 EXPECT_EQ(Options::Language::JAVA, options->TargetLanguage());
131 EXPECT_EQ(true, options->FailOnParcelable());
Jiyong Park3c35e392018-08-30 13:10:30 +0900132 EXPECT_EQ(1u, options->ImportDirs().size());
Jiyong Park74595c12018-07-23 15:22:50 +0900133 EXPECT_EQ(0u, options->PreprocessedFiles().size());
134 EXPECT_EQ(string{kCompileCommandInput}, options->InputFiles().front());
135 EXPECT_EQ(string{kCompileCommandJavaOutput}, options->OutputFile());
136 EXPECT_EQ(false, options->AutoDepFile());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800137 EXPECT_EQ(false, options->DependencyFileNinja());
138}
139
Jiyong Park74595c12018-07-23 15:22:50 +0900140TEST(OptionsTests, ParsesCompileJavaNinja) {
141 unique_ptr<Options> options = GetOptions(kCompileJavaCommandNinja);
142 EXPECT_EQ(Options::Task::COMPILE, options->GetTask());
143 EXPECT_EQ(Options::Language::JAVA, options->TargetLanguage());
144 EXPECT_EQ(true, options->FailOnParcelable());
Jiyong Park3c35e392018-08-30 13:10:30 +0900145 EXPECT_EQ(1u, options->ImportDirs().size());
Jiyong Park74595c12018-07-23 15:22:50 +0900146 EXPECT_EQ(0u, options->PreprocessedFiles().size());
147 EXPECT_EQ(string{kCompileCommandInput}, options->InputFiles().front());
148 EXPECT_EQ(string{kCompileCommandJavaOutput}, options->OutputFile());
149 EXPECT_EQ(false, options->AutoDepFile());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800150 EXPECT_EQ(true, options->DependencyFileNinja());
Christopher Wiley3616d132015-09-01 11:07:48 -0700151}
Christopher Wiley4427d862015-09-14 11:07:39 -0700152
Jiyong Park74595c12018-07-23 15:22:50 +0900153TEST(OptionsTests, ParsesCompileCpp) {
154 unique_ptr<Options> options = GetOptions(kCompileCppCommand, Options::Language::CPP);
Jiyong Park3c35e392018-08-30 13:10:30 +0900155 ASSERT_EQ(1u, options->ImportDirs().size());
Jiyong Park8c380532018-08-30 14:55:26 +0900156 EXPECT_EQ(string{kCompileCommandIncludePath}.substr(2), *options->ImportDirs().begin());
Jiyong Park74595c12018-07-23 15:22:50 +0900157 EXPECT_EQ(string{kCompileDepFile}.substr(2), options->DependencyFile());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800158 EXPECT_EQ(false, options->DependencyFileNinja());
Jiyong Park74595c12018-07-23 15:22:50 +0900159 EXPECT_EQ(kCompileCommandInput, options->InputFiles().front());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800160 EXPECT_EQ(kCompileCommandHeaderDir, options->OutputHeaderDir());
Jiyong Park74595c12018-07-23 15:22:50 +0900161 EXPECT_EQ(kCompileCommandCppOutput, options->OutputFile());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800162}
163
Jiyong Park74595c12018-07-23 15:22:50 +0900164TEST(OptionsTests, ParsesCompileCppNinja) {
165 unique_ptr<Options> options = GetOptions(kCompileCppCommandNinja, Options::Language::CPP);
Jiyong Park3c35e392018-08-30 13:10:30 +0900166 ASSERT_EQ(1u, options->ImportDirs().size());
Jiyong Park8c380532018-08-30 14:55:26 +0900167 EXPECT_EQ(string{kCompileCommandIncludePath}.substr(2), *options->ImportDirs().begin());
Jiyong Park74595c12018-07-23 15:22:50 +0900168 EXPECT_EQ(string{kCompileDepFile}.substr(2), options->DependencyFile());
Dan Willemsen93298ee2016-11-10 23:55:55 -0800169 EXPECT_EQ(true, options->DependencyFileNinja());
Jiyong Park74595c12018-07-23 15:22:50 +0900170 EXPECT_EQ(kCompileCommandInput, options->InputFiles().front());
Christopher Wiley054afbd2015-10-16 17:08:43 -0700171 EXPECT_EQ(kCompileCommandHeaderDir, options->OutputHeaderDir());
Jiyong Park74595c12018-07-23 15:22:50 +0900172 EXPECT_EQ(kCompileCommandCppOutput, options->OutputFile());
Christopher Wiley4432ccf2015-09-18 18:32:08 -0700173}
174
Jiyong Park74595c12018-07-23 15:22:50 +0900175TEST(OptionsTests, ParsesCompileJavaMultiInput) {
176 const char* argv[] = {
177 "aidl",
178 "--lang=java",
179 kCompileCommandIncludePath,
180 "-o src_out",
181 "directory/input1.aidl",
182 "directory/input2.aidl",
183 "directory/input3.aidl",
184 nullptr,
185 };
186 unique_ptr<Options> options = GetOptions(argv);
187 EXPECT_EQ(Options::Task::COMPILE, options->GetTask());
188 EXPECT_EQ(Options::Language::JAVA, options->TargetLanguage());
189 EXPECT_EQ(false, options->FailOnParcelable());
Jiyong Park3c35e392018-08-30 13:10:30 +0900190 EXPECT_EQ(1u, options->ImportDirs().size());
Jiyong Park74595c12018-07-23 15:22:50 +0900191 EXPECT_EQ(0u, options->PreprocessedFiles().size());
192 const vector<string> expected_input{"directory/input1.aidl", "directory/input2.aidl",
193 "directory/input3.aidl"};
194 EXPECT_EQ(expected_input, options->InputFiles());
195 EXPECT_EQ(string{""}, options->OutputFile());
196 EXPECT_EQ(false, options->AutoDepFile());
197 EXPECT_EQ(false, options->DependencyFileNinja());
198 EXPECT_EQ(string{""}, options->OutputHeaderDir());
Jiyong Park05463732018-08-09 16:03:02 +0900199 EXPECT_EQ(string{"src_out/"}, options->OutputDir());
Christopher Wiley4432ccf2015-09-18 18:32:08 -0700200}
201
Jiyong Park74595c12018-07-23 15:22:50 +0900202TEST(OptionsTests, ParsesCompileJavaInvalid) {
203 // -o option is required
204 const char* arg_with_no_out_dir[] = {
205 "aidl",
206 "--lang=java",
207 kCompileCommandIncludePath,
208 "directory/input1.aidl",
209 "directory/input2.aidl",
210 "directory/input3.aidl",
211 nullptr,
Christopher Wiley4432ccf2015-09-18 18:32:08 -0700212 };
Jiyong Park74595c12018-07-23 15:22:50 +0900213 EXPECT_EQ(false, GetOptions(arg_with_no_out_dir)->Ok());
214
215 // -h options is not for Java
216 const char* arg_with_header_dir[] = {
217 "aidl", "--lang=java", kCompileCommandIncludePath, "-o src_out",
218 "-h header_out", "directory/input1.aidl", "directory/input2.aidl", "directory/input3.aidl",
219 nullptr,
Christopher Wiley4432ccf2015-09-18 18:32:08 -0700220 };
Jiyong Park74595c12018-07-23 15:22:50 +0900221 EXPECT_EQ(false, GetOptions(arg_with_header_dir)->Ok());
222}
223
224TEST(OptionsTests, ParsesCompileCppMultiInput) {
225 const char* argv[] = {
226 "aidl",
227 "--lang=cpp",
228 kCompileCommandIncludePath,
229 "-h header_out",
230 "-o src_out",
231 "directory/input1.aidl",
232 "directory/input2.aidl",
233 "directory/input3.aidl",
234 nullptr,
235 };
236 unique_ptr<Options> options = GetOptions(argv);
237 EXPECT_EQ(Options::Task::COMPILE, options->GetTask());
238 EXPECT_EQ(Options::Language::CPP, options->TargetLanguage());
239 EXPECT_EQ(false, options->FailOnParcelable());
Jiyong Park3c35e392018-08-30 13:10:30 +0900240 EXPECT_EQ(1u, options->ImportDirs().size());
Jiyong Park74595c12018-07-23 15:22:50 +0900241 EXPECT_EQ(0u, options->PreprocessedFiles().size());
242 const vector<string> expected_input{"directory/input1.aidl", "directory/input2.aidl",
243 "directory/input3.aidl"};
244 EXPECT_EQ(expected_input, options->InputFiles());
245 EXPECT_EQ(string{""}, options->OutputFile());
246 EXPECT_EQ(false, options->AutoDepFile());
247 EXPECT_EQ(false, options->DependencyFileNinja());
Jiyong Park05463732018-08-09 16:03:02 +0900248 EXPECT_EQ(string{"header_out/"}, options->OutputHeaderDir());
249 EXPECT_EQ(string{"src_out/"}, options->OutputDir());
Jiyong Park74595c12018-07-23 15:22:50 +0900250}
251
252TEST(OptionsTests, ParsesCompileCppInvalid) {
253 // -o option is required
254 const char* arg_with_no_out_dir[] = {
255 "aidl",
256 "--lang=cpp",
257 kCompileCommandIncludePath,
258 "directory/input1.aidl",
259 "directory/input2.aidl",
260 "directory/input3.aidl",
261 nullptr,
262 };
263 EXPECT_EQ(false, GetOptions(arg_with_no_out_dir)->Ok());
264
265 // -h options is required as well
266 const char* arg_with_no_header_dir[] = {
267 "aidl",
268 "--lang=cpp",
269 kCompileCommandIncludePath,
270 "-o src_out",
271 "directory/input1.aidl",
272 "directory/input2.aidl",
273 "directory/input3.aidl",
274 nullptr,
275 };
276 EXPECT_EQ(false, GetOptions(arg_with_no_header_dir)->Ok());
Christopher Wileya590de82015-09-15 15:46:28 -0700277}
278
Christopher Wiley4427d862015-09-14 11:07:39 -0700279} // namespace android
280} // namespace aidl