Skip to content

Commit

Permalink
Generate proto bindings in OUT_DIR, remove precompiled bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
sashahilton00 committed Jul 25, 2020
1 parent 4a21b5c commit e97911e
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 43,134 deletions.
33 changes: 17 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ version = "0.1.2"

[dependencies]
base64 = "0.10"
env_logger = {version = "0.6", default-features = false, features = ["termcolor","humantime","atty"]}
env_logger = {version = "0.6", default-features = false, features = ["termcolor","humantime","atty"]}
futures = "0.1"
getopts = "0.2"
hyper = "0.11"
log = "0.4"
num-bigint = "0.2"
protobuf = "~2.14.0"
protobuf = "~2.16.2"
rand = "0.7"
rpassword = "3.0"
tokio-core = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion connect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ futures = "0.1"
hyper = "0.11"
log = "0.4"
num-bigint = "0.2"
protobuf = "~2.14.0"
protobuf = "~2.16.2"
rand = "0.7"
serde = "1.0"
serde_derive = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ log = "0.4"
num-bigint = "0.2"
num-integer = "0.1"
num-traits = "0.2"
protobuf = "~2.14.0"
protobuf = "~2.16.2"
rand = "0.7"
serde = "1.0"
serde_derive = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2018"
byteorder = "1.3"
futures = "0.1"
linear-map = "1.2"
protobuf = "~2.14.0"
protobuf = "~2.16.2"
log = "0.4"

[dependencies.librespot-core]
Expand Down
7 changes: 4 additions & 3 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ license="MIT"
edition = "2018"

[dependencies]
protobuf = "~2.14.0"
protobuf = "~2.16.2"

[build-dependencies]
protobuf-codegen-pure = "~2.14.0"
protobuf-codegen = "~2.14.0"
protobuf-codegen-pure = "~2.16.2"
protobuf-codegen = "~2.16.2"
glob = "0.3.0"
111 changes: 58 additions & 53 deletions protocol/build.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,69 @@
extern crate protobuf_codegen; // Does the business
extern crate protobuf_codegen_pure; // Helper function
extern crate glob;

use std::env;
use std::fs::{read_to_string, write};
use std::path::Path;
use std::path::PathBuf;
use std::{
env, fs,
ops::Deref,
path::{Path, PathBuf},
};

use protobuf_codegen_pure::parse_and_typecheck;
use protobuf_codegen_pure::Customize;
fn out_dir() -> PathBuf {
Path::new(&env::var("OUT_DIR").expect("env")).to_path_buf()
}

fn main() {
let customizations = Customize {
..Default::default()
};
fn cleanup() {
let _ = fs::remove_dir_all(&out_dir());
}

let lib_str = read_to_string("src/lib.rs").unwrap();
fn compile() {
let proto_dir = Path::new(&env::var("CARGO_MANIFEST_DIR").expect("env")).join("proto");

// Iterate over the desired module names.
for line in lib_str.lines() {
if !line.starts_with("pub mod ") && !line.starts_with("mod ") {
continue;
}
let len = line.len();
let files = &[
proto_dir.join("authentication.proto"),
proto_dir.join("keyexchange.proto"),
proto_dir.join("mercury.proto"),
proto_dir.join("metadata.proto"),
proto_dir.join("playlist4changes.proto"),
proto_dir.join("playlist4content.proto"),
proto_dir.join("playlist4issues.proto"),
proto_dir.join("playlist4meta.proto"),
proto_dir.join("playlist4ops.proto"),
proto_dir.join("pubsub.proto"),
proto_dir.join("spirc.proto"),
];

let name;
if line.starts_with("pub mod ") {
name = &line[8..len - 1]; // Remove keywords and semi-colon
} else {
name = &line[4..len - 1]; // Remove keywords and semi-colon
}
let slices = files.iter().map(Deref::deref).collect::<Vec<_>>();

//let out_dir = env::var("OUT_DIR").is_err();
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_dir = out_dir();
fs::create_dir(&out_dir).expect("create_dir");

protobuf_codegen_pure::Codegen::new()
.out_dir(&out_dir)
.inputs(&slices)
.include(&proto_dir)
.run()
.expect("Codegen failed.");
}

// Build the paths to relevant files.
let src_fname = &format!("proto/{}.proto", name);
let dest_fname = &out_dir.join(format!("{}.rs", name));
let src = Path::new(src_fname);
let dest = Path::new(dest_fname);
// Get the contents of the existing generated file.
let mut existing = "".to_string();
if dest.exists() {
// Removing CRLF line endings if present.
existing = read_to_string(dest).unwrap().replace("\r\n", "\n");
}
fn generate_mod_rs() {
let out_dir = out_dir();

println!("Regenerating {} from {}", dest.display(), src.display());
let mods = glob::glob(&out_dir.join("*.rs").to_string_lossy())
.expect("glob")
.filter_map(|p| {
p.ok()
.map(|p| format!("pub mod {};", p.file_stem().unwrap().to_string_lossy()))
})
.collect::<Vec<_>>()
.join("\n");

// Parse the proto files as the protobuf-codegen-pure crate does.
let p = parse_and_typecheck(&[&Path::new("proto")], &[src]).expect("protoc");
// But generate them with the protobuf-codegen crate directly.
// Then we can keep the result in-memory.
let result = protobuf_codegen::gen(&p.file_descriptors, &p.relative_paths, &customizations);
// Protoc result as a byte array.
let new = &result.first().unwrap().content;
// Convert to utf8 to compare with existing.
let new = std::str::from_utf8(&new).unwrap();
// Save newly generated file if changed.
if new != existing {
write(dest, &new).unwrap();
}
}
let mod_rs = out_dir.join("mod.rs");
fs::write(&mod_rs, format!("// @generated\n{}\n", mods)).expect("write");

println!("cargo:rustc-env=PROTO_MOD_RS={}", mod_rs.to_string_lossy());
}

fn main() {
cleanup();
compile();
generate_mod_rs();
}
4 changes: 4 additions & 0 deletions protocol/proto/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// generated protobuf files will be included here. See build.rs for details
#![allow(renamed_and_removed_lints)]

include!(env!("PROTO_MOD_RS"));
Loading

0 comments on commit e97911e

Please sign in to comment.