xref: /rk3399_rockchip-uboot/tools/moveconfig.py (revision 7740f653e6b3634383603e31ad2f3decac8becf1)
1#!/usr/bin/env python2
2#
3# Author: Masahiro Yamada <yamada.masahiro@socionext.com>
4#
5# SPDX-License-Identifier:	GPL-2.0+
6#
7
8"""
9Move config options from headers to defconfig files.
10
11Since Kconfig was introduced to U-Boot, we have worked on moving
12config options from headers to Kconfig (defconfig).
13
14This tool intends to help this tremendous work.
15
16
17Usage
18-----
19
20This tool takes one input file.  (let's say 'recipe' file here.)
21The recipe describes the list of config options you want to move.
22Each line takes the form:
23<config_name> <type> <default>
24(the fields must be separated with whitespaces.)
25
26<config_name> is the name of config option.
27
28<type> is the type of the option.  It must be one of bool, tristate,
29string, int, and hex.
30
31<default> is the default value of the option.  It must be appropriate
32value corresponding to the option type.  It must be either y or n for
33the bool type.  Tristate options can also take m (although U-Boot has
34not supported the module feature).
35
36You can add two or more lines in the recipe file, so you can move
37multiple options at once.
38
39Let's say, for example, you want to move CONFIG_CMD_USB and
40CONFIG_SYS_TEXT_BASE.
41
42The type should be bool, hex, respectively.  So, the recipe file
43should look like this:
44
45  $ cat recipe
46  CONFIG_CMD_USB bool n
47  CONFIG_SYS_TEXT_BASE hex 0x00000000
48
49Next you must edit the Kconfig to add the menu entries for the configs
50you are moving.
51
52And then run this tool giving the file name of the recipe
53
54  $ tools/moveconfig.py recipe
55
56The tool walks through all the defconfig files to move the config
57options specified by the recipe file.
58
59The log is also displayed on the terminal.
60
61Each line is printed in the format
62<defconfig_name>   :  <action>
63
64<defconfig_name> is the name of the defconfig
65(without the suffix _defconfig).
66
67<action> shows what the tool did for that defconfig.
68It looks like one of the followings:
69
70 - Move 'CONFIG_... '
71   This config option was moved to the defconfig
72
73 - Default value 'CONFIG_...'.  Do nothing.
74   The value of this option is the same as default.
75   We do not have to add it to the defconfig.
76
77 - 'CONFIG_...' already exists in Kconfig.  Do nothing.
78   This config option is already defined in Kconfig.
79   We do not need/want to touch it.
80
81 - Undefined.  Do nothing.
82   This config option was not found in the config header.
83   Nothing to do.
84
85 - Failed to process.  Skip.
86   An error occurred during processing this defconfig.  Skipped.
87   (If -e option is passed, the tool exits immediately on error.)
88
89Finally, you will be asked, Clean up headers? [y/n]:
90
91If you say 'y' here, the unnecessary config defines are removed
92from the config headers (include/configs/*.h).
93It just uses the regex method, so you should not rely on it.
94Just in case, please do 'git diff' to see what happened.
95
96
97How does it works?
98------------------
99
100This tool runs configuration and builds include/autoconf.mk for every
101defconfig.  The config options defined in Kconfig appear in the .config
102file (unless they are hidden because of unmet dependency.)
103On the other hand, the config options defined by board headers are seen
104in include/autoconf.mk.  The tool looks for the specified options in both
105of them to decide the appropriate action for the options.  If the option
106is found in the .config or the value is the same as the specified default,
107the option does not need to be touched.  If the option is found in
108include/autoconf.mk, but not in the .config, and the value is different
109from the default, the tools adds the option to the defconfig.
110
111For faster processing, this tool handles multi-threading.  It creates
112separate build directories where the out-of-tree build is run.  The
113temporary build directories are automatically created and deleted as
114needed.  The number of threads are chosen based on the number of the CPU
115cores of your system although you can change it via -j (--jobs) option.
116
117
118Toolchains
119----------
120
121Appropriate toolchain are necessary to generate include/autoconf.mk
122for all the architectures supported by U-Boot.  Most of them are available
123at the kernel.org site, some are not provided by kernel.org.
124
125The default per-arch CROSS_COMPILE used by this tool is specified by
126the list below, CROSS_COMPILE.  You may wish to update the list to
127use your own.  Instead of modifying the list directly, you can give
128them via environments.
129
130
131Available options
132-----------------
133
134 -c, --color
135   Surround each portion of the log with escape sequences to display it
136   in color on the terminal.
137
138 -n, --dry-run
139   Peform a trial run that does not make any changes.  It is useful to
140   see what is going to happen before one actually runs it.
141
142 -e, --exit-on-error
143   Exit immediately if Make exits with a non-zero status while processing
144   a defconfig file.
145
146 -j, --jobs
147   Specify the number of threads to run simultaneously.  If not specified,
148   the number of threads is the same as the number of CPU cores.
149
150To see the complete list of supported options, run
151
152  $ tools/moveconfig.py -h
153
154"""
155
156import fnmatch
157import multiprocessing
158import optparse
159import os
160import re
161import shutil
162import subprocess
163import sys
164import tempfile
165import time
166
167SHOW_GNU_MAKE = 'scripts/show-gnu-make'
168SLEEP_TIME=0.03
169
170# Here is the list of cross-tools I use.
171# Most of them are available at kernel.org
172# (https://www.kernel.org/pub/tools/crosstool/files/bin/), except the followings:
173# arc: https://github.com/foss-for-synopsys-dwc-arc-processors/toolchain/releases
174# blackfin: http://sourceforge.net/projects/adi-toolchain/files/
175# nds32: http://osdk.andestech.com/packages/
176# nios2: https://sourcery.mentor.com/GNUToolchain/subscription42545
177# sh: http://sourcery.mentor.com/public/gnu_toolchain/sh-linux-gnu
178CROSS_COMPILE = {
179    'arc': 'arc-linux-',
180    'aarch64': 'aarch64-linux-',
181    'arm': 'arm-unknown-linux-gnueabi-',
182    'avr32': 'avr32-linux-',
183    'blackfin': 'bfin-elf-',
184    'm68k': 'm68k-linux-',
185    'microblaze': 'microblaze-linux-',
186    'mips': 'mips-linux-',
187    'nds32': 'nds32le-linux-',
188    'nios2': 'nios2-linux-gnu-',
189    'openrisc': 'or32-linux-',
190    'powerpc': 'powerpc-linux-',
191    'sh': 'sh-linux-gnu-',
192    'sparc': 'sparc-linux-',
193    'x86': 'i386-linux-'
194}
195
196STATE_IDLE = 0
197STATE_DEFCONFIG = 1
198STATE_AUTOCONF = 2
199STATE_SAVEDEFCONFIG = 3
200
201ACTION_MOVE = 0
202ACTION_DEFAULT_VALUE = 1
203ACTION_ALREADY_EXIST = 2
204ACTION_UNDEFINED = 3
205
206COLOR_BLACK        = '0;30'
207COLOR_RED          = '0;31'
208COLOR_GREEN        = '0;32'
209COLOR_BROWN        = '0;33'
210COLOR_BLUE         = '0;34'
211COLOR_PURPLE       = '0;35'
212COLOR_CYAN         = '0;36'
213COLOR_LIGHT_GRAY   = '0;37'
214COLOR_DARK_GRAY    = '1;30'
215COLOR_LIGHT_RED    = '1;31'
216COLOR_LIGHT_GREEN  = '1;32'
217COLOR_YELLOW       = '1;33'
218COLOR_LIGHT_BLUE   = '1;34'
219COLOR_LIGHT_PURPLE = '1;35'
220COLOR_LIGHT_CYAN   = '1;36'
221COLOR_WHITE        = '1;37'
222
223### helper functions ###
224def get_devnull():
225    """Get the file object of '/dev/null' device."""
226    try:
227        devnull = subprocess.DEVNULL # py3k
228    except AttributeError:
229        devnull = open(os.devnull, 'wb')
230    return devnull
231
232def check_top_directory():
233    """Exit if we are not at the top of source directory."""
234    for f in ('README', 'Licenses'):
235        if not os.path.exists(f):
236            sys.exit('Please run at the top of source directory.')
237
238def get_make_cmd():
239    """Get the command name of GNU Make.
240
241    U-Boot needs GNU Make for building, but the command name is not
242    necessarily "make". (for example, "gmake" on FreeBSD).
243    Returns the most appropriate command name on your system.
244    """
245    process = subprocess.Popen([SHOW_GNU_MAKE], stdout=subprocess.PIPE)
246    ret = process.communicate()
247    if process.returncode:
248        sys.exit('GNU Make not found')
249    return ret[0].rstrip()
250
251def color_text(color_enabled, color, string):
252    """Return colored string."""
253    if color_enabled:
254        return '\033[' + color + 'm' + string + '\033[0m'
255    else:
256        return string
257
258def log_msg(color_enabled, color, defconfig, msg):
259    """Return the formated line for the log."""
260    return defconfig[:-len('_defconfig')].ljust(37) + ': ' + \
261        color_text(color_enabled, color, msg) + '\n'
262
263def update_cross_compile():
264    """Update per-arch CROSS_COMPILE via enviroment variables
265
266    The default CROSS_COMPILE values are available
267    in the CROSS_COMPILE list above.
268
269    You can override them via enviroment variables
270    CROSS_COMPILE_{ARCH}.
271
272    For example, if you want to override toolchain prefixes
273    for ARM and PowerPC, you can do as follows in your shell:
274
275    export CROSS_COMPILE_ARM=...
276    export CROSS_COMPILE_POWERPC=...
277    """
278    archs = []
279
280    for arch in os.listdir('arch'):
281        if os.path.exists(os.path.join('arch', arch, 'Makefile')):
282            archs.append(arch)
283
284    # arm64 is a special case
285    archs.append('aarch64')
286
287    for arch in archs:
288        env = 'CROSS_COMPILE_' + arch.upper()
289        cross_compile = os.environ.get(env)
290        if cross_compile:
291            CROSS_COMPILE[arch] = cross_compile
292
293def cleanup_one_header(header_path, patterns, dry_run):
294    """Clean regex-matched lines away from a file.
295
296    Arguments:
297      header_path: path to the cleaned file.
298      patterns: list of regex patterns.  Any lines matching to these
299                patterns are deleted.
300      dry_run: make no changes, but still display log.
301    """
302    with open(header_path) as f:
303        lines = f.readlines()
304
305    matched = []
306    for i, line in enumerate(lines):
307        for pattern in patterns:
308            m = pattern.search(line)
309            if m:
310                print '%s: %s: %s' % (header_path, i + 1, line),
311                matched.append(i)
312                break
313
314    if dry_run or not matched:
315        return
316
317    with open(header_path, 'w') as f:
318        for i, line in enumerate(lines):
319            if not i in matched:
320                f.write(line)
321
322def cleanup_headers(config_attrs, dry_run):
323    """Delete config defines from board headers.
324
325    Arguments:
326      config_attrs: A list of dictionaris, each of them includes the name,
327                    the type, and the default value of the target config.
328      dry_run: make no changes, but still display log.
329    """
330    while True:
331        choice = raw_input('Clean up headers? [y/n]: ').lower()
332        print choice
333        if choice == 'y' or choice == 'n':
334            break
335
336    if choice == 'n':
337        return
338
339    patterns = []
340    for config_attr in config_attrs:
341        config = config_attr['config']
342        patterns.append(re.compile(r'#\s*define\s+%s\W' % config))
343        patterns.append(re.compile(r'#\s*undef\s+%s\W' % config))
344
345    for (dirpath, dirnames, filenames) in os.walk('include'):
346        for filename in filenames:
347            if not fnmatch.fnmatch(filename, '*~'):
348                cleanup_one_header(os.path.join(dirpath, filename), patterns,
349                                   dry_run)
350
351### classes ###
352class KconfigParser:
353
354    """A parser of .config and include/autoconf.mk."""
355
356    re_arch = re.compile(r'CONFIG_SYS_ARCH="(.*)"')
357    re_cpu = re.compile(r'CONFIG_SYS_CPU="(.*)"')
358
359    def __init__(self, config_attrs, options, build_dir):
360        """Create a new parser.
361
362        Arguments:
363          config_attrs: A list of dictionaris, each of them includes the name,
364                        the type, and the default value of the target config.
365          options: option flags.
366          build_dir: Build directory.
367        """
368        self.config_attrs = config_attrs
369        self.options = options
370        self.build_dir = build_dir
371
372    def get_cross_compile(self):
373        """Parse .config file and return CROSS_COMPILE.
374
375        Returns:
376          A string storing the compiler prefix for the architecture.
377        """
378        arch = ''
379        cpu = ''
380        dotconfig = os.path.join(self.build_dir, '.config')
381        for line in open(dotconfig):
382            m = self.re_arch.match(line)
383            if m:
384                arch = m.group(1)
385                continue
386            m = self.re_cpu.match(line)
387            if m:
388                cpu = m.group(1)
389
390        assert arch, 'Error: arch is not defined in %s' % defconfig
391
392        # fix-up for aarch64
393        if arch == 'arm' and cpu == 'armv8':
394            arch = 'aarch64'
395
396        return CROSS_COMPILE.get(arch, '')
397
398    def parse_one_config(self, config_attr, defconfig_lines, autoconf_lines):
399        """Parse .config, defconfig, include/autoconf.mk for one config.
400
401        This function looks for the config options in the lines from
402        defconfig, .config, and include/autoconf.mk in order to decide
403        which action should be taken for this defconfig.
404
405        Arguments:
406          config_attr: A dictionary including the name, the type,
407                       and the default value of the target config.
408          defconfig_lines: lines from the original defconfig file.
409          autoconf_lines: lines from the include/autoconf.mk file.
410
411        Returns:
412          A tupple of the action for this defconfig and the line
413          matched for the config.
414        """
415        config = config_attr['config']
416        not_set = '# %s is not set' % config
417
418        if config_attr['type'] in ('bool', 'tristate') and \
419           config_attr['default'] == 'n':
420            default = not_set
421        else:
422            default = config + '=' + config_attr['default']
423
424        for line in defconfig_lines:
425            line = line.rstrip()
426            if line.startswith(config + '=') or line == not_set:
427                return (ACTION_ALREADY_EXIST, line)
428
429        if config_attr['type'] in ('bool', 'tristate'):
430            value = not_set
431        else:
432            value = '(undefined)'
433
434        for line in autoconf_lines:
435            line = line.rstrip()
436            if line.startswith(config + '='):
437                value = line
438                break
439
440        if value == default:
441            action = ACTION_DEFAULT_VALUE
442        elif value == '(undefined)':
443            action = ACTION_UNDEFINED
444        else:
445            action = ACTION_MOVE
446
447        return (action, value)
448
449    def update_defconfig(self, defconfig):
450        """Parse files for the config options and update the defconfig.
451
452        This function parses the given defconfig, the generated .config
453        and include/autoconf.mk searching the target options.
454        Move the config option(s) to the defconfig or do nothing if unneeded.
455        Also, display the log to show what happened to this defconfig.
456
457        Arguments:
458          defconfig: defconfig name.
459        """
460
461        defconfig_path = os.path.join('configs', defconfig)
462        dotconfig_path = os.path.join(self.build_dir, '.config')
463        autoconf_path = os.path.join(self.build_dir, 'include', 'autoconf.mk')
464        results = []
465
466        with open(defconfig_path) as f:
467            defconfig_lines = f.readlines()
468
469        with open(autoconf_path) as f:
470            autoconf_lines = f.readlines()
471
472        for config_attr in self.config_attrs:
473            result = self.parse_one_config(config_attr, defconfig_lines,
474                                           autoconf_lines)
475            results.append(result)
476
477        log = ''
478
479        for (action, value) in results:
480            if action == ACTION_MOVE:
481                actlog = "Move '%s'" % value
482                log_color = COLOR_LIGHT_GREEN
483            elif action == ACTION_DEFAULT_VALUE:
484                actlog = "Default value '%s'.  Do nothing." % value
485                log_color = COLOR_LIGHT_BLUE
486            elif action == ACTION_ALREADY_EXIST:
487                actlog = "'%s' already defined in Kconfig.  Do nothing." % value
488                log_color = COLOR_LIGHT_PURPLE
489            elif action == ACTION_UNDEFINED:
490                actlog = "Undefined.  Do nothing."
491                log_color = COLOR_DARK_GRAY
492            else:
493                sys.exit("Internal Error. This should not happen.")
494
495            log += log_msg(self.options.color, log_color, defconfig, actlog)
496
497        # Some threads are running in parallel.
498        # Print log in one shot to not mix up logs from different threads.
499        print log,
500
501        if not self.options.dry_run:
502            with open(dotconfig_path, 'a') as f:
503                for (action, value) in results:
504                    if action == ACTION_MOVE:
505                        f.write(value + '\n')
506
507        os.remove(os.path.join(self.build_dir, 'include', 'config', 'auto.conf'))
508        os.remove(autoconf_path)
509
510class Slot:
511
512    """A slot to store a subprocess.
513
514    Each instance of this class handles one subprocess.
515    This class is useful to control multiple threads
516    for faster processing.
517    """
518
519    def __init__(self, config_attrs, options, devnull, make_cmd):
520        """Create a new process slot.
521
522        Arguments:
523          config_attrs: A list of dictionaris, each of them includes the name,
524                        the type, and the default value of the target config.
525          options: option flags.
526          devnull: A file object of '/dev/null'.
527          make_cmd: command name of GNU Make.
528        """
529        self.options = options
530        self.build_dir = tempfile.mkdtemp()
531        self.devnull = devnull
532        self.make_cmd = (make_cmd, 'O=' + self.build_dir)
533        self.parser = KconfigParser(config_attrs, options, self.build_dir)
534        self.state = STATE_IDLE
535        self.failed_boards = []
536
537    def __del__(self):
538        """Delete the working directory
539
540        This function makes sure the temporary directory is cleaned away
541        even if Python suddenly dies due to error.  It should be done in here
542        because it is guranteed the destructor is always invoked when the
543        instance of the class gets unreferenced.
544
545        If the subprocess is still running, wait until it finishes.
546        """
547        if self.state != STATE_IDLE:
548            while self.ps.poll() == None:
549                pass
550        shutil.rmtree(self.build_dir)
551
552    def add(self, defconfig):
553        """Assign a new subprocess for defconfig and add it to the slot.
554
555        If the slot is vacant, create a new subprocess for processing the
556        given defconfig and add it to the slot.  Just returns False if
557        the slot is occupied (i.e. the current subprocess is still running).
558
559        Arguments:
560          defconfig: defconfig name.
561
562        Returns:
563          Return True on success or False on failure
564        """
565        if self.state != STATE_IDLE:
566            return False
567        cmd = list(self.make_cmd)
568        cmd.append(defconfig)
569        self.ps = subprocess.Popen(cmd, stdout=self.devnull)
570        self.defconfig = defconfig
571        self.state = STATE_DEFCONFIG
572        return True
573
574    def poll(self):
575        """Check the status of the subprocess and handle it as needed.
576
577        Returns True if the slot is vacant (i.e. in idle state).
578        If the configuration is successfully finished, assign a new
579        subprocess to build include/autoconf.mk.
580        If include/autoconf.mk is generated, invoke the parser to
581        parse the .config and the include/autoconf.mk, and then set the
582        slot back to the idle state.
583
584        Returns:
585          Return True if the subprocess is terminated, False otherwise
586        """
587        if self.state == STATE_IDLE:
588            return True
589
590        if self.ps.poll() == None:
591            return False
592
593        if self.ps.poll() != 0:
594
595            print >> sys.stderr, log_msg(self.options.color,
596                                         COLOR_LIGHT_RED,
597                                         self.defconfig,
598                                         "failed to process.")
599            if self.options.exit_on_error:
600                sys.exit("Exit on error.")
601            else:
602                # If --exit-on-error flag is not set,
603                # skip this board and continue.
604                # Record the failed board.
605                self.failed_boards.append(self.defconfig)
606                self.state = STATE_IDLE
607                return True
608
609        if self.state == STATE_AUTOCONF:
610            self.parser.update_defconfig(self.defconfig)
611
612            """Save off the defconfig in a consistent way"""
613            cmd = list(self.make_cmd)
614            cmd.append('savedefconfig')
615            self.ps = subprocess.Popen(cmd, stdout=self.devnull,
616                                       stderr=self.devnull)
617            self.state = STATE_SAVEDEFCONFIG
618            return False
619
620        if self.state == STATE_SAVEDEFCONFIG:
621            defconfig_path = os.path.join(self.build_dir, 'defconfig')
622            shutil.move(defconfig_path,
623                        os.path.join('configs', self.defconfig))
624            self.state = STATE_IDLE
625            return True
626
627        cross_compile = self.parser.get_cross_compile()
628        cmd = list(self.make_cmd)
629        if cross_compile:
630            cmd.append('CROSS_COMPILE=%s' % cross_compile)
631        cmd.append('KCONFIG_IGNORE_DUPLICATES=1')
632        cmd.append('include/config/auto.conf')
633        self.ps = subprocess.Popen(cmd, stdout=self.devnull)
634        self.state = STATE_AUTOCONF
635        return False
636
637    def get_failed_boards(self):
638        """Returns a list of failed boards (defconfigs) in this slot.
639        """
640        return self.failed_boards
641
642class Slots:
643
644    """Controller of the array of subprocess slots."""
645
646    def __init__(self, config_attrs, options):
647        """Create a new slots controller.
648
649        Arguments:
650          config_attrs: A list of dictionaris containing the name, the type,
651                        and the default value of the target CONFIG.
652          options: option flags.
653        """
654        self.options = options
655        self.slots = []
656        devnull = get_devnull()
657        make_cmd = get_make_cmd()
658        for i in range(options.jobs):
659            self.slots.append(Slot(config_attrs, options, devnull, make_cmd))
660
661    def add(self, defconfig):
662        """Add a new subprocess if a vacant slot is found.
663
664        Arguments:
665          defconfig: defconfig name to be put into.
666
667        Returns:
668          Return True on success or False on failure
669        """
670        for slot in self.slots:
671            if slot.add(defconfig):
672                return True
673        return False
674
675    def available(self):
676        """Check if there is a vacant slot.
677
678        Returns:
679          Return True if at lease one vacant slot is found, False otherwise.
680        """
681        for slot in self.slots:
682            if slot.poll():
683                return True
684        return False
685
686    def empty(self):
687        """Check if all slots are vacant.
688
689        Returns:
690          Return True if all the slots are vacant, False otherwise.
691        """
692        ret = True
693        for slot in self.slots:
694            if not slot.poll():
695                ret = False
696        return ret
697
698    def show_failed_boards(self):
699        """Display all of the failed boards (defconfigs)."""
700        failed_boards = []
701
702        for slot in self.slots:
703            failed_boards += slot.get_failed_boards()
704
705        if len(failed_boards) > 0:
706            msg = [ "The following boards were not processed due to error:" ]
707            msg += failed_boards
708            for line in msg:
709                print >> sys.stderr, color_text(self.options.color,
710                                                COLOR_LIGHT_RED, line)
711
712def move_config(config_attrs, options):
713    """Move config options to defconfig files.
714
715    Arguments:
716      config_attrs: A list of dictionaris, each of them includes the name,
717                    the type, and the default value of the target config.
718      options: option flags
719    """
720    check_top_directory()
721
722    if len(config_attrs) == 0:
723        print 'Nothing to do. exit.'
724        sys.exit(0)
725
726    print 'Move the following CONFIG options (jobs: %d)' % options.jobs
727    for config_attr in config_attrs:
728        print '  %s (type: %s, default: %s)' % (config_attr['config'],
729                                                config_attr['type'],
730                                                config_attr['default'])
731
732    # All the defconfig files to be processed
733    defconfigs = []
734    for (dirpath, dirnames, filenames) in os.walk('configs'):
735        dirpath = dirpath[len('configs') + 1:]
736        for filename in fnmatch.filter(filenames, '*_defconfig'):
737            defconfigs.append(os.path.join(dirpath, filename))
738
739    slots = Slots(config_attrs, options)
740
741    # Main loop to process defconfig files:
742    #  Add a new subprocess into a vacant slot.
743    #  Sleep if there is no available slot.
744    for defconfig in defconfigs:
745        while not slots.add(defconfig):
746            while not slots.available():
747                # No available slot: sleep for a while
748                time.sleep(SLEEP_TIME)
749
750    # wait until all the subprocesses finish
751    while not slots.empty():
752        time.sleep(SLEEP_TIME)
753
754    slots.show_failed_boards()
755
756    cleanup_headers(config_attrs, options.dry_run)
757
758def bad_recipe(filename, linenum, msg):
759    """Print error message with the file name and the line number and exit."""
760    sys.exit("%s: line %d: error : " % (filename, linenum) + msg)
761
762def parse_recipe(filename):
763    """Parse the recipe file and retrieve the config attributes.
764
765    This function parses the given recipe file and gets the name,
766    the type, and the default value of the target config options.
767
768    Arguments:
769      filename: path to file to be parsed.
770    Returns:
771      A list of dictionaris, each of them includes the name,
772      the type, and the default value of the target config.
773    """
774    config_attrs = []
775    linenum = 1
776
777    for line in open(filename):
778        tokens = line.split()
779        if len(tokens) != 3:
780            bad_recipe(filename, linenum,
781                       "%d fields in this line.  Each line must contain 3 fields"
782                       % len(tokens))
783
784        (config, type, default) = tokens
785
786        # prefix the option name with CONFIG_ if missing
787        if not config.startswith('CONFIG_'):
788            config = 'CONFIG_' + config
789
790        # sanity check of default values
791        if type == 'bool':
792            if not default in ('y', 'n'):
793                bad_recipe(filename, linenum,
794                           "default for bool type must be either y or n")
795        elif type == 'tristate':
796            if not default in ('y', 'm', 'n'):
797                bad_recipe(filename, linenum,
798                           "default for tristate type must be y, m, or n")
799        elif type == 'string':
800            if default[0] != '"' or default[-1] != '"':
801                bad_recipe(filename, linenum,
802                           "default for string type must be surrounded by double-quotations")
803        elif type == 'int':
804            try:
805                int(default)
806            except:
807                bad_recipe(filename, linenum,
808                           "type is int, but default value is not decimal")
809        elif type == 'hex':
810            if len(default) < 2 or default[:2] != '0x':
811                bad_recipe(filename, linenum,
812                           "default for hex type must be prefixed with 0x")
813            try:
814                int(default, 16)
815            except:
816                bad_recipe(filename, linenum,
817                           "type is hex, but default value is not hexadecimal")
818        else:
819            bad_recipe(filename, linenum,
820                       "unsupported type '%s'. type must be one of bool, tristate, string, int, hex"
821                       % type)
822
823        config_attrs.append({'config': config, 'type': type, 'default': default})
824        linenum += 1
825
826    return config_attrs
827
828def main():
829    try:
830        cpu_count = multiprocessing.cpu_count()
831    except NotImplementedError:
832        cpu_count = 1
833
834    parser = optparse.OptionParser()
835    # Add options here
836    parser.add_option('-c', '--color', action='store_true', default=False,
837                      help='display the log in color')
838    parser.add_option('-n', '--dry-run', action='store_true', default=False,
839                      help='perform a trial run (show log with no changes)')
840    parser.add_option('-e', '--exit-on-error', action='store_true',
841                      default=False,
842                      help='exit immediately on any error')
843    parser.add_option('-j', '--jobs', type='int', default=cpu_count,
844                      help='the number of jobs to run simultaneously')
845    parser.usage += ' recipe_file\n\n' + \
846                    'The recipe_file should describe config options you want to move.\n' + \
847                    'Each line should contain config_name, type, default_value\n\n' + \
848                    'Example:\n' + \
849                    'CONFIG_FOO bool n\n' + \
850                    'CONFIG_BAR int 100\n' + \
851                    'CONFIG_BAZ string "hello"\n'
852
853    (options, args) = parser.parse_args()
854
855    if len(args) != 1:
856        parser.print_usage()
857        sys.exit(1)
858
859    config_attrs = parse_recipe(args[0])
860
861    update_cross_compile()
862
863    move_config(config_attrs, options)
864
865if __name__ == '__main__':
866    main()
867