1 /*
2  * Copyright (C) 2021 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  */
16 
17 #include <android-base/logging.h>
18 #include <android-base/strings.h>
19 #include <cstdlib>
20 #include <string_view>
21 
22 #include "derive_classpath.h"
23 
ArgumentMatches(std::string_view argument,std::string_view prefix,std::string_view * value)24 bool ArgumentMatches(std::string_view argument, std::string_view prefix, std::string_view* value) {
25   if (android::base::StartsWith(argument, prefix)) {
26     *value = argument.substr(prefix.size());
27     return true;
28   }
29   return false;
30 }
31 
32 // Command line flags need to be considered as a de facto API since there may be callers outside
33 // of the SdkExtensions APEX, which needs to run on older Android versions. For example, otapreopt
34 // currently executes derive_classpath with a single output file. When changing the flags, make sure
35 // it won't break on older Android.
ParseArgs(android::derive_classpath::Args & args,int argc,char ** argv)36 bool ParseArgs(android::derive_classpath::Args& args, int argc, char** argv) {
37   // Parse flags
38   std::vector<std::string_view> positional_args;
39   for (int i = 1; i < argc; ++i) {
40     const std::string_view arg = argv[i];
41     std::string_view value;
42     if (ArgumentMatches(arg, "--bootclasspath-fragment=", &value)) {
43       if (!args.system_bootclasspath_fragment.empty()) {
44         LOG(ERROR) << "Duplicated flag --bootclasspath-fragment is specified";
45         return false;
46       }
47       args.system_bootclasspath_fragment = value;
48     } else if (ArgumentMatches(arg, "--systemserverclasspath-fragment=", &value)) {
49       if (!args.system_systemserverclasspath_fragment.empty()) {
50         LOG(ERROR) << "Duplicated flag --systemserverclasspath-fragment is specified";
51         return false;
52       }
53       args.system_systemserverclasspath_fragment = value;
54     } else if (ArgumentMatches(arg, "--scan-dirs=", &value)) {
55       if (!args.scan_dirs.empty()) {
56         LOG(ERROR) << "Duplicated flag --scan-dirs is specified";
57         return false;
58       }
59       args.scan_dirs = android::base::Split(std::string(value), ",");
60     } else {
61       positional_args.emplace_back(arg);
62     }
63   }
64 
65   // Validate flag combinations
66   if (!args.scan_dirs.empty() && (!args.system_bootclasspath_fragment.empty() ||
67                                   !args.system_systemserverclasspath_fragment.empty())) {
68     LOG(ERROR) << "--scan-dirs should not be accompanied by other flags";
69     return false;
70   }
71 
72   // Handle positional args
73   if (positional_args.size() == 0) {
74     args.output_path = android::derive_classpath::kGeneratedClasspathExportsFilepath;
75   } else if (positional_args.size() == 1) {
76     args.output_path = positional_args[0];
77   } else {
78     LOG(ERROR) << "Unrecognized positional arguments: "
79                << android::base::Join(positional_args, ' ');
80     return false;
81   }
82   return true;
83 }
84 
main(int argc,char ** argv)85 int main(int argc, char** argv) {
86   android::derive_classpath::Args args;
87   if (!ParseArgs(args, argc, argv)) {
88     return EXIT_FAILURE;
89   }
90   if (!android::derive_classpath::GenerateClasspathExports(args)) {
91     return EXIT_FAILURE;
92   }
93   return EXIT_SUCCESS;
94 }
95