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.io.PrintStream;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.TreeMap;
25 
26 /**
27  * Common parts between MakeConfig and the to-be-added GenericConfig, BazelConfig and SoongConfig.
28  */
29 public class ConfigBase {
30     protected String mPhase;
31     protected List<String> mRootNodes;
32 
33     /**
34      * State of the make varaible environment from before the first config file.
35      */
36     protected Map<String, Str> mInitialVariables = new HashMap();
37 
38     /**
39      * State of the make varaible environment from after the first config file.
40      */
41     protected Map<String, Str> mFinalVariables = new HashMap();
42 
43 
44     /**
45      * The variables that are handled specially.
46      */
47     protected final TreeMap<String, VarType> mProductVars = new TreeMap();
48 
setPhase(String phase)49     public void setPhase(String phase) {
50         mPhase = phase;
51     }
52 
getPhase()53     public String getPhase() {
54         return mPhase;
55     }
56 
setRootNodes(List<String> filenames)57     public void setRootNodes(List<String> filenames) {
58         mRootNodes = new ArrayList(filenames);
59     }
60 
getRootNodes()61     public List<String> getRootNodes() {
62         return mRootNodes;
63     }
64 
addProductVar(String name, VarType type)65     public void addProductVar(String name, VarType type) {
66         mProductVars.put(name, type);
67     }
68 
getProductVars()69     public TreeMap<String, VarType> getProductVars() {
70         return mProductVars;
71     }
72 
getVarType(String name)73     public VarType getVarType(String name) {
74         final VarType t = mProductVars.get(name);
75         if (t != null) {
76             return t;
77         } else {
78             return VarType.UNKNOWN;
79         }
80     }
81 
isProductVar(String name)82     public boolean isProductVar(String name) {
83         return mProductVars.get(name) != null;
84     }
85 
86     /**
87      * Return the state the make variable environment from before the first config file.
88      */
getInitialVariables()89     public Map<String, Str> getInitialVariables() {
90         return mInitialVariables;
91     }
92 
93     /**
94      * Return the state the make variable environment from before the first config file.
95      */
getFinalVariables()96     public Map<String, Str> getFinalVariables() {
97         return mFinalVariables;
98     }
99 
100     /**
101      * Copy common base class fields from that to this.
102      */
copyFrom(ConfigBase that)103     public void copyFrom(ConfigBase that) {
104         setPhase(that.getPhase());
105         setRootNodes(that.getRootNodes());
106         for (Map.Entry<String, VarType> entry: that.getProductVars().entrySet()) {
107             addProductVar(entry.getKey(), entry.getValue());
108         }
109         mInitialVariables = new HashMap(that.getInitialVariables());
110         mFinalVariables = new HashMap(that.getFinalVariables());
111     }
112 }
113