-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
100 lines (83 loc) · 2.78 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use bindgen;
use cc;
use failure;
use git2;
use std::env;
use std::fs;
use std::path::PathBuf;
fn major_version() -> u8 {
env::var("CARGO_PKG_VERSION_MAJOR")
.unwrap()
.parse()
.unwrap()
}
fn minor_version() -> u8 {
env::var("CARGO_PKG_VERSION_MAJOR")
.unwrap()
.parse()
.unwrap()
}
fn version() -> String {
format!("{}.{}", major_version(), minor_version())
}
fn output() -> PathBuf {
PathBuf::from(env::var("OUT_DIR").unwrap())
}
fn main() -> Result<(), failure::Error> {
let url = "https://github.com/MusicPlayerDaemon/libmpdclient";
let repo = git2::Repository::clone(url, &output())?;
let mut build = cc::Build::new();
let mut bindgen = bindgen::Builder::default();
if !cfg!(feature = "latest") {
repo.set_head(&format!("refs/tags/v{}", version()))?;
}
fs::write(output().join("config.h"), format!(r#"
#pragma once
#define DEFAULT_HOST "localhost"
#define DEFAULT_PORT 6600
#define DEFAULT_SOCKET "/var/run/mpd/socket"
#define ENABLE_TCP
#define HAVE_GETADDRINFO
#define HAVE_STRNDUP
#define PACKAGE "libmpdclient"
#define VERSION "{}"
"#, version()))?;
fs::write(output().join("include/mpd/version.h"), format!(r#"
#ifndef MPD_VERSION_H
#define MPD_VERSION_H
#define LIBMPDCLIENT_MAJOR_VERSION {}
#define LIBMPDCLIENT_MINOR_VERSION {}
#define LIBMPDCLIENT_PATCH_VERSION 0
#define LIBMPDCLIENT_CHECK_VERSION(major, minor, patch) \
((major) < LIBMPDCLIENT_MAJOR_VERSION || \
((major) == LIBMPDCLIENT_MAJOR_VERSION && \
((minor) < LIBMPDCLIENT_MINOR_VERSION || \
((minor) == LIBMPDCLIENT_MINOR_VERSION && \
(patch) <= LIBMPDCLIENT_PATCH_VERSION))))
#endif
"#, major_version(), minor_version()))?;
build.include(output().join("include"))
.include(output())
.include(output().join("src"))
.shared_flag(false);
for entry in fs::read_dir(output().join("src"))? {
let entry = entry?;
if let Some(extension) = entry.path().extension() {
if extension == "c" && entry.file_name() != "example.c" {
build.file(entry.path());
}
}
}
for entry in fs::read_dir(output().join("include/mpd"))? {
let entry = entry?;
if let Some(extension) = entry.path().extension() {
if extension == "h" {
bindgen = bindgen.header(entry.path().to_str().unwrap());
}
}
}
build.compile("mpdclient");
let bindings = bindgen.generate().expect("Could not generate bindings.");
bindings.write_to_file(output().join("libmpdclient.rs"))?;
Ok(())
}