From 997086a5690742e1b1d721ec6b94bc0e7d7597fe Mon Sep 17 00:00:00 2001 From: Will Drewry Date: Wed, 28 Oct 2015 21:27:42 -0700 Subject: cfgtree.mk: file-based configuration cfgtree.mk is part of an effort to make product configuration less error-prone in part by removing superfluous Makefile editing. For instance, a common error is dropping the trailing backslash on PRODUCT_PACKAGES. Secondarily, simplifying product configuration storage enables cleaner automation. Brunch can use the same files to manage a product without an extra database or separate configuration tool (as it has today with config and reconfig). Tertiarily, a simple file loading model enables consistent enforcement (over $(shell cat ..)) and allows sub-makefiles to pull values from the product path. E.g., configuring a crash server or product id. This change adds cfgtree.mk which contains the function cfgtree-get. cfgtree-get will retrieve a file relative to a common configuration tree root, ignoring all file lines that begin with '#'. If a request is optional, cfgtree-get-if-exists should be used. Optional retrievals fail silently while required entries will cause the build to error out on the Makefile line requesting the value. For example, a new product makefile may look like: # Automatically generated by brunch. CFGTREE_ROOT := $(LOCAL_PATH) include device/generic/brillo/brillo_base.mk PRODUCT_NAME := $(call get_product_name_from_file) PRODUCT_BRAND := $(call cfgtree-get,brand) PRODUCT_DEVICE := $(call cfgtree-get,device) PRODUCT_MANUFACTURER := $(call cfgtree-get,manufacturer) PRODUCT_PACKAGES += $(call cfgtree-get-if-exists,packages) PRODUCT_COPY_FILES += $(call cfgtree-get-if-exists,copy_files) # Reserved for future use. CREATED_BY_BDK_VERSION := $(call cfgtree-get-if-exists,bdk/created_by_version) $(call bdk_update_hook) # Add any extra Android product.mk directive in extras.mk -include $(LOCAL_PATH)/extras.mk This allows direct inspection of the setting of important variables without requiring users to set them in the more finicky Makefile environment. Even though the approach keeps the magic to a minimum, Android's magic can still be used to introspect for product variable values with: m -j24 dump-products cfgtree.mk can be used directly in Android.mk files as well. For instance, the BRILLO_PRODUCT_ID can be populated using this mechanism by updating system/extras/brillo_config/Android.mk: BRILLO_PRODUCT_ID := $(call cfgtree-get,brillo/product_id) Of course, a two stage load-then-assign can be used if the ability to override is desired. (cfgtree_get should be used with immediate assignment ':='.) The same can be done in system/core/crash_reporter/Android.mk: BRILLO_CRASH_SERVER := $(call cfgtree-get,brillo/crash_server) (While the crash_reporter rule could just copy the file directly, it would mean that the value could no longer be defined as a variable or that the 'crash_server' rule would need to check for the files existence first. In either case, if the source data has changed, make should update on an incremental re-run.) BUG=25343470 DDOC=https://docs.google.com/document/d/1xZ1Fexl6j9JWK8OED8ZcAnvc-42w0jYiqDBaVfWqHH4/edit# TEST=manual. brunch needs to be updated still. Change-Id: Ice1235da88cf11ffa1da42460cda8cb6dd393c07 --- brillo_base.mk | 9 ++++++++ cfgtree.mk | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 cfgtree.mk diff --git a/brillo_base.mk b/brillo_base.mk index 0119b58..71d9fae 100644 --- a/brillo_base.mk +++ b/brillo_base.mk @@ -33,6 +33,9 @@ OSRELEASED_DIRECTORY := os-release.d PRODUCT_COPY_FILES += \ tools/bdk/VERSION:system/etc/$(OSRELEASED_DIRECTORY)/bdk_version +# Include the cfgtree helpers for loading config values from disk. +include device/generic/brillo/cfgtree.mk + # Skip API checks. WITHOUT_CHECK_API := true # Don't try to build and run all tests by default. Several tests have @@ -317,10 +320,16 @@ define generate-initrc-file endef # Called from the product makefile, it sets any derived defaults. +# DEPRECATED define set-product-defaults $(eval PRODUCT_NAME := $$(basename $$(notdir \ $$(filter $$(LOCAL_PATH)/%.mk,$$(MAKEFILE_LIST))))) endef +# Returns the product name as guessed from the calling file. +define get_product_name_from_file +$(firstword $(basename $(notdir \ + $(filter $(LOCAL_PATH)/%.mk,$(MAKEFILE_LIST))))) +endef HARDWARE_BSP_PREFIX := hardware/bsp HARDWARE_BSP_PREBUILTS_PREFIX := vendor/bsp diff --git a/cfgtree.mk b/cfgtree.mk new file mode 100644 index 0000000..dea3943 --- /dev/null +++ b/cfgtree.mk @@ -0,0 +1,71 @@ +# +# Copyright (C) 2015 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. +# + +# This file provides a helper which loads the contents of a given file +# relative to a common root path. The helper ensures there is consistent +# path resolution for loading and error messaging for missing or empty +# files that are required. +# +# The primary usage of this file is for loading product-level configuration +# from plaintext files. An example usage in a makefile is: +# BRILLO_CRASH_SERVER := $(call cfgtree_get,brillo/crash_server,required) +# +# Note the use of an immediate assignment. If any other variable assignment is +# used, be sure to test that it resolves as and when expected. +# +# Populating the file is as simple as: +# cd $PRODUCT_DIR +# mkdir -p config/brillo +# echo https://some.server/handle/crash.cgi > config/brillo/crash_server +# +# Any lines beginning with '#' will be ignored, but if a '#' is required, it +# can be inserted by simply starting the line with a space which will be +# stripped off during the loading process. +# +# Note, there is no central mapping of variables to files. If any files will +# be used from different contexts, be sure to keep all users in sync. +# +# For any PRODUCT_* variable set using this mechanism, it is possible to +# verify a correct assignment by calling: +# +# m dump-products +# + +# Path to load from +CFGTREE_ROOT ?= $(LOCAL_PATH) +CFGTREE_PREFIX ?= config +CFGTREE_PATH := $(CFGTREE_ROOT)/$(CFGTREE_PREFIX) + +# Command to run to dump the file content. +CFGTREE_CMD ?= grep -v -e '^$(shell printf '\043')' + +# $(1) is the file to load from cfgtree +# +# Returns the output of $(CFGTREE_CMD) $(1)/$(2) if the file exists. +# Normally, this is all the text excluding lines starting with '#'. +# +define cfgtree-get +$(strip \ + $(if $(filter 0,$(words $(call cfgtree-get-if-exists,$(1)))),\ + $(error Required configuration file \ + $(CFGTREE_PREFIX)/$(1) is empty or missing.),\ + $(call cfgtree-get-if-exists,$(1)))\ +) +endef + +define cfgtree-get-if-exists +$(strip $(shell $(CFGTREE_CMD) $(CFGTREE_PATH)/$(1) 2>/dev/null)) +endef -- cgit v1.2.3