1 # Copyright 2011-2015 The Rust Project Developers. See the COPYRIGHT
2 # file at the top-level directory of this distribution and at
3 # http://rust-lang.org/COPYRIGHT.
5 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 # option. This file may not be copied, modified, or distributed
9 # except according to those terms.
18 import distutils.spawn
22 sha_func = hashlib.sha1
29 if sys.version_info >= (3,) and type(b) == bytes:
30 return b.decode('ascii')
34 src_dir = scrub(os.getenv("CFG_SRC_DIR"))
36 raise Exception("missing env var CFG_SRC_DIR")
38 snapshotfile = os.path.join(src_dir, "src", "snapshots.txt")
39 download_url_base = "https://static.rust-lang.org/stage0-snapshots"
40 download_dir_base = "dl"
41 download_unpack_base = os.path.join(download_dir_base, "unpack")
44 "linux": ["bin/rustc"],
45 "macos": ["bin/rustc"],
46 "winnt": ["bin/rustc.exe"],
47 "freebsd": ["bin/rustc"],
48 "dragonfly": ["bin/rustc"],
49 "openbsd": ["bin/rustc"],
52 winnt_runtime_deps_32 = ["libgcc_s_dw2-1.dll", "libstdc++-6.dll"]
53 winnt_runtime_deps_64 = ["libgcc_s_seh-1.dll", "libstdc++-6.dll"]
56 def parse_line(n, line):
59 if re.match(r"\s*$", line):
62 if re.match(r"^T\s*$", line):
65 match = re.match(r"\s+([\w_-]+) ([a-fA-F\d]{40})\s*$", line)
67 return {"type": "file",
68 "platform": match.group(1),
69 "hash": match.group(2).lower()}
71 match = re.match(r"([ST]) (\d{4}-\d{2}-\d{2}) ([a-fA-F\d]+)\s*$", line)
73 raise Exception("%s:%d:E syntax error: " % (snapshotfile, n))
74 return {"type": "snapshot",
75 "date": match.group(2),
76 "rev": match.group(3)}
79 def partial_snapshot_name(date, rev, platform):
80 return ("rust-stage0-%s-%s-%s.tar.bz2" %
81 (date, rev, platform))
84 def full_snapshot_name(date, rev, platform, hsh):
85 return ("rust-stage0-%s-%s-%s-%s.tar.bz2" %
86 (date, rev, platform, hsh))
89 def get_kernel(triple):
96 if os_name == "windows":
98 if os_name == "darwin":
100 if os_name == "freebsd":
102 if os_name == "dragonfly":
104 if os_name == "openbsd":
110 arch = triple.split('-')[0]
116 def get_platform(triple):
117 return "%s-%s" % (get_kernel(triple), get_cpu(triple))
120 def cmd_out(cmdline):
121 p = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
122 return scrub(p.communicate()[0].strip())
125 def local_rev_info(field):
126 return cmd_out(["git", "--git-dir=" + os.path.join(src_dir, ".git"),
128 "--format=%%%s" % field, "HEAD"])
131 def local_rev_full_sha():
132 return local_rev_info("H").split()[0]
135 def local_rev_short_sha():
136 return local_rev_info("h").split()[0]
139 def local_rev_committer_date():
140 return local_rev_info("ci")
143 def get_url_to_file(u, f):
144 # no security issue, just to stop partial download leaving a stale file
148 if distutils.spawn.find_executable("curl"):
149 returncode = subprocess.call(["curl", "-o", tmpf, u])
150 elif distutils.spawn.find_executable("wget"):
151 returncode = subprocess.call(["wget", "-O", tmpf, u])
158 raise Exception("failed to fetch url")
162 def snap_filename_hash_part(snap):
163 match = re.match(r".*([a-fA-F\d]{40}).tar.bz2$", snap)
165 raise Exception("unable to find hash in filename: " + snap)
166 return match.group(1)
171 h.update(open(x, "rb").read())
172 return scrub(h.hexdigest())
175 def get_winnt_runtime_deps(platform):
176 """Returns a list of paths of Rust's system runtime dependencies"""
177 if platform == "winnt-x86_64":
178 deps = winnt_runtime_deps_64
180 deps = winnt_runtime_deps_32
182 path_dirs = os.environ["PATH"].split(os.pathsep)
184 for dir in path_dirs:
185 filepath = os.path.join(dir, name)
186 if os.path.isfile(filepath):
187 runtime_deps.append(filepath)
190 raise Exception("Could not find runtime dependency: %s" % name)
194 def make_snapshot(stage, triple):
195 kernel = get_kernel(triple)
196 platform = get_platform(triple)
197 rev = local_rev_short_sha()
198 date = local_rev_committer_date().split()[0]
200 file0 = partial_snapshot_name(date, rev, platform)
203 cs = re.split(r"[\\/]", fn)
205 return os.sep.join(cs[-2:])
207 tar = tarfile.open(file0, "w:bz2")
209 for name in snapshot_files[kernel]:
211 if stage == "stage1" and re.match(r"^lib/(lib)?std.*", name):
213 fn_glob = os.path.join(triple, dir, name)
214 matches = glob.glob(fn_glob)
216 raise Exception("Not found file with name like " + fn_glob)
217 if len(matches) == 1:
218 tar.add(matches[0], "rust-stage0/" + in_tar_name(matches[0]))
220 raise Exception("Found stale files: \n %s\n"
221 "Please make a clean build." % "\n ".join(matches))
223 if kernel == "winnt":
224 for path in get_winnt_runtime_deps(platform):
225 tar.add(path, "rust-stage0/bin/" + os.path.basename(path))
226 tar.add(os.path.join(os.path.dirname(__file__), "third-party"),
227 "rust-stage0/bin/third-party")
232 file1 = full_snapshot_name(date, rev, platform, h)
234 shutil.move(file0, file1)
239 def curr_snapshot_rev():
245 f = open(snapshotfile)
246 for line in f.readlines():
248 parsed = parse_line(i, line)
252 if parsed["type"] == "snapshot":
253 date = parsed["date"]
259 raise Exception("no snapshot entries in file")
264 def determine_curr_snapshot(triple):
266 platform = get_platform(triple)
274 f = open(snapshotfile)
275 for line in f.readlines():
277 parsed = parse_line(i, line)
281 if found_snap and parsed["type"] == "file":
282 if parsed["platform"] == platform:
286 elif parsed["type"] == "snapshot":
287 date = parsed["date"]
292 raise Exception("no snapshot entries in file")
295 raise Exception("no snapshot file found for platform %s, rev %s" %
298 return full_snapshot_name(date, rev, platform, hsh)