aboutsummaryrefslogtreecommitdiffstats
path: root/tools/kgit-config-cleaner
blob: 2880b7dcdbc1a1c6f30e64480ec566218d133e7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-only

#  Copyright (c) 2012 Wind River Systems, Inc.

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.

#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#  See the GNU General Public License for more details.


# example:
#  cd build/tmp/work/routerstationpro-poky-linux/linux-yocto-3.0.32*/linux/meta/cfg/yocto/standard/routerstationpro
#  kgit-config-cleaner -v --force --type all --audit-dir `pwd` \
#   -i ../../../kernel-cache/bsp/routerstationpro/routerstationpro.cfg  \
#   -o cleaned_config

usage()
{
cat << EOF

  kgit-config-cleaner [--force] [--dry-run] [--type <audit type list>] 
                      --audit-dir <dir> [ -k <keep configs> ] \
                      -i <input config> -o <output config>

   kgit-config-cleaner is run against an input file, and the results of an
   initial kernel configuration that has produced a Kconfig option audit. It
   produces a modified output file that can then be fed back into the process
   to refine a kernel configuration.


   type: comma separated list of audit types to process.
          all: the default. process all audit files
          errors: process fragment errors
          invalid: process invalid options
          mismatch: process mismatched options
          redefined: process redefined options
          non-hardare: process non-hardware options
     
   input config: The input file to be cleaned. This is typically a defconfig.

   output config: The filename of your choice. It is a copy of 
                  input config, with offending lines removed. 

   keep config: An input config file with a list of configurations that should
                be maintained regardless of other cleaning activities. These
                are typically config options that should be declared as
                'hardware' for the given board, and the keep entry would no
                longer be necessary.
                Note: This file is normally called 'hardware.cfg', so it can be
                reused by the build process in future iterations.
                
   audit dir: the directory that contains the output of a yocto kernel 
              configuration and audit. The input files in this directory are
              automatically processed and used to produce the output
 
   --force: force overwrite the output file
   --dry-run: don't modify any files, but dump what would be done
   --verbose: run with extra output
  
EOF
}

if [ -z "$1" ]; then
    usage
    exit
fi

while [ $# -gt 0 ]; do
    case "$1" in
    --i|-i) input=$2
	    shift
            ;;
    --o|-o)
            output=$2
            shift
            ;;
    --k|-k)
	    keep_config=$2
	    shift
	    ;;
    --audit-dir)
            audit_dir=$2
            shift
            ;;
    --type)
            audit_types=$2
            shift
            ;;
    --force|-f) force=t
            ;;
    --dry-run) dry_run=t
            ;;
    -v) verbose=t
            ;;
    *) break
            ;;
    esac
    shift
done


if [ -z "$input" ]; then
    usage
    exit 1
fi
if [ -z "$output" ]; then
    usage
    exit 1
fi

if [ -z "$audit_dir" ]; then
    usage
    exit 1
fi

if [ -z "$audit_types" ] || [ "$audit_types" == "all" ]; then
    fragment_errors_flag=t
    invalid_cfg_flag=t
    mismatch_flag=t
    redefinition_flag=t
    specified_non_hardware_flag=t
else
    types=$(echo $audit_types | tr "," "\n")
    for t in $types; do
	case $t in
	    errors)
		fragment_errors_flag=t
		;;
	    invalid)
		invalid_cfg_flag=t
		;;
	    mismatch)
		mismatch_flag=t
		;;
	    redefined)
		redefinition_flag=t
		;;
	    non-hardware)
		specified_non_hardware_flag=t
		;;
	    *) 
		echo "Invalid audit type $t passed"
		exit 1
		;;
	esac
    done
fi

echo "INFO: running configuration cleaning on:"
echo "$input"
echo ""

fragment_errors_file="fragment_errors.txt"
invalid_cfg_file="invalid.cfg"
mismatch_file="mismatch.txt"
redefinition_file="redefinition.txt"
specified_non_hardware_file="specified_non_hdw.cfg"
specified_hardware_file="specified_hdw.cfg"

