compile revision 11320:42ecb523c64a
12623SN/A#!/usr/bin/env python
22623SN/A# Copyright (c) 2006 The Regents of The University of Michigan
32623SN/A# All rights reserved.
42623SN/A#
52623SN/A# Redistribution and use in source and binary forms, with or without
62623SN/A# modification, are permitted provided that the following conditions are
72623SN/A# met: redistributions of source code must retain the above copyright
82623SN/A# notice, this list of conditions and the following disclaimer;
92623SN/A# redistributions in binary form must reproduce the above copyright
102623SN/A# notice, this list of conditions and the following disclaimer in the
112623SN/A# documentation and/or other materials provided with the distribution;
122623SN/A# neither the name of the copyright holders nor the names of its
132623SN/A# contributors may be used to endorse or promote products derived from
142623SN/A# this software without specific prior written permission.
152623SN/A#
162623SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172623SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182623SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192623SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202623SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212623SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222623SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232623SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242623SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252623SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262623SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu#
282665Ssaidi@eecs.umich.edu# Authors: Nathan Binkert
292623SN/A
302623SN/Aimport os, re, sys
313170Sstever@eecs.umich.edufrom os.path import isdir, isfile, join as joinpath
325103Ssaidi@eecs.umich.edu
332623SN/Ahomedir = os.environ['HOME']
344040Ssaidi@eecs.umich.edu
352623SN/Adef do_compile():
362623SN/A    #
373348Sbinkertn@umich.edu    # Find SCons
383348Sbinkertn@umich.edu    #
394762Snate@binkert.org    search_dirs = [ joinpath(homedir, 'local/lib'), '/opt/local/lib',
402901Ssaidi@eecs.umich.edu                    '/usr/local/lib', '/usr/lib' ]
412623SN/A
422623SN/A    if os.environ.has_key("SCONS_LIB_DIR"):
432623SN/A        search_dirs.append(os.environ["SCONS_LIB_DIR"])
442623SN/A
452856Srdreslin@umich.edu    local = re.compile(r'^scons-local-([0-9]*)\.([0-9]*)\.([0-9]*)$')
462856Srdreslin@umich.edu    standard = re.compile(r'^scons-([0-9]*)\.([0-9]*)\.([0-9]*)$')
472856Srdreslin@umich.edu
482856Srdreslin@umich.edu    scons_dirs = []
492856Srdreslin@umich.edu    for dir in search_dirs:
502856Srdreslin@umich.edu        if not isdir(dir):
512856Srdreslin@umich.edu            continue
522856Srdreslin@umich.edu
532856Srdreslin@umich.edu        entries = os.listdir(dir)
542856Srdreslin@umich.edu        for entry in entries:
552623SN/A            if not entry.startswith('scons'):
562623SN/A                continue
572623SN/A
582623SN/A            version = (0,0,0)
592623SN/A            path = joinpath(dir, entry)
602623SN/A
612680Sktlim@umich.edu            match = local.search(entry)
622680Sktlim@umich.edu            if not match:
632623SN/A                match = standard.search(entry)
642623SN/A
655712Shsul@eecs.umich.edu            if match:
662623SN/A                version = match.group(1), match.group(2), match.group(3)
672623SN/A
682623SN/A            scons_dirs.append((version, path))
692623SN/A
702623SN/A    scons_dirs.sort()
713349Sbinkertn@umich.edu    scons_dirs.reverse()
722623SN/A
732623SN/A    if not scons_dirs:
742623SN/A        print >>sys.stderr, \
752623SN/A              "could not find scons in the following dirs: %s" % search_dirs
762623SN/A        sys.exit(1)
772623SN/A
783349Sbinkertn@umich.edu    sys.path = [ scons_dirs[0][1] ] + sys.path
792623SN/A
803184Srdreslin@umich.edu    # invoke SCons
813184Srdreslin@umich.edu    import SCons.Script
822623SN/A    SCons.Script.main()
832623SN/A
842623SN/A#
852623SN/A# do argument parsing
862623SN/A#
873647Srdreslin@umich.eduprogname = sys.argv[0]
883647Srdreslin@umich.edu
893647Srdreslin@umich.eduimport optparse
903647Srdreslin@umich.edu
913647Srdreslin@umich.eduusage = '''%prog [compile options] <version> [SCons options]
922631SN/A
933647Srdreslin@umich.edu%prog assumes that the user has a directory called ~/m5/<version> where
942631SN/Athe source tree resides, and a directory called ~/build, where %prog
952623SN/Awill create ~/build/<version> if it does not exist and build the resulting
962623SN/Asimulators there.
972623SN/A
982948Ssaidi@eecs.umich.eduIf ~/build is set up in such a way that it points to a local disk on
992948Ssaidi@eecs.umich.edueach host, compiles will be very efficient.  For example:
1003349Sbinkertn@umich.edu~/build -> /z/<username>/.build  (Assuming that /z is a local disk and
1012948Ssaidi@eecs.umich.edunot NFS mounted, whereas your home directory is NFS mounted).
1022948Ssaidi@eecs.umich.edu'''
1035606Snate@binkert.orgversion = '%prog 0.1'
1042948Ssaidi@eecs.umich.eduparser = optparse.OptionParser(usage=usage, version=version,
1052948Ssaidi@eecs.umich.edu                               formatter=optparse.TitledHelpFormatter())
1065529Snate@binkert.orgparser.disable_interspersed_args()
1075894Sgblack@eecs.umich.edu
1085894Sgblack@eecs.umich.edu# current option group
1092623SN/Agroup = None
1102623SN/A
1113647Srdreslin@umich.edudef set_group(*args, **kwargs):
1123647Srdreslin@umich.edu    '''set the current option group'''
1133647Srdreslin@umich.edu    global group
1143647Srdreslin@umich.edu    if not args and not kwargs:
1152623SN/A        group = None
1162839Sktlim@umich.edu    else:
1173222Sktlim@umich.edu        group = parser.add_option_group(*args, **kwargs)
1182901Ssaidi@eecs.umich.edu
1192623SN/Adef add_option(*args, **kwargs):
1202623SN/A    if group:
1212623SN/A        return group.add_option(*args, **kwargs)
1222623SN/A    else:
1232623SN/A        return parser.add_option(*args, **kwargs)
1242623SN/A
1252623SN/Adef bool_option(name, default, help):
1262623SN/A    '''add a boolean option called --name and --no-name.
1272623SN/A    Display help depending on which is the default'''
1282623SN/A
1292915Sktlim@umich.edu    tname = '--%s' % name
1302915Sktlim@umich.edu    fname = '--no-%s' % name
1312623SN/A    dest = name.replace('-', '_')
1322623SN/A    if default:
1332623SN/A        thelp = optparse.SUPPRESS_HELP
1342623SN/A        fhelp = help
1352623SN/A    else:
1362623SN/A        thelp = help
1372915Sktlim@umich.edu        fhelp = optparse.SUPPRESS_HELP
1382915Sktlim@umich.edu
1392623SN/A    add_option(tname, action="store_true", default=default, help=thelp)
1402798Sktlim@umich.edu    add_option(fname, action="store_false", dest=dest, help=fhelp)
1412798Sktlim@umich.edu
1422901Ssaidi@eecs.umich.eduadd_option('-n', '--no-compile', default=False, action='store_true',
1432839Sktlim@umich.edu           help="don't actually compile, just echo SCons command line")
1442798Sktlim@umich.eduadd_option('--everything', default=False, action='store_true',
1452839Sktlim@umich.edu           help="compile everything that can be compiled")
1462798Sktlim@umich.eduadd_option('-E', "--experimental", action='store_true', default=False,
1475496Ssaidi@eecs.umich.edu           help="enable experimental builds")
1482901Ssaidi@eecs.umich.eduadd_option('-v', "--verbose", default=False, action='store_true',
1492901Ssaidi@eecs.umich.edu           help="be verbose")
1502798Sktlim@umich.edu
1512839Sktlim@umich.eduset_group("Output binary types")
1522839Sktlim@umich.edubool_option("debug", default=False, help="compile debug binaries")
1532901Ssaidi@eecs.umich.edubool_option("opt", default=False, help="compile opt binaries")
1542798Sktlim@umich.edubool_option("fast", default=False, help="compile fast binaries")
1552623SN/Abool_option("prof", default=False, help="compile profile binaries")
1562623SN/Aadd_option('-a', "--all-bin", default=False, action='store_true',
1572623SN/A           help="compile debug, opt, and fast binaries")
1582798Sktlim@umich.edu
1592623SN/Aset_group("ISA options")
1605221Ssaidi@eecs.umich.edubool_option("alpha", default=False, help="compile Alpha")
1612798Sktlim@umich.edubool_option("mips", default=False, help="compile MIPS")
1624762Snate@binkert.orgbool_option("sparc", default=False, help="compile SPARC")
1633201Shsul@eecs.umich.eduadd_option('-i', "--all-isa", default=False, action='store_true',
1645710Scws3k@cs.virginia.edu           help="compile all ISAs")
1655710Scws3k@cs.virginia.edu
1662915Sktlim@umich.eduset_group("Emulation options")
1675710Scws3k@cs.virginia.edubool_option("syscall", default=True,
1682623SN/A            help="Do not compile System Call Emulation mode")
1692798Sktlim@umich.edubool_option("fullsys", default=True,
1702901Ssaidi@eecs.umich.edu            help="Do not compile Full System mode")
1712798Sktlim@umich.edu
1722798Sktlim@umich.edudef usage(exitcode=None):
1732798Sktlim@umich.edu    parser.print_help()
1742798Sktlim@umich.edu    if exitcode is not None:
1752798Sktlim@umich.edu        sys.exit(exitcode)
1765496Ssaidi@eecs.umich.edu
1772798Sktlim@umich.edu(options, args) = parser.parse_args()
1785099Ssaidi@eecs.umich.edu
1792867Sktlim@umich.eduif options.everything:
1802867Sktlim@umich.edu    options.all_bin = True
1812867Sktlim@umich.edu    options.prof = True
1825710Scws3k@cs.virginia.edu    options.all_isa = True
1835606Snate@binkert.org
1842623SN/Aif options.all_bin:
1852623SN/A    options.debug = True
1862623SN/A    options.opt = True
1872623SN/A    options.fast = True
1882623SN/A
1892623SN/Abinaries = []
1904192Sktlim@umich.eduif options.debug:
1912623SN/A    binaries.append('m5.debug')
1922680Sktlim@umich.eduif options.opt:
1932623SN/A    binaries.append('m5.opt')
1942680Sktlim@umich.eduif options.fast:
1952680Sktlim@umich.edu    binaries.append('m5.fast')
1962680Sktlim@umich.eduif options.prof:
1972623SN/A    binaries.append('m5.prof')
1982623SN/A
1992623SN/Aif not binaries:
2002623SN/A    binaries.append('m5.debug')
2013201Shsul@eecs.umich.edu
2023201Shsul@eecs.umich.eduif options.all_isa:
2033201Shsul@eecs.umich.edu    options.alpha = True
2043201Shsul@eecs.umich.edu    options.mips = True
2055169Ssaidi@eecs.umich.edu    options.sparc = True
2065101Ssaidi@eecs.umich.edu
2072623SN/Aisas = []
2082623SN/Aif options.alpha:
2092623SN/A    isas.append('alpha')
2102623SN/Aif options.mips:
2112623SN/A    isas.append('mips')
2122623SN/Aif options.sparc:
2135221Ssaidi@eecs.umich.edu    isas.append('sparc')
2145221Ssaidi@eecs.umich.edu
2152623SN/Aif not isas:
2162683Sktlim@umich.edu    isas.append('alpha')
2172623SN/A
2182623SN/Amodes = []
2192623SN/Aif options.syscall:
2202623SN/A    modes.append('syscall')
2212623SN/Aif options.fullsys:
2223686Sktlim@umich.edu    modes.append('fullsys')
2232623SN/A
2245606Snate@binkert.orgif not modes:
2252623SN/A    sys.exit("must specify at least one mode")
2262623SN/A
2272623SN/A#
2282623SN/A# Convert options into SCons command line arguments
2292623SN/A#
2302623SN/A
2315221Ssaidi@eecs.umich.edu# valid combinations of ISA and emulation mode
2325221Ssaidi@eecs.umich.eduvalid = { ('alpha', 'syscall') : 'ALPHA_SE',
2332623SN/A          ('alpha', 'fullsys') : 'ALPHA_FS',
2342683Sktlim@umich.edu          ('mips',  'syscall') : 'MIPS_SE',
2352623SN/A          ('sparc', 'syscall') : 'SPARC_SE' }
2366043Sgblack@eecs.umich.edu
2376043Sgblack@eecs.umich.edu# experimental combinations of ISA and emulation mode
2386043Sgblack@eecs.umich.eduexperiment = { ('mips', 'fullsys') : 'MIPS_FS',
2392644Sstever@eecs.umich.edu               ('sparc', 'fullsys') : 'SPARC_FS' }
2402623SN/A
2412644Sstever@eecs.umich.eduif options.experimental:
2422644Sstever@eecs.umich.edu    valid.update(experiment)
2432623SN/A
2442623SN/Abuilds = []
2452623SN/Afor isa in isas:
2462623SN/A    for mode in modes:
2472623SN/A        try:
2485728Sgblack@eecs.umich.edu            build = valid[(isa, mode)]
2495728Sgblack@eecs.umich.edu            builds.append(build)
2505728Sgblack@eecs.umich.edu        except KeyError:
2515728Sgblack@eecs.umich.edu            pass
2525728Sgblack@eecs.umich.edu
2535728Sgblack@eecs.umich.eduif not builds:
2545728Sgblack@eecs.umich.edu    sys.exit("must specify at least one valid combination of ISA and mode")
2555728Sgblack@eecs.umich.edu
2565728Sgblack@eecs.umich.eduif not args:
2575728Sgblack@eecs.umich.edu    usage(2)
2585728Sgblack@eecs.umich.edu
2595728Sgblack@eecs.umich.eduversion = args[0]
2605728Sgblack@eecs.umich.edudel args[0]
2615728Sgblack@eecs.umich.edu
2625728Sgblack@eecs.umich.edufor bin in binaries:
2635728Sgblack@eecs.umich.edu    for build in builds:
2645728Sgblack@eecs.umich.edu        args.append('%s/%s' % (build, bin))
2655728Sgblack@eecs.umich.edu
2665728Sgblack@eecs.umich.edu#
2675728Sgblack@eecs.umich.edu# set up compile
2682623SN/A#
2695894Sgblack@eecs.umich.edubuild_base = joinpath(homedir, 'build')
2705894Sgblack@eecs.umich.edum5_base = joinpath(homedir, 'm5')
2715894Sgblack@eecs.umich.edu
2725744Sgblack@eecs.umich.eduif not isdir(build_base):
2735894Sgblack@eecs.umich.edu    sys.exit('build directory %s not found' % build_base)
2745894Sgblack@eecs.umich.edu
2755894Sgblack@eecs.umich.eduif not isdir(m5_base):
2765894Sgblack@eecs.umich.edu    sys.exit('m5 base directory %s not found' % m5_base)
2775744Sgblack@eecs.umich.edu
2785894Sgblack@eecs.umich.edum5_dir = joinpath(m5_base, version)
2795894Sgblack@eecs.umich.eduif not isdir(m5_dir):
2805894Sgblack@eecs.umich.edu    sys.exit('source directory %s not found' % m5_dir)
2815894Sgblack@eecs.umich.edu
2825894Sgblack@eecs.umich.edu# support M5 1.x
2835894Sgblack@eecs.umich.eduoldstyle = isfile(joinpath(m5_dir, 'SConscript'))
2845894Sgblack@eecs.umich.eduif oldstyle:
2855894Sgblack@eecs.umich.edu    ext_dir = joinpath(m5_base, 'ext')
2865894Sgblack@eecs.umich.edu    test_dir = joinpath(m5_base, 'test.' + version)
2875894Sgblack@eecs.umich.edu
2885894Sgblack@eecs.umich.edu    if not isdir(ext_dir):
2895894Sgblack@eecs.umich.edu        sys.exit('ext directory not found at %s' % ext_dir)
2905894Sgblack@eecs.umich.edu
2915894Sgblack@eecs.umich.edu    if not isdir(test_dir):
2925894Sgblack@eecs.umich.edu        sys.exit('test directory not found at %s' % test_dir)
2936102Sgblack@eecs.umich.edu
2945894Sgblack@eecs.umich.edubuild_dir = joinpath(build_base, version)
2955894Sgblack@eecs.umich.eduif not isdir(build_dir):
2965894Sgblack@eecs.umich.edu    os.mkdir(build_dir)
2975894Sgblack@eecs.umich.edu    # need some symlinks for m5 1.x
2985894Sgblack@eecs.umich.edu    if oldstyle:
2995894Sgblack@eecs.umich.edu        os.symlink(m5_dir, joinpath(build_dir, 'm5'))
3005894Sgblack@eecs.umich.edu        os.symlink(ext_dir, joinpath(build_dir, 'ext'))
3015894Sgblack@eecs.umich.edu        os.symlink(test_dir, joinpath(build_dir, 'test'))
3025894Sgblack@eecs.umich.edu        os.symlink(joinpath(m5_dir, 'build', 'SConstruct'),
3035894Sgblack@eecs.umich.edu                   joinpath(build_dir, 'SConstruct'))
3045894Sgblack@eecs.umich.edu        os.symlink(joinpath(m5_dir, 'build', 'default_options'),
3055894Sgblack@eecs.umich.edu                   joinpath(build_dir, 'default_options'))
3065894Sgblack@eecs.umich.edu
3075894Sgblack@eecs.umich.edusys.argv = [ progname ]
3085894Sgblack@eecs.umich.eduif oldstyle:
3095894Sgblack@eecs.umich.edu    os.chdir(build_dir)
3105894Sgblack@eecs.umich.edu    sys.argv.extend(args)
3115894Sgblack@eecs.umich.eduelse:
3125894Sgblack@eecs.umich.edu    os.chdir(m5_dir)
3135894Sgblack@eecs.umich.edu    for arg in args:
3145894Sgblack@eecs.umich.edu        if not arg.startswith('-') and '=' not in arg:
3155894Sgblack@eecs.umich.edu            arg = joinpath(build_dir, 'build', arg)
3165894Sgblack@eecs.umich.edu        sys.argv.append(arg)
3175894Sgblack@eecs.umich.edu
3185890Sgblack@eecs.umich.eduif options.no_compile or options.verbose:
3195894Sgblack@eecs.umich.edu    for arg in sys.argv[1:]:
3205894Sgblack@eecs.umich.edu        print arg
3215894Sgblack@eecs.umich.edu
3225894Sgblack@eecs.umich.eduif not options.no_compile:
3235894Sgblack@eecs.umich.edu    do_compile()
3245894Sgblack@eecs.umich.edu