summaryrefslogtreecommitdiffstats
path: root/tools/aidl/options.cpp
blob: 9de1957c886c6bedd6f386c88491cb275d42ea53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

#include "options.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int
usage()
{
    fprintf(stderr,
            "usage: aidl OPTIONS INPUT [OUTPUT]\n"
            "       aidl --preprocess OUTPUT INPUT...\n"
            "\n"
            "OPTIONS:\n"
            "   -I<DIR>    search path for import statements.\n"
            "   -d<FILE>   generate dependency file.\n"
            "   -a         generate dependency file next to the output file with the name based on the input file.\n"
            "   -p<FILE>   file created by --preprocess to import.\n"
            "   -o<FOLDER> base output folder for generated files.\n"
            "   -b         fail when trying to compile a parcelable.\n"
            "\n"
            "INPUT:\n"
            "   An aidl interface file.\n"
            "\n"
            "OUTPUT:\n"
            "   The generated interface files.\n"
            "   If omitted and the -o option is not used, the input filename is used, with the .aidl extension changed to a .java extension.\n"
            "   If the -o option is used, the generated files will be placed in the base output folder, under their package folder\n"
           );
    return 1;
}

int
parse_options(int argc, const char* const* argv, Options *options)
{
    int i = 1;

    if (argc >= 2 && 0 == strcmp(argv[1], "--preprocess")) {
        if (argc < 4) {
            return usage();
        }
        options->outputFileName = argv[2];
        for (int i=3; i<argc; i++) {
            options->filesToPreprocess.push_back(argv[i]);
        }
        options->task = PREPROCESS_AIDL;
        return 0;
    }

    options->task = COMPILE_AIDL;
    options->failOnParcelable = false;
    options->autoDepFile = false;
    options->generateNoOpMethods = false;

    // OPTIONS
    while (i < argc) {
        const char* s = argv[i];
        int len = strlen(s);
        if (s[0] == '-') {
            if (len > 1) {
                // -I<system-import-path>
                if (s[1] == 'I') {
                    if (len > 2) {
                        options->importPaths.push_back(s+2);
                    } else {
                        fprintf(stderr, "-I option (%d) requires a path.\n", i);
                        return usage();
                    }
                }
                else if (s[1] == 'd') {
                    if (len > 2) {
                        options->depFileName = s+2;
                    } else {
                        fprintf(stderr, "-d option (%d) requires a file.\n", i);
                        return usage();
                    }
                }
                else if (s[1] == 'a') {
                    options->autoDepFile = true;
                }
                else if (s[1] == 'p') {
                    if (len > 2) {
                        options->preprocessedFiles.push_back(s+2);
                    } else {
                        fprintf(stderr, "-p option (%d) requires a file.\n", i);
                        return usage();
                    }
                }
                else if (s[1] == 'o') {
                    if (len > 2) {
                        options->outputBaseFolder = s+2;
                    } else {
                        fprintf(stderr, "-o option (%d) requires a path.\n", i);
                        return usage();
                    }
                }
                else if (len == 2 && s[1] == 'b') {
                    options->failOnParcelable = true;
                }
                else if (s[1] == 'n') {
                    options->generateNoOpMethods = true;
                }
                else {
                    // s[1] is not known
                    fprintf(stderr, "unknown option (%d): %s\n", i, s);
                    return usage();
                }
            } else {
                // len <= 1
                fprintf(stderr, "unknown option (%d): %s\n", i, s);
                return usage();
            }
        } else {
            // s[0] != '-'
            break;
        }
        i++;
    }

    // INPUT
    if (i < argc) {
        options->inputFileName = argv[i];
        i++;
    } else {
        fprintf(stderr, "INPUT required\n");
        return usage();
    }

    // OUTPUT
    if (i < argc) {
        options->outputFileName = argv[i];
        i++;
    } else if (options->outputBaseFolder.length() == 0) {
        // copy input into output and change the extension from .aidl to .java
        options->outputFileName = options->inputFileName;
        string::size_type pos = options->outputFileName.size()-5;
        if (options->outputFileName.compare(pos, 5, ".aidl") == 0) {  // 5 = strlen(".aidl")
            options->outputFileName.replace(pos, 5, ".java"); // 5 = strlen(".aidl")
        } else {
            fprintf(stderr, "INPUT is not an .aidl file.\n");
            return usage();
        }
     }

    // anything remaining?
    if (i != argc) {
        fprintf(stderr, "unknown option%s:", (i==argc-1?(const char*)"":(const char*)"s"));
        for (; i<argc-1; i++) {
            fprintf(stderr, " %s", argv[i]);
        }
        fprintf(stderr, "\n");
        return usage();
    }

    return 0;
}