blob: a8905568b5d3b66478aaecc48bb98a8947d0115e [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001
2#include "options.h"
3
4static int
5usage()
6{
7 fprintf(stderr,
8 "usage: aidl OPTIONS INPUT [OUTPUT]\n"
9 " aidl --preprocess OUTPUT INPUT...\n"
10 "\n"
11 "OPTIONS:\n"
12 " -I<DIR> search path for import statements.\n"
13 " -d<FILE> generate dependency file.\n"
14 " -p<FILE> file created by --preprocess to import.\n"
15 " -b fail when trying to compile a parcelable.\n"
16 "\n"
17 "INPUT:\n"
18 " An aidl interface file.\n"
19 "\n"
20 "OUTPUT:\n"
21 " The generated interface files. If omitted, the input filename is used, with the .aidl extension changed to a .java extension.\n"
22 );
23 return 1;
24}
25
26int
27parse_options(int argc, const char* const* argv, Options *options)
28{
29 int i = 1;
30
31 if (argc >= 2 && 0 == strcmp(argv[1], "--preprocess")) {
32 if (argc < 4) {
33 return usage();
34 }
35 options->outputFileName = argv[2];
36 for (int i=3; i<argc; i++) {
37 options->filesToPreprocess.push_back(argv[i]);
38 }
39 options->task = PREPROCESS_AIDL;
40 return 0;
41 }
42
43 options->task = COMPILE_AIDL;
44 options->failOnParcelable = false;
45
46 // OPTIONS
47 while (i < argc) {
48 const char* s = argv[i];
49 int len = strlen(s);
50 if (s[0] == '-') {
51 if (len > 1) {
52 // -I<system-import-path>
53 if (s[1] == 'I') {
54 if (len > 2) {
55 options->importPaths.push_back(s+2);
56 } else {
57 fprintf(stderr, "-I option (%d) requires a path.\n", i);
58 return usage();
59 }
60 }
61 else if (s[1] == 'd') {
62 if (len > 2) {
63 options->depFileName = s+2;
64 } else {
65 fprintf(stderr, "-d option (%d) requires a file.\n", i);
66 return usage();
67 }
68 }
69 else if (s[1] == 'p') {
70 if (len > 2) {
71 options->preprocessedFiles.push_back(s+2);
72 } else {
73 fprintf(stderr, "-p option (%d) requires a file.\n", i);
74 return usage();
75 }
76 }
77 else if (len == 2 && s[1] == 'b') {
78 options->failOnParcelable = true;
79 }
80 else {
81 // s[1] is not known
82 fprintf(stderr, "unknown option (%d): %s\n", i, s);
83 return usage();
84 }
85 } else {
86 // len <= 1
87 fprintf(stderr, "unknown option (%d): %s\n", i, s);
88 return usage();
89 }
90 } else {
91 // s[0] != '-'
92 break;
93 }
94 i++;
95 }
96
97 // INPUT
98 if (i < argc) {
99 options->inputFileName = argv[i];
100 i++;
101 } else {
102 fprintf(stderr, "INPUT required\n");
103 return usage();
104 }
105
106 // OUTPUT
107 if (i < argc) {
108 options->outputFileName = argv[i];
109 i++;
110 } else {
111 // copy input into output and change the extension from .aidl to .java
112 options->outputFileName = options->inputFileName;
113 string::size_type pos = options->outputFileName.size()-5;
114 if (options->outputFileName.compare(pos, 5, ".aidl") == 0) { // 5 = strlen(".aidl")
115 options->outputFileName.replace(pos, 5, ".java"); // 5 = strlen(".aidl")
116 } else {
117 fprintf(stderr, "INPUT is not an .aidl file.\n");
118 return usage();
119 }
120 }
121
122 // anything remaining?
123 if (i != argc) {
124 fprintf(stderr, "unknown option%s:", (i==argc-1?(const char*)"":(const char*)"s"));
125 for (; i<argc-1; i++) {
126 fprintf(stderr, " %s", argv[i]);
127 }
128 fprintf(stderr, "\n");
129 return usage();
130 }
131
132 return 0;
133}
134