Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 510d6b8

Browse files
committedMay 17, 2017
Move submodule initialization to bootstrap.py
1 parent e119a63 commit 510d6b8

File tree

2 files changed

+37
-121
lines changed

2 files changed

+37
-121
lines changed
 

‎src/bootstrap/bootstrap.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import datetime
1515
import hashlib
1616
import os
17+
import re
1718
import shutil
1819
import subprocess
1920
import sys
@@ -297,8 +298,10 @@ def bin_root(self):
297298

298299
def get_toml(self, key):
299300
for line in self.config_toml.splitlines():
300-
if line.startswith(key + ' ='):
301-
return self.get_string(line)
301+
match = re.match(r'^{}\s*=(.*)$'.format(key), line)
302+
if match is not None:
303+
value = match.group(1)
304+
return self.get_string(value) or value.strip()
302305
return None
303306

304307
def get_mk(self, key):
@@ -329,6 +332,8 @@ def rustc(self):
329332

330333
def get_string(self, line):
331334
start = line.find('"')
335+
if start == -1:
336+
return None
332337
end = start + 1 + line[start + 1:].find('"')
333338
return line[start + 1:end]
334339

@@ -386,7 +391,7 @@ def build_bootstrap(self):
386391
args.append("--frozen")
387392
self.run(args, env)
388393

389-
def run(self, args, env):
394+
def run(self, args, env=None):
390395
proc = subprocess.Popen(args, env=env)
391396
ret = proc.wait()
392397
if ret != 0:
@@ -529,6 +534,32 @@ def build_triple(self):
529534

530535
return "{}-{}".format(cputype, ostype)
531536

537+
def update_submodules(self):
538+
if (not os.path.exists(os.path.join(self.rust_root, ".git"))) or \
539+
self.get_toml('submodules') == "false" or \
540+
self.get_mk('CFG_DISABLE_MANAGE_SUBMODULES') == "1":
541+
return
542+
543+
print('Updating submodules')
544+
self.run(["git", "-C", self.rust_root, "submodule", "-q", "sync"])
545+
# FIXME: nobody does, but this won't work well with whitespace in
546+
# submodule path
547+
submodules = [s.split()[1] for s in subprocess.check_output(
548+
["git", "config", "--file", os.path.join(
549+
self.rust_root, ".gitmodules"), "--get-regexp", "path"]).splitlines()]
550+
for module in submodules:
551+
if module.endswith(b"llvm") and \
552+
(self.get_toml('llvm-config') or self.get_mk('CFG_LLVM_ROOT')):
553+
continue
554+
if module.endswith(b"jemalloc") and \
555+
(self.get_toml('jemalloc') or self.get_mk('CFG_JEMALLOC_ROOT')):
556+
continue
557+
self.run(["git", "-C", self.rust_root,
558+
"submodule", "update", "--init", module])
559+
self.run(["git", "-C", self.rust_root, "submodule", "-q",
560+
"foreach", "git", "reset", "-q", "--hard"])
561+
self.run(["git", "-C", self.rust_root, "submodule",
562+
"-q", "foreach", "git", "clean", "-qdfx"])
532563
def bootstrap():
533564
parser = argparse.ArgumentParser(description='Build rust')
534565
parser.add_argument('--config')
@@ -597,6 +628,8 @@ def bootstrap():
597628
else:
598629
rb._download_url = 'https://static.rust-lang.org'
599630

631+
rb.update_submodules()
632+
600633
# Fetch/build the bootstrap
601634
rb.build = rb.build_triple()
602635
rb.download_stage0()

‎src/bootstrap/lib.rs

Lines changed: 1 addition & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ use std::env;
8282
use std::ffi::OsString;
8383
use std::fs::{self, File};
8484
use std::io::Read;
85-
use std::path::{Component, PathBuf, Path};
85+
use std::path::{PathBuf, Path};
8686
use std::process::Command;
8787

8888
use build_helper::{run_silent, run_suppressed, output, mtime};
@@ -285,129 +285,12 @@ impl Build {
285285
self.verbose(&format!("auto-detected local-rebuild {}", local_release));
286286
self.local_rebuild = true;
287287
}
288-
self.verbose("updating submodules");
289-
self.update_submodules();
290288
self.verbose("learning about cargo");
291289
metadata::build(self);
292290

293291
step::run(self);
294292
}
295293

