Yegor Timoshenko | c2e4941 | 2018-10-07 01:58:27 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Stefan Reinauer | 172d335 | 2015-03-24 21:46:52 +0100 | [diff] [blame] | 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 | |
| 17 | CONFIG=$1 |
| 18 | NEWCONFIG=$2 |
| 19 | |
| 20 | CONF=build/util/kconfig/conf |
| 21 | KCONFIG=src/Kconfig |
| 22 | DOTCONFIG=.config |
| 23 | PREVCONFIG=.config.prev |
| 24 | TMPCONFIG=.config.mini |
| 25 | |
| 26 | recreate_config() |
| 27 | { |
| 28 | $CONF --olddefconfig $KCONFIG &> /dev/null |
| 29 | } |
| 30 | |
| 31 | if [ "$CONFIG" == "" ]; then |
| 32 | printf "usage: util/miniconfig/miniconfig [path to config file] <path to new config file>\n" |
| 33 | exit 0 |
| 34 | fi |
| 35 | |
| 36 | if [ ! -r "$CONFIG" ]; then |
| 37 | printf "Can't read $CONFIG.\n" |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | if [ "$CONFIG" == .config ]; then |
| 42 | printf "Can't use .config, it's overwritten. Make a backup.\n" |
| 43 | exit 1 |
| 44 | fi |
| 45 | |
| 46 | if [ ! -x "$CONF" ]; then |
| 47 | printf "conf utility at $CONF not available.\n" |
| 48 | exit 1 |
| 49 | fi |
| 50 | |
| 51 | # Start out by creating a default config file for a mainboard |
| 52 | VENDOR=$( grep ^CONFIG_VENDOR "$CONFIG" ) |
| 53 | BOARD=$( grep ^CONFIG_BOARD "$CONFIG" | grep -v ROMSIZE | grep -v SPECIFIC_OPTIONS ) |
| 54 | |
| 55 | printf "$VENDOR\n$BOARD\n" > "$TMPCONFIG" |
| 56 | cp "$TMPCONFIG" "$DOTCONFIG" |
| 57 | recreate_config |
| 58 | |
| 59 | LINES=$( cat "$CONFIG" | wc -l ) |
| 60 | CUR=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 | |
| 66 | cat "$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 )) |
| 77 | done |
| 78 | |
| 79 | echo |
| 80 | |
| 81 | if [ "$NEWCONFIG" != "" ]; then |
| 82 | printf "Writing new, minimized config to $NEWCONFIG\n" |
| 83 | mv "$TMPCONFIG" "$NEWCONFIG" |
| 84 | else |
| 85 | printf "Overwriting $CONFIG with new, minimized config.\n" |
| 86 | mv "$TMPCONFIG" "$CONFIG" |
| 87 | fi |