14b5f7214Sjohn stultz#!/bin/sh 20c874100SMasahiro Yamada# SPDX-License-Identifier: GPL-2.0 30c874100SMasahiro Yamada# 44b5f7214Sjohn stultz# merge_config.sh - Takes a list of config fragment values, and merges 54b5f7214Sjohn stultz# them one by one. Provides warnings on overridden values, and specified 64b5f7214Sjohn stultz# values that did not make it to the resulting .config file (due to missed 74b5f7214Sjohn stultz# dependencies or config symbol removal). 84b5f7214Sjohn stultz# 94b5f7214Sjohn stultz# Portions reused from kconf_check and generate_cfg: 104b5f7214Sjohn stultz# http://git.yoctoproject.org/cgit/cgit.cgi/yocto-kernel-tools/tree/tools/kconf_check 114b5f7214Sjohn stultz# http://git.yoctoproject.org/cgit/cgit.cgi/yocto-kernel-tools/tree/tools/generate_cfg 124b5f7214Sjohn stultz# 134b5f7214Sjohn stultz# Copyright (c) 2009-2010 Wind River Systems, Inc. 144b5f7214Sjohn stultz# Copyright 2011 Linaro 154b5f7214Sjohn stultz 16cdfca821SMark Brownset -e 17cdfca821SMark Brown 184b5f7214Sjohn stultzclean_up() { 194b5f7214Sjohn stultz rm -f $TMP_FILE 20a9b72284SAnders Roxell rm -f $MERGE_FILE 214b5f7214Sjohn stultz} 224b5f7214Sjohn stultz 234b5f7214Sjohn stultzusage() { 244b5f7214Sjohn stultz echo "Usage: $0 [OPTIONS] [CONFIG [...]]" 254b5f7214Sjohn stultz echo " -h display this help text" 264b5f7214Sjohn stultz echo " -m only merge the fragments, do not execute the make command" 274b5f7214Sjohn stultz echo " -n use allnoconfig instead of alldefconfig" 289875c42dSJohn Stultz echo " -r list redundant entries when merging fragments" 29a9b72284SAnders Roxell echo " -y make builtin have precedence over modules" 30ed94fea5SGabriel de Perthuis echo " -O dir to put generated output files. Consider setting \$KCONFIG_CONFIG instead." 31a325db2dSMatthias Maennich echo " -s strict mode. Fail if the fragment redefines any value." 3246dff8d7SMark Brown echo " -Q disable warning messages for overridden options." 332cd3faf8SPetr Vorel echo 342cd3faf8SPetr Vorel echo "Used prefix: '$CONFIG_PREFIX'. You can redefine it with \$CONFIG_ environment variable." 354b5f7214Sjohn stultz} 364b5f7214Sjohn stultz 37bc8f8f5fSMasahiro YamadaRUNMAKE=true 384b5f7214Sjohn stultzALLTARGET=alldefconfig 399875c42dSJohn StultzWARNREDUN=false 40a9b72284SAnders RoxellBUILTIN=false 41409f117eSZhangfei GaoOUTPUT=. 42a325db2dSMatthias MaennichSTRICT=false 432cd3faf8SPetr VorelCONFIG_PREFIX=${CONFIG_-CONFIG_} 4446dff8d7SMark BrownWARNOVERRIDE=echo 454b5f7214Sjohn stultz 464b5f7214Sjohn stultzwhile true; do 474b5f7214Sjohn stultz case $1 in 484b5f7214Sjohn stultz "-n") 494b5f7214Sjohn stultz ALLTARGET=allnoconfig 504b5f7214Sjohn stultz shift 514b5f7214Sjohn stultz continue 524b5f7214Sjohn stultz ;; 534b5f7214Sjohn stultz "-m") 54bc8f8f5fSMasahiro Yamada RUNMAKE=false 554b5f7214Sjohn stultz shift 564b5f7214Sjohn stultz continue 574b5f7214Sjohn stultz ;; 584b5f7214Sjohn stultz "-h") 594b5f7214Sjohn stultz usage 604b5f7214Sjohn stultz exit 614b5f7214Sjohn stultz ;; 629875c42dSJohn Stultz "-r") 639875c42dSJohn Stultz WARNREDUN=true 649875c42dSJohn Stultz shift 659875c42dSJohn Stultz continue 669875c42dSJohn Stultz ;; 67a9b72284SAnders Roxell "-y") 68a9b72284SAnders Roxell BUILTIN=true 69a9b72284SAnders Roxell shift 70a9b72284SAnders Roxell continue 71a9b72284SAnders Roxell ;; 72409f117eSZhangfei Gao "-O") 73409f117eSZhangfei Gao if [ -d $2 ];then 74409f117eSZhangfei Gao OUTPUT=$(echo $2 | sed 's/\/*$//') 75409f117eSZhangfei Gao else 76409f117eSZhangfei Gao echo "output directory $2 does not exist" 1>&2 77409f117eSZhangfei Gao exit 1 78409f117eSZhangfei Gao fi 79409f117eSZhangfei Gao shift 2 80409f117eSZhangfei Gao continue 81409f117eSZhangfei Gao ;; 82a325db2dSMatthias Maennich "-s") 83a325db2dSMatthias Maennich STRICT=true 84a325db2dSMatthias Maennich shift 85a325db2dSMatthias Maennich continue 86a325db2dSMatthias Maennich ;; 8746dff8d7SMark Brown "-Q") 8846dff8d7SMark Brown WARNOVERRIDE=true 8946dff8d7SMark Brown shift 9046dff8d7SMark Brown continue 9146dff8d7SMark Brown ;; 924b5f7214Sjohn stultz *) 934b5f7214Sjohn stultz break 944b5f7214Sjohn stultz ;; 954b5f7214Sjohn stultz esac 964b5f7214Sjohn stultzdone 974b5f7214Sjohn stultz 982163e7b3SGabriel de Perthuisif [ "$#" -lt 1 ] ; then 9909950bc2SOlof Johansson usage 10009950bc2SOlof Johansson exit 10109950bc2SOlof Johanssonfi 10209950bc2SOlof Johansson 103ed94fea5SGabriel de Perthuisif [ -z "$KCONFIG_CONFIG" ]; then 104ed94fea5SGabriel de Perthuis if [ "$OUTPUT" != . ]; then 105ed94fea5SGabriel de Perthuis KCONFIG_CONFIG=$(readlink -m -- "$OUTPUT/.config") 106ed94fea5SGabriel de Perthuis else 107ed94fea5SGabriel de Perthuis KCONFIG_CONFIG=.config 108ed94fea5SGabriel de Perthuis fi 109ed94fea5SGabriel de Perthuisfi 110ed94fea5SGabriel de Perthuis 11109280615SJosh BoyerINITFILE=$1 11209280615SJosh Boyershift; 1134b5f7214Sjohn stultz 114b9fe99c5SMasahiro Yamadaif [ ! -r "$INITFILE" ]; then 115b9fe99c5SMasahiro Yamada echo "The base file '$INITFILE' does not exist. Exit." >&2 116b9fe99c5SMasahiro Yamada exit 1 117b9fe99c5SMasahiro Yamadafi 118b9fe99c5SMasahiro Yamada 1194b5f7214Sjohn stultzMERGE_LIST=$* 1206bbe4385SMasahiro YamadaSED_CONFIG_EXP1="s/^\(${CONFIG_PREFIX}[a-zA-Z0-9_]*\)=.*/\1/p" 1216bbe4385SMasahiro YamadaSED_CONFIG_EXP2="s/^# \(${CONFIG_PREFIX}[a-zA-Z0-9_]*\) is not set$/\1/p" 1222cd3faf8SPetr Vorel 1234b5f7214Sjohn stultzTMP_FILE=$(mktemp ./.tmp.config.XXXXXXXXXX) 124a9b72284SAnders RoxellMERGE_FILE=$(mktemp ./.merge_tmp.config.XXXXXXXXXX) 1254b5f7214Sjohn stultz 12609280615SJosh Boyerecho "Using $INITFILE as base" 127cdfca821SMark Brown 128cdfca821SMark Browntrap clean_up EXIT 129cdfca821SMark Brown 13009280615SJosh Boyercat $INITFILE > $TMP_FILE 13109280615SJosh Boyer 1324980bdf0SMasahiro Yamada# Merge files, printing warnings on overridden values 133a9b72284SAnders Roxellfor ORIG_MERGE_FILE in $MERGE_LIST ; do 134a9b72284SAnders Roxell echo "Merging $ORIG_MERGE_FILE" 135a9b72284SAnders Roxell if [ ! -r "$ORIG_MERGE_FILE" ]; then 136a9b72284SAnders Roxell echo "The merge file '$ORIG_MERGE_FILE' does not exist. Exit." >&2 13778a6854eSSam Bobroff exit 1 13878a6854eSSam Bobroff fi 139a9b72284SAnders Roxell cat $ORIG_MERGE_FILE > $MERGE_FILE 1406bbe4385SMasahiro Yamada CFG_LIST=$(sed -n -e "$SED_CONFIG_EXP1" -e "$SED_CONFIG_EXP2" $MERGE_FILE) 1414b5f7214Sjohn stultz 1424b5f7214Sjohn stultz for CFG in $CFG_LIST ; do 1433a975b8cSMasahiro Yamada grep -q -w $CFG $TMP_FILE || continue 1444b5f7214Sjohn stultz PREV_VAL=$(grep -w $CFG $TMP_FILE) 1454b5f7214Sjohn stultz NEW_VAL=$(grep -w $CFG $MERGE_FILE) 146a9b72284SAnders Roxell BUILTIN_FLAG=false 147a9b72284SAnders Roxell if [ "$BUILTIN" = "true" ] && [ "${NEW_VAL#CONFIG_*=}" = "m" ] && [ "${PREV_VAL#CONFIG_*=}" = "y" ]; then 1481073c15fSMirsad Goran Todorovac ${WARNOVERRIDE} Previous value: $PREV_VAL 14946dff8d7SMark Brown ${WARNOVERRIDE} New value: $NEW_VAL 15046dff8d7SMark Brown ${WARNOVERRIDE} -y passed, will not demote y to m 15146dff8d7SMark Brown ${WARNOVERRIDE} 152a9b72284SAnders Roxell BUILTIN_FLAG=true 153a9b72284SAnders Roxell elif [ "x$PREV_VAL" != "x$NEW_VAL" ] ; then 15446dff8d7SMark Brown ${WARNOVERRIDE} Value of $CFG is redefined by fragment $ORIG_MERGE_FILE: 15546dff8d7SMark Brown ${WARNOVERRIDE} Previous value: $PREV_VAL 15646dff8d7SMark Brown ${WARNOVERRIDE} New value: $NEW_VAL 15746dff8d7SMark Brown ${WARNOVERRIDE} 158a325db2dSMatthias Maennich if [ "$STRICT" = "true" ]; then 159a325db2dSMatthias Maennich STRICT_MODE_VIOLATED=true 160a325db2dSMatthias Maennich fi 1619875c42dSJohn Stultz elif [ "$WARNREDUN" = "true" ]; then 16246dff8d7SMark Brown ${WARNOVERRIDE} Value of $CFG is redundant by fragment $ORIG_MERGE_FILE: 1634b5f7214Sjohn stultz fi 164a9b72284SAnders Roxell if [ "$BUILTIN_FLAG" = "false" ]; then 1654b5f7214Sjohn stultz sed -i "/$CFG[ =]/d" $TMP_FILE 166a9b72284SAnders Roxell else 167a9b72284SAnders Roxell sed -i "/$CFG[ =]/d" $MERGE_FILE 168a9b72284SAnders Roxell fi 1694b5f7214Sjohn stultz done 170*86a1aaeeSAnders Roxell # In case the previous file lacks a new line at the end 171*86a1aaeeSAnders Roxell echo >> $TMP_FILE 1724b5f7214Sjohn stultz cat $MERGE_FILE >> $TMP_FILE 1734b5f7214Sjohn stultzdone 1744b5f7214Sjohn stultz 175a325db2dSMatthias Maennichif [ "$STRICT_MODE_VIOLATED" = "true" ]; then 176a325db2dSMatthias Maennich echo "The fragment redefined a value and strict mode had been passed." 177a325db2dSMatthias Maennich exit 1 178a325db2dSMatthias Maennichfi 179a325db2dSMatthias Maennich 180bc8f8f5fSMasahiro Yamadaif [ "$RUNMAKE" = "false" ]; then 181ed94fea5SGabriel de Perthuis cp -T -- "$TMP_FILE" "$KCONFIG_CONFIG" 1824b5f7214Sjohn stultz echo "#" 183ed94fea5SGabriel de Perthuis echo "# merged configuration written to $KCONFIG_CONFIG (needs make)" 1844b5f7214Sjohn stultz echo "#" 1854b5f7214Sjohn stultz exit 1864b5f7214Sjohn stultzfi 1874b5f7214Sjohn stultz 188a45c7dfbSJohn Stultz# If we have an output dir, setup the O= argument, otherwise leave 189a45c7dfbSJohn Stultz# it blank, since O=. will create an unnecessary ./source softlink 190a45c7dfbSJohn StultzOUTPUT_ARG="" 191a45c7dfbSJohn Stultzif [ "$OUTPUT" != "." ] ; then 192a45c7dfbSJohn Stultz OUTPUT_ARG="O=$OUTPUT" 193a45c7dfbSJohn Stultzfi 194a45c7dfbSJohn Stultz 195a45c7dfbSJohn Stultz 1964b5f7214Sjohn stultz# Use the merged file as the starting point for: 1974b5f7214Sjohn stultz# alldefconfig: Fills in any missing symbols with Kconfig default 1984b5f7214Sjohn stultz# allnoconfig: Fills in any missing symbols with # CONFIG_* is not set 199a45c7dfbSJohn Stultzmake KCONFIG_ALLCONFIG=$TMP_FILE $OUTPUT_ARG $ALLTARGET 2004b5f7214Sjohn stultz 2014b5f7214Sjohn stultz 2024b5f7214Sjohn stultz# Check all specified config values took (might have missed-dependency issues) 2036bbe4385SMasahiro Yamadafor CFG in $(sed -n -e "$SED_CONFIG_EXP1" -e "$SED_CONFIG_EXP2" $TMP_FILE); do 2044b5f7214Sjohn stultz 205320d41bbSJohn Stultz REQUESTED_VAL=$(grep -w -e "$CFG" $TMP_FILE) 20660bef52cSGuillaume Tucker ACTUAL_VAL=$(grep -w -e "$CFG" "$KCONFIG_CONFIG" || true) 2074b5f7214Sjohn stultz if [ "x$REQUESTED_VAL" != "x$ACTUAL_VAL" ] ; then 2084b5f7214Sjohn stultz echo "Value requested for $CFG not in final .config" 2094b5f7214Sjohn stultz echo "Requested value: $REQUESTED_VAL" 2104b5f7214Sjohn stultz echo "Actual value: $ACTUAL_VAL" 2114b5f7214Sjohn stultz echo "" 2124b5f7214Sjohn stultz fi 2134b5f7214Sjohn stultzdone 214