296-
/// Updates all git submodules that we have.
297-
///
298-
/// This will detect if any submodules are out of date an run the necessary
299-
/// commands to sync them all with upstream.
300-
fn update_submodules(&self) {
301-
struct Submodule<'a> {
302-
path: &'a Path,
303-
state: State,
304-
}
305-
306-
enum State {
307-
// The submodule may have staged/unstaged changes
308-
MaybeDirty,
309-
// Or could be initialized but never updated
310-
NotInitialized,
311-
// The submodule, itself, has extra commits but those changes haven't been commited to
312-
// the (outer) git repository
313-
OutOfSync,
314-
}
315-
316-
if !self.src_is_git || !self.config.submodules {
317-
return
318-
}
319-
let git = || {
320-
let mut cmd = Command::new("git");
321-
cmd.current_dir(&self.src);
322-
return cmd
323-
};
324-
let git_submodule = || {
325-
let mut cmd = Command::new("git");
326-
cmd.current_dir(&self.src).arg("submodule");
327-
return cmd
328-
};
329-
330-
// FIXME: this takes a seriously long time to execute on Windows and a
331-
// nontrivial amount of time on Unix, we should have a better way
332-
// of detecting whether we need to run all the submodule commands
333-
// below.
334-
let out = output(git_submodule().arg("status"));
335-
let mut submodules = vec![];
336-
for line in out.lines() {
337-
// NOTE `git submodule status` output looks like this:
338-
//
339-
// -5066b7dcab7e700844b0e2ba71b8af9dc627a59b src/liblibc
340-
// +b37ef24aa82d2be3a3cc0fe89bf82292f4ca181c src/compiler-rt (remotes/origin/..)
341-
// e058ca661692a8d01f8cf9d35939dfe3105ce968 src/jemalloc (3.6.0-533-ge058ca6)
342-
//
343-
// The first character can be '-', '+' or ' ' and denotes the `State` of the submodule
344-
// Right next to this character is the SHA-1 of the submodule HEAD
345-
// And after that comes the path to the submodule
346-
let path = Path::new(line[1..].split(' ').skip(1).next().unwrap());
347-
let state = if line.starts_with('-') {
348-
State::NotInitialized
349-
} else if line.starts_with('+') {
350-
State::OutOfSync
351-
} else if line.starts_with(' ') {
352-
State::MaybeDirty
353-
} else {
354-
panic!("unexpected git submodule state: {:?}", line.chars().next());
355-
};
356-
357-
submodules.push(Submodule { path: path, state: state })
358-
}
359-
360-
self.run(git_submodule().arg("sync"));
361-
362-
for submodule in submodules {
363-
// If using llvm-root then don't touch the llvm submodule.
364-
if submodule.path.components().any(|c| c == Component::Normal("llvm".as_ref())) &&
365-
self.config.target_config.get(&self.config.build)
366-
.and_then(|c| c.llvm_config.as_ref()).is_some()
367-
{
368-
continue
369-
}
370-
371-
if submodule.path.components().any(|c| c == Component::Normal("jemalloc".as_ref())) &&
372-
!self.config.use_jemalloc
373-
{
374-
continue
375-
}
376-
377-
// `submodule.path` is the relative path to a submodule (from the repository root)
378-
// `submodule_path` is the path to a submodule from the cwd
379-
380-
// use `submodule.path` when e.g. executing a submodule specific command from the
381-
// repository root
382-
// use `submodule_path` when e.g. executing a normal git command for the submodule
383-
// (set via `current_dir`)
384-
let submodule_path = self.src.join(submodule.path);
385-
386-
match submodule.state {
387-
State::MaybeDirty => {
388-
// drop staged changes
389-
self.run(git().current_dir(&submodule_path)
390-
.args(&["reset", "--hard"]));
391-
// drops unstaged changes
392-
self.run(git().current_dir(&submodule_path)
393-
.args(&["clean", "-fdx"]));
394-
},
395-
State::NotInitialized => {
396-
self.run(git_submodule().arg("init").arg(submodule.path));
397-
self.run(git_submodule().arg("update").arg(submodule.path));
398-
},
399-
State::OutOfSync => {
400-
// drops submodule commits that weren't reported to the (outer) git repository
401-
self.run(git_submodule().arg("update").arg(submodule.path));
402-
self.run(git().current_dir(&submodule_path)
403-
.args(&["reset", "--hard"]));
404-
self.run(git().current_dir(&submodule_path)
405-
.args(&["clean", "-fdx"]));
406-
},
407-
}
408-
}
409-
}
410-
411294
/// Clear out `dir` if `input` is newer.
412295
///
413296
/// After this executes, it will also ensure that `dir` exists.

0 commit comments

Comments
 (0)
Please sign in to comment.