Skip to content

Commit

Permalink
Merge pull request #504 from librespot-org/build-fixes
Browse files Browse the repository at this point in the history
General fixes to protobuf bindings, depreciation warnings and publish.sh
  • Loading branch information
sashahilton00 authored Jul 26, 2020
2 parents 99be49a + 16462b3 commit 4886d4e
Show file tree
Hide file tree
Showing 20 changed files with 93 additions and 43,119 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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"
Expand Down
4 changes: 0 additions & 4 deletions audio/src/lewton_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ impl fmt::Display for VorbisError {
}

impl error::Error for VorbisError {
fn description(&self) -> &str {
error::Error::description(&self.0)
}

fn source(&self) -> Option<&(dyn error::Error + 'static)> {
error::Error::source(&self.0)
}
Expand Down
3 changes: 1 addition & 2 deletions core/src/proxytunnel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::error::Error;
use std::io;
use std::str::FromStr;

Expand Down Expand Up @@ -58,7 +57,7 @@ impl<T: AsyncRead + AsyncWrite> Future for ProxyTunnel<T> {
let status = match response.parse(&buf) {
Ok(status) => status,
Err(err) => {
return Err(io::Error::new(io::ErrorKind::Other, err.description()));
return Err(io::Error::new(io::ErrorKind::Other, err.to_string()));
}
};

Expand Down
1 change: 1 addition & 0 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ protobuf = "~2.14.0"
[build-dependencies]
protobuf-codegen-pure = "~2.14.0"
protobuf-codegen = "~2.14.0"
glob = "0.3.0"
118 changes: 64 additions & 54 deletions protocol/build.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,69 @@
extern crate protobuf_codegen; // Does the business
extern crate protobuf_codegen_pure; // Helper function
extern crate glob;

use std::fs::{read_to_string, write};
use std::path::Path;
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 cleanup() {
let _ = fs::remove_dir_all(&out_dir());
}

fn compile() {
let proto_dir = Path::new(&env::var("CARGO_MANIFEST_DIR").expect("env")).join("proto");

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 slices = files.iter().map(Deref::deref).collect::<Vec<_>>();

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.");
}

fn generate_mod_rs() {
let out_dir = out_dir();

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");

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() {
let customizations = Customize {
..Default::default()
};

let lib_str = read_to_string("src/lib.rs").unwrap();

// 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 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
}

// Build the paths to relevant files.
let src_fname = &format!("proto/{}.proto", name);
let dest_fname = &format!("src/{}.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");
}

println!("Regenerating {} from {}", dest.display(), src.display());

// 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();
}
}
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 4886d4e

Please sign in to comment.