forked from fede1024/rust-rdkafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
242 lines (213 loc) · 8.11 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use std::borrow::Borrow;
use std::env;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::{self, Command};
fn run_command_or_fail<P, S>(dir: &str, cmd: P, args: &[S])
where
P: AsRef<Path>,
S: Borrow<str> + AsRef<OsStr>,
{
let cmd = cmd.as_ref();
let cmd = if cmd.components().count() > 1 && cmd.is_relative() {
// If `cmd` is a relative path (and not a bare command that should be
// looked up in PATH), absolutize it relative to `dir`, as otherwise the
// behavior of std::process::Command is undefined.
// https://github.com/rust-lang/rust/issues/37868
PathBuf::from(dir)
.join(cmd)
.canonicalize()
.expect("canonicalization failed")
} else {
PathBuf::from(cmd)
};
eprintln!(
"Running command: \"{} {}\" in dir: {}",
cmd.display(),
args.join(" "),
dir
);
let ret = Command::new(cmd).current_dir(dir).args(args).status();
match ret.map(|status| (status.success(), status.code())) {
Ok((true, _)) => return,
Ok((false, Some(c))) => panic!("Command failed with error code {}", c),
Ok((false, None)) => panic!("Command got killed"),
Err(e) => panic!("Command failed with error: {}", e),
}
}
fn main() {
let librdkafka_version = env!("CARGO_PKG_VERSION")
.split('-')
.next()
.expect("Crate version is not valid");
if env::var("DEP_OPENSSL_VENDORED").is_ok() {
let openssl_root = env::var("DEP_OPENSSL_ROOT").expect("DEP_OPENSSL_ROOT is not set");
env::set_var("CFLAGS", format!("-I{}/include", openssl_root));
env::set_var("LDFLAGS", format!("-L{}/lib", openssl_root));
}
if env::var("CARGO_FEATURE_DYNAMIC_LINKING").is_ok() {
eprintln!("librdkafka will be linked dynamically");
let pkg_probe = pkg_config::Config::new()
.cargo_metadata(true)
.atleast_version(librdkafka_version)
.probe("rdkafka");
match pkg_probe {
Ok(library) => {
eprintln!("librdkafka found on the system:");
eprintln!(" Name: {:?}", library.libs);
eprintln!(" Path: {:?}", library.link_paths);
eprintln!(" Version: {}", library.version);
}
Err(_) => {
eprintln!(
"librdkafka {} cannot be found on the system",
librdkafka_version
);
eprintln!("Dynamic linking failed. Exiting.");
process::exit(1);
}
}
} else {
if !Path::new("librdkafka/LICENSE").exists() {
eprintln!("Setting up submodules");
run_command_or_fail("../", "git", &["submodule", "update", "--init"]);
}
eprintln!("Building and linking librdkafka statically");
build_librdkafka();
}
}
#[cfg(not(feature = "cmake-build"))]
fn build_librdkafka() {
let mut configure_flags: Vec<String> = Vec::new();
let mut cflags = Vec::new();
if let Ok(var) = env::var("CFLAGS") {
cflags.push(var);
}
let mut ldflags = Vec::new();
if let Ok(var) = env::var("LDFLAGS") {
ldflags.push(var);
}
if env::var("CARGO_FEATURE_SSL").is_ok() {
configure_flags.push("--enable-ssl".into());
if let Ok(openssl_root) = env::var("DEP_OPENSSL_ROOT") {
cflags.push(format!("-I{}/include", openssl_root));
ldflags.push(format!("-L{}/lib", openssl_root));
}
} else {
configure_flags.push("--disable-ssl".into());
}
if env::var("CARGO_FEATURE_GSSAPI").is_ok() {
configure_flags.push("--enable-gssapi".into());
println!("cargo:rustc-link-lib=sasl2");
} else {
configure_flags.push("--disable-gssapi".into());
}
if env::var("CARGO_FEATURE_LIBZ").is_ok() {
// There is no --enable-zlib option, but it is enabled by default.
if let Ok(z_root) = env::var("DEP_Z_ROOT") {
cflags.push(format!("-I{}/include", z_root));
ldflags.push(format!("-L{}/build", z_root));
}
} else {
configure_flags.push("--disable-zlib".into());
}
if env::var("CARGO_FEATURE_ZSTD").is_ok() {
configure_flags.push("--enable-zstd".into());
if let Ok(zstd_root) = env::var("DEP_ZSTD_ROOT") {
cflags.push(format!("-I{}/include", zstd_root));
ldflags.push(format!("-L{}", zstd_root));
}
} else {
configure_flags.push("--disable-zstd".into());
}
if env::var("CARGO_FEATURE_EXTERNAL_LZ4").is_ok() {
configure_flags.push("--enable-lz4-ext".into());
if let Ok(lz4_root) = env::var("DEP_LZ4_ROOT") {
cflags.push(format!("-I{}/include", lz4_root));
ldflags.push(format!("-L{}", lz4_root));
}
} else {
configure_flags.push("--disable-lz4-ext".into());
}
env::set_var("CFLAGS", cflags.join(" "));
env::set_var("LDFLAGS", ldflags.join(" "));
let out_dir = env::var("OUT_DIR").expect("OUT_DIR missing");
if !Path::new(&out_dir).join("LICENSE").exists() {
// We're not allowed to build in-tree directly, as ~/.cargo/registry is
// globally shared. mklove doesn't support out-of-tree builds [0], so we
// work around the issue by creating a clone of librdkafka inside of
// OUT_DIR, and build inside of *that* tree.
//
// https://github.com/edenhill/mklove/issues/17
println!("Cloning librdkafka");
run_command_or_fail(".", "cp", &["-a", "librdkafka/.", &out_dir]);
}
println!("Configuring librdkafka");
run_command_or_fail(&out_dir, "./configure", configure_flags.as_slice());
println!("Compiling librdkafka");
env::set_var("MAKEFLAGS", env::var_os("CARGO_MAKEFLAGS").expect("CARGO_MAKEFLAGS env var missing"));
run_command_or_fail(
&out_dir,
if cfg!(target_os = "freebsd") { "gmake" } else { "make" },
&["libs"],
);
println!(
"cargo:rustc-link-search=native={}/src",
out_dir,
);
println!("cargo:rustc-link-lib=static=rdkafka");
println!("cargo:root={}", out_dir);
}
#[cfg(feature = "cmake-build")]
fn build_librdkafka() {
let mut config = cmake::Config::new("librdkafka");
config
.define("RDKAFKA_BUILD_STATIC", "1")
.define("RDKAFKA_BUILD_TESTS", "0")
.define("RDKAFKA_BUILD_EXAMPLES", "0")
// CMAKE_INSTALL_LIBDIR is inferred as "lib64" on some platforms, but we
// want a stable location that we can add to the linker search path.
// Since we're not actually installing to /usr or /usr/local, there's no
// harm to always using "lib" here.
.define("CMAKE_INSTALL_LIBDIR", "lib");
if env::var("CARGO_FEATURE_LIBZ").is_ok() {
config.define("WITH_ZLIB", "1");
config.register_dep("z");
if let Ok(z_root) = env::var("DEP_Z_ROOT") {
env::set_var("CMAKE_LIBRARY_PATH", format!("{}/build", z_root));
}
} else {
config.define("WITH_ZLIB", "0");
}
if env::var("CARGO_FEATURE_SSL").is_ok() {
config.define("WITH_SSL", "1");
config.register_dep("openssl");
} else {
config.define("WITH_SSL", "0");
}
if env::var("CARGO_FEATURE_GSSAPI").is_ok() {
config.define("WITH_SASL", "1");
println!("cargo:rustc-link-lib=sasl2");
} else {
config.define("WITH_SASL", "0");
}
if env::var("CARGO_FEATURE_ZSTD").is_ok() {
config.define("WITH_ZSTD", "1");
config.register_dep("zstd");
} else {
config.define("WITH_ZSTD", "0");
}
if env::var("CARGO_FEATURE_EXTERNAL_LZ4").is_ok() {
config.define("ENABLE_LZ4_EXT", "1");
config.register_dep("lz4");
} else {
config.define("ENABLE_LZ4_EXT", "0");
}
if let Ok(system_name) = env::var("CMAKE_SYSTEM_NAME") {
config.define("CMAKE_SYSTEM_NAME", system_name);
}
println!("Configuring and compiling librdkafka");
let dst = config.build();
println!("cargo:rustc-link-search=native={}/lib", dst.display());
println!("cargo:rustc-link-lib=static=rdkafka");
}