SConscript revision 1530
1# -*- mode:python -*- 2 3# Copyright (c) 2005 The Regents of The University of Michigan 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are 8# met: redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer; 10# redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution; 13# neither the name of the copyright holders nor the names of its 14# contributors may be used to endorse or promote products derived from 15# this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29import os, os.path, re, sys 30 31Import('env') 32 33# tell python where to find m5 python code 34sys.path.append(os.path.join(env['SRCDIR'], 'python')) 35 36import m5scons 37 38def WriteEmbeddedPyFile(target, source, path, name, ext, filename): 39 if isinstance(source, str): 40 source = file(source, 'r') 41 42 if isinstance(target, str): 43 target = file(target, 'w') 44 45 print >>target, "AddModule(%s, %s, %s, %s, '''\\" % \ 46 (`path`, `name`, `ext`, `filename`) 47 48 for line in source: 49 line = line 50 # escape existing backslashes 51 line = line.replace('\\', '\\\\') 52 # escape existing triple quotes 53 line = line.replace("'''", r"\'\'\'") 54 55 print >>target, line, 56 57 print >>target, "''')" 58 print >>target 59 60def WriteCFile(target, source, name): 61 if isinstance(source, str): 62 source = file(source, 'r') 63 64 if isinstance(target, str): 65 target = file(target, 'w') 66 67 print >>target, 'const char %s_string[] = {' % name 68 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