From f9230975f54c3777219ba09d784557b3841f7acc Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 19 Mar 2018 13:44:51 -0700 Subject: [PATCH] refactor(build): generate revision via build.rs --- Cargo.lock | 1 + components/compositing/Cargo.toml | 4 ++ components/compositing/build.rs | 65 +++++++++++++++++++++++++++++++ python/servo/command_base.py | 32 --------------- 4 files changed, 70 insertions(+), 32 deletions(-) create mode 100644 components/compositing/build.rs diff --git a/Cargo.lock b/Cargo.lock index 933bfb90757..a852132f868 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -431,6 +431,7 @@ dependencies = [ "servo_url 0.0.1", "style_traits 0.0.1", "time 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "webrender 0.57.0 (git+https://github.com/servo/webrender)", "webrender_api 0.57.0 (git+https://github.com/servo/webrender)", ] diff --git a/components/compositing/Cargo.toml b/components/compositing/Cargo.toml index 736fef8a57d..03255d90ffa 100644 --- a/components/compositing/Cargo.toml +++ b/components/compositing/Cargo.toml @@ -4,6 +4,7 @@ version = "0.0.1" authors = ["The Servo Project Developers"] license = "MPL-2.0" publish = false +build = "build.rs" [lib] name = "compositing" @@ -29,3 +30,6 @@ style_traits = {path = "../style_traits"} time = "0.1.17" webrender = {git = "https://github.com/servo/webrender", features = ["capture"]} webrender_api = {git = "https://github.com/servo/webrender", features = ["ipc"]} + +[build-dependencies] +toml = "0.4.5" diff --git a/components/compositing/build.rs b/components/compositing/build.rs new file mode 100644 index 00000000000..d5c18728dc9 --- /dev/null +++ b/components/compositing/build.rs @@ -0,0 +1,65 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +extern crate toml; + +use std::env; +use std::fs::File; +use std::io::{Read, Write}; + +const WEBRENDER_REVISION_TEMPLATE: &'static str = +"/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/// auto-generated by cargo. do not manually modify. +pub const REVISION: &'static str = "; + +fn main() { + let current_path = env::current_dir().expect("Failed to access path to lockfile"); + let lockfile_path = current_path.join("..").join("..").join("Cargo.lock"); + let revision_file_path = current_path.join("webrender_revision.rs"); + + let mut existing_revision_exported = String::new(); + match File::open(&revision_file_path) { + Ok(mut f) => { + f.read_to_string(&mut existing_revision_exported).unwrap_or_default(); + }, + Err(_) => () + } + + let mut lockfile = String::new(); + File::open(lockfile_path).expect("Cannot open lockfile") + .read_to_string(&mut lockfile) + .expect("Failed to read lockfile"); + + match toml::from_str::(&lockfile) { + Ok(result) => { + let packages = result.get("package").expect("Cargo lockfile should contain package list"); + + match *packages { + toml::Value::Array(ref arr) => { + let source = arr + .iter() + .find(|pkg| pkg.get("name").and_then(|name| name.as_str()).unwrap_or("") == "webrender") + .and_then(|pkg| pkg.get("source").and_then(|source| source.as_str())) + .unwrap_or("unknown"); + + let parsed: Vec<&str> = source.split("#").collect(); + let revision = if parsed.len() > 1 { parsed[1] } else { source }; + + if let Some(_) = existing_revision_exported.find(revision) { + return + } + + let revision_contents = format!("{}\"{}\";", WEBRENDER_REVISION_TEMPLATE, revision); + let mut revision_module_file = File::create(&revision_file_path).unwrap(); + write!(&mut revision_module_file, "{}", revision_contents).unwrap(); + }, + _ => panic!("Cannot find package definitions in lockfile") + } + }, + Err(e) => panic!(e) + } +} diff --git a/python/servo/command_base.py b/python/servo/command_base.py index b36aa5bbe2b..a200fa05f24 100644 --- a/python/servo/command_base.py +++ b/python/servo/command_base.py @@ -213,36 +213,6 @@ def set_osmesa_env(bin_path, env): return env -def generate_webrender_revision(): - """Read current package id of web render to generate revision""" - - lockfile_path = path.join(os.getcwd(), "Cargo.lock") - if not os.path.isfile(lockfile_path): - return - - with open(lockfile_path) as f: - lockfile = toml.loads(f.read()) - - webrender_revision = "" - for package in lockfile.get("package", []): - pkgName = package.get("name") - if ("webrender" == pkgName): - webrender_revision = package.get("source") - - revision = '''/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/// auto-generated by mach. do not manually modify. -pub const REVISION: &'static str = \"%s\";''' % webrender_revision - - compositor_path = os.path.join(os.getcwd(), "components", "compositing", "webrender_revision.rs") - - revision_file = open(compositor_path, "w") - revision_file.write(revision) - revision_file.close() - - class BuildNotFound(Exception): def __init__(self, message): self.message = message @@ -690,8 +660,6 @@ class CommandBase(object): if "msvc" in target_platform: Registrar.dispatch("bootstrap", context=self.context) - generate_webrender_revision() - self.context.bootstrapped = True def ensure_clobbered(self, target_dir=None):