Skip to content

Commit 558d1e0

Browse files
committed
Update the LLVM Import Script to include Windows Support
Additionally, update it so that it's compatibile with the current stable branch (ab94816b593daf1c623322b0fb981abb0b156833)
1 parent 5c7cbd4 commit 558d1e0

File tree

7 files changed

+873
-56
lines changed

7 files changed

+873
-56
lines changed

Utilities/import-llvm

Lines changed: 203 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#!/usr/bin/env python
22

33
# This is a helper script for importing the things we use from LLVM.
4-
#
5-
# NOTE: We have slightly modified copies of some of the files, to reduce the
6-
# dependency surface area, so if you run this to update the sources, please
7-
# examine the diffs to remove things that don't make sense.
84

95
import errno
106
import optparse
@@ -13,52 +9,139 @@ import re
139
import shutil
1410
import sys
1511
import subprocess
12+
import platform
1613

1714
# ADT types.
18-
ADT_imports = ['EpochTracker', 'iterator', 'iterator_range', 'Hashing', 'None', 'Optional',
19-
'Statistic', 'StringExtras', 'STLExtras', 'AllocatorList', 'Triple']
20-
21-
# ADT basic data structures.
22-
ADT_imports += ['APFloat', 'APInt', 'APSInt', 'ArrayRef', 'PointerIntPair', 'SetVector', 'StringRef', 'StringSwitch',
23-
'Twine', 'IntrusiveRefCntPtr', 'ilist', 'ilist_base', 'ilist_node', 'ilist_node_base',
24-
'ilist_node_options', 'ilist_iterator', 'simple_ilist', 'OptionSet', 'PointerUnion']
25-
26-
# ADT Mapping structures.
27-
ADT_imports += ['DenseMap', 'DenseMapInfo', 'DenseSet', 'FoldingSet', 'StringMap', 'StringSet']
28-
29-
# ADT "Small" structures.
30-
ADT_imports += ['SmallPtrSet', 'SmallSet', 'SmallString', 'SmallVector']
31-
32-
# ADT Algorithms.
33-
ADT_imports += ['edit_distance']
15+
ADT_imports = [
16+
'APFloat',
17+
'APInt',
18+
'APSInt',
19+
'AllocatorList',
20+
'ArrayRef',
21+
'Bit',
22+
'DenseMap',
23+
'DenseMapInfo',
24+
'DenseSet',
25+
'EpochTracker',
26+
'FoldingSet',
27+
'Hashing',
28+
'IntrusiveRefCntPtr',
29+
'None',
30+
'Optional',
31+
'PointerIntPair',
32+
'PointerUnion',
33+
'STLExtras',
34+
'SmallPtrSet',
35+
'SmallSet',
36+
'SmallString',
37+
'SmallVector',
38+
'Statistic',
39+
'StringExtras',
40+
'StringMap',
41+
'StringRef',
42+
'StringSet',
43+
'StringSwitch',
44+
'Triple',
45+
'Twine',
46+
'edit_distance',
47+
'ilist',
48+
'ilist_base',
49+
'ilist_iterator',
50+
'ilist_node',
51+
'ilist_node_base',
52+
'ilist_node_options',
53+
'iterator',
54+
'iterator_range',
55+
'simple_ilist'
56+
]
3457

