-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmanual_cpp
executable file
·105 lines (83 loc) · 2.92 KB
/
manual_cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
from __future__ import print_function
import os
import sys
import subprocess
import os.path
def cpp_arg(arg):
return \
arg.startswith("-I") or \
arg.startswith("-D") or \
arg.startswith("-U")
def check_call(args, **kwargs):
if os.getenv("V") == "1":
print(" ".join(args))
return subprocess.check_call(args, **kwargs)
def check_call_redirect(args, filename=None, **kwargs):
if os.getenv("V") == "1":
print(" ".join(args), ">", filename)
with open(filename, "wb") as fd:
try:
return subprocess.check_call(args, stdout=fd, **kwargs)
except subprocess.CalledProcessError as e:
os.remove(filename)
raise SystemExit(e.returncode)
def is_nvidia_backend():
try:
output = subprocess.check_output(["hipcc", "--version"], universal_newlines=True)
if "nvcc" in output.lower():
return True
else:
return False
except (subprocess.CalledProcessError, FileNotFoundError):
# Could not run hipcc --version
return False
args = sys.argv[1:]
cpp_args = list(filter(cpp_arg, args))
files = list(filter(lambda q: q.endswith((".F90", ".cu", ".hip")), args))
args = list(filter(lambda q: not q.endswith((".F90", ".cu", ".hip")), args))
if len(files) > 1:
raise Exception("Specify exactly one source file (.F90, .cu, or .hip)")
elif len(files) == 0:
# No ".F90", ".cu", ".hip" file specified, execute program as-is
try:
os.execvp(args[0], args[0:])
except OSError as e:
print("Error executing '{0}': {1}".format(args[0], e.args[1]))
raise SystemExit(1)
elif len(files) == 1:
file, = files
# extract the base name and extension of the source file
base, ext = os.path.splitext(file)
tmp_filename = "manually_preprocessed_" + file.replace("/", "_")
try:
output = args.index("-o")
outputname = args[output + 1]
tmp_filename += "-" + outputname.replace("/", "_") + ext
except ValueError:
pass
tmp_filename = tmp_filename[-250:]
if (ext==".cu"):
# preprocess
list_preprocess_call_cu = ["nvcc"] + cpp_args + ["-E"] + [file] + ["-o"] + [tmp_filename]
check_call(list_preprocess_call_cu)
# compile
check_call(sys.argv[1:])
elif (ext==".hip"):
compiler="hipcc"
hip_on_nvidia_args = []
if (is_nvidia_backend()):
compiler="nvcc"
hip_on_nvidia_args.append("-x")
hip_on_nvidia_args.append("cu")
list_preprocess_call_hip = [compiler] + hip_on_nvidia_args + cpp_args + ["-E"] + [file] + ["-o"] + [tmp_filename]
check_call(list_preprocess_call_hip)
# compile
check_call(sys.argv[1:])
elif (ext==".F90"):
# preprocess
check_call_redirect(["cpp", "-P", "-traditional", "-Wall", "-Werror"] + cpp_args + [file], filename=tmp_filename)
# compile
check_call(args + [tmp_filename])
# cleanup (may be commented out for better debuggability
os.remove(tmp_filename)