process_cfg_only_audit_file()
{
    aud_file=$1
    in_file=$2
    out_file=$3
    override_file=$4
    count=0

    echo ""
    echo "INFO: processing `basename $aud_file`"

    rm -f /tmp/removal.txt

    # check every config specified in the intput file against the
    # supplied audit file. If the audit says it was bad, then it should
    # be removed. This is a brute force first step
    cfg_list=`grep '^\(# \)\{0,1\}CONFIG_[a-zA-Z0-9_]*[=\( is not set\)]' \
	      $in_file | sed 's/^\(# \)\{0,1\}\(CONFIG_[a-zA-Z0-9_]*\)[= ].*/\2/'`
    for o in $cfg_list; do
	remove=
	keep=
	option_line=`grep "^\(# \)\{0,1\}$o[=\( is not set\)]" $in_file`
	grep -q $o $aud_file
	if [ $? -eq 0 ]; then
	    # check to see if the option is in the keep file
	    if [ -n "$keep_config" ] || [ -n "$override_file" ]; then
		grep -q -w $o $keep_config $override_file
		if [ $? -eq 0 ]; then
		    if [ -n "$verbose" ]; then
			echo "   INFO: option $o would have been removed, but it is"
			echo "         specified in the keep configuration list"
		    fi
		    keep=t
		fi
	    fi
	    if [ -z "$keep" ]; then
		count=`expr $count + 1`
		if [ -n "$dry_run" ]; then
		    echo "   [dry-run] option $o should be removed from the configuration"
		else
		    remove=t
		    echo $option_line >> /tmp/removal.txt
		fi
	    fi
	fi
	if [ -n "$remove" ]; then
	    if [ -n "$verbose" ]; then
		echo "   INFO: removing $option_line"
	    fi
	    option_line=`echo $option_line | sed 's%/%.%g'`
	    sed -i "/^$option_line/d" $out_file
	    if [ $? -ne 0 ]; then
		echo "   WARNING: could not remove $option_line"
	    fi
	fi
    done

    if [ $count -eq 0 ]; then
	echo "   No `basename $aud_file` infractions are set by the input file"
    fi
}

# The file is not only a bare listing of CONFIG_<blah> and 
# hence requires special processing. This is a placeholder
# function
process_audit_file()
{
    true
}

generate_option_list()
{
    in_file=$1
    out_file=$2

    if [ -n "$verbose" ]; then
	echo "INFO: generating option list from $in_file"
    fi
    cat $in_file | grep -o 'CONFIG_[a-zA-Z0-9_]*' | uniq >> $out_file
}

process_audit_directory()
{
    if [ -n "$fragment_errors_flags" ] && [ -s "$audit_dir/$fragment_errors_file" ]; then
	echo "INFO: processing $fragment_errors_file"
	process_audit_file "$audit_dir/$fragment_errors_file" "$input" "$output"
    fi
    if [ -n "$invalid_cfg_flag" ] && [ -s "$audit_dir/$invalid_cfg_file" ]; then
	echo "INFO: processing $invalid_cfg_file"
    	process_cfg_only_audit_file "$audit_dir/$invalid_cfg_file" "$input" "$output"
    fi
    if [ -n "$mismatch_flag" ] && [ -s "$audit_dir/$mismatch_file" ]; then
	rm -f /tmp/mismatch.cfg
	generate_option_list $audit_dir/$mismatch_file /tmp/mismatch.cfg
	process_cfg_only_audit_file /tmp/mismatch.cfg "$input" "$output"
    fi
    if [ -n "$redefinition_flag" ] && [ -s "$audit_dir/$redefinition_file" ]; then
	rm -f /tmp/redefinition.cfg
	generate_option_list $audit_dir/$redefinition_file /tmp/redefinition.cfg
	process_cfg_only_audit_file /tmp/redefinition.cfg "$input" "$output" "$specified_hardware_file"
    fi
    if [ -n "$specified_non_hardware_flag" ] && [ -s "$audit_dir/$specified_non_hardware_file" ]; then
    	process_cfg_only_audit_file "$audit_dir/$specified_non_hardware_file" "$input" "$output"
    fi
}

if [ -s "$output" ] && [ -z "$force" ]; then
    echo "output file $output exists, use -f to force output"
    exit 1
fi

cp -f $input $output

process_audit_directory

echo "INFO: processing complete, output is saved in file: $output"