-
Notifications
You must be signed in to change notification settings - Fork 4
/
gfortran
executable file
·145 lines (112 loc) · 3.96 KB
/
gfortran
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
import sys
import os
import shlex
import subprocess
import string
import random
import shutil
def join_args(args):
return ' '.join(shlex.quote(x) for x in args)
if "-dumpversion" in sys.argv:
print("10.2.0")
sys.exit(0)
arguments = []
sys_args = sys.argv
del sys_args[0]
try:
for arg in os.environ["ARCHFLAGS"].split(" "):
sys_args.append(arg)
except KeyError:
pass
if "-h" in sys_args or "--help" in sys_args:
print("The running executable emulates a Fortran compiler. If '-arch arm64' is passed, the compiler will produce an arm64 object file for iOS from the passed source. If not, gfortran located in '/usr/local/bin/' will be executed.")
print("Because flang runs in a Docker container, only files under '/Users/', '/var/folders' or '/Library' can be compiled.")
sys.exit(0)
all_args_are_object_files = True
for arg in sys_args:
if os.path.isfile(arg) and not arg.endswith(".o"):
all_args_are_object_files = False
if (not "-c" and not all_args_are_object_files) in sys_args or not "-arch arm64" in " ".join(sys_args):
print("The executed Fortran compiler only supports producing object files for iOS arm64, falling back to gfortran.", file=sys.stderr)
print("To compile sources for iOS arm64, make sure to add -arch arm64 and -c.", file=sys.stderr)
sys.exit(os.system(shlex.join(["/usr/local/bin/gfortran"]+sys_args)))
if "-bundle" in sys_args or all_args_are_object_files:
args = ["clang", "-undefined", "dynamic_lookup", "-shared"]
try:
for arg in os.environ["LDFLAGS"].split(" "):
args.append(arg)
except KeyError:
pass
for arg in sys_args:
if arg != "-bundle":
args.append(arg)
command = shlex.join(args)
sys.exit(os.system(command))
def convert_to_docker_path(arg):
if arg.startswith("/"):
arg = "/¡"+arg
else:
arg = os.path.join(os.getcwd(), arg)
return arg
the_previous_parameter_was_dash_o = False
the_previous_parameter_was_dash_c = False
the_previous_parameter_was_dash_arch = False
output_path = None
source = None
for arg in sys_args:
if arg == "-c":
the_previous_parameter_was_dash_c = True
elif the_previous_parameter_was_dash_c:
the_previous_parameter_was_dash_c = False
source = arg
if arg == "-o":
the_previous_parameter_was_dash_o = True
continue
elif the_previous_parameter_was_dash_o:
the_previous_parameter_was_dash_o = False
output_path = arg
continue
if arg == "-arch":
the_previous_parameter_was_dash_arch = True
continue
elif the_previous_parameter_was_dash_arch:
the_previous_parameter_was_dash_arch = False
continue
if os.path.exists(arg):
arg = convert_to_docker_path(arg)
if arg.startswith("-I"):
path = arg.split("-I")[-1]
arg = "-I"+convert_to_docker_path(path)
if arg.startswith("/¡"):
arg = arg[2:]
arguments.append(arg)
if output_path is None and source is not None:
parts = source.split(".")
del parts[-1]
output_path = os.getcwd()+"/"+".".join(parts)+".o"
dir = os.path.dirname(os.path.abspath(__file__))
cwd = os.getcwd()
arguments.insert(0, os.path.abspath(os.path.join(dir, "flang.sh")))
arguments.insert(1, "--save-temps")
flang_command = join_args(arguments)
inbox = os.path.join(cwd, ".inbox"+''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5)))
try:
os.mkdir(inbox)
except FileExistsError:
pass
os.chdir(inbox)
os.system(flang_command)
file_path = None
for file in os.listdir("."):
if not file.endswith(".ll"):
try:
os.remove(file)
except FileNotFoundError:
pass
else:
file_path = os.path.join(os.getcwd(), file)
os.chdir(cwd)
llc = [os.path.join(dir, "llc"), "-mtriple=arm64-apple-ios", "-filetype=obj", file_path, "-o", output_path]
subprocess.run(llc, stdout=None, stderr=None)
shutil.rmtree(inbox)