__init__.py revision 2677:af874b8d437c
1# Copyright (c) 2005 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright 9# notice, this list of conditions and the following disclaimer in the 10# documentation and/or other materials provided with the distribution; 11# neither the name of the copyright holders nor the names of its 12# contributors may be used to endorse or promote products derived from 13# this software without specific prior written permission. 14# 15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26# 27# Authors: Nathan Binkert 28# Steve Reinhardt 29 30import sys, os, time 31 32import __main__ 33 34briefCopyright = ''' 35Copyright (c) 2001-2006 36The Regents of The University of Michigan 37All Rights Reserved 38''' 39 40fullCopyright = ''' 41Copyright (c) 2001-2006 42The Regents of The University of Michigan 43All Rights Reserved 44 45Permission is granted to use, copy, create derivative works and 46redistribute this software and such derivative works for any purpose, 47so long as the copyright notice above, this grant of permission, and 48the disclaimer below appear in all copies made; and so long as the 49name of The University of Michigan is not used in any advertising or 50publicity pertaining to the use or distribution of this software 51without specific, written prior authorization. 52 53THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE 54UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND WITHOUT 55WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER EXPRESS OR 56IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF 57MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE REGENTS OF 58THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE FOR ANY DAMAGES, 59INCLUDING DIRECT, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL 60DAMAGES, WITH RESPECT TO ANY CLAIM ARISING OUT OF OR IN CONNECTION 61WITH THE USE OF THE SOFTWARE, EVEN IF IT HAS BEEN OR IS HEREAFTER 62ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 63''' 64 65def sayHello(f): 66 print >> f, "M5 Simulator System" 67 print >> f, briefCopyright 68 print >> f, "M5 compiled on", __main__.compileDate 69 hostname = os.environ.get('HOSTNAME') 70 if not hostname: 71 hostname = os.environ.get('HOST') 72 if hostname: 73 print >> f, "M5 executing on", hostname 74 print >> f, "M5 simulation started", time.ctime() 75 76sayHello(sys.stderr) 77 78# define this here so we can use it right away if necessary 79def panic(string): 80 print >>sys.stderr, 'panic:', string 81 sys.exit(1) 82 83def m5execfile(f, global_dict): 84 # copy current sys.path 85 oldpath = sys.path[:] 86 # push file's directory onto front of path 87 sys.path.insert(0, os.path.abspath(os.path.dirname(f))) 88 execfile(f, global_dict) 89 # restore original path 90 sys.path = oldpath 91 92# Prepend given directory to system module search path. 93def AddToPath(path): 94 # if it's a relative path and we know what directory the current 95 # python script is in, make the path relative to that directory. 96 if not os.path.isabs(path) and sys.path[0]: 97 path = os.path.join(sys.path[0], path) 98 path = os.path.realpath(path) 99 # sys.path[0] should always refer to the current script's directory, 100 # so place the new dir right after that. 101 sys.path.insert(1, path) 102 103# find the m5 compile options: must be specified as a dict in 104# __main__.m5_build_env. 105import __main__ 106if not hasattr(__main__, 'm5_build_env'): 107 panic("__main__ must define m5_build_env") 108 109# make a SmartDict out of the build options for our local use 110import smartdict 111build_env = smartdict.SmartDict() 112build_env.update(__main__.m5_build_env) 113 114# make a SmartDict out of the OS environment too 115env = smartdict.SmartDict() 116env.update(os.environ) 117 118# import the main m5 config code 119from config import * 120 121# import the built-in object definitions 122from objects import * 123 124 125args_left = sys.argv[1:] 126configfile_found = False 127 128while args_left: 129 arg = args_left.pop(0) 130 if arg.startswith('--'): 131 # if arg starts with '--', parse as a special python option 132 # of the format --<python var>=<string value> 133 try: 134 (var, val) = arg.split('=', 1) 135 var = var[2:] 136 except ValueError: 137 panic("Could not parse configuration argument '%s'\n" 138 "Expecting --<variable>=<value>\n" % arg); 139 exec "%s = %s" % (var, repr(val)) 140 elif arg.startswith('-'): 141 # if the arg starts with '-', it should be a simulator option 142 # with a format similar to getopt. 143 optchar = arg[1] 144 if len(arg) > 2: 145 args_left.insert(0, arg[2:]) 146 if optchar == 'd': 147 outdir = args_left.pop(0) 148 elif optchar == 'h': 149 showBriefHelp(sys.stderr) 150 sys.exit(1) 151 elif optchar == 'E': 152 env_str = args_left.pop(0) 153 split_result = env_str.split('=', 1) 154 var = split_result[0] 155 if len(split_result == 2): 156 val = split_result[1] 157 else: 158 val = True 159 env[var] = val 160 elif optchar == 'I': 161 AddToPath(args_left.pop(0)) 162 elif optchar == 'P': 163 exec args_left.pop(0) 164 else: 165 showBriefHelp(sys.stderr) 166 panic("invalid argument '%s'\n" % arg_str) 167 else: 168 # In any other case, treat the option as a configuration file 169 # name and load it. 170 if not arg.endswith('.py'): 171 panic("Config file '%s' must end in '.py'\n" % arg) 172 configfile_found = True 173 m5execfile(arg, globals()) 174 175 176if not configfile_found: 177 panic("no configuration file specified!") 178 179if globals().has_key('root') and isinstance(root, Root): 180 sys.stdout = file('config.ini', 'w') 181 instantiate(root) 182else: 183 print 'Instantiation skipped: no root object found.' 184 185