blob: 52b0972a89ee7ac0c6b20d087c131102e8332d0c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001
2#include "options.h"
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8static int
9usage()
10{
11 fprintf(stderr,
12 "usage: aidl OPTIONS INPUT [OUTPUT]\n"
13 " aidl --preprocess OUTPUT INPUT...\n"
14 "\n"
15 "OPTIONS:\n"
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070016 " -I<DIR> search path for import statements.\n"
17 " -d<FILE> generate dependency file.\n"
Xavier Ducrohet18fff112011-08-25 11:58:17 -070018 " -a generate dependency file next to the output file with the name based on the input file.\n"
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070019 " -p<FILE> file created by --preprocess to import.\n"
20 " -o<FOLDER> base output folder for generated files.\n"
21 " -b fail when trying to compile a parcelable.\n"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022 "\n"
23 "INPUT:\n"
24 " An aidl interface file.\n"
25 "\n"
26 "OUTPUT:\n"
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070027 " The generated interface files.\n"
28 " If omitted and the -o option is not used, the input filename is used, with the .aidl extension changed to a .java extension.\n"
29 " If the -o option is used, the generated files will be placed in the base output folder, under their package folder\n"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 );
31 return 1;
32}
33
34int
35parse_options(int argc, const char* const* argv, Options *options)
36{
37 int i = 1;
38
39 if (argc >= 2 && 0 == strcmp(argv[1], "--preprocess")) {
40 if (argc < 4) {
41 return usage();
42 }
43 options->outputFileName = argv[2];
44 for (int i=3; i<argc; i++) {
45 options->filesToPreprocess.push_back(argv[i]);
46 }
47 options->task = PREPROCESS_AIDL;
48 return 0;
49 }
50
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 // OPTIONS
52 while (i < argc) {
53 const char* s = argv[i];
54 int len = strlen(s);
55 if (s[0] == '-') {
56 if (len > 1) {
57 // -I<system-import-path>
58 if (s[1] == 'I') {
59 if (len > 2) {
60 options->importPaths.push_back(s+2);
61 } else {
62 fprintf(stderr, "-I option (%d) requires a path.\n", i);
63 return usage();
64 }
65 }
66 else if (s[1] == 'd') {
67 if (len > 2) {
68 options->depFileName = s+2;
69 } else {
70 fprintf(stderr, "-d option (%d) requires a file.\n", i);
71 return usage();
72 }
73 }
Xavier Ducrohet18fff112011-08-25 11:58:17 -070074 else if (s[1] == 'a') {
75 options->autoDepFile = true;
76 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 else if (s[1] == 'p') {
78 if (len > 2) {
79 options->preprocessedFiles.push_back(s+2);
80 } else {
81 fprintf(stderr, "-p option (%d) requires a file.\n", i);
82 return usage();
83 }
84 }
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070085 else if (s[1] == 'o') {
86 if (len > 2) {
87 options->outputBaseFolder = s+2;
88 } else {
89 fprintf(stderr, "-o option (%d) requires a path.\n", i);
90 return usage();
91 }
92 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 else if (len == 2 && s[1] == 'b') {
94 options->failOnParcelable = true;
95 }
96 else {
97 // s[1] is not known
98 fprintf(stderr, "unknown option (%d): %s\n", i, s);
99 return usage();
100 }
101 } else {
102 // len <= 1
103 fprintf(stderr, "unknown option (%d): %s\n", i, s);
104 return usage();
105 }
106 } else {
107 // s[0] != '-'
108 break;
109 }
110 i++;
111 }
112
113 // INPUT
114 if (i < argc) {
115 options->inputFileName = argv[i];
116 i++;
117 } else {
118 fprintf(stderr, "INPUT required\n");
119 return usage();
120 }
121
122 // OUTPUT
123 if (i < argc) {
124 options->outputFileName = argv[i];
125 i++;
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700126 } else if (options->outputBaseFolder.length() == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 // copy input into output and change the extension from .aidl to .java
128 options->outputFileName = options->inputFileName;
129 string::size_type pos = options->outputFileName.size()-5;
130 if (options->outputFileName.compare(pos, 5, ".aidl") == 0) { // 5 = strlen(".aidl")
131 options->outputFileName.replace(pos, 5, ".java"); // 5 = strlen(".aidl")
132 } else {
133 fprintf(stderr, "INPUT is not an .aidl file.\n");
134 return usage();
135 }
136 }
137
138 // anything remaining?
139 if (i != argc) {
140 fprintf(stderr, "unknown option%s:", (i==argc-1?(const char*)"":(const char*)"s"));
141 for (; i<argc-1; i++) {
142 fprintf(stderr, " %s", argv[i]);
143 }
144 fprintf(stderr, "\n");
145 return usage();
146 }
147
148 return 0;
149}
150