blob: 065a7846f03af2d7005234d11f0298abdc09adaf [file] [log] [blame]
Stefan Reinauer172d3352015-03-24 21:46:52 +01001#!/bin/bash
2#
3# miniconfig - utility to minimize your coreboot config files
4#
5# Copyright 2015 Google Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; version 2 of the License.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16
17CONFIG=$1
18NEWCONFIG=$2
19
20CONF=build/util/kconfig/conf
21KCONFIG=src/Kconfig
22DOTCONFIG=.config
23PREVCONFIG=.config.prev
24TMPCONFIG=.config.mini
25
26recreate_config()
27{
28 $CONF --olddefconfig $KCONFIG &> /dev/null
29}
30
31if [ "$CONFIG" == "" ]; then
32 printf "usage: util/miniconfig/miniconfig [path to config file] <path to new config file>\n"
33 exit 0
34fi
35
36if [ ! -r "$CONFIG" ]; then
37 printf "Can't read $CONFIG.\n"
38 exit 1
39fi
40
41if [ "$CONFIG" == .config ]; then
42 printf "Can't use .config, it's overwritten. Make a backup.\n"
43 exit 1
44fi
45
46if [ ! -x "$CONF" ]; then
47 printf "conf utility at $CONF not available.\n"
48 exit 1
49fi
50
51# Start out by creating a default config file for a mainboard
52VENDOR=$( grep ^CONFIG_VENDOR "$CONFIG" )
53BOARD=$( grep ^CONFIG_BOARD "$CONFIG" | grep -v ROMSIZE | grep -v SPECIFIC_OPTIONS )
54
55printf "$VENDOR\n$BOARD\n" > "$TMPCONFIG"
56cp "$TMPCONFIG" "$DOTCONFIG"
57recreate_config
58
59LINES=$( cat "$CONFIG" | wc -l )
60CUR=1
61
62# Now go through each line of the existing, large config file, add it to our
63# new minimal config file, and see if it makes a difference when running "make
64# olddefconfig". If it does, keep the line, otherwise discard it.
65
66cat "$CONFIG" | while read L; do
67 printf "\rProcessing $CONFIG - $CUR / $LINES (%d%%)" $(( $CUR * 100 / $LINES))
68 mv "$DOTCONFIG" "$PREVCONFIG"
69 cp "$TMPCONFIG" "$DOTCONFIG"
70 echo "$L" >> "$DOTCONFIG"
71 recreate_config
72
73 if ! diff -q "$DOTCONFIG" "$PREVCONFIG" > /dev/null; then
74 echo "$L" >> "$TMPCONFIG"
75 fi
76 CUR=$(( $CUR + 1 ))
77done
78
79echo
80
81if [ "$NEWCONFIG" != "" ]; then
82 printf "Writing new, minimized config to $NEWCONFIG\n"
83 mv "$TMPCONFIG" "$NEWCONFIG"
84else
85 printf "Overwriting $CONFIG with new, minimized config.\n"
86 mv "$TMPCONFIG" "$CONFIG"
87fi