blob: 26a29d26bbe1c50f508e6e81ed4d8bfe2ad9fcdb [file] [log] [blame]
Christopher Wiley4427d862015-09-14 11:07:39 -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 */
Adam Lesinskiffa16862014-01-23 18:17:42 -080016
17#include "options.h"
18
19#include <stdio.h>
Adam Lesinskiffa16862014-01-23 18:17:42 -080020
Christopher Wiley4427d862015-09-14 11:07:39 -070021using std::string;
22using std::unique_ptr;
23
24namespace android {
25namespace aidl {
26namespace {
27
Christopher Wiley89eaab52015-09-15 14:46:46 -070028unique_ptr<JavaOptions> java_usage() {
Christopher Wiley4427d862015-09-14 11:07:39 -070029 fprintf(stderr,
30 "usage: aidl OPTIONS INPUT [OUTPUT]\n"
31 " aidl --preprocess OUTPUT INPUT...\n"
32 "\n"
33 "OPTIONS:\n"
34 " -I<DIR> search path for import statements.\n"
35 " -d<FILE> generate dependency file.\n"
36 " -a generate dependency file next to the output file with "
37 "the name based on the input file.\n"
38 " -p<FILE> file created by --preprocess to import.\n"
39 " -o<FOLDER> base output folder for generated files.\n"
40 " -b fail when trying to compile a parcelable.\n"
41 "\n"
42 "INPUT:\n"
43 " An aidl interface file.\n"
44 "\n"
45 "OUTPUT:\n"
46 " The generated interface files.\n"
47 " If omitted and the -o option is not used, the input filename is "
48 "used, with the .aidl extension changed to a .java extension.\n"
49 " If the -o option is used, the generated files will be placed in "
50 "the base output folder, under their package folder\n");
Christopher Wiley89eaab52015-09-15 14:46:46 -070051 return unique_ptr<JavaOptions>(nullptr);
Adam Lesinskiffa16862014-01-23 18:17:42 -080052}
53
Christopher Wiley4427d862015-09-14 11:07:39 -070054} // namespace
Adam Lesinskiffa16862014-01-23 18:17:42 -080055
Christopher Wiley89eaab52015-09-15 14:46:46 -070056unique_ptr<JavaOptions> JavaOptions::Parse(int argc, const char* const* argv) {
57 unique_ptr<JavaOptions> options(new JavaOptions());
Christopher Wiley4427d862015-09-14 11:07:39 -070058 int i = 1;
59
60 if (argc >= 2 && 0 == strcmp(argv[1], "--preprocess")) {
61 if (argc < 4) {
Christopher Wiley89eaab52015-09-15 14:46:46 -070062 return java_usage();
Christopher Wiley4427d862015-09-14 11:07:39 -070063 }
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -070064 options->output_file_name_ = argv[2];
Christopher Wiley4427d862015-09-14 11:07:39 -070065 for (int i = 3; i < argc; i++) {
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -070066 options->files_to_preprocess_.push_back(argv[i]);
Christopher Wiley4427d862015-09-14 11:07:39 -070067 }
68 options->task = PREPROCESS_AIDL;
69 return options;
70 }
71
Christopher Wileyd93c5b72015-09-14 13:21:37 -070072 options->task = COMPILE_AIDL_TO_JAVA;
Christopher Wiley4427d862015-09-14 11:07:39 -070073 // OPTIONS
74 while (i < argc) {
75 const char* s = argv[i];
Christopher Wileyd93c5b72015-09-14 13:21:37 -070076 const size_t len = strlen(s);
77 if (s[0] != '-') {
78 break;
79 }
80 if (len <= 1) {
81 fprintf(stderr, "unknown option (%d): %s\n", i, s);
Christopher Wiley89eaab52015-09-15 14:46:46 -070082 return java_usage();
Christopher Wileyd93c5b72015-09-14 13:21:37 -070083 }
84 // -I<system-import-path>
85 if (s[1] == 'I') {
86 if (len > 2) {
87 options->import_paths_.push_back(s + 2);
Christopher Wiley4427d862015-09-14 11:07:39 -070088 } else {
Christopher Wileyd93c5b72015-09-14 13:21:37 -070089 fprintf(stderr, "-I option (%d) requires a path.\n", i);
Christopher Wiley89eaab52015-09-15 14:46:46 -070090 return java_usage();
Christopher Wiley4427d862015-09-14 11:07:39 -070091 }
Christopher Wileyd93c5b72015-09-14 13:21:37 -070092 } else if (s[1] == 'd') {
93 if (len > 2) {
94 options->dep_file_name_ = s + 2;
95 } else {
96 fprintf(stderr, "-d option (%d) requires a file.\n", i);
Christopher Wiley89eaab52015-09-15 14:46:46 -070097 return java_usage();
Christopher Wileyd93c5b72015-09-14 13:21:37 -070098 }
99 } else if (strcmp(s, "-a") == 0) {
100 options->auto_dep_file_ = true;
101 } else if (s[1] == 'p') {
102 if (len > 2) {
103 options->preprocessed_files_.push_back(s + 2);
104 } else {
105 fprintf(stderr, "-p option (%d) requires a file.\n", i);
Christopher Wiley89eaab52015-09-15 14:46:46 -0700106 return java_usage();
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700107 }
108 } else if (s[1] == 'o') {
109 if (len > 2) {
110 options->output_base_folder_= s + 2;
111 } else {
112 fprintf(stderr, "-o option (%d) requires a path.\n", i);
Christopher Wiley89eaab52015-09-15 14:46:46 -0700113 return java_usage();
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700114 }
115 } else if (strcmp(s, "-b") == 0) {
116 options->fail_on_parcelable_ = true;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800117 } else {
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700118 // s[1] is not known
119 fprintf(stderr, "unknown option (%d): %s\n", i, s);
Christopher Wiley89eaab52015-09-15 14:46:46 -0700120 return java_usage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800121 }
Christopher Wiley4427d862015-09-14 11:07:39 -0700122 i++;
123 }
Christopher Wiley4427d862015-09-14 11:07:39 -0700124 // INPUT
125 if (i < argc) {
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700126 options->input_file_name_ = argv[i];
Christopher Wiley4427d862015-09-14 11:07:39 -0700127 i++;
128 } else {
129 fprintf(stderr, "INPUT required\n");
Christopher Wiley89eaab52015-09-15 14:46:46 -0700130 return java_usage();
Christopher Wiley4427d862015-09-14 11:07:39 -0700131 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800132
Christopher Wiley4427d862015-09-14 11:07:39 -0700133 // OUTPUT
134 if (i < argc) {
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700135 options->output_file_name_ = argv[i];
Christopher Wiley4427d862015-09-14 11:07:39 -0700136 i++;
Christopher Wileyd93c5b72015-09-14 13:21:37 -0700137 } else if (options->output_base_folder_.empty()) {
Christopher Wiley4427d862015-09-14 11:07:39 -0700138 // copy input into output and change the extension from .aidl to .java
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700139 options->output_file_name_= options->input_file_name_;
Christopher Wiley4427d862015-09-14 11:07:39 -0700140 const size_t suffix_len = 5; // 5 = strlen(".aidl")
Christopher Wiley8f8cc9b2015-09-14 13:47:40 -0700141 string::size_type pos = options->output_file_name_.size() - suffix_len;
142 if (options->output_file_name_.compare(pos, suffix_len, ".aidl") == 0) {
143 options->output_file_name_.replace(pos, suffix_len, ".java");
Christopher Wiley4427d862015-09-14 11:07:39 -0700144 } else {
145 fprintf(stderr, "INPUT is not an .aidl file.\n");
Christopher Wiley89eaab52015-09-15 14:46:46 -0700146 return java_usage();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800147 }
Christopher Wiley4427d862015-09-14 11:07:39 -0700148 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800149
Christopher Wiley4427d862015-09-14 11:07:39 -0700150 // anything remaining?
151 if (i != argc) {
152 fprintf(stderr, "unknown option%s:",
153 (i == argc - 1 ? (const char*)"" : (const char*)"s"));
154 for (; i < argc - 1; i++) {
155 fprintf(stderr, " %s", argv[i]);
156 }
157 fprintf(stderr, "\n");
Christopher Wiley89eaab52015-09-15 14:46:46 -0700158 return java_usage();
Christopher Wiley4427d862015-09-14 11:07:39 -0700159 }
160
161 return options;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800162}
163
Christopher Wiley4427d862015-09-14 11:07:39 -0700164} // namespace android
165} // namespace aidl