114184Sgabeblack@google.com# -*- mode:python -*-
214184Sgabeblack@google.com
314184Sgabeblack@google.com# Copyright (c) 2009 The Hewlett-Packard Development Company
414184Sgabeblack@google.com# All rights reserved.
514184Sgabeblack@google.com#
614184Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
714184Sgabeblack@google.com# modification, are permitted provided that the following conditions are
814184Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
914184Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
1014184Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
1114184Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
1214184Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
1314184Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
1414184Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
1514184Sgabeblack@google.com# this software without specific prior written permission.
1614184Sgabeblack@google.com#
1714184Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1814184Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1914184Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2014184Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2114184Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2214184Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2314184Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2414184Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2514184Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2614184Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2714184Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2814184Sgabeblack@google.com#
2914184Sgabeblack@google.com# Authors: Nathan Binkert
3014184Sgabeblack@google.com
3114184Sgabeblack@google.comimport os
3214184Sgabeblack@google.comimport re
3314184Sgabeblack@google.comimport sys
3414184Sgabeblack@google.com
3514184Sgabeblack@google.comfrom os.path import isdir, isfile, join as joinpath
3614184Sgabeblack@google.com
3714184Sgabeblack@google.comfrom SCons.Scanner import Classic
3814184Sgabeblack@google.com
3914184Sgabeblack@google.comfrom gem5_scons import Transform
4014184Sgabeblack@google.com
4114184Sgabeblack@google.comImport('*')
4214184Sgabeblack@google.com
4314184Sgabeblack@google.comif env['PROTOCOL'] == 'None':
4414184Sgabeblack@google.com    Return()
4514184Sgabeblack@google.com
4614184Sgabeblack@google.comoutput_dir = Dir('.')
4714184Sgabeblack@google.comhtml_dir = Dir('html')
4814184Sgabeblack@google.comslicc_dir = Dir('../slicc')
4914184Sgabeblack@google.com
5014184Sgabeblack@google.comsys.path[1:1] = [ Dir('..').Dir('..').srcnode().abspath ]
5114184Sgabeblack@google.comfrom slicc.parser import SLICC
5214184Sgabeblack@google.com
5314184Sgabeblack@google.comslicc_depends = []
5414184Sgabeblack@google.comfor root,dirs,files in os.walk(slicc_dir.srcnode().abspath):
5514184Sgabeblack@google.com    for f in files:
5614184Sgabeblack@google.com        if f.endswith('.py'):
5714184Sgabeblack@google.com            slicc_depends.append(File(joinpath(root, f)))
5814184Sgabeblack@google.com
5914184Sgabeblack@google.com#
6014184Sgabeblack@google.com# Use SLICC
6114184Sgabeblack@google.com#
6214184Sgabeblack@google.comenv["SLICC_PATH"] = protocol_dirs
6314184Sgabeblack@google.comslicc_scanner = Classic("SliccScanner", ['.sm', '.slicc'], "SLICC_PATH",
6414184Sgabeblack@google.com                        r'''include[ \t]["'](.*)["'];''')
6514184Sgabeblack@google.comenv.Append(SCANNERS=slicc_scanner)
6614184Sgabeblack@google.com
6714184Sgabeblack@google.comdef slicc_emitter(target, source, env):
6814184Sgabeblack@google.com    assert len(source) == 1
6914184Sgabeblack@google.com    filepath = source[0].srcnode().abspath
7014184Sgabeblack@google.com
7114184Sgabeblack@google.com    slicc = SLICC(filepath, protocol_base.abspath, verbose=False)
7214184Sgabeblack@google.com    slicc.process()
7314184Sgabeblack@google.com    slicc.writeCodeFiles(output_dir.abspath, slicc_includes)
7414184Sgabeblack@google.com    if env['SLICC_HTML']:
7514184Sgabeblack@google.com        slicc.writeHTMLFiles(html_dir.abspath)
7614184Sgabeblack@google.com
7714184Sgabeblack@google.com    target.extend([output_dir.File(f) for f in sorted(slicc.files())])
7814184Sgabeblack@google.com    return target, source
7914184Sgabeblack@google.com
8014184Sgabeblack@google.comdef slicc_action(target, source, env):
8114184Sgabeblack@google.com    assert len(source) == 1
8214184Sgabeblack@google.com    filepath = source[0].srcnode().abspath
8314184Sgabeblack@google.com
8414184Sgabeblack@google.com    slicc = SLICC(filepath, protocol_base.abspath, verbose=True)
8514184Sgabeblack@google.com    slicc.process()
8614184Sgabeblack@google.com    slicc.writeCodeFiles(output_dir.abspath, slicc_includes)
8714184Sgabeblack@google.com    if env['SLICC_HTML']:
8814184Sgabeblack@google.com        slicc.writeHTMLFiles(html_dir.abspath)
8914184Sgabeblack@google.com
9014184Sgabeblack@google.comslicc_builder = Builder(action=MakeAction(slicc_action, Transform("SLICC")),
9114184Sgabeblack@google.com                        emitter=slicc_emitter)
9214184Sgabeblack@google.com
9314184Sgabeblack@google.comprotocol = env['PROTOCOL']
9414184Sgabeblack@google.comprotocol_dir = None
9514184Sgabeblack@google.comfor path in protocol_dirs:
9614184Sgabeblack@google.com    if os.path.exists(os.path.join(path, "%s.slicc" % protocol)):
9714184Sgabeblack@google.com        protocol_dir = Dir(path)
9814184Sgabeblack@google.com        break
9914184Sgabeblack@google.com
10014184Sgabeblack@google.comif not protocol_dir:
10114184Sgabeblack@google.com    raise ValueError, "Could not find %s.slicc in protocol_dirs" % protocol
10214184Sgabeblack@google.com
10314184Sgabeblack@google.comsources = [ protocol_dir.File("%s.slicc" % protocol) ]
10414184Sgabeblack@google.com
10514184Sgabeblack@google.comenv.Append(BUILDERS={'SLICC' : slicc_builder})
10614184Sgabeblack@google.comnodes = env.SLICC([], sources)
10714184Sgabeblack@google.comenv.Depends(nodes, slicc_depends)
10814184Sgabeblack@google.com
10914184Sgabeblack@google.comfor f in nodes:
11014184Sgabeblack@google.com    s = str(f)
11114184Sgabeblack@google.com    if s.endswith('.cc'):
11214184Sgabeblack@google.com        Source(f)
11314184Sgabeblack@google.com    elif s.endswith('.py'):
11414184Sgabeblack@google.com        SimObject(f)
11514184Sgabeblack@google.com
116