Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2fd88e06 authored by Joe Onorato's avatar Joe Onorato Committed by Gerrit Code Review
Browse files

Merge changes I9c1995f8,Id6763781,I52e5c07f,I4706e32f,I7d74b226

* changes:
  Keep the first and last snapshot of variables.
  Generate GenericConfig objects from MakeConfig objects.
  Emit and parse the product config variables from kati/make
  Add class to fork and exec kati, based on the commandline option given.
  Add a CSV parser to parse the output from kati.
parents 5fdf26c2 64f3db2e
Loading
Loading
Loading
Loading

core/dumpconfig.mk

0 → 100644
+129 −0
Original line number Diff line number Diff line
# Read and dump the product configuration.

# Called from the product-config tool, not from the main build system.

#
# Ensure we are being called correctly
#
ifndef KATI
    $(warning Kati must be used to call dumpconfig.mk, not make.)
    $(error stopping)
endif

ifdef DEFAULT_GOAL
    $(warning Calling dumpconfig.mk from inside the make build system is not)
    $(warning supported. It is only meant to be called via kati by product-confing.)
    $(error stopping)
endif

ifndef TARGET_PRODUCT
    $(warning dumpconfig.mk requires TARGET_PRODUCT to be set)
    $(error stopping)
endif

ifndef TARGET_BUILD_VARIANT
    $(warning dumpconfig.mk requires TARGET_BUILD_VARIANT to be set)
    $(error stopping)
endif

ifneq (build/make/core/config.mk,$(wildcard build/make/core/config.mk))
    $(warning dumpconfig must be called from the root of the source tree)
    $(error stopping)
endif

ifeq (,$(DUMPCONFIG_FILE))
    $(warning dumpconfig requires DUMPCONFIG_FILE to be set)
    $(error stopping)
endif

# Before we do anything else output the format version.
$(file > $(DUMPCONFIG_FILE),dumpconfig_version,1)
$(file >> $(DUMPCONFIG_FILE),dumpconfig_file,$(DUMPCONFIG_FILE))

# Default goal for dumpconfig
dumpconfig:
	$(file >> $(DUMPCONFIG_FILE),***DONE***)
	@echo ***DONE***

# TODO(Remove): These need to be set externally
OUT_DIR := out
TMPDIR = /tmp/build-temp
BUILD_DATETIME_FILE := $(OUT_DIR)/build_date.txt

# Escape quotation marks for CSV, and wraps in quotation marks.
define escape-for-csv
"$(subst ","",$1)"
endef

# Args:
#   $(1): include stack
define dump-import-start
$(eval $(file >> $(DUMPCONFIG_FILE),import,$(strip $(1))))
endef

# Args:
#   $(1): include stack
define dump-import-done
$(eval $(file >> $(DUMPCONFIG_FILE),imported,$(strip $(1))))
endef

# Args:
#   $(1): Current file
#   $(2): Inherited file
define dump-inherit
$(eval $(file >> $(DUMPCONFIG_FILE),inherit,$(strip $(1)),$(strip $(2))))
endef

# Args:
#   $(1): Config phase (PRODUCT or DEVICE)
#   $(2): Root nodes to import
#   $(3): All variable names
#   $(4): Single-value variables
#   $(5): Makefile being processed
define dump-phase-start
$(eval $(file >> $(DUMPCONFIG_FILE),phase,$(strip $(1)),$(strip $(2)))) \
$(foreach var,$(3), \
    $(eval $(file >> $(DUMPCONFIG_FILE),var,$(if $(filter $(4),$(var)),single,list),$(var))) \
) \
$(call dump-config-vals,$(strip $(5)),initial)
endef

# Args:
#   $(1): Makefile being processed
define dump-phase-end
$(call dump-config-vals,$(strip $(1)),final)
endef

define dump-debug
$(eval $(file >> $(DUMPCONFIG_FILE),debug,$(1)))
endef

# Skip these when dumping. They're not used and they cause a lot of noise in the dump.
DUMPCONFIG_SKIP_VARS := \
	.VARIABLES \
	.KATI_SYMBOLS \
	1 \
	2 \
	LOCAL_PATH \
	MAKEFILE_LIST \
	PARENT_PRODUCT_FILES \
	current_mk \
	inherit_var \
	np \
	_node_import_context \
	_included \
	_include_stack \
	_in \
	_nic.%

