SConscript revision 2623
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 331530SN/Aimport scons_helper 344202Sbinkertn@umich.edu 354202Sbinkertn@umich.edudef WriteEmbeddedPyFile(target, source, path, name, ext, filename): 363645Sbinkertn@umich.edu if isinstance(source, str): 375522Snate@binkert.org source = file(source, 'r') 384382Sbinkertn@umich.edu 394382Sbinkertn@umich.edu if isinstance(target, str): 405471Snate@binkert.org target = file(target, 'w') 414382Sbinkertn@umich.edu 425801Snate@binkert.org print >>target, "AddModule(%s, %s, %s, %s, '''\\" % \ 435801Snate@binkert.org (`path`, `name`, `ext`, `filename`) 444382Sbinkertn@umich.edu 454382Sbinkertn@umich.edu for line in source: 465470Snate@binkert.org line = line 474382Sbinkertn@umich.edu # escape existing backslashes 484382Sbinkertn@umich.edu line = line.replace('\\', '\\\\') 494762Snate@binkert.org # escape existing triple quotes 504382Sbinkertn@umich.edu line = line.replace("'''", r"\'\'\'") 514382Sbinkertn@umich.edu 524382Sbinkertn@umich.edu print >>target, line, 535799Snate@binkert.org 545467Snate@binkert.org print >>target, "''')" 555467Snate@binkert.org print >>target 566502Snate@binkert.org 576501Snate@binkert.orgdef WriteCFile(target, source, name): 585467Snate@binkert.org if isinstance(source, str): 595467Snate@binkert.org source = file(source, 'r') 605467Snate@binkert.org 616500Snate@binkert.org if isinstance(target, str): 622667Sstever@eecs.umich.edu target = file(target, 'w') 634382Sbinkertn@umich.edu 644382Sbinkertn@umich.edu print >>target, 'const char %s_string[] = {' % name 654382Sbinkertn@umich.edu 664382Sbinkertn@umich.edu count = 0 674382Sbinkertn@umich.edu from array import array 684382Sbinkertn@umich.edu try: 694382Sbinkertn@umich.edu while True: 70 foo = array('B') 71 foo.fromfile(source, 10000) 72 l = [ str(i) for i in foo.tolist() ] 73 count += len(l) 74 for i in xrange(0,9999,20): 75 print >>target, ','.join(l[i:i+20]) + ',' 76 except EOFError: 77 l = [ str(i) for i in foo.tolist() ] 78 count += len(l) 79 for i in xrange(0,len(l),20): 80 print >>target, ','.join(l[i:i+20]) + ',' 81 print >>target, ','.join(l[i:]) + ',' 82 83 print >>target, '};' 84 print >>target, 'const int %s_length = %d;' % (name, count) 85 print >>target 86 87def splitpath(path): 88 dir,file = os.path.split(path) 89 path = [] 90 assert(file) 91 while dir: 92 dir,base = os.path.split(dir) 93 path.insert(0, base) 94 return path, file 95 96def MakeEmbeddedPyFile(target, source, env): 97 target = file(str(target[0]), 'w') 98 99 tree = {} 100 for src in source: 101 src = str(src) 102 path,pyfile = splitpath(src) 103 node = tree 104 for dir in path: 105 if not node.has_key(dir): 106 node[dir] = { } 107 node = node[dir] 108 109 name,ext = pyfile.split('.') 110 if name == '__init__': 111 node['.hasinit'] = True 112 node[pyfile] = (src,name,ext,src) 113 114 done = False 115 while not done: 116 done = True 117 for name,entry in tree.items(): 118 if not isinstance(entry, dict): continue 119 if entry.has_key('.hasinit'): continue 120 121 done = False 122 del tree[name] 123 for key,val in entry.iteritems(): 124 if tree.has_key(key): 125 raise NameError, \ 126 "dir already has %s can't add it again" % key 127 tree[key] = val 128 129 files = [] 130 def populate(node, path = []): 131 names = node.keys() 132 names.sort() 133 for name in names: 134 if name == '.hasinit': 135 continue 136 137 entry = node[name] 138 if isinstance(entry, dict): 139 if not entry.has_key('.hasinit'): 140 raise NameError, 'package directory missing __init__.py' 141 populate(entry, path + [ name ]) 142 else: 143 pyfile,name,ext,filename = entry 144 files.append((pyfile, path, name, ext, filename)) 145 populate(tree) 146 147 for pyfile, path, name, ext, filename in files: 148 WriteEmbeddedPyFile(target, pyfile, path, name, ext, filename) 149 150def MakeDefinesPyFile(target, source, env): 151 f = file(str(target[0]), 'w') 152 print >>f, "import __main__" 153 print >>f, "__main__.m5_build_env = ", 154 print >>f, source[0] 155 f.close() 156 157CFileCounter = 0 158def 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