1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Usage: configinit.sh config-spec-file build-output-dir results-dir
5#
6# Create a .config file from the spec file.  Run from the kernel source tree.
7# Exits with 0 if all went well, with 1 if all went well but the config
8# did not match, and some other number for other failures.
9#
10# The first argument is the .config specification file, which contains
11# desired settings, for example, "CONFIG_NO_HZ=y".  For best results,
12# this should be a full pathname.
13#
14# The second argument is a optional path to a build output directory,
15# for example, "O=/tmp/foo".  If this argument is omitted, the .config
16# file will be generated directly in the current directory.
17#
18# Copyright (C) IBM Corporation, 2013
19#
20# Authors: Paul E. McKenney <paulmck@linux.ibm.com>
21
22T=${TMPDIR-/tmp}/configinit.sh.$$
23trap 'rm -rf $T' 0
24mkdir $T
25
26# Capture config spec file.
27
28c=$1
29buildloc=$2
30resdir=$3
31builddir=
32if echo $buildloc | grep -q '^O='
33then
34	builddir=`echo $buildloc | sed -e 's/^O=//'`
35	if test ! -d $builddir
36	then
37		mkdir $builddir
38	fi
39else
40	echo Bad build directory: \"$buildloc\"
41	exit 2
42fi
43
44sed -e 's/^\(CONFIG[0-9A-Z_]*\)=.*$/grep -v "^# \1" |/' < $c > $T/u.sh
45sed -e 's/^\(CONFIG[0-9A-Z_]*=\).*$/grep -v \1 |/' < $c >> $T/u.sh
46grep '^grep' < $T/u.sh > $T/upd.sh
47echo "cat - $c" >> $T/upd.sh
48make mrproper
49make $buildloc distclean > $resdir/Make.distclean 2>&1
50make $buildloc $TORTURE_DEFCONFIG > $resdir/Make.defconfig.out 2>&1
51mv $builddir/.config $builddir/.config.sav
52sh $T/upd.sh < $builddir/.config.sav > $builddir/.config
53cp $builddir/.config $builddir/.config.new
54yes '' | make $buildloc oldconfig > $resdir/Make.oldconfig.out 2> $resdir/Make.oldconfig.err
55
56# verify new config matches specification.
57configcheck.sh $builddir/.config $c
58
59exit 0
60