@@ -16,6 +16,91 @@ _swift_arch_map = {
1616 "macOS-X64" : "darwin_x86_64" ,
1717}
1818
19+ _swift_version = _swift_prebuilt_version .rpartition ("." )[0 ]
20+
21+ _toolchain_info = {
22+ "linux" : struct (
23+ platform = "ubuntu2004" ,
24+ suffix = "ubuntu20.04" ,
25+ extension = "tar.gz" ,
26+ sha = "dd4b04c7f95c4ada4a2aacb66864b1ed20358313aaa4c880dc3974bf1eefa275" ,
27+ ),
28+ "macos" : struct (
29+ platform = "xcode" ,
30+ suffix = "osx" ,
31+ extension = "pkg" ,
32+ sha = "417d46f73b2e6b5da82ebbc8a5f4979f7187691fd42119157ba56d5a8bc89eda" ,
33+ ),
34+ }
35+
36+ def _get_toolchain_url (info ):
37+ return "https://download.swift.org/%s/%s/%s/%s-%s.%s" % (
38+ _swift_version .lower (),
39+ info .platform ,
40+ _swift_version ,
41+ _swift_version ,
42+ info .suffix ,
43+ info .extension ,
44+ )
45+
46+ def _toolchains (workspace_name ):
47+ rules = {
48+ "tar.gz" : http_archive ,
49+ "pkg" : _pkg_archive ,
50+ }
51+ for arch , info in _toolchain_info .items ():
52+ rule = rules [info .extension ]
53+ rule (
54+ name = "swift_toolchain_%s" % arch ,
55+ url = _get_toolchain_url (info ),
56+ sha256 = info .sha ,
57+ build_file = _build (workspace_name , "swift-toolchain-%s" % arch ),
58+ strip_prefix = "%s-%s" % (_swift_version , info .suffix ),
59+ )
60+
61+ def _run (repository_ctx , message , cmd , working_directory = "." ):
62+ repository_ctx .report_progress (message )
63+ res = repository_ctx .execute (
64+ ["bash" , "-c" , cmd ],
65+ working_directory = working_directory ,
66+ )
67+ if res .return_code != 0 :
68+ fail (message )
69+
70+ def _pkg_archive_impl (repository_ctx ):
71+ archive = "file.pkg"
72+ url = repository_ctx .attr .url
73+ dir = "%s-package.pkg" % repository_ctx .attr .strip_prefix
74+ repository_ctx .report_progress ("downloading %s" % url )
75+ res = repository_ctx .download (
76+ url ,
77+ output = archive ,
78+ sha256 = repository_ctx .attr .sha256 ,
79+ )
80+ if not repository_ctx .attr .sha256 :
81+ print ("Rule '%s' indicated that a canonical reproducible form " % repository_ctx .name +
82+ "can be obtained by modifying arguments sha256 = \" %s\" " % res .sha256 )
83+ _run (repository_ctx , "extracting %s" % dir , "xar -xf %s" % archive )
84+ repository_ctx .delete (archive )
85+ _run (
86+ repository_ctx ,
87+ "extracting Payload from %s" % dir ,
88+ "cat %s/Payload | gunzip -dc | cpio -i" % dir ,
89+ )
90+ repository_ctx .delete (dir )
91+ repository_ctx .symlink (repository_ctx .attr .build_file , "BUILD" )
92+ repository_ctx .file ("WORKSPACE" )
93+
94+ _pkg_archive = repository_rule (
95+ implementation = _pkg_archive_impl ,
96+ attrs = {
97+ "url" : attr .string (mandatory = True ),
98+ "sha256" : attr .string (),
99+ "strip_prefix" : attr .string (),
100+ "build_file" : attr .label (mandatory = True ),
101+ },
102+ )
103+
19104def _github_archive (* , name , repository , commit , build_file = None , sha256 = None ):
20105 github_name = repository [repository .index ("/" ) + 1 :]
21106 maybe (
@@ -53,6 +138,8 @@ def load_dependencies(workspace_name):
53138 ],
54139 )
55140
141+ _toolchains (workspace_name )
142+
56143 _github_archive (
57144 name = "picosha2" ,
58145 build_file = _build (workspace_name , "picosha2" ),
0 commit comments