1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3# 4# merge_config.sh - Takes a list of config fragment values, and merges 5# them one by one. Provides warnings on overridden values, and specified 6# values that did not make it to the resulting .config file (due to missed 7# dependencies or config symbol removal). 8# 9# Portions reused from kconf_check and generate_cfg: 10# http://git.yoctoproject.org/cgit/cgit.cgi/yocto-kernel-tools/tree/tools/kconf_check 11# http://git.yoctoproject.org/cgit/cgit.cgi/yocto-kernel-tools/tree/tools/generate_cfg 12# 13# Copyright (c) 2009-2010 Wind River Systems, Inc. 14# Copyright 2011 Linaro 15# 16# Based on buildroot(2022.08.x)/support/kconfig/merge_config.sh 17# Copyright (c) 2022 Jeffy Chen <jeffy.chen@rock-chips.com> 18# 19# This program is free software; you can redistribute it and/or modify 20# it under the terms of the GNU General Public License version 2 as 21# published by the Free Software Foundation. 22# 23# This program is distributed in the hope that it will be useful, 24# but WITHOUT ANY WARRANTY; without even the implied warranty of 25# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 26# See the GNU General Public License for more details. 27 28set -e 29 30clean_up() { 31 rm -f $TMP_FILE 32 rm -f $TMP_MERGE_FILE 33} 34 35usage() { 36 echo "Usage: $0 [OPTIONS] [CONFIG [...]]" 37 echo " -h display this help text" 38 echo " -m only merge the fragments, do not execute the make command" 39 echo " -n use allnoconfig instead of alldefconfig" 40 echo " -r list redundant entries when merging fragments" 41 echo " -O dir to put generated output files. Consider setting \$KCONFIG_CONFIG instead." 42} 43 44RUNMAKE=true 45ALLTARGET=alldefconfig 46WARNREDUN=false 47OUTPUT=. 48 49while true; do 50 case $1 in 51 "-n") 52 ALLTARGET=allnoconfig 53 shift 54 continue 55 ;; 56 "-m") 57 RUNMAKE=false 58 shift 59 continue 60 ;; 61 "-h") 62 usage 63 exit 64 ;; 65 "-r") 66 WARNREDUN=true 67 shift 68 continue 69 ;; 70 "-O") 71 if [ -d $2 ];then 72 OUTPUT=$(echo $2 | sed 's/\/*$//') 73 else 74 echo "output directory $2 does not exist" 1>&2 75 exit 1 76 fi 77 shift 2 78 continue 79 ;; 80 *) 81 break 82 ;; 83 esac 84done 85 86if [ "$#" -lt 1 ] ; then 87 usage 88 exit 89fi 90 91if [ -z "$KCONFIG_CONFIG" ]; then 92 if [ "$OUTPUT" != . ]; then 93 KCONFIG_CONFIG=$(readlink -m -- "$OUTPUT/.config") 94 else 95 KCONFIG_CONFIG=.config 96 fi 97fi 98 99INITFILE=$1 100shift; 101 102if [ ! -r "$INITFILE" ]; then 103 echo "The base file '$INITFILE' does not exist. Exit." >&2 104 exit 1 105fi 106 107MERGE_LIST=$* 108SED_CONFIG_EXP1="s/^\([a-zA-Z0-9_]*\)+\{0,1\}=.*/\1/p" 109SED_CONFIG_EXP2="s/^# \([a-zA-Z0-9_]*\) is not set$/\1/p" 110SED_CONFIG_EXP3="s/^# \([a-zA-Z0-9_]*\) is reset to default$/\1/p" 111 112SED_CLEAR_EXP1="s/^\([a-zA-Z0-9_]*\)+=/\1=/" # Append 113SED_CLEAR_EXP2="/^# \([a-zA-Z0-9_]*\) is reset to default$/d" # Reset 114 115TMP_FILE=$(mktemp ./.tmp.config.XXXXXXXXXX) 116TMP_MERGE_FILE=$(mktemp ./.merge_tmp.config.XXXXXXXXXX) 117 118echo "Using $INITFILE as base" 119 120trap clean_up EXIT 121 122cat $INITFILE | sed -e "$SED_CLEAR_EXP1" -e "$SED_CLEAR_EXP2" >> $TMP_FILE 123 124# Merge files, printing warnings on overridden values 125for MERGE_FILE in $MERGE_LIST ; do 126 echo "Merging $MERGE_FILE" 127 if [ ! -r "$MERGE_FILE" ]; then 128 echo "The merge file '$MERGE_FILE' does not exist. Exit." >&2 129 exit 1 130 fi 131 CFG_LIST=$(sed -n -e "$SED_CONFIG_EXP1" -e "$SED_CONFIG_EXP2" \ 132 -e "$SED_CONFIG_EXP3" $MERGE_FILE) 133 134 cat $MERGE_FILE > $TMP_MERGE_FILE 135 for CFG in $CFG_LIST ; do 136 grep -q -w $CFG $TMP_FILE || continue 137 PREV_VAL=$(grep -w $CFG $TMP_FILE) 138 ORIG_NEW_VAL=$(grep -w $CFG $TMP_MERGE_FILE) 139 140 if [ "$ORIG_NEW_VAL" = "# $CFG is reset to default" ]; then 141 # Reset 142 NEW_VAL= 143 elif [ "${ORIG_NEW_VAL%=*}" = "$CFG+" ]; then 144 # Append 145 if echo "$PREV_VAL" | grep -q "^#"; then 146 # Replace 147 NEW_VAL=${ORIG_NEW_VAL/+=/=} 148 else 149 ORIG_NEW_STR=$(echo "$ORIG_NEW_VAL" | \ 150 sed "s/^.*=\"\(.*\)\"/\1/") 151 PREV_STR=$(echo "$PREV_VAL" | \ 152 sed "s/^.*=\"\(.*\)\"/\1/") 153 154 NEW_STR= 155 for s in $ORIG_NEW_STR; do 156 echo "$PREV_STR" | xargs -n 1 | \ 157 grep "^$s$" >/dev/null || \ 158 NEW_STR="$NEW_STR $s" 159 done 160 161 if [ -z "$NEW_STR" ]; then 162 # Skip 163 NEW_VAL="$PREV_VAL" 164 else 165 # Append string value 166 NEW_VAL="${PREV_VAL%\"} ${NEW_STR# }\"" 167 fi 168 169 # Update fragment 170 sed -i "s#\<$CFG\>.*#$NEW_VAL#" $TMP_MERGE_FILE 171 fi 172 else 173 # Normal replace 174 NEW_VAL=$ORIG_NEW_VAL 175 fi 176 177 if [ "$PREV_VAL" != "$NEW_VAL" ] ; then 178 echo "Value of $CFG is redefined by $MERGE_FILE:" 179 echo -e "Previous value:\t$PREV_VAL" 180 if [ "$NEW_VAL" != "$ORIG_NEW_VAL" ]; then 181 echo -e "Modify value:\t$ORIG_NEW_VAL" 182 fi 183 echo -e "New value:\t$NEW_VAL" 184 echo 185 elif [ "$WARNREDUN" = "true" ]; then 186 echo "Value of $CFG is redundant by $MERGE_FILE:" 187 fi 188 sed -i "/\<$CFG[ =]/d" $TMP_FILE 189 done 190 cat $TMP_MERGE_FILE | sed -e "$SED_CLEAR_EXP1" \ 191 -e "$SED_CLEAR_EXP2" >> $TMP_FILE 192done 193 194if [ "$RUNMAKE" = "false" ]; then 195 cp -T -- "$TMP_FILE" "$KCONFIG_CONFIG" 196 echo "#" 197 echo "# merged configuration written to $KCONFIG_CONFIG (needs make)" 198 echo "#" 199 exit 200fi 201 202# If we have an output dir, setup the O= argument, otherwise leave 203# it blank, since O=. will create an unnecessary ./source softlink 204OUTPUT_ARG="" 205if [ "$OUTPUT" != "." ] ; then 206 OUTPUT_ARG="O=$OUTPUT" 207fi 208 209 210# Use the merged file as the starting point for: 211# alldefconfig: Fills in any missing symbols with Kconfig default 212# allnoconfig: Fills in any missing symbols with # CONFIG_* is not set 213make KCONFIG_ALLCONFIG=$TMP_FILE $OUTPUT_ARG $ALLTARGET 214 215 216# Check all specified config values took (might have missed-dependency issues) 217for CFG in $(sed -n -e "$SED_CONFIG_EXP1" -e "$SED_CONFIG_EXP2" $TMP_FILE); do 218 219 REQUESTED_VAL=$(grep -w -e "$CFG" $TMP_FILE) 220 ACTUAL_VAL=$(grep -w -e "$CFG" "$KCONFIG_CONFIG" || true) 221 if [ "$REQUESTED_VAL" != "$ACTUAL_VAL" ] ; then 222 echo "Value requested for $CFG not in final .config" 223 echo "Requested value: $REQUESTED_VAL" 224 echo "Actual value: $ACTUAL_VAL" 225 echo "" 226 fi 227done 228