Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 16 | |
| 17 | #include "options.h" |
| 18 | |
| 19 | #include <stdio.h> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 20 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 21 | using std::string; |
| 22 | using std::unique_ptr; |
| 23 | |
| 24 | namespace android { |
| 25 | namespace aidl { |
| 26 | namespace { |
| 27 | |
Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 28 | unique_ptr<JavaOptions> java_usage() { |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 29 | 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 Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 51 | return unique_ptr<JavaOptions>(nullptr); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 54 | } // namespace |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 55 | |
Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 56 | unique_ptr<JavaOptions> JavaOptions::Parse(int argc, const char* const* argv) { |
| 57 | unique_ptr<JavaOptions> options(new JavaOptions()); |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 58 | int i = 1; |
| 59 | |
| 60 | if (argc >= 2 && 0 == strcmp(argv[1], "--preprocess")) { |
| 61 | if (argc < 4) { |
Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 62 | return java_usage(); |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 63 | } |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 64 | options->output_file_name_ = argv[2]; |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 65 | for (int i = 3; i < argc; i++) { |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 66 | options->files_to_preprocess_.push_back(argv[i]); |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 67 | } |
| 68 | options->task = PREPROCESS_AIDL; |
| 69 | return options; |
| 70 | } |
| 71 | |
Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 72 | options->task = COMPILE_AIDL_TO_JAVA; |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 73 | // OPTIONS |
| 74 | while (i < argc) { |
| 75 | const char* s = argv[i]; |
Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 76 | 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 Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 82 | return java_usage(); |
Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 83 | } |
| 84 | // -I<system-import-path> |
| 85 | if (s[1] == 'I') { |
| 86 | if (len > 2) { |
| 87 | options->import_paths_.push_back(s + 2); |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 88 | } else { |
Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 89 | fprintf(stderr, "-I option (%d) requires a path.\n", i); |
Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 90 | return java_usage(); |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 91 | } |
Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 92 | } 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 Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 97 | return java_usage(); |
Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 98 | } |
| 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 Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 106 | return java_usage(); |
Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 107 | } |
| 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 Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 113 | return java_usage(); |
Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 114 | } |
| 115 | } else if (strcmp(s, "-b") == 0) { |
| 116 | options->fail_on_parcelable_ = true; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 117 | } else { |
Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 118 | // s[1] is not known |
| 119 | fprintf(stderr, "unknown option (%d): %s\n", i, s); |
Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 120 | return java_usage(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 121 | } |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 122 | i++; |
| 123 | } |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 124 | // INPUT |
| 125 | if (i < argc) { |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 126 | options->input_file_name_ = argv[i]; |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 127 | i++; |
| 128 | } else { |
| 129 | fprintf(stderr, "INPUT required\n"); |
Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 130 | return java_usage(); |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 131 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 132 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 133 | // OUTPUT |
| 134 | if (i < argc) { |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 135 | options->output_file_name_ = argv[i]; |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 136 | i++; |
Christopher Wiley | d93c5b7 | 2015-09-14 13:21:37 -0700 | [diff] [blame] | 137 | } else if (options->output_base_folder_.empty()) { |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 138 | // copy input into output and change the extension from .aidl to .java |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 139 | options->output_file_name_= options->input_file_name_; |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 140 | const size_t suffix_len = 5; // 5 = strlen(".aidl") |
Christopher Wiley | 8f8cc9b | 2015-09-14 13:47:40 -0700 | [diff] [blame] | 141 | 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 Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 144 | } else { |
| 145 | fprintf(stderr, "INPUT is not an .aidl file.\n"); |
Christopher Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 146 | return java_usage(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 147 | } |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 148 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 149 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 150 | // 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 Wiley | 89eaab5 | 2015-09-15 14:46:46 -0700 | [diff] [blame^] | 158 | return java_usage(); |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | return options; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 164 | } // namespace android |
| 165 | } // namespace aidl |