1 # Copyright 2011-2014 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.
11 import re, os, sys, glob, tarfile, shutil, subprocess, tempfile, distutils.spawn
15 sha_func = hashlib.sha1
21 if sys.version_info >= (3,) and type(b) == bytes:
22 return b.decode('ascii')
26 src_dir = scrub(os.getenv("CFG_SRC_DIR"))
28 raise Exception("missing env var CFG_SRC_DIR")
30 snapshotfile = os.path.join(src_dir, "src", "snapshots.txt")
31 download_url_base = "https://static.rust-lang.org/stage0-snapshots"
32 download_dir_base = "dl"
33 download_unpack_base = os.path.join(download_dir_base, "unpack")
36 "linux": ["bin/rustc"],
37 "macos": ["bin/rustc"],
38 "winnt": ["bin/rustc.exe"],
39 "freebsd": ["bin/rustc"],
40 "dragonfly": ["bin/rustc"],
43 winnt_runtime_deps_32 = ["libgcc_s_dw2-1.dll",
45 winnt_runtime_deps_64 = ["libgcc_s_seh-1.dll",
48 def parse_line(n, line):
51 if re.match(r"\s*$", line): return None
53 if re.match(r"^T\s*$", line): return None
55 match = re.match(r"\s+([\w_-]+) ([a-fA-F\d]{40})\s*$", line)
57 return { "type": "file",
58 "platform": match.group(1),
59 "hash": match.group(2).lower() }
61 match = re.match(r"([ST]) (\d{4}-\d{2}-\d{2}) ([a-fA-F\d]+)\s*$", line);
63 raise Exception("%s:%d:E syntax error: " % (snapshotfile, n))
64 return {"type": "snapshot",
65 "date": match.group(2),
66 "rev": match.group(3)}
69 def partial_snapshot_name(date, rev, platform):
70 return ("rust-stage0-%s-%s-%s.tar.bz2"
71 % (date, rev, platform))
73 def full_snapshot_name(date, rev, platform, hsh):
74 return ("rust-stage0-%s-%s-%s-%s.tar.bz2"
75 % (date, rev, platform, hsh))
78 def get_kernel(triple):
84 if os_name == "windows":
86 if os_name == "darwin":
88 if os_name == "freebsd":
90 if os_name == "dragonfly":
95 arch = triple.split('-')[0]
100 def get_platform(triple):
101 return "%s-%s" % (get_kernel(triple), get_cpu(triple))
104 def cmd_out(cmdline):
105 p = subprocess.Popen(cmdline,
106 stdout=subprocess.PIPE)
107 return scrub(p.communicate()[0].strip())
110 def local_rev_info(field):
111 return cmd_out(["git", "--git-dir=" + os.path.join(src_dir, ".git"),
113 "--format=%%%s" % field, "HEAD"])
116 def local_rev_full_sha():
117 return local_rev_info("H").split()[0]
120 def local_rev_short_sha():
121 return local_rev_info("h").split()[0]
124 def local_rev_committer_date():
125 return local_rev_info("ci")
127 def get_url_to_file(u,f):
128 # no security issue, just to stop partial download leaving a stale file
132 if distutils.spawn.find_executable("curl"):
133 returncode = subprocess.call(["curl", "-o", tmpf, u])
134 elif distutils.spawn.find_executable("wget"):
135 returncode = subprocess.call(["wget", "-O", tmpf, u])
142 raise Exception("failed to fetch url")
145 def snap_filename_hash_part(snap):
146 match = re.match(r".*([a-fA-F\d]{40}).tar.bz2$", snap)
148 raise Exception("unable to find hash in filename: " + snap)
149 return match.group(1)
153 h.update(open(x, "rb").read())
154 return scrub(h.hexdigest())
156 # Returns a list of paths of Rust's system runtime dependencies
157 def get_winnt_runtime_deps(platform):
158 if platform == "winnt-x86_64":
159 deps = winnt_runtime_deps_64
161 deps = winnt_runtime_deps_32
163 path_dirs = os.environ["PATH"].split(os.pathsep)
165 for dir in path_dirs:
166 filepath = os.path.join(dir, name)
167 if os.path.isfile(filepath):
168 runtime_deps.append(filepath)
171 raise Exception("Could not find runtime dependency: %s" % name)
174 def make_snapshot(stage, triple):
175 kernel = get_kernel(triple)
176 platform = get_platform(triple)
177 rev = local_rev_short_sha()
178 date = local_rev_committer_date().split()[0]
180 file0 = partial_snapshot_name(date, rev, platform)
183 cs = re.split(r"[\\/]", fn)
185 return os.sep.join(cs[-2:])
187 tar = tarfile.open(file0, "w:bz2")
189 for name in snapshot_files[kernel]:
191 if stage == "stage1" and re.match(r"^lib/(lib)?std.*", name):
193 fn_glob = os.path.join(triple, dir, name)
194 matches = glob.glob(fn_glob)
196 raise Exception("Not found file with name like " + fn_glob)
197 if len(matches) == 1:
198 tar.add(matches[0], "rust-stage0/" + in_tar_name(matches[0]))
200 raise Exception("Found stale files: \n %s\n"
201 "Please make a clean build." % "\n ".join(matches))
204 for path in get_winnt_runtime_deps(platform):
205 tar.add(path, "rust-stage0/bin/" + os.path.basename(path))
206 tar.add(os.path.join(os.path.dirname(__file__), "third-party"),
207 "rust-stage0/bin/third-party")
212 file1 = full_snapshot_name(date, rev, platform, h)
214 shutil.move(file0, file1)
218 def curr_snapshot_rev():
224 f = open(snapshotfile)
225 for line in f.readlines():
227 parsed = parse_line(i, line)
228 if (not parsed): continue
230 if parsed["type"] == "snapshot":
231 date = parsed["date"]
237 raise Exception("no snapshot entries in file")
241 def determine_curr_snapshot(triple):
243 platform = get_platform(triple)
251 f = open(snapshotfile)
252 for line in f.readlines():
254 parsed = parse_line(i, line)
255 if (not parsed): continue
257 if found_snap and parsed["type"] == "file":
258 if parsed["platform"] == platform:
262 elif parsed["type"] == "snapshot":
263 date = parsed["date"]
268 raise Exception("no snapshot entries in file")
271 raise Exception("no snapshot file found for platform %s, rev %s" %
274 return full_snapshot_name(date, rev, platform, hsh)