summaryrefslogtreecommitdiff
path: root/tests/scripts/optdata/bootstrap.py
blob: 1cf55fa70cefbc779df29e30de06cc326db32ddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python

"""
    This script prepares the local source tree to be built with
    custom optdata. Simply run this script and follow the
    instructions to inject manually created optdata into the build.
"""

import argparse
import json
import os
from os import path
import shutil
import subprocess
import sys

# Display the docstring if the user passes -h|--help
argparse.ArgumentParser(description=__doc__).parse_args()

SCRIPT_ROOT = path.dirname(path.realpath(__file__))
REPO_ROOT = path.realpath(path.join(SCRIPT_ROOT, '..', '..', '..'))

NUGET_SRC_DIR = path.join(REPO_ROOT, 'src', '.nuget')
assert path.exists(NUGET_SRC_DIR), \
    "Expected %s to exist; please check whether REPO_ROOT is really %s" % (NUGET_SRC_DIR, REPO_ROOT)

ORIGIN_FILE = path.join(SCRIPT_ROOT, 'project.json')
TARGET_FILE = path.join(NUGET_SRC_DIR, 'optdata', 'project.json')

ARCH_LIST = ['x64', 'x86']
TOOL_LIST = ['IBC', 'PGO']

def get_buildos():
    """Returns the Build_OS component used by the build system."""
    if os.name == 'nt':
        return 'Windows_NT'
    else:
        sysname = os.uname()[0]
        return 'OSX' if sysname.lower() == 'Darwin'.lower() else sysname

def get_optdata_version(tool):
    """Returns the version string specified in project.json for the given tool."""
    package_name = 'optimization.%s.CoreCLR' % (tool)
    with open(ORIGIN_FILE) as json_file:
        return json.load(json_file)['dependencies'][package_name]

def get_optdata_dir(tool, arch):
    """Returns an absolute path to the directory that should contain optdata given a tool,arch"""
    package_name = 'optimization.%s-%s.%s.CoreCLR' % (get_buildos(), arch, tool)
    package_version = get_optdata_version(tool)
    return path.join(REPO_ROOT, 'packages', package_name, package_version, 'data')

def check_for_unstaged_changes(file_path):
    """Returns whether a file in git has untracked changes."""
    if not path.exists(file_path):
        return False
    try:
        subprocess.check_call(['git', 'diff', '--quiet', '--', file_path])
        return False
    except subprocess.CalledProcessError:
        return True

def main():
    """Entry point"""
    if check_for_unstaged_changes(TARGET_FILE):
        print("ERROR: You seem to have unstaged changes to %s that would be overwritten."
              % (TARGET_FILE))
        print("Please clean, commit, or stash them before running this script.")
        return 1

    if not path.exists(path.dirname(TARGET_FILE)):
        os.makedirs(path.dirname(TARGET_FILE))
    shutil.copyfile(ORIGIN_FILE, TARGET_FILE)

    print("Bootstrapping optdata is complete.")
    for tool in TOOL_LIST:
        for arch in ARCH_LIST:
            optdata_dir = get_optdata_dir(tool, arch)
            print("  * Copy %s %s files into: %s" % (arch, tool, optdata_dir))
    print("NOTE: Make sure to add 'skiprestoreoptdata' as a switch on the build command line!")

    return 0

if __name__ == '__main__':
    sys.exit(main())