Skip to content

Commit

Permalink
feat: add manual generation of abi, runtime-capi
Browse files Browse the repository at this point in the history
When using build.rs files several files get regenerated every build
which causes rebuilds even when nothing really changed. This commit
changes this behavior to make the user manually run a command when the
files should be regenerated.
  • Loading branch information
baszalmstra committed Dec 29, 2019
1 parent 23a4efc commit 63a49e4
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 60 deletions.
4 changes: 3 additions & 1 deletion .cargo/config
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[alias]
# Automatically generates the ast and syntax kinds files
gen-syntax = "run --package tools --bin tools -- gen-syntax"
gen-syntax = "run --package tools --bin tools -- gen-syntax"
gen-runtime-capi = "run --package tools --bin tools -- gen-runtime-capi"
gen-abi = "run --package tools --bin tools -- gen-abi"
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ jobs:

- name: Cargo check
uses: actions-rs/cargo@v1
env:
RUST_BACKTRACE: 1
continue-on-error: ${{ matrix.config.toolchain != 'stable' }}
with:
command: test
Expand Down
3 changes: 0 additions & 3 deletions crates/mun_abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ version = "0.2.0"
authors = ["The Mun Team <team@mun-lang.org>"]
edition = "2018"

[build-dependencies]
bindgen = "0.51"