3558
# Support types and infrastructure.
3659
Support_imports = [
37-
'AlignOf', 'Allocator', 'Atomic', 'CBindingWrapping', 'Casting', 'Capacity', 'CommandLine', 'Compiler',
38-
'Endian', 'Errno', 'ErrorHandling', 'Errc', 'ErrorOr', 'Error', 'Format',
39-
'ManagedStatic', 'MathExtras', 'Mutex', 'MutexGuard', 'Memory',
40-
'MemoryBuffer', 'PointerLikeTypeTraits', 'Recycler', 'SwapByteOrder',
41-
'Timer', 'TimeValue', 'Threading', 'Unicode', 'UniqueLock', 'Unix', 'WindowsError',
42-
'Valgrind', 'circular_raw_ostream', 'raw_ostream', 'type_traits', 'JSON']
43-
44-
# Stuff we don't want, but have to pull in.
45-
Support_imports += [
46-
'COFF', 'ConvertUTF', 'ConvertUTFWrapper', 'Debug', 'FileSystem',
47-
'FileUtilities', 'Host', 'Locale', 'MachO', 'Path', 'Process', 'Program', 'SMLoc',
48-
'SourceMgr', 'Signals', 'StringSaver', 'ToolOutputFile', 'TrailingObjects', 'Unicode', 'UnicodeCharRanges',
49-
'MemAlloc', 'Chrono', 'FormatProviders', 'FormatVariadic', 'FormatCommon',
50-
'FormatVariadicDetails', 'NativeFormatting', 'DJB', 'ReverseIteration', 'MD5',
51-
'SmallVectorMemoryBuffer', 'WithColor', 'Options', 'PrettyStackTrace', 'Watchdog',
52-
'TargetParser', 'ARMBuildAttributes', 'ARMTargetParser.def', 'AArch64TargetParser.def', 'X86TargetParser.def', 'LineIterator']
60+
'AArch64TargetParser',
61+
'APFloat',
62+
'ARMBuildAttributes',
63+
'ARMTargetParser',
64+
'AlignOf',
65+
'Allocator',
66+
'Atomic',
67+
'CBindingWrapping',
68+
'Casting',
69+
'Chrono',
70+
'CommandLine',
71+
'Compiler',
72+
'ConvertUTF',
73+
'ConvertUTFWrapper',
74+
'DJB',
75+
'DataTypes',
76+
'Debug',
77+
'Endian',
78+
'Errc',
79+
'Errno',
80+
'Error',
81+
'ErrorHandling',
82+
'ErrorOr',
83+
'FileSystem',
84+
'FileUtilities',
85+
'Format',
86+
'FormatAdapters',
87+
'FormatCommon',
88+
'FormatProviders',
89+
'FormatVariadic',
90+
'FormatVariadicDetails',
91+
'Host',
92+
'LineIterator',
93+
'Locale',
94+
'MD5',
95+
'ManagedStatic',
96+
'MathExtras',
97+
'MemAlloc',
98+
'Memory',
99+
'MemoryBuffer',
100+
'Mutex',
101+
'MutexGuard',
102+
'NativeFormatting',
103+
'Options',
104+
'Path',
105+
'PointerLikeTypeTraits',
106+
'Process',
107+
'Program',
108+
'Recycler',
109+
'ReverseIteration',
110+
'SMLoc',
111+
'Signals',
112+
'SmallVectorMemoryBuffer',
113+
'SourceMgr',
114+
'StringSaver',
115+
'Support',
116+
'SwapByteOrder',
117+
'TargetParser',
118+
'Threading',
119+
'Timer',
120+
'Types',
121+
'Unicode',
122+
'Unicode',
123+
'UnicodeCaseFold',
124+
'UnicodeCharRanges',
125+
'UniqueLock',
126+
'Unix',
127+
'Valgrind',
128+
'VersionTuple',
129+
'WindowsError',
130+
'WindowsSupport',
131+
'WithColor',
132+
'X86TargetParser',
133+
'circular_raw_ostream',
134+
'raw_ostream',
135+
'type_traits'
136+
]
53137

54138
# Dependencies from llvm-c needed by Support.
55-
C_imports = ['Types', 'DataTypes', 'Support', 'ErrorHandling']
56-
57-
# Support data structures.
58-
Support_imports += ['YAMLParser', 'YAMLTraits']
59-
60-
# Source files to exclude.
61-
Support_source_excludes = set(['Host'])
139+
C_imports = [
140+
'DataTypes',
141+
'ErrorHandling',
142+
'Support',
143+
'Types'
144+
]
62145

