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
use pkg_config::{Config, Error as PkgConfigError};
use shlex::Shlex;
use std::io::Error as IoError;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use thiserror::Error;
pub struct Folly {
pub lib_dirs: Vec<PathBuf>,
pub include_paths: Vec<PathBuf>,
pub other_cflags: Vec<String>,
_priv: (),
}
#[derive(Error, Debug)]
pub enum FollyError {
#[error("`fmt` dependency couldn't be located")]
FmtDependency(PkgConfigError),
#[error("`gflags` dependency couldn't be located")]
GflagsDependency(PkgConfigError),
#[error("main `folly` package couldn't be located")]
MainPackage(IoError),
#[error("could not find `boost_context`; make sure either `libboost_context.a` or \
`libboost_context-mt.a` is located in the same directory as Folly")]
BoostContext,
}
pub fn probe_folly() -> Result<Folly, FollyError> {
Config::new()
.statik(true)
.probe("fmt")
.map_err(FollyError::FmtDependency)?;
Config::new()
.statik(true)
.probe("gflags")
.map_err(FollyError::GflagsDependency)?;
let mut folly = Folly::new();
let output = Command::new("pkg-config")
.args(&["--static", "--libs", "libfolly"])
.output()
.map_err(FollyError::MainPackage)?;
let output = String::from_utf8(output.stdout).expect("`pkg-config --libs` wasn't UTF-8!");
for arg in Shlex::new(&output) {
if arg.starts_with('-') {
if let Some(rest) = arg.strip_prefix("-L") {
folly.lib_dirs.push(PathBuf::from(rest));
} else if let Some(rest) = arg.strip_prefix("-l") {
println!("cargo:rustc-link-lib={}", rest);
}
continue;
}
let path = PathBuf::from_str(&arg).unwrap();
let (parent, lib_name) = match (path.parent(), path.file_stem()) {
(Some(parent), Some(lib_name)) => (parent, lib_name),
_ => continue,
};
let lib_name = lib_name.to_string_lossy();
if let Some(rest) = lib_name.strip_prefix("lib") {
println!("cargo:rustc-link-search={}", parent.display());
println!("cargo:rustc-link-lib={}", rest);
}
}
let mut found_boost_context = false;
for lib_dir in &folly.lib_dirs {
println!("cargo:rustc-link-search={}", lib_dir.display());
if found_boost_context {
continue;
}
for possible_lib_name in &["boost_context", "boost_context-mt"] {
let mut lib_dir = (*lib_dir).clone();
lib_dir.push(&format!("lib{}.a", possible_lib_name));
if !lib_dir.exists() {
continue;
}
println!("cargo:rustc-link-lib={}", possible_lib_name);
found_boost_context = true;
break;
}
}
if !found_boost_context {
return Err(FollyError::BoostContext);
}
let output = Command::new("pkg-config")
.args(&["--static", "--cflags", "libfolly"])
.output()
.map_err(FollyError::MainPackage)?;
let output = String::from_utf8(output.stdout).expect("`pkg-config --libs` wasn't UTF-8!");
for arg in output.split_whitespace() {
if let Some(rest) = arg.strip_prefix("-I") {
let path = Path::new(rest);
if path.starts_with("/Library/Developer/CommandLineTools/SDKs")
&& path.ends_with("usr/include")
{
let sysroot = path.parent().unwrap().parent().unwrap();
folly.other_cflags.push("-isysroot".to_owned());
folly.other_cflags.push(sysroot.to_string_lossy().into_owned());
} else {
folly.include_paths.push(path.to_owned());
}
}
}
Ok(folly)
}
impl Folly {
fn new() -> Self {
Self {
lib_dirs: vec![],
include_paths: vec![],
other_cflags: vec![],
_priv: (),
}
}
}