[dependencies]
md5 = "0.6.1"
2 changes: 2 additions & 0 deletions crates/mun_abi/src/autogen.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`
/* automatically generated by rust-bindgen */

#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
Expand Down
3 changes: 0 additions & 3 deletions crates/mun_runtime_capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@ mun_abi = { path = "../mun_abi" }
mun_runtime = { path = "../mun_runtime" }
parking_lot = "0.9.0"
rand = "0.7.2"

[build-dependencies]
cbindgen = "0.9.1"
11 changes: 0 additions & 11 deletions crates/mun_runtime_capi/build.rs

This file was deleted.

2 changes: 2 additions & 0 deletions crates/tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ teraron = "0.0.1"
clap = "2.32.0"
failure = "0.1.4"
ron = "0.4.2"
cbindgen = "0.9.1"
bindgen = "0.51"
33 changes: 20 additions & 13 deletions crates/mun_abi/build.rs → crates/tools/src/abi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::env;
use std::path::PathBuf;
use crate::{project_root, reformat, update, Result};
use failure::format_err;
use teraron::Mode;

pub const RUNTIME_CAPI_DIR: &str = "crates/mun_runtime_capi";
pub const ABI_DIR: &str = "crates/mun_abi";

use bindgen::{self, callbacks::EnumVariantValue, callbacks::ParseCallbacks};

Expand Down Expand Up @@ -29,9 +33,17 @@ impl ParseCallbacks for RemoveVendorName {
}
}

fn main() {
/// Generates the FFI bindings for the Mun ABI
pub fn generate(mode: Mode) -> Result<()> {
let crate_dir = project_root().join(ABI_DIR);
let output_file_path = crate_dir.join("src/autogen.rs");
let input_file_path = crate_dir.join("c/include/mun_abi.h");

let input_file_str = input_file_path
.to_str()
.ok_or_else(|| failure::err_msg("could not create path to mun_abi.h"))?;
let bindings = bindgen::Builder::default()
.header("c/include/mun_abi.h")
.header(input_file_str)
.whitelist_type("Mun.*")
.blacklist_type("MunPrivacy.*")
// Remove type aliasing on Linux
Expand All @@ -45,13 +57,8 @@ fn main() {
.raw_line("#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]")
.raw_line("use crate::Privacy;")
.generate()
.expect("Unable to generate bindings for 'mun_abi.h'");

let out_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
bindings
.write_to_file(out_path.join("src/autogen.rs"))
.expect(&format!(
"Couldn't write bindings to '{}'",
out_path.as_path().to_string_lossy()
));
.map_err(|_| format_err!("Unable to generate bindings from 'mun_abi.h'"))?;

let file_contents = reformat(bindings.to_string())?;
update(&output_file_path, &file_contents, mode)
}
47 changes: 21 additions & 26 deletions crates/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,9 @@ use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
pub use teraron::{Mode, Overwrite, Verify};

pub const GRAMMAR: &str = "crates/mun_syntax/src/grammar.ron";
pub const SYNTAX_KINDS: &str = "crates/mun_syntax/src/syntax_kind/generated.rs.tera";
pub const AST: &str = "crates/mun_syntax/src/ast/generated.rs.tera";

pub fn generate_all(mode: Mode) -> Result<()> {
let grammar = project_root().join(GRAMMAR);
let syntax_kinds = project_root().join(SYNTAX_KINDS);
let ast = project_root().join(AST);
generate(&syntax_kinds, &grammar, mode)?;
generate(&ast, &grammar, mode)?;
Ok(())
}

pub fn generate(template: &Path, src: &Path, mode: Mode) -> Result<()> {
let file_name = template.file_stem().unwrap().to_str().unwrap();
let tgt = template.with_file_name(file_name);
let template = fs::read_to_string(template)?;
let src: ron::Value = {
let text = fs::read_to_string(src)?;
ron::de::from_str(&text)?
};
let content = teraron::render(&template, src)?;
let content = reformat(content)?;
update(&tgt, &content, mode)
}
pub mod abi;
pub mod runtime_capi;
pub mod syntax;

/// A helper to update file on disk if it has changed.
/// With verify = false,
Expand Down Expand Up @@ -79,8 +57,25 @@ mod tests {

#[test]
fn grammar_is_fresh() {
if let Err(error) = super::generate_all(Mode::Verify) {
if let Err(error) = super::syntax::generate(Mode::Verify) {
panic!("{}. Please update it by running `cargo gen-syntax`", error);
}
}

#[test]
fn runtime_capi_is_fresh() {
if let Err(error) = super::runtime_capi::generate(Mode::Verify) {
panic!(
"{}. Please update it by running `cargo gen-runtime-capi`",
error
);
}
}

#[test]
fn abi_is_fresh() {
if let Err(error) = super::abi::generate(Mode::Verify) {
panic!("{}. Please update it by running `cargo gen-abi`", error);
}
}
}
8 changes: 6 additions & 2 deletions crates/tools/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use clap::{App, SubCommand};

use tools::{generate_all, Overwrite, Result};
use tools::{Overwrite, Result};

fn main() -> Result<()> {
let matches = App::new("tasks")
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("gen-syntax"))
.subcommand(SubCommand::with_name("gen-runtime-capi"))
.subcommand(SubCommand::with_name("gen-abi"))
.get_matches();
match matches
.subcommand_name()
.expect("Subcommand must be specified")
{
"gen-syntax" => generate_all(Overwrite)?,
"gen-syntax" => tools::syntax::generate(Overwrite)?,
"gen-abi" => tools::abi::generate(Overwrite)?,
"gen-runtime-capi" => tools::runtime_capi::generate(Overwrite)?,
_ => unreachable!(),
}
Ok(())
Expand Down
16 changes: 16 additions & 0 deletions crates/tools/src/runtime_capi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::{project_root, update, Result};
use teraron::Mode;

pub const RUNTIME_CAPI_DIR: &str = "crates/mun_runtime_capi";

/// Generates the FFI bindings for the Mun runtime
pub fn generate(mode: Mode) -> Result<()> {
let crate_dir = project_root().join(RUNTIME_CAPI_DIR);
let file_path = crate_dir.join("ffi/include/mun/runtime_capi.h");

let mut file_contents = Vec::<u8>::new();
cbindgen::generate(crate_dir)?.write(&mut file_contents);

let file_contents = String::from_utf8(file_contents)?.replace("\r\n", "\n");
update(&file_path, &file_contents, mode)
}
32 changes: 32 additions & 0 deletions crates/tools/src/syntax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::{project_root, reformat, update, Result};
use std::fs;
use std::path::Path;
use teraron::Mode;

pub const GRAMMAR: &str = "crates/mun_syntax/src/grammar.ron";
pub const SYNTAX_KINDS: &str = "crates/mun_syntax/src/syntax_kind/generated.rs.tera";
pub const AST: &str = "crates/mun_syntax/src/ast/generated.rs.tera";

/// Generates the generated.rs for AST and syntax nodes.
pub fn generate(mode: Mode) -> Result<()> {
let grammar = project_root().join(GRAMMAR);
let syntax_kinds = project_root().join(SYNTAX_KINDS);
let ast = project_root().join(AST);
generate_from_template(&syntax_kinds, &grammar, mode)?;
generate_from_template(&ast, &grammar, mode)?;
Ok(())
}

/// Generate file contents from a template
fn generate_from_template(template: &Path, src: &Path, mode: Mode) -> Result<()> {
let file_name = template.file_stem().unwrap().to_str().unwrap();
let tgt = template.with_file_name(file_name);
let template = fs::read_to_string(template)?;
let src: ron::Value = {
let text = fs::read_to_string(src)?;
ron::de::from_str(&text)?
};
let content = teraron::render(&template, src)?;
let content = reformat(content)?;
update(&tgt, &content, mode)
}

0 comments on commit 63a49e4

Please sign in to comment.