SConscript revision 1530
112855Sgabeblack@google.com# -*- mode:python -*- 212855Sgabeblack@google.com 312855Sgabeblack@google.com# Copyright (c) 2005 The Regents of The University of Michigan 412855Sgabeblack@google.com# All rights reserved. 512855Sgabeblack@google.com# 612855Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without 712855Sgabeblack@google.com# modification, are permitted provided that the following conditions are 812855Sgabeblack@google.com# met: redistributions of source code must retain the above copyright 912855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer; 1012855Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright 1112855Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the 1212855Sgabeblack@google.com# documentation and/or other materials provided with the distribution; 1312855Sgabeblack@google.com# neither the name of the copyright holders nor the names of its 1412855Sgabeblack@google.com# contributors may be used to endorse or promote products derived from 1512855Sgabeblack@google.com# this software without specific prior written permission. 1612855Sgabeblack@google.com# 1712855Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1812855Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1912855Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2012855Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2112855Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2212855Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2312855Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2412855Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2512855Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2612855Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2712855Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2812855Sgabeblack@google.com 2912855Sgabeblack@google.comimport os, os.path, re, sys 3012855Sgabeblack@google.com 3112855Sgabeblack@google.comImport('env') 3212855Sgabeblack@google.com 3312855Sgabeblack@google.com# tell python where to find m5 python code 3412855Sgabeblack@google.comsys.path.append(os.path.join(env['SRCDIR'], 'python')) 3512855Sgabeblack@google.com 3612855Sgabeblack@google.comimport m5scons 3712855Sgabeblack@google.com 3812855Sgabeblack@google.comdef WriteEmbeddedPyFile(target, source, path, name, ext, filename): 3912855Sgabeblack@google.com if isinstance(source, str): 4012855Sgabeblack@google.com source = file(source, 'r') 4112855Sgabeblack@google.com 4212855Sgabeblack@google.com if isinstance(target, str): 4312855Sgabeblack@google.com target = file(target, 'w') 4412855Sgabeblack@google.com 4512855Sgabeblack@google.com print >>target, "AddModule(%s, %s, %s, %s, '''\\" % \ 4612855Sgabeblack@google.com (`path`, `name`, `ext`, `filename`) 4712855Sgabeblack@google.com 4812855Sgabeblack@google.com for line in source: 4912855Sgabeblack@google.com line = line 5012855Sgabeblack@google.com # escape existing backslashes 5112855Sgabeblack@google.com line = line.replace('\\', '\\\\') 5212855Sgabeblack@google.com # escape existing triple quotes 5312855Sgabeblack@google.com line = line.replace("'''", r"\'\'\'") 5412855Sgabeblack@google.com 5512855Sgabeblack@google.com print >>target, line, 5612855Sgabeblack@google.com 5712855Sgabeblack@google.com print >>target, "''')" 5812855Sgabeblack@google.com print >>target 5912855Sgabeblack@google.com 6012855Sgabeblack@google.comdef WriteCFile(target, source, name): 6112855Sgabeblack@google.com if isinstance(source, str): 6212855Sgabeblack@google.com source = file(source, 'r') 6312855Sgabeblack@google.com 6412855Sgabeblack@google.com if isinstance(target, str): 6512855Sgabeblack@google.com target = file(target, 'w') 6612855Sgabeblack@google.com 6712855Sgabeblack@google.com print >>target, 'const char %s_string[] = {' % name 6812855Sgabeblack@google.com 69 count = 0 70 from array import array 71 try: 72 while True: 73 foo = array('B') 74 foo.fromfile(source, 10000) 75 l = [ str(i) for i in foo.tolist() ] 76 count += len(l) 77 for i in xrange(0,9999,20): 78 print >>target, ','.join(l[i:i+20]) + ',' 79 except EOFError: 80 l = [ str(i) for i in foo.tolist() ] 81 count += len(l) 82 for i in xrange(0,len(l),20): 83 print >>target, ','.join(l[i:i+20]) + ',' 84 print >>target, ','.join(l[i:]) + ',' 85 86 print >>target, '};' 87 print >>target, 'const int %s_length = %d;' % (name, count) 88 print >>target 89 90def splitpath(path): 91 dir,file = os.path.split(path) 92 path = [] 93 assert(file) 94 while dir: 95 dir,base = os.path.split(dir) 96 path.insert(0, base) 97 return path, file 98 99def MakeEmbeddedPyFile(target, source, env): 100 target = file(str(target[0]), 'w') 101 102 tree = {} 103 for src in source: 104 src = str(src) 105 path,pyfile = splitpath(src) 106 node = tree 107 for dir in path: 108 if not node.has_key(dir): 109 node[dir] = { } 110 node = node[dir] 111 112 name,ext = pyfile.split('.') 113 if name == '__init__': 114 node['.hasinit'] = True 115 node[pyfile] = (src,name,ext,src) 116 117 done = False 118 while not done: 119 done = True 120 for name,entry in tree.items(): 121 if not isinstance(entry, dict): continue 122 if entry.has_key('.hasinit'): continue 123 124 done = False 125 del tree[name] 126 for key,val in entry.iteritems(): 127 if tree.has_key(key): 128 raise NameError, \ 129 "dir already has %s can't add it again" % key 130 tree[key] = val 131 132 files = [] 133 def populate(node, path = []): 134 names = node.keys() 135 names.sort() 136 for name in names: 137 if name == '.hasinit': 138 continue 139 140 entry = node[name] 141 if isinstance(entry, dict): 142 if not entry.has_key('.hasinit'): 143 raise NameError, 'package directory missing __init__.py' 144 populate(entry, path + [ name ]) 145 else: 146 pyfile,name,ext,filename = entry 147 files.append((pyfile, path, name, ext, filename)) 148 populate(tree) 149 150 for pyfile, path, name, ext, filename in files: 151 WriteEmbeddedPyFile(target, pyfile, path, name, ext, filename) 152 153def MakeDefinesPyFile(target, source, env): 154 f = file(str(target[0]), 'w') 155 print >>f, "import __main__" 156 print >>f, "__main__.m5_build_env = ", 157 print >>f, m5scons.flatten_defines(env['CPPDEFINES']) 158 f.close() 159 160CFileCounter = 0 161def MakePythonCFile(target, source, env): 162 global CFileCounter 163 target = file(str(target[0]), 'w') 164 165 print >>target, '''\ 166#include "base/embedfile.hh" 167 168namespace { 169''' 170 for src in source: 171 src = str(src) 172 fname = os.path.basename(src) 173 name = 'embedded_file%d' % CFileCounter 174 CFileCounter += 1 175 WriteCFile(target, src, name) 176 print >>target, '''\ 177EmbedMap %(name)s("%(fname)s", 178 %(name)s_string, %(name)s_length); 179 180''' % locals() 181 print >>target, '''\ 182 183/* namespace */ } 184''' 185 186embedded_py_files = [ 'mpy_importer.py', '../util/pbs/jobfile.py' ] 187objpath = os.path.join(env['SRCDIR'], 'python/m5') 188for root, dirs, files in os.walk(objpath, topdown=True): 189 for i,dir in enumerate(dirs): 190 if dir == 'SCCS': 191 del dirs[i] 192 break 193 194 assert(root.startswith(objpath)) 195 for f in files: 196 if f.endswith('.mpy') or f.endswith('.py'): 197 embedded_py_files.append(os.path.join(root, f)) 198 199embedfile_hh = os.path.join(env['SRCDIR'], 'base/embedfile.hh') 200env.Command('defines.py', None, MakeDefinesPyFile) 201env.Command('embedded_py.py', embedded_py_files, MakeEmbeddedPyFile) 202env.Depends('embedded_py.cc', embedfile_hh) 203env.Command('embedded_py.cc', 204 ['string_importer.py', 'defines.py', 'embedded_py.py'], 205 MakePythonCFile) 206