1414
1515def getoptions ():
1616 parser = argparse .ArgumentParser (description = "package swift for codeql compilation" )
17- parser .add_argument (f"--llvm-build-tree" , required = True , type = resolve ,
18- metavar = "DIR" , help = f"path to LLVM build tree" )
19- parser .add_argument (f"--swift-build-tree" , required = True , type = resolve ,
20- metavar = "DIR" , help = f"path to Swift build tree" )
17+ parser .add_argument (f"--build-tree" , required = True , type = resolve ,
18+ metavar = "DIR" , help = f"path to the build tree" )
2119 parser .add_argument (f"--swift-source-tree" , required = True , type = resolve ,
2220 metavar = "DIR" , help = f"path to Swift source tree" )
2321
@@ -34,57 +32,54 @@ def getoptions():
3432 return opts
3533
3634
37- Libs = namedtuple ("Libs" , ("archive" , "static" , "shared" , "linker_flags" ))
38-
3935EXPORTED_LIB = "CodeQLSwiftFrontendTool"
4036
37+ Libs = namedtuple ("Libs" , ("static" , "shared" , "linker_flags" ))
38+
4139
4240def resolve (p ):
4341 return pathlib .Path (p ).resolve ()
4442
4543
4644def run (prog , * , cwd , env = None , input = None ):
47- print ("running" , " " . join ( prog ) , f"(cwd={ cwd } )" )
45+ print ("running" , * prog , f"(cwd={ cwd } )" )
4846 if env is not None :
4947 runenv = dict (os .environ )
5048 runenv .update (env )
5149 else :
5250 runenv = None
53- subprocess .run (prog , cwd = cwd , env = runenv , input = input , text = True )
51+ subprocess .run (prog , cwd = cwd , env = runenv , input = input , text = True , check = True )
5452
5553
5654def get_platform ():
5755 return "linux" if platform .system () == "Linux" else "macos"
5856
5957
60- def configure_dummy_project (tmp , * , llvm = None , swift = None ):
58+ def configure_dummy_project (tmp , prefixes ):
6159 print ("configuring dummy cmake project" )
6260 script_dir = pathlib .Path (os .path .realpath (__file__ )).parent
6361 print (script_dir )
6462 shutil .copy (script_dir / "CMakeLists.txt" , tmp / "CMakeLists.txt" )
6563 shutil .copy (script_dir / "empty.cpp" , tmp / "empty.cpp" )
6664 tgt = tmp / "build"
6765 tgt .mkdir ()
68- run ([ "cmake" , f"-DCMAKE_PREFIX_PATH= { llvm } ; { swift } " , "-DBUILD_SHARED_LIBS=OFF" , ".." ],
69- cwd = tgt )
66+ prefixes = ';' . join ( str ( p ) for p in prefixes )
67+ run ([ "cmake" , f"-DCMAKE_PREFIX_PATH= { prefixes } " , "-DBUILD_SHARED_LIBS=OFF" , ".." ], cwd = tgt )
7068 return tgt
7169
7270
7371def get_libs (configured ):
7472 print ("extracting linking information from dummy project" )
7573 with open (configured / "CMakeFiles" / "codeql-swift-artifacts.dir" / "link.txt" ) as link :
7674 libs = link .read ().split ()
77- libs = libs [libs .index ('codeql-swift-artifacts' )+ 1 :] # skip up to -o dummy
78- ret = Libs ([], [], [], [] )
75+ libs = libs [libs .index ('codeql-swift-artifacts' ) + 1 :] # skip up to -o dummy
76+ ret = Libs ([], [], [])
7977 for l in libs :
8078 if l .endswith (".a" ):
81- ret .static .append (str (( configured / l ).resolve () ))
79+ ret .static .append (( configured / l ).absolute ( ))
8280 elif l .endswith (".so" ) or l .endswith (".tbd" ) or l .endswith (".dylib" ):
83- l = pathlib .Path (l ).stem
84- ret .shared .append (f"-l{ l [3 :]} " ) # drop 'lib' prefix and '.so' suffix
85- elif l .startswith ("-l" ):
86- ret .shared .append (l )
87- elif l .startswith ("-L" ) or l .startswith ("-Wl" ):
81+ ret .shared .append ((configured / l ).absolute ())
82+ elif l .startswith (("-L" , "-Wl" , "-l" )):
8883 ret .linker_flags .append (l )
8984 else :
9085 raise ValueError (f"cannot understand link.txt: " + l )
@@ -94,60 +89,27 @@ def get_libs(configured):
9489def get_tgt (tgt , filename ):
9590 if tgt .is_dir ():
9691 tgt /= filename
97- return tgt .resolve ()
92+ return tgt .absolute ()
9893
9994
10095def create_static_lib (tgt , libs ):
10196 tgt = get_tgt (tgt , f"lib{ EXPORTED_LIB } .a" )
10297 print (f"packaging { tgt .name } " )
10398 if sys .platform == 'linux' :
104- includedlibs = "\n " .join (f"addlib { l } " for l in libs .archive + libs . static )
99+ includedlibs = "\n " .join (f"addlib { l } " for l in libs .static )
105100 mriscript = f"create { tgt } \n { includedlibs } \n save\n end"
106101 run (["ar" , "-M" ], cwd = tgt .parent , input = mriscript )
107102 else :
108103 libtool_args = ["libtool" , "-static" ]
109- libtool_args .extend (libs .archive )
110104 libtool_args .extend (libs .static )
111105 libtool_args .append ("-o" )
112106 libtool_args .append (str (tgt ))
113107 run (libtool_args , cwd = tgt .parent )
114108 return tgt
115109
116110
117- def create_shared_lib (tgt , libs ):
118- ext = "so"
119- if sys .platform != 'linux' :
120- ext = "dylib"
121- libname = f"lib{ EXPORTED_LIB } .{ ext } "
122- tgt = get_tgt (tgt , libname )
123- print (f"packaging { libname } " )
124- compiler = os .environ .get ("CC" , "clang" )
125- cmd = [compiler , "-shared" ]
126- cmd .extend (libs .linker_flags )
127-
128- if sys .platform == 'linux' :
129- cmd .append ("-Wl,--whole-archive" )
130- else :
131- cmd .append ("-Wl,-all_load" )
132-
133- cmd .append (f"-o{ tgt } " )
134- cmd .extend (libs .archive )
135-
136- if sys .platform == 'linux' :
137- cmd .append ("-Wl,--no-whole-archive" )
138- else :
139- cmd .append ("-lc++" )
140-
141- cmd .extend (libs .static )
142- cmd .extend (libs .shared )
143- run (cmd , cwd = tgt .parent )
144- if sys .platform != "linux" :
145- run (["install_name_tool" , "-id" , f"@executable_path/{ libname } " , libname ], cwd = tgt .parent )
146- return tgt
147-
148-
149111def copy_includes (src , tgt ):
150- print ("copying includes" )
112+ print (f "copying includes from { src } " )
151113 for dir , exts in (("include" , ("h" , "def" , "inc" )), ("stdlib" , ("h" ,))):
152114 srcdir = src / dir
153115 for ext in exts :
@@ -159,7 +121,7 @@ def copy_includes(src, tgt):
159121
160122def export_sdk (tgt , swift_source_tree , swift_build_tree ):
161123 print ("assembling sdk" )
162- srcdir = swift_build_tree / "lib" / "swift"
124+ srcdir = swift_build_tree / "lib" / "swift"
163125 tgtdir = tgt / "usr" / "lib" / "swift"
164126 if get_platform () == "linux" :
165127 srcdir /= "linux"
@@ -168,8 +130,8 @@ def export_sdk(tgt, swift_source_tree, swift_build_tree):
168130 srcdir /= "macosx"
169131 for mod in srcdir .glob ("*.swiftmodule" ):
170132 shutil .copytree (mod , tgtdir / mod .name )
171- shutil .copytree (swift_source_tree / "stdlib" / "public" / "SwiftShims" ,
172- tgt / "usr" / "include " / "SwiftShims " ,
133+ shutil .copytree (swift_source_tree / "stdlib" / "public" / "SwiftShims" / "swift" / "shims" ,
134+ tgt / "usr" / "lib " / "swift" / "shims " ,
173135 ignore = shutil .ignore_patterns ('CMakeLists.txt' ))
174136
175137
@@ -181,11 +143,11 @@ def export_stdlibs(exported_dir, swift_build_tree):
181143 ext = 'so'
182144 lib_dir = swift_build_tree / 'lib/swift' / platform
183145 stdlibs = [
184- f'libswiftCore.{ ext } ' ,
185- 'libswiftCompatibility50.a' ,
186- 'libswiftCompatibility51.a' ,
187- 'libswiftCompatibilityConcurrency.a' ,
188- 'libswiftCompatibilityDynamicReplacements.a' ]
146+ f'libswiftCore.{ ext } ' ,
147+ 'libswiftCompatibility50.a' ,
148+ 'libswiftCompatibility51.a' ,
149+ 'libswiftCompatibilityConcurrency.a' ,
150+ 'libswiftCompatibilityDynamicReplacements.a' ]
189151 for stdlib in stdlibs :
190152 lib_path = lib_dir / stdlib
191153 if lib_path .exists ():
@@ -197,13 +159,11 @@ def export_stdlibs(exported_dir, swift_build_tree):
197159
198160def export_libs (exported_dir , libs , swift_build_tree ):
199161 print ("exporting libraries" )
200- exportedlibs = [
201- create_static_lib (exported_dir , libs ),
202- create_shared_lib (exported_dir , libs )
203- ]
204-
205- for l in exportedlibs :
206- l .rename (exported_dir / l .name )
162+ create_static_lib (exported_dir , libs )
163+ for lib in libs .shared :
164+ # export libraries under the build tree (e.g. libSwiftSyntax.so)
165+ if lib .is_relative_to (swift_build_tree .parent ):
166+ shutil .copy (lib , exported_dir )
207167 export_stdlibs (exported_dir , swift_build_tree )
208168
209169
@@ -213,7 +173,8 @@ def export_headers(exported_dir, swift_source_tree, llvm_build_tree, swift_build
213173 llvm_source_tree = swift_source_tree .parent / 'llvm-project/llvm'
214174 clang_source_tree = swift_source_tree .parent / 'llvm-project/clang'
215175 clang_tools_build_tree = llvm_build_tree / 'tools/clang'
216- header_dirs = [ llvm_source_tree , clang_source_tree , swift_source_tree , llvm_build_tree , swift_build_tree , clang_tools_build_tree ]
176+ header_dirs = [llvm_source_tree , clang_source_tree , swift_source_tree , llvm_build_tree , swift_build_tree ,
177+ clang_tools_build_tree ]
217178 for h in header_dirs :
218179 copy_includes (h , exported_dir )
219180
@@ -230,18 +191,21 @@ def main(opts):
230191 if os .path .exists (tmp ):
231192 shutil .rmtree (tmp )
232193 os .mkdir (tmp )
233- configured = configure_dummy_project (tmp , llvm = opts .llvm_build_tree , swift = opts .swift_build_tree )
194+ llvm_build_tree = next (opts .build_tree .glob ("llvm-*" ))
195+ swift_build_tree = next (opts .build_tree .glob ("swift-*" ))
196+ earlyswiftsyntax_build_tree = next (opts .build_tree .glob ("earlyswiftsyntax-*" ))
197+ configured = configure_dummy_project (tmp , prefixes = [llvm_build_tree , swift_build_tree ,
198+ earlyswiftsyntax_build_tree / "cmake" / "modules" ])
234199 libs = get_libs (configured )
235200
236201 exported = tmp / "exported"
237202 exported .mkdir ()
238- export_libs (exported , libs , opts . swift_build_tree )
239- export_headers (exported , opts .swift_source_tree , opts . llvm_build_tree , opts . swift_build_tree )
240- export_sdk (exported / "sdk" , opts .swift_source_tree , opts . swift_build_tree )
203+ export_libs (exported , libs , swift_build_tree )
204+ export_headers (exported , opts .swift_source_tree , llvm_build_tree , swift_build_tree )
205+ export_sdk (exported / "sdk" , opts .swift_source_tree , swift_build_tree )
241206
242207 zip_dir (exported , opts .output )
243208
244209
245210if __name__ == "__main__" :
246211 main (getoptions ())
247-
0 commit comments