SConscript revision 2155
11046SN/A# -*- mode:python -*- 21046SN/A 31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan 41046SN/A# All rights reserved. 51046SN/A# 61046SN/A# Redistribution and use in source and binary forms, with or without 71046SN/A# modification, are permitted provided that the following conditions are 81046SN/A# met: redistributions of source code must retain the above copyright 91046SN/A# notice, this list of conditions and the following disclaimer; 101046SN/A# redistributions in binary form must reproduce the above copyright 111046SN/A# notice, this list of conditions and the following disclaimer in the 121046SN/A# documentation and/or other materials provided with the distribution; 131046SN/A# neither the name of the copyright holders nor the names of its 141046SN/A# contributors may be used to endorse or promote products derived from 151046SN/A# this software without specific prior written permission. 161046SN/A# 171046SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 181046SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 191046SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 201046SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 211046SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 221046SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 231046SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 241046SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 251046SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 261046SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 271046SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 282665Ssaidi@eecs.umich.edu 292665Ssaidi@eecs.umich.eduimport os, os.path, re, sys 302665Ssaidi@eecs.umich.edu 311046SN/AImport('env') 324202Sbinkertn@umich.edu 334202Sbinkertn@umich.eduimport scons_helper 342655Sstever@eecs.umich.edu 352655Sstever@eecs.umich.edudef WriteEmbeddedPyFile(target, source, path, name, ext, filename): 362655Sstever@eecs.umich.edu if isinstance(source, str): 372655Sstever@eecs.umich.edu source = file(source, 'r') 381530SN/A 394202Sbinkertn@umich.edu if isinstance(target, str): 401530SN/A target = file(target, 'w') 412667Sstever@eecs.umich.edu 422667Sstever@eecs.umich.edu print >>target, "AddModule(%s, %s, %s, %s, '''\\" % \ 431046SN/A (`path`, `name`, `ext`, `filename`) 442655Sstever@eecs.umich.edu 452655Sstever@eecs.umich.edu for line in source: 462655Sstever@eecs.umich.edu line = line 472655Sstever@eecs.umich.edu # escape existing backslashes 482655Sstever@eecs.umich.edu line = line.replace('\\', '\\\\') 491046SN/A # escape existing triple quotes 502655Sstever@eecs.umich.edu line = line.replace("'''", r"\'\'\'") 512655Sstever@eecs.umich.edu 522655Sstever@eecs.umich.edu print >>target, line, 532655Sstever@eecs.umich.edu 541046SN/A print >>target, "''')" 552655Sstever@eecs.umich.edu print >>target 562655Sstever@eecs.umich.edu 572655Sstever@eecs.umich.edudef WriteCFile(target, source, name): 582667Sstever@eecs.umich.edu if isinstance(source, str): 592655Sstever@eecs.umich.edu source = file(source, 'r') 602655Sstever@eecs.umich.edu 612655Sstever@eecs.umich.edu if isinstance(target, str): 622655Sstever@eecs.umich.edu target = file(target, 'w') 632655Sstever@eecs.umich.edu 642655Sstever@eecs.umich.edu print >>target, 'const char %s_string[] = {' % name 652655Sstever@eecs.umich.edu 662655Sstever@eecs.umich.edu count = 0 671046SN/A from array import array 682655Sstever@eecs.umich.edu try: 692655Sstever@eecs.umich.edu while True: 702667Sstever@eecs.umich.edu foo = array('B') 711046SN/A foo.fromfile(source, 10000) 722655Sstever@eecs.umich.edu l = [ str(i) for i in foo.tolist() ] 731046SN/A count += len(l) 742655Sstever@eecs.umich.edu for i in xrange(0,9999,20): 752655Sstever@eecs.umich.edu print >>target, ','.join(l[i:i+20]) + ',' 761439SN/A except EOFError: 771530SN/A l = [ str(i) for i in foo.tolist() ] 782889Sbinkertn@umich.edu count += len(l) 791530SN/A for i in xrange(0,len(l),20): 801439SN/A print >>target, ','.join(l[i:i+20]) + ',' 811858SN/A print >>target, ','.join(l[i:]) + ',' 822667Sstever@eecs.umich.edu 831858SN/A print >>target, '};' 842889Sbinkertn@umich.edu print >>target, 'const int %s_length = %d;' % (name, count) 852889Sbinkertn@umich.edu print >>target 862889Sbinkertn@umich.edu 872889Sbinkertn@umich.edudef splitpath(path): 882889Sbinkertn@umich.edu dir,file = os.path.split(path) 892889Sbinkertn@umich.edu path = [] 902889Sbinkertn@umich.edu assert(file) 912889Sbinkertn@umich.edu while dir: 922889Sbinkertn@umich.edu dir,base = os.path.split(dir) 932889Sbinkertn@umich.edu path.insert(0, base) 942889Sbinkertn@umich.edu return path, file 952655Sstever@eecs.umich.edu 962655Sstever@eecs.umich.edudef MakeEmbeddedPyFile(target, source, env): 972667Sstever@eecs.umich.edu target = file(str(target[0]), 'w') 982889Sbinkertn@umich.edu 992655Sstever@eecs.umich.edu tree = {} 1004053Sbinkertn@umich.edu for src in source: 1012655Sstever@eecs.umich.edu src = str(src) 1024086Sbinkertn@umich.edu path,pyfile = splitpath(src) 1034086Sbinkertn@umich.edu node = tree 1044086Sbinkertn@umich.edu for dir in path: 1054086Sbinkertn@umich.edu if not node.has_key(dir): 1063869Sbinkertn@umich.edu node[dir] = { } 1073869Sbinkertn@umich.edu node = node[dir] 1084086Sbinkertn@umich.edu 1094202Sbinkertn@umich.edu name,ext = pyfile.split('.') 1104202Sbinkertn@umich.edu if name == '__init__': 1114202Sbinkertn@umich.edu node['.hasinit'] = True 1124202Sbinkertn@umich.edu node[pyfile] = (src,name,ext,src) 1134202Sbinkertn@umich.edu 1143645Sbinkertn@umich.edu done = False 1154123Sbinkertn@umich.edu while not done: 1163869Sbinkertn@umich.edu done = True 1173871Sbinkertn@umich.edu for name,entry in tree.items(): 1184045Sbinkertn@umich.edu if not isinstance(entry, dict): continue 1194123Sbinkertn@umich.edu if entry.has_key('.hasinit'): continue 1204078Sbinkertn@umich.edu 1214042Sbinkertn@umich.edu done = False 1222667Sstever@eecs.umich.edu del tree[name] 1234086Sbinkertn@umich.edu for key,val in entry.iteritems(): 1244086Sbinkertn@umich.edu if tree.has_key(key): 1254086Sbinkertn@umich.edu raise NameError, \ 1264086Sbinkertn@umich.edu "dir already has %s can't add it again" % key 1274086Sbinkertn@umich.edu tree[key] = val 1284086Sbinkertn@umich.edu 1294086Sbinkertn@umich.edu files = [] 1304086Sbinkertn@umich.edu def populate(node, path = []): 1314086Sbinkertn@umich.edu names = node.keys() 1324086Sbinkertn@umich.edu names.sort() 1334086Sbinkertn@umich.edu for name in names: 1344086Sbinkertn@umich.edu if name == '.hasinit': 1354086Sbinkertn@umich.edu continue 1364086Sbinkertn@umich.edu 1374086Sbinkertn@umich.edu entry = node[name] 1384086Sbinkertn@umich.edu if isinstance(entry, dict): 1394086Sbinkertn@umich.edu if not entry.has_key('.hasinit'): 1404086Sbinkertn@umich.edu raise NameError, 'package directory missing __init__.py' 1414086Sbinkertn@umich.edu populate(entry, path + [ name ]) 1424086Sbinkertn@umich.edu else: 1434086Sbinkertn@umich.edu pyfile,name,ext,filename = entry 1444086Sbinkertn@umich.edu files.append((pyfile, path, name, ext, filename)) 1454086Sbinkertn@umich.edu populate(tree) 1464086Sbinkertn@umich.edu 1474086Sbinkertn@umich.edu for pyfile, path, name, ext, filename in files: 1484086Sbinkertn@umich.edu WriteEmbeddedPyFile(target, pyfile, path, name, ext, filename) 1492655Sstever@eecs.umich.edu 1502655Sstever@eecs.umich.edudef MakeDefinesPyFile(target, source, env): 1512655Sstever@eecs.umich.edu f = file(str(target[0]), 'w') 1524202Sbinkertn@umich.edu print >>f, "import __main__" 1532655Sstever@eecs.umich.edu print >>f, "__main__.m5_build_env = ", 1542655Sstever@eecs.umich.edu print >>f, source[0] 1552655Sstever@eecs.umich.edu f.close() 1562655Sstever@eecs.umich.edu 1572655Sstever@eecs.umich.eduCFileCounter = 0 1582655Sstever@eecs.umich.edudef MakePythonCFile(target, source, env): 159 global CFileCounter 160 target = file(str(target[0]), 'w') 161 162 print >>target, '''\ 163#include "base/embedfile.hh" 164 165namespace { 166''' 167 for src in source: 168 src = str(src) 169 fname = os.path.basename(src) 170 name = 'embedded_file%d' % CFileCounter 171 CFileCounter += 1 172 WriteCFile(target, src, name) 173 print >>target, '''\ 174EmbedMap %(name)s("%(fname)s", 175 %(name)s_string, %(name)s_length); 176 177''' % locals() 178 print >>target, '''\ 179 180/* namespace */ } 181''' 182 183# base list of .py files to embed 184embedded_py_files = [ '../util/pbs/jobfile.py' ] 185# add all .py files in python/m5 186objpath = os.path.join(env['SRCDIR'], 'python', 'm5') 187for root, dirs, files in os.walk(objpath, topdown=True): 188 for i,dir in enumerate(dirs): 189 if dir == 'SCCS': 190 del dirs[i] 191 break 192 193 assert(root.startswith(objpath)) 194 for f in files: 195 if f.endswith('.py'): 196 embedded_py_files.append(os.path.join(root, f)) 197 198embedfile_hh = os.path.join(env['SRCDIR'], 'base/embedfile.hh') 199 200optionDict = dict([(opt, env[opt]) for opt in env.ExportOptions]) 201env.Command('defines.py', Value(optionDict), MakeDefinesPyFile) 202 203env.Command('embedded_py.py', embedded_py_files, MakeEmbeddedPyFile) 204env.Depends('embedded_py.cc', embedfile_hh) 205env.Command('embedded_py.cc', 206 ['string_importer.py', 'defines.py', 'embedded_py.py'], 207 MakePythonCFile) 208