112244Sgabeblack@google.com# Copyright (c) 2013, 2015-2017 ARM Limited
212244Sgabeblack@google.com# All rights reserved.
312244Sgabeblack@google.com#
412244Sgabeblack@google.com# The license below extends only to copyright in the software and shall
512244Sgabeblack@google.com# not be construed as granting a license to any other intellectual
612244Sgabeblack@google.com# property including but not limited to intellectual property relating
712244Sgabeblack@google.com# to a hardware implementation of the functionality of the software
812244Sgabeblack@google.com# licensed hereunder.  You may use the software subject to the license
912244Sgabeblack@google.com# terms below provided that you ensure that this notice is replicated
1012244Sgabeblack@google.com# unmodified and in its entirety in all distributions of the software,
1112244Sgabeblack@google.com# modified or unmodified, in source code or in binary form.
1212244Sgabeblack@google.com#
1312244Sgabeblack@google.com# Copyright (c) 2011 Advanced Micro Devices, Inc.
1412244Sgabeblack@google.com# Copyright (c) 2009 The Hewlett-Packard Development Company
1512244Sgabeblack@google.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
1612244Sgabeblack@google.com# All rights reserved.
1712244Sgabeblack@google.com#
1812244Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
1912244Sgabeblack@google.com# modification, are permitted provided that the following conditions are
2012244Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
2112244Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
2212244Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
2312244Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
2412244Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
2512244Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
2612244Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
2712244Sgabeblack@google.com# this software without specific prior written permission.
2812244Sgabeblack@google.com#
2912244Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3012244Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3112244Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3212244Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3312244Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3412244Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3512244Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3612244Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3712244Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3812244Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3912244Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4012244Sgabeblack@google.com
4112558Ssiddhesh.poyarekar@gmail.comfrom __future__ import print_function
4212244Sgabeblack@google.comimport re
4312244Sgabeblack@google.comimport sys
4412244Sgabeblack@google.com
4512244Sgabeblack@google.comimport gem5_scons.util
4612244Sgabeblack@google.com
4712244Sgabeblack@google.commercurial_style_message = """
4812244Sgabeblack@google.comYou're missing the gem5 style hook, which automatically checks your code
4912244Sgabeblack@google.comagainst the gem5 style rules on hg commit and qrefresh commands.
5012244Sgabeblack@google.comThis script will now install the hook in your .hg/hgrc file.
5112244Sgabeblack@google.comPress enter to continue, or ctrl-c to abort: """
5212244Sgabeblack@google.com
5312244Sgabeblack@google.commercurial_style_upgrade_message = """
5412244Sgabeblack@google.comYour Mercurial style hooks are not up-to-date. This script will now
5512244Sgabeblack@google.comtry to automatically update them. A backup of your hgrc will be saved
5612244Sgabeblack@google.comin .hg/hgrc.old.
5712244Sgabeblack@google.comPress enter to continue, or ctrl-c to abort: """
5812244Sgabeblack@google.com
5912244Sgabeblack@google.commercurial_style_hook_template = """
6012244Sgabeblack@google.com# The following lines were automatically added by gem5/SConstruct
6112244Sgabeblack@google.com# to provide the gem5 style-checking hooks
6212244Sgabeblack@google.com[extensions]
6312244Sgabeblack@google.comhgstyle = %s/util/hgstyle.py
6412244Sgabeblack@google.com
6512244Sgabeblack@google.com[hooks]
6612244Sgabeblack@google.compretxncommit.style = python:hgstyle.check_style
6712244Sgabeblack@google.compre-qrefresh.style = python:hgstyle.check_style
6812244Sgabeblack@google.com# End of SConstruct additions
6912244Sgabeblack@google.com
7012244Sgabeblack@google.com"""
7112244Sgabeblack@google.com
7212244Sgabeblack@google.commercurial_lib_not_found = """
7312244Sgabeblack@google.comMercurial libraries cannot be found, ignoring style hook.  If
7412244Sgabeblack@google.comyou are a gem5 developer, please fix this and run the style
7512244Sgabeblack@google.comhook. It is important.
7612244Sgabeblack@google.com"""
7712244Sgabeblack@google.com
7812244Sgabeblack@google.comdef install_style_hooks(env):
7912244Sgabeblack@google.com    hgdir = env.Dir('#.hg')
8012244Sgabeblack@google.com
8112244Sgabeblack@google.com    style_hook = True
8212244Sgabeblack@google.com    style_hooks = tuple()
8312244Sgabeblack@google.com    hgrc = hgdir.File('hgrc')
8412244Sgabeblack@google.com    hgrc_old = hgdir.File('hgrc.old')
8512244Sgabeblack@google.com    try:
8612244Sgabeblack@google.com        from mercurial import ui
8712244Sgabeblack@google.com        ui = ui.ui()
8812244Sgabeblack@google.com        ui.readconfig(hgrc.abspath)
8912244Sgabeblack@google.com        style_hooks = (ui.config('hooks', 'pretxncommit.style', None),
9012244Sgabeblack@google.com                       ui.config('hooks', 'pre-qrefresh.style', None))
9112244Sgabeblack@google.com        style_hook = all(style_hooks)
9212244Sgabeblack@google.com        style_extension = ui.config('extensions', 'style', None)
9312244Sgabeblack@google.com    except ImportError:
9412558Ssiddhesh.poyarekar@gmail.com        print(mercurial_lib_not_found)
9512244Sgabeblack@google.com
9612244Sgabeblack@google.com    if "python:style.check_style" in style_hooks:
9712244Sgabeblack@google.com        # Try to upgrade the style hooks
9812558Ssiddhesh.poyarekar@gmail.com        print(mercurial_style_upgrade_message)
9912244Sgabeblack@google.com        # continue unless user does ctrl-c/ctrl-d etc.
10012244Sgabeblack@google.com        try:
10112244Sgabeblack@google.com            raw_input()
10212244Sgabeblack@google.com        except:
10312558Ssiddhesh.poyarekar@gmail.com            print("Input exception, exiting scons.\n")
10412244Sgabeblack@google.com            sys.exit(1)
10512244Sgabeblack@google.com        shutil.copyfile(hgrc.abspath, hgrc_old.abspath)
10612244Sgabeblack@google.com        re_style_hook = re.compile(r"^([^=#]+)\.style\s*=\s*([^#\s]+).*")
10712244Sgabeblack@google.com        re_style_extension = re.compile("style\s*=\s*([^#\s]+).*")
10812244Sgabeblack@google.com        old, new = open(hgrc_old.abspath, 'r'), open(hgrc.abspath, 'w')
10912244Sgabeblack@google.com        for l in old:
11012244Sgabeblack@google.com            m_hook = re_style_hook.match(l)
11112244Sgabeblack@google.com            m_ext = re_style_extension.match(l)
11212244Sgabeblack@google.com            if m_hook:
11312244Sgabeblack@google.com                hook, check = m_hook.groups()
11412244Sgabeblack@google.com                if check != "python:style.check_style":
11512558Ssiddhesh.poyarekar@gmail.com                    print("Warning: %s.style is using a non-default " \
11612558Ssiddhesh.poyarekar@gmail.com                        "checker: %s" % (hook, check))
11712244Sgabeblack@google.com                if hook not in ("pretxncommit", "pre-qrefresh"):
11812558Ssiddhesh.poyarekar@gmail.com                    print("Warning: Updating unknown style hook: %s" % hook)
11912244Sgabeblack@google.com
12012244Sgabeblack@google.com                l = "%s.style = python:hgstyle.check_style\n" % hook
12112244Sgabeblack@google.com            elif m_ext and m_ext.group(1) == style_extension:
12212244Sgabeblack@google.com                l = "hgstyle = %s/util/hgstyle.py\n" % env.root.abspath
12312244Sgabeblack@google.com
12412244Sgabeblack@google.com            new.write(l)
12512244Sgabeblack@google.com    elif not style_hook:
12612558Ssiddhesh.poyarekar@gmail.com        print(mercurial_style_message, end=' ')
12712244Sgabeblack@google.com        # continue unless user does ctrl-c/ctrl-d etc.
12812244Sgabeblack@google.com        try:
12912244Sgabeblack@google.com            raw_input()
13012244Sgabeblack@google.com        except:
13112558Ssiddhesh.poyarekar@gmail.com            print("Input exception, exiting scons.\n")
13212244Sgabeblack@google.com            sys.exit(1)
13312244Sgabeblack@google.com        hgrc_path = '%s/.hg/hgrc' % env.root.abspath
13412558Ssiddhesh.poyarekar@gmail.com        print("Adding style hook to", hgrc_path, "\n")
13512244Sgabeblack@google.com        try:
13612244Sgabeblack@google.com            with open(hgrc_path, 'a') as f:
13712244Sgabeblack@google.com                f.write(mercurial_style_hook_template % env.root.abspath)
13812244Sgabeblack@google.com        except:
13912558Ssiddhesh.poyarekar@gmail.com            print("Error updating", hgrc_path)
14012244Sgabeblack@google.com            sys.exit(1)
14112244Sgabeblack@google.com
14212244Sgabeblack@google.comdef generate(env):
14312244Sgabeblack@google.com    if exists(env) and not gem5_scons.util.ignore_style():
14412244Sgabeblack@google.com        install_style_hooks(env)
14512244Sgabeblack@google.com
14612244Sgabeblack@google.comdef exists(env):
14712244Sgabeblack@google.com    return env.Dir('#.hg').exists()
148