Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 135 lines (108 sloc) 4.42 KB
#!/usr/bin/env python
#
import sys
import os
import glob
import getopt
from itertools import takewhile
def path_as_list(path):
previous_head = None
(head, tail) = os.path.split(path)
path_list = []
while head != previous_head:
path_list = [tail] + path_list
previous_head = head
(head, tail) = os.path.split(head)
path_list = [head] + path_list
return path_list
def breakwhere(predicate, iterable):
left, right = [], []
in_left = True
for x in iterable:
if in_left and predicate(x):
in_left = False
if in_left:
left.append(x)
else:
right.append(x)
return (left, right)
def usage(program):
print "Usage:", program, "[--split] core/benchmark [variant-base-name] variant-code"
def main(program, arguments):
options, args = getopt.getopt(arguments, "", ["split", "stage="])
do_split = False
stage = 2
for key, arg in options:
if key == "--split":
do_split = True
elif key == "--stage":
stage = int(arg)
else:
raise Exception("Unrecognised option: " + key)
if len(args) == 2:
mode = args[0]
supplied_variant_base_name = None
variant_code = args[1]
elif len(args) == 3:
mode = args[0]
supplied_variant_base_name = args[1]
variant_code = args[2]
else:
usage(program)
return 2
ghc_opts = '-O ' + os.environ.get("EXTRA_HC_OPTS", '')
current_directory = os.getcwd()
full_directory_path = path_as_list(current_directory)
(ghc_root_from_nofib, nofib_relative) = breakwhere(lambda x: x == 'nofib', full_directory_path)
(ghc_root_from_libraries, libraries_relative) = breakwhere(lambda x: x == 'libraries', full_directory_path)
if ghc_root_from_nofib != full_directory_path:
is_nofib = True
ghc_root = os.path.join(*ghc_root_from_nofib)
output_relative = len(nofib_relative) > 1 and os.path.join(*nofib_relative[1:]) or "" # Drop the 'nofib' prefix here..
elif ghc_root_from_libraries != full_directory_path:
is_nofib = False
ghc_root = os.path.join(*ghc_root_from_libraries)
output_relative = os.path.join(*libraries_relative) # ..but don't drop the 'libraries' prefix here
else:
raise Exception("Could not find GHC root!")
ghc = os.path.join(ghc_root, 'ghc', 'stage' + str(stage) + '-inplace', 'ghc')
investigation_root, inferred_variant_base_name = os.path.split(ghc_root)
if supplied_variant_base_name:
variant_base_name = supplied_variant_base_name
else:
variant_base_name = inferred_variant_base_name
variant = variant_base_name + '-' + variant_code
print "Variant:", variant, "Stage:", stage
if mode == 'core':
ghc_opts += " -fforce-recomp -fno-code -c -dverbose-core2core -ddump-stg"
print "Options:", ghc_opts
output_root = os.path.join(investigation_root, 'results', 'code', variant, output_relative)
print "Output to", output_root
if not os.path.exists(output_root):
os.makedirs(output_root)
for source_path in glob.glob(os.path.join(current_directory, '*.hs')) + glob.glob(os.path.join(current_directory, '*.lhs')):
source_file = os.path.basename(source_path)
print "Compiling", source_file
output_path = os.path.join(output_root, source_file)
output_file = open(output_path, 'w')
lines = os.popen(ghc + ' ' + ghc_opts + ' ' + source_path + ' 2>&1', 'r').readlines()
output_file.writelines(lines)
output_file.close()
if do_split:
print "Splitting"
os.system('ghc-dump-split ' + output_path)
elif mode == 'benchmark':
if not is_nofib or output_relative:
raise Exception("Can only benchmark if we are in the nofib root directory!")
print "Options:", ghc_opts
output_path = os.path.join(investigation_root, 'results', variant + '.log')
print "Output to", output_path
command = 'make clean && make boot && HC="' + ghc + '" EXTRA_HC_OPTS="' + ghc_opts + '" make -k >& ' + output_path
print "Running:", command
os.system(command)
else:
usage(program)
return 2
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[0], sys.argv[1:]))