servo/components/style/build.rs
author Simon Sapin <simon.sapin@exyr.org>
Sat, 10 Dec 2016 01:16:26 -0800
changeset 340377 daaa89ca8da2cf8fa5142c69ba50bcf12b306204
parent 339807 f80d41a83a964db3f17304c1e20c158dcf2cc1bb
child 340379 78635ebabfc9e63971829a549388d1b61a75a503
permissions -rw-r--r--
servo: Merge #14535 - Introduce a PropertyId enum and use it instead of strings of property names (from servo:property-id); r=mbrubeck <!-- Please describe your changes on the following line: --> * `LonghandId` and `ShorthandId` are C-like enums * `Atom` is used for the name of custom properties. * `PropertyDeclarationId` is the identifier for `PropertyDeclaration`, after parsing and shorthand expansion. (Longhand or custom property.) * `PropertyId` represents any CSS property, e.g. in CSSOM. (Longhand, shorthand, or custom.) CC @upsuper --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 6dd4b4822fa788694153ee61a04dd9a5dfb748ec

/* 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 walkdir;
extern crate phf_codegen;

use std::env;
use std::fs::File;
use std::io::{BufWriter, BufReader, BufRead, Write};
use std::path::Path;
use std::process::{Command, exit};
use walkdir::WalkDir;

#[cfg(windows)]
fn find_python() -> String {
    if Command::new("python2.7.exe").arg("--version").output().is_ok() {
        return "python2.7.exe".to_owned();
    }

    if Command::new("python27.exe").arg("--version").output().is_ok() {
        return "python27.exe".to_owned();
    }

    if Command::new("python.exe").arg("--version").output().is_ok() {
        return "python.exe".to_owned();
    }

    panic!(concat!("Can't find python (tried python2.7.exe, python27.exe, and python.exe)! ",
                   "Try fixing PATH or setting the PYTHON env var"));
}

#[cfg(not(windows))]
fn find_python() -> String {
    if Command::new("python2.7").arg("--version").output().unwrap().status.success() {
        "python2.7"
    } else {
        "python"
    }.to_owned()
}

fn main() {
    println!("cargo:rerun-if-changed=build.rs");
    for entry in WalkDir::new("properties") {
        let entry = entry.unwrap();
        match entry.path().extension().and_then(|e| e.to_str()) {
            Some("mako") | Some("rs") | Some("py") | Some("zip") => {
                println!("cargo:rerun-if-changed={}", entry.path().display());
            }
            _ => {}
        }
    }

    let python = env::var("PYTHON").ok().unwrap_or_else(find_python);
    let script = Path::new(file!()).parent().unwrap().join("properties").join("build.py");
    let product = if cfg!(feature = "gecko") { "gecko" } else { "servo" };
    let status = Command::new(python)
        .arg(&script)
        .arg(product)
        .arg("style-crate")
        .arg(if cfg!(feature = "testing") { "testing" } else { "regular" })
        .status()
        .unwrap();
    if !status.success() {
        exit(1)
    }

    let path = Path::new(&env::var("OUT_DIR").unwrap()).join("static_ids.rs");
    let static_ids = Path::new(&env::var("OUT_DIR").unwrap()).join("static_ids.txt");
    let mut file = BufWriter::new(File::create(&path).unwrap());
    let static_ids = BufReader::new(File::open(&static_ids).unwrap());

    write!(&mut file, "static STATIC_IDS: ::phf::Map<&'static str, StaticId> = ").unwrap();
    let mut map = phf_codegen::Map::new();
    for result in static_ids.lines() {
        let line = result.unwrap();
        let mut split = line.split('\t');
        let key = split.next().unwrap().to_owned();
        let value = split.next().unwrap();
        map.entry(key, value);
    }
    map.build(&mut file).unwrap();
    write!(&mut file, ";\n").unwrap();
}