1 /*
2  * Copyright (C) 2020 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 package com.android.build.config;
18 
19 import java.util.List;
20 import java.util.Map;
21 import java.util.TreeMap;
22 import java.util.TreeSet;
23 
24 public class Main {
25     private final Errors mErrors;
26     private final Options mOptions;
27 
Main(Errors errors, Options options)28     public Main(Errors errors, Options options) {
29         mErrors = errors;
30         mOptions = options;
31     }
32 
run()33     void run() {
34         // TODO: Check the build environment to make sure we're running in a real
35         // build environment, e.g. actually inside a source tree, with TARGET_PRODUCT
36         // and TARGET_BUILD_VARIANT defined, etc.
37         Kati kati = new KatiImpl(mErrors, mOptions);
38         Map<String, MakeConfig> makeConfigs = kati.loadProductConfig();
39         if (makeConfigs == null || mErrors.hadError()) {
40             return;
41         }
42         if (false) {
43             for (MakeConfig makeConfig: (new TreeMap<String, MakeConfig>(makeConfigs)).values()) {
44                 System.out.println();
45                 System.out.println("=======================================");
46                 System.out.println("PRODUCT CONFIG FILES : " + makeConfig.getPhase());
47                 System.out.println("=======================================");
48                 makeConfig.printToStream(System.out);
49             }
50         }
51 
52         ConvertMakeToGenericConfig m2g = new ConvertMakeToGenericConfig(mErrors);
53         GenericConfig generic = m2g.convert(makeConfigs);
54         if (false) {
55             System.out.println("======================");
56             System.out.println("REGENERATED MAKE FILES");
57             System.out.println("======================");
58             MakeWriter.write(System.out, generic, 0);
59         }
60 
61         // TODO: Lookup shortened name as used in PRODUCT_NAME / TARGET_PRODUCT
62         FlatConfig flat = FlattenConfig.flatten(mErrors, generic);
63         if (false) {
64             System.out.println("=======================");
65             System.out.println("FLATTENED VARIABLE LIST");
66             System.out.println("=======================");
67             MakeWriter.write(System.out, flat, 0);
68         }
69 
70         OutputChecker checker = new OutputChecker(flat);
71         checker.reportErrors(mErrors);
72 
73         // TODO: Run kati and extract the variables and convert all that into starlark files.
74 
75         // TODO: Run starlark with all the generated ones and the hand written ones.
76 
77         // TODO: Get the variables that were defined in starlark and use that to write
78         // out the make, soong and bazel input files.
79     }
80 
main(String[] args)81     public static void main(String[] args) {
82         Errors errors = new Errors();
83         int exitCode = 0;
84 
85         try {
86             Options options = Options.parse(errors, args, System.getenv());
87             if (errors.hadError()) {
88                 Options.printHelp(System.err);
89                 System.err.println();
90                 throw new CommandException();
91             }
92 
93             switch (options.getAction()) {
94                 case DEFAULT:
95                     (new Main(errors, options)).run();
96                     return;
97                 case HELP:
98                     Options.printHelp(System.out);
99                     return;
100             }
101         } catch (CommandException | Errors.FatalException ex) {
102             // These are user errors, so don't show a stack trace
103             exitCode = 1;
104         } catch (Throwable ex) {
105             // These are programming errors in the code of this tool, so print the exception.
106             // We'll try to print this.  If it's something unrecoverable, then we'll hope
107             // for the best. We will still print the errors below, because they can be useful
108             // for debugging.
109             ex.printStackTrace(System.err);
110             System.err.println();
111             exitCode = 1;
112         } finally {
113             // Print errors and warnings
114             errors.printErrors(System.err);
115             if (errors.hadError()) {
116                 exitCode = 1;
117             }
118             System.exit(exitCode);
119         }
120     }
121 }
122