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.List; 22 import java.util.Map; 23 import java.util.TreeMap; 24 25 public class MakeConfig extends ConfigBase { 26 /** 27 * The config files that were imported in this config pass. 28 */ 29 protected final ArrayList<ConfigFile> mConfigFiles = new ArrayList(); 30 31 public enum BlockType { 32 UNSET, 33 BEFORE, 34 INHERIT, 35 AFTER 36 } 37 38 public static class ConfigFile { 39 /** 40 * The name of the file, relative to the tree root. 41 */ 42 private final String mFilename; 43 44 /** 45 * Sections of variable definitions and import statements. Product config 46 * files will always have at least one block. 47 */ 48 private final ArrayList<Block> mBlocks = new ArrayList(); 49 ConfigFile(String filename)50 public ConfigFile(String filename) { 51 mFilename = filename; 52 } 53 getFilename()54 public String getFilename() { 55 return mFilename; 56 } 57 addBlock(Block block)58 public void addBlock(Block block) { 59 mBlocks.add(block); 60 } 61 getBlocks()62 public ArrayList<Block> getBlocks() { 63 return mBlocks; 64 } 65 } 66 67 /** 68 * A set of variables that were defined. 69 */ 70 public static class Block { 71 private final BlockType mBlockType; 72 private final TreeMap<String, Str> mValues = new TreeMap(); 73 private Str mInheritedFile; 74 Block(BlockType blockType)75 public Block(BlockType blockType) { 76 mBlockType = blockType; 77 } 78 getBlockType()79 public BlockType getBlockType() { 80 return mBlockType; 81 } 82 addVar(String varName, Str varValue)83 public void addVar(String varName, Str varValue) { 84 mValues.put(varName, varValue); 85 } 86 getVar(String varName)87 public Str getVar(String varName) { 88 return mValues.get(varName); 89 } 90 getVars()91 public TreeMap<String, Str> getVars() { 92 return mValues; 93 } 94 setInheritedFile(Str filename)95 public void setInheritedFile(Str filename) { 96 mInheritedFile = filename; 97 } 98 getInheritedFile()99 public Str getInheritedFile() { 100 return mInheritedFile; 101 } 102 } 103 104 /** 105 * Adds the given config file. Returns any one previously added, or null. 106 */ addConfigFile(ConfigFile file)107 public ConfigFile addConfigFile(ConfigFile file) { 108 ConfigFile prev = null; 109 for (ConfigFile f: mConfigFiles) { 110 if (f.getFilename().equals(file.getFilename())) { 111 prev = f; 112 break; 113 } 114 } 115 mConfigFiles.add(file); 116 return prev; 117 } 118 getConfigFiles()119 public List<ConfigFile> getConfigFiles() { 120 return mConfigFiles; 121 } 122 printToStream(PrintStream out)123 public void printToStream(PrintStream out) { 124 out.println("MakeConfig {"); 125 out.println(" phase: " + mPhase); 126 out.println(" rootNodes: " + mRootNodes); 127 out.print(" singleVars: [ "); 128 for (Map.Entry<String,VarType> entry: mProductVars.entrySet()) { 129 if (entry.getValue() == VarType.SINGLE) { 130 out.print(entry.getKey()); 131 out.print(" "); 132 } 133 } 134 out.println("]"); 135 out.print(" listVars: [ "); 136 for (Map.Entry<String,VarType> entry: mProductVars.entrySet()) { 137 if (entry.getValue() == VarType.LIST) { 138 out.print(entry.getKey()); 139 out.print(" "); 140 } 141 } 142 out.println("]"); 143 out.println(" configFiles: ["); 144 for (final ConfigFile configFile: mConfigFiles) { 145 out.println(" ConfigFile {"); 146 out.println(" filename: " + configFile.getFilename()); 147 out.println(" blocks: ["); 148 for (Block block: configFile.getBlocks()) { 149 out.println(" Block {"); 150 out.println(" type: " + block.getBlockType()); 151 if (block.getBlockType() == BlockType.INHERIT) { 152 out.println(" inherited: " + block.getInheritedFile()); 153 } 154 out.println(" values: {"); 155 for (Map.Entry<String,Str> var: block.getVars().entrySet()) { 156 if (!var.getKey().equals("PRODUCT_PACKAGES")) { 157 continue; 158 } 159 out.println(" " + var.getKey() + ": " + var.getValue()); 160 } 161 out.println(" }"); 162 out.println(" }"); 163 } 164 out.println(" ]"); 165 out.println(" }"); 166 } 167 out.println(" ] // configFiles"); 168 out.println("} // MakeConfig"); 169 } 170 } 171