# Args:
#   $(1): Makefile that was included
#   $(2): block (before,import,after,initial,final)
define dump-config-vals
$(foreach var,$(filter-out $(DUMPCONFIG_SKIP_VARS),$(.KATI_SYMBOLS)),\
    $(eval $(file >> $(DUMPCONFIG_FILE),val,$(call escape-for-csv,$(1)),$(2),$(call escape-for-csv,$(var)),$(call escape-for-csv,$($(var))),$(call escape-for-csv,$(KATI_variable_location $(var))))) \
)
endef

include build/make/core/config.mk
+7 −1
Original line number Diff line number Diff line
@@ -195,7 +195,11 @@ define _import-node
  $(call clear-var-list, $(3))
  $(eval LOCAL_PATH := $(patsubst %/,%,$(dir $(2))))
  $(eval MAKEFILE_LIST :=)
  $(call dump-import-start,$(_include_stack))
  $(call dump-config-vals,$(2),before)
  $(eval include $(2))
  $(call dump-import-done,$(_include_stack))
  $(call dump-config-vals,$(2),after)
  $(eval _included := $(filter-out $(2),$(MAKEFILE_LIST)))
  $(eval MAKEFILE_LIST :=)
  $(eval LOCAL_PATH :=)
@@ -250,6 +254,7 @@ endef
#       of the default list semantics
#
define import-nodes
$(call dump-phase-start,$(1),$(2),$(3),$(4),build/make/core/node_fns.mk) \
$(if \
  $(foreach _in,$(2), \
    $(eval _node_import_context := _nic.$(1).[[$(_in)]]) \
@@ -263,5 +268,6 @@ $(if \
    $(if $(_include_stack),$(eval $(error ASSERTION FAILED: _include_stack \
                should be empty here: $(_include_stack))),) \
   ) \
,)
,) \
$(call dump-phase-end,build/make/core/node_fns.mk)
endef
+3 −1
Original line number Diff line number Diff line
@@ -460,7 +460,9 @@ define inherit-product
  $(eval current_mk := $(strip $(word 1,$(_include_stack)))) \
  $(eval inherit_var := PRODUCTS.$(current_mk).INHERITS_FROM) \
  $(eval $(inherit_var) := $(sort $($(inherit_var)) $(np))) \
  $(eval PARENT_PRODUCT_FILES := $(sort $(PARENT_PRODUCT_FILES) $(current_mk)))
  $(eval PARENT_PRODUCT_FILES := $(sort $(PARENT_PRODUCT_FILES) $(current_mk))) \
  $(call dump-inherit,$(strip $(word 1,$(_include_stack))),$(1)) \
  $(call dump-config-vals,$(current_mk),inherit)
endef

# Specifies a number of path prefixes, relative to PRODUCT_OUT, where the
+121 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.build.config;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Common parts between MakeConfig and the to-be-added GenericConfig, BazelConfig and SoongConfig.
 */
public class ConfigBase {
    protected String mPhase;
    protected List<String> mRootNodes;

    /**
     * State of the make varaible environment from before the first config file.
     */
    protected Map<String, Str> mInitialVariables = new HashMap();

    /**
     * State of the make varaible environment from after the first config file.
     */
    protected Map<String, Str> mFinalVariables = new HashMap();


    /**
     * The variables that are handled specially.
     */
    protected final TreeMap<String, VarType> mProductVars = new TreeMap();

    /**
     * Whether a product config variable is a list or single-value variable.
     */
    public enum VarType {
        LIST,
        SINGLE,
        UNKNOWN // For non-product vars
    }

    public void setPhase(String phase) {
        mPhase = phase;
    }

    public String getPhase() {
        return mPhase;
    }

    public void setRootNodes(List<String> filenames) {
        mRootNodes = new ArrayList(filenames);
    }

    public List<String> getRootNodes() {
        return mRootNodes;
    }

    public void addProductVar(String name, VarType type) {
        mProductVars.put(name, type);
    }

    public TreeMap<String, VarType> getProductVars() {
        return mProductVars;
    }

    public VarType getVarType(String name) {
        final VarType t = mProductVars.get(name);
        if (t != null) {
            return t;
        } else {
            return VarType.UNKNOWN;
        }
    }

    public boolean isProductVar(String name) {
        return mProductVars.get(name) != null;
    }

    /**
     * Return the state the make variable environment from before the first config file.
     */
    public Map<String, Str> getInitialVariables() {
        return mInitialVariables;
    }

    /**
     * Return the state the make variable environment from before the first config file.
     */
    public Map<String, Str> getFinalVariables() {
        return mFinalVariables;
    }

    /**
     * Copy common base class fields from that to this.
     */
    public void copyFrom(ConfigBase that) {
        setPhase(that.getPhase());
        setRootNodes(that.getRootNodes());
        for (Map.Entry<String, ConfigBase.VarType> entry: that.getProductVars().entrySet()) {
            addProductVar(entry.getKey(), entry.getValue());
        }
        mInitialVariables = new HashMap(that.getInitialVariables());
        mFinalVariables = new HashMap(that.getFinalVariables());
    }
}
+206 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.build.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Converts a MakeConfig into a Generic config by applying heuristics about
 * the types of variable assignments that we do.
 */
public class ConvertMakeToGenericConfig {
    private final Errors mErrors;

    public ConvertMakeToGenericConfig(Errors errors) {
        mErrors = errors;
    }

    public GenericConfig convert(MakeConfig make) {
        final GenericConfig result = new GenericConfig();

        // Base class fields
        result.copyFrom(make);

        // Each file
        for (MakeConfig.ConfigFile f: make.getConfigFiles()) {
            final GenericConfig.ConfigFile genericFile
                    = new GenericConfig.ConfigFile(f.getFilename());
            result.addConfigFile(genericFile);

            final List<MakeConfig.Block> blocks = f.getBlocks();

            // Some assertions:
            // TODO: Include better context for these errors.
            // There should always be at least a BEGIN and an AFTER, so assert this.
            if (blocks.size() < 2) {
                throw new RuntimeException("expected at least blocks.size() >= 2. Actcual size: "
                        + blocks.size());
            }
            if (blocks.get(0).getBlockType() != MakeConfig.BlockType.BEFORE) {
                throw new RuntimeException("expected first block to be BEFORE");
            }
            if (blocks.get(blocks.size() - 1).getBlockType() != MakeConfig.BlockType.AFTER) {
                throw new RuntimeException("expected first block to be AFTER");
            }
            // Everything in between should be an INHERIT block.
            for (int index = 1; index < blocks.size() - 1; index++) {
                if (blocks.get(index).getBlockType() != MakeConfig.BlockType.INHERIT) {
                    throw new RuntimeException("expected INHERIT at block " + index);
                }
            }

            // Each block represents a snapshot of the interpreter variable state (minus a few big
            // sets of variables which we don't export because they're used in the internals
            // of node_fns.mk, so we know they're not necessary here). The first (BEFORE) one
            // is everything that is set before the file is included, so it forms the base
            // for everything else.
            MakeConfig.Block prevBlock = blocks.get(0);

            for (int index = 1; index < blocks.size(); index++) {
                final MakeConfig.Block block = blocks.get(index);
                for (final Map.Entry<String, Str> entry: block.getVars().entrySet()) {
                    final String varName = entry.getKey();
                    final GenericConfig.Assign assign = convertAssignment(block.getBlockType(),
                            block.getInheritedFile(), make.getVarType(varName), varName,
                            entry.getValue(), prevBlock.getVar(varName));
                    if (assign != null) {
                        genericFile.addStatement(assign);
                    }
                }
                // Handle variables that are in prevBlock but not block -- they were
                // deleted. Is this even possible, or do they show up as ""?  We will
                // treat them as positive assigments to empty string
                for (String prevName: prevBlock.getVars().keySet()) {
                    if (!block.getVars().containsKey(prevName)) {
                        genericFile.addStatement(
                                new GenericConfig.Assign(prevName, new Str("")));
                    }
                }
                if (block.getBlockType() == MakeConfig.BlockType.INHERIT) {
                    genericFile.addStatement(
                            new GenericConfig.Inherit(block.getInheritedFile()));
                }
                // For next iteration
                prevBlock = block;
            }
        }
        return result;
    }

    /**
     * Converts one variable from a MakeConfig Block into a GenericConfig Assignment.
     */
    GenericConfig.Assign convertAssignment(MakeConfig.BlockType blockType, Str inheritedFile,
            ConfigBase.VarType varType, String varName, Str varVal, Str prevVal) {
        if (prevVal == null) {
            // New variable.
            return new GenericConfig.Assign(varName, varVal);
        } else if (!varVal.equals(prevVal)) {
            // The value changed from the last block.
            if (varVal.equals("")) {
                // It was set to empty
                return new GenericConfig.Assign(varName, varVal);
            } else {
                // Product vars have the @inherit processing. Other vars we
                // will just ignore and put in one section at the end, based
                // on the difference between the BEFORE and AFTER blocks.
                if (varType == ConfigBase.VarType.UNKNOWN) {
                    if (blockType == MakeConfig.BlockType.AFTER) {
                        // For UNKNOWN variables, we don't worry about the
                        // intermediate steps, just take the final value.
                        return new GenericConfig.Assign(varName, varVal);
                    } else {
                        return null;
                    }
                } else {
                    return convertInheritedVar(blockType, inheritedFile,
                            varName, varVal, prevVal);
                }
            }
        } else {
            // Variable not touched
            return null;
        }
    }

    /**
     * Handle the special inherited values, where the inherit-product puts in the
     * @inherit:... markers, adding Statements to the ConfigFile.
     */
    GenericConfig.Assign convertInheritedVar(MakeConfig.BlockType blockType, Str inheritedFile,
            String varName, Str varVal, Str prevVal) {
        String varText = varVal.toString();
        String prevText = prevVal.toString().trim();
        if (blockType == MakeConfig.BlockType.INHERIT) {
            // inherit-product appends @inherit:... so drop that.
            final String marker = "@inherit:" + inheritedFile;
            if (varText.endsWith(marker)) {
                varText = varText.substring(0, varText.length() - marker.length()).trim();
            } else {
                mErrors.ERROR_IMPROPER_PRODUCT_VAR_MARKER.add(varVal.getPosition(),
                        "Variable didn't end with marker \"" + marker + "\": " + varText);
            }
        }

        if (!varText.equals(prevText)) {
            // If the variable value was actually changed.
            final ArrayList<String> words = split(varText, prevText);
            if (words.size() == 0) {
                // Pure Assignment, none of the previous value is present.
                return new GenericConfig.Assign(varName, new Str(varVal.getPosition(), varText));
            } else {
                // Self referential value (prepend, append, both).
                if (words.size() > 2) {
                    // This is indicative of a construction that might not be quite
                    // what we want.  The above code will do something that works if it was
                    // of the form "VAR := a $(VAR) b $(VAR) c", but if the original code
                    // something else this won't work. This doesn't happen in AOSP, but
                    // it's a theoretically possibility, so someone might do it.
                    mErrors.WARNING_VARIABLE_RECURSION.add(varVal.getPosition(),
                            "Possible unsupported variable recursion: "
                                + varName + " = " + varVal + " (prev=" + prevVal + ")");
                }
                return new GenericConfig.Assign(varName, Str.toList(varVal.getPosition(), words));
            }
        } else {
            // Variable not touched
            return null;
        }
    }

    /**
     * Split 'haystack' on occurrences of 'needle'. Trims each string of whitespace
     * to preserve make list semantics.
     */
    private static ArrayList<String> split(String haystack, String needle) {
        final ArrayList<String> result = new ArrayList();
        final int needleLen = needle.length();
        if (needleLen == 0) {
            return result;
        }
        int start = 0;
        int end;
        while ((end = haystack.indexOf(needle, start)) >= 0) {
            result.add(haystack.substring(start, end).trim());
            start = end + needleLen;
        }
        result.add(haystack.substring(start).trim());
        return result;
    }
}
Loading