-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclang_compile_asm.py
More file actions
209 lines (176 loc) · 6.61 KB
/
clang_compile_asm.py
File metadata and controls
209 lines (176 loc) · 6.61 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import sublime
import sublime_plugin
import re
import subprocess
import tempfile
import threading
import os
class ClangCompileAsmCommand(sublime_plugin.WindowCommand):
encoding = 'utf-8'
proc = None
panel = None
panel_lock = threading.Lock()
cfi_re = re.compile(r'''$\s*\.(loh|cfi_).*$''', re.MULTILINE)
def is_enabled(self, arch=None, sdk=None, extra_args=None, device_os=None):
return True
def run(self, arch=None, sdk=None, extra_args=None, device_os=None):
active_view = self.window.active_view()
vars = self.window.extract_variables()
settings = sublime.load_settings('Compile Assembly.sublime-settings')
if settings is None:
return
if 'file_name' in vars:
file_name = vars['file_name']
working_dir = vars['file_path']
else:
# If the buffer being compiled is unnamed, give it a default name, using
# the most permissive compile options (ObjC++).
file_name = active_view.name() if active_view.name() else 'untitled.mm'
working_dir = tempfile.gettempdir()
(base_file_name, file_extension) = os.path.splitext(file_name)
asm_file_name = '%s.%s.asm' % (base_file_name, arch)
if file_extension in ['.m', '.mm']:
use_objc_arc = '-fno-objc-arc' not in extra_args
else:
use_objc_arc = False
compile_options = settings.get("compile_options%s" % (file_extension))
use_modules = self.shouldUseModules(active_view)
# A lock is used to ensure only one thread is
# touching the output panel at a time
with self.panel_lock:
self.panel = self.window.find_open_file(asm_file_name)
if self.panel is None:
self.panel = self.window.new_file()
self.panel.set_name(asm_file_name)
self.panel.set_scratch(True)
syntax_file = settings.get('syntax_file.%s' % (arch))
if syntax_file is not None:
self.panel.set_syntax_file(syntax_file)
if self.proc is not None:
self.proc.terminate()
self.proc = None
args = ['xcrun']
if sdk is not None:
args.extend(['--sdk', sdk])
if settings.has('clang_path') and os.path.exists(settings.get('clang_path')):
# Support for custom builds of clang.
args.append(settings.get('clang_path'))
# Custom builds of clang may need to provide an -isysroot for Apple
# framework paths.
if settings.has('clang_sysroot'):
args.extend(['-isysroot', settings.get('clang_sysroot')])
else:
args.append('clang')
if arch is not None:
if arch == 'llvm':
args.extend(['-arch', 'arm64', '-emit-llvm'])
elif device_os is not None:
args.extend(['-target', self.getTarget(sdk, arch, device_os)])
else:
args.extend(['-arch', arch])
if use_objc_arc:
args.append('-fobjc-arc')
if use_modules:
args.append('-fmodules')
optimization_level = settings.get('optimization_level', '-Os')
if optimization_level is not None:
args.append(optimization_level)
if not self.skipStandardWarnings(active_view):
compile_warning_flags = settings.get('compile_warning_flags', [])
if compile_warning_flags:
args.extend(compile_warning_flags)
if extra_args is not None:
args.extend(extra_args)
if compile_options is not None:
args.extend(compile_options)
else:
# As a fallback if no compile options are found, default to Objective-C++.
args.extend(['-x', 'objective-c++', '-std=c++11'])
args.extend(self.fileCompileArguments(active_view))
output_type = self.getOutputType(active_view)
if output_type:
args.extend(output_type)
else:
args.append('-S')
args.append('-o-')
args.append('-')
# args.append(vars['file_name'])
self.proc = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=working_dir
)
self.killed = False
current_file_text = active_view.substr(sublime.Region(0, active_view.size()))
self.do_write('; Compiled with: %s\n' % (' '.join(args)))
self.do_write('; -- Piped %d bytes to compiler\n' % (len(current_file_text)))
threading.Thread(
target=self.write_handle,
args=(self.proc.stdin, current_file_text,)
).start()
threading.Thread(
target=self.read_handle,
args=(self.proc.stdout,)
).start()
def write_handle(self, handle, file_text):
try:
os.write(handle.fileno(), file_text.encode(self.encoding))
os.close(handle.fileno())
except (UnicodeEncodeError) as e:
msg = 'Error decoding input using %s - %s'
self.queue_write(msg % (self.encoding, str(e)))
def read_handle(self, handle):
chunk_size = 2 ** 13
out = b''
while True:
try:
data = os.read(handle.fileno(), chunk_size)
out += data
if len(data) == chunk_size:
continue
if data == b'' and out == b'':
raise IOError('EOF')
self.queue_write(out.decode(self.encoding))
if data == b'':
raise IOError('EOF')
out = b''
except (UnicodeDecodeError) as e:
msg = 'Error decoding output using %s - %s'
self.queue_write(msg % (self.encoding, str(e)))
break
except (IOError):
break
def queue_write(self, text):
text = self.cfi_re.sub('', text)
sublime.set_timeout(lambda: self.do_write(text), 1)
def do_write(self, text):
with self.panel_lock:
self.panel.run_command('content_append', {'text': text})
def getTarget(self, sdk, arch, device_os):
output = subprocess.check_output(['xcrun', '--sdk', sdk, '--show-sdk-platform-version'])
os_version = output.decode('utf-8').rstrip()
return "{arch}-apple-{os}{os_version}".format(arch=arch, os=device_os, os_version=os_version)
def skipStandardWarnings(self, view):
region = view.find(r'sublime-compile-assembly-skip-warnings', 0)
return region is not None and not region.empty()
def getOutputType(self, view):
region = view.find(r'sublime-compile-assembly-output:\s*[^\n]*', 0)
if region is not None and not region.empty():
return view.substr(region).strip().split()
return None
def fileCompileArguments(self, view):
args = []
for region in view.find_all(r'sublime-compile-assembly-args:\s*[^\n]*', 0):
arg_string = view.substr(region)[30:].strip()
if len(arg_string) > 0:
args.extend(arg_string.split())
return args
def shouldUseModules(self, view):
region = view.find(r'^\s*@import\b', 0)
return region is not None and not region.empty()
class ContentAppend(sublime_plugin.TextCommand):
def run(self, edit, text):
loc = self.view.size()
self.view.insert(edit, loc, text)