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