63146
llvm_srcroot = None
64147
sourcekit_srcroot = None
@@ -80,6 +163,10 @@ def copyfile(src, dst):
80163
note("cp %r %r" % (src, dst))
81164
shutil.copyfile(src, dst)
82165

166+
def movefile(src, dst):
167+
note("mv %r %r" % (src, dst))
168+
shutil.movefile(src, dst)
169+
83170
def includes_prefix(source, include_dir):
84171
defs = subprocess.check_output([
85172
'clang', '-std=c++14', '-dM', '-E',
@@ -137,12 +224,32 @@ def main():
137224
llvm_srcroot, = args
138225
sourcekit_srcroot = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
139226

227+
# Remove the old llvm import
228+
sourcekit_llvm_include = os.path.join(sourcekit_srcroot, 'include', 'llvm')
229+
sourcekit_llvm_lib = os.path.join(sourcekit_srcroot, 'lib', 'LLVMSupport')
230+
if os.path.exists(sourcekit_llvm_include):
231+
shutil.rmtree(sourcekit_llvm_include)
232+
if os.path.exists(sourcekit_llvm_lib):
233+
shutil.rmtree(sourcekit_llvm_lib)
234+
235+
# Add the platform specific config files
236+
#
237+
# NOTE: If you're updating to a newer llvm, you may have to manually merge
238+
# these with the newer version produced by the llvm build
239+
sourcekit_llvm_config = os.path.join(sourcekit_srcroot, 'Utilities', 'import-llvm.d', 'include', 'llvm', 'Config')
240+
shutil.copytree(sourcekit_llvm_config, os.path.join(sourcekit_llvm_include, 'Config'))
241+
140242
def import_header(dir, name):
141243
src = os.path.join(llvm_srcroot, 'include', 'llvm', dir, name)
244+
c_src = os.path.join(llvm_srcroot, 'include', 'llvm-c', name)
142245
if os.path.exists(src):
143246
dst = os.path.join(sourcekit_srcroot, 'include', 'llvm', dir, name)
144247
mkdir_p(os.path.dirname(dst))
145248
copyfile(src, dst)
249+
if os.path.exists(c_src):
250+
dst = os.path.join(sourcekit_srcroot, 'include', 'llvm-c', name)
251+
mkdir_p(os.path.dirname(dst))
252+
copyfile(c_src, dst)
146253

147254
def import_c_header(name):
148255
src = os.path.join(llvm_srcroot, 'include', 'llvm-c', name)
@@ -162,23 +269,32 @@ def main():
162269

163270
for name in ADT_imports:
164271
import_header('ADT', name+'.h')
165-
if name not in Support_source_excludes:
166-
import_source('Support', name+'.c')
167-
import_source('Support', name+'.cpp')
272+
import_source('Support', name+'.c')
273+
import_source('Support', name+'.cpp')
168274
for name in Support_imports:
169-
if name.endswith('.def'):
170-
import_header('Support', name)
171-
continue
172275
import_header('Support', name+'.h')
173-
if name not in Support_source_excludes:
174-
import_source('Support', name+'.c')
175-
import_source('Support', name+'.cpp')
176-
import_source('Support', os.path.join('Unix', name+'.h'))
177-
import_source('Support', os.path.join('Unix', name+'.inc'))
178-
276+
import_header('Support', name+'.def')
277+
import_source('Support', name+'.c')
278+
import_source('Support', name+'.cpp')
279+
import_source('Support', name+'.inc')
280+
import_source('Support', name+'.h')
281+
import_source('Support', os.path.join('Unix', name+'.h'))
282+
import_source('Support', os.path.join('Unix', name+'.inc'))
283+
import_source('Support', os.path.join('Windows', name+'.h'))
284+
import_source('Support', os.path.join('Windows', name+'.inc'))
179285
for name in C_imports:
180286
import_c_header(name + '.h')
181287

288+
# Create symlinks so SwiftPM can find headers
289+
os.makedirs(os.path.join(sourcekit_llvm_lib, 'include'))
290+
os.symlink(os.path.join('..', '..', '..', 'include', 'llvm'),
291+
os.path.join(sourcekit_llvm_lib, 'include', 'llvm'))
292+
os.symlink(os.path.join('..', '..', '..', 'include', 'llvm-c'),
293+
os.path.join(sourcekit_llvm_lib, 'include', 'llvm-c'))
294+
295+
copyfile(os.path.join(sourcekit_srcroot, 'Utilities', 'import-llvm.d', 'lib', 'LLVMSupport', 'include', 'module.modulemap'),
296+
os.path.join(sourcekit_srcroot, 'lib', 'LLVMSupport', 'include', 'module.modulemap'))
297+
182298
print ""
183299
print "Adding prefix header includes"
184300

@@ -189,11 +305,42 @@ def main():
189305
]
190306

191307
include_dir = os.path.join(sourcekit_srcroot, 'include')
192-
193308
for base in base_dirs:
194309
for root, dirs, files in os.walk(base):
195310
for file in files:
196311
maybe_add_prefix(os.path.join(root, file), include_dir)
197312

313+
# Finish off with a bunch of really gross hacks. These all offend my better
314+
# sensibilities and what have you.
315+
316+
# TODO: Indexstoredb uses an out of tree/non merged OptionSet.h We should
317+
# remove uses of this or get it merged upstream.
318+
copyfile(os.path.join(sourcekit_srcroot, 'Utilities', 'import-llvm.d', 'include', 'llvm', 'ADT', 'OptionSet.h'),
319+
os.path.join(sourcekit_srcroot, 'include', 'llvm', 'ADT', 'OptionSet.h'))
320+
321+
# AArch64TargetParser uses a relative path to find the .def file, so we
322+
# copy this one to the expected location :(
323+
def_location = os.path.join(sourcekit_srcroot, 'lib', 'include', 'llvm', 'Support')
324+
if os.path.exists(def_location):
325+
shutil.rmtree(def_location)
326+
os.makedirs(def_location)
327+
copyfile(os.path.join(sourcekit_srcroot, 'include', 'llvm', 'Support', 'AArch64TargetParser.def'),
328+
os.path.join(def_location, 'AArch64TargetParser.def'))
329+
330+
# Timer.cpp and Statistics.cpp use YAMLTraits to assert some JSON is valid. We don't want to
331+
# pull in the YAML/Regex so we'll remove the "offending" lines.
332+
timer_location = os.path.join(sourcekit_srcroot, 'lib', 'LLVMSupport', 'Support', 'Timer.cpp')
333+
statistic_location = os.path.join(sourcekit_srcroot, 'lib', 'LLVMSupport', 'Support', 'Statistic.cpp')
334+
subprocess.check_output(['sed', '-i', '-e', '391,394d', timer_location])
335+
subprocess.check_output(['sed', '-i', '-e', '23d', timer_location])
336+
subprocess.check_output(['sed', '-i', '-e', '214,217d', statistic_location])
337+
subprocess.check_output(['sed', '-i', '-e', '33d', statistic_location])
338+
339+
# Unix/Signals.inc pulls uses demangling to demangle stack trace symbols.
340+
# Again, we'll remove it to avoid pulling in demangling
341+
signals_location = os.path.join(sourcekit_srcroot, 'lib', 'LLVMSupport', 'Support', 'Unix', 'Signals.inc')
342+
subprocess.check_output(['sed', '-i', '-e', 's/itaniumDemangle.*/NULL;/g', signals_location])
343+
subprocess.check_output(['sed', '-i', '-e', '39d', signals_location])
344+
198345
if __name__ == '__main__':
199346
main()

0 commit comments

Comments
 (0)