Skip to content

Commit

Permalink
Switch over to using find-folly crate
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Aug 31, 2022
1 parent bc5e28d commit 10dc5e2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 108 deletions.
34 changes: 32 additions & 2 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions examples/folly/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ features = ["thread-pool"]

[build-dependencies]
cxx-build = "1"
pkg-config = "0.3"
shlex = "1"
find-folly = "0.1"
109 changes: 5 additions & 104 deletions examples/folly/build.rs
Original file line number Diff line number Diff line change
@@ -1,108 +1,9 @@
// cxx-async/examples/folly/build.rs

use pkg_config::Config;
use shlex::Shlex;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use find_folly;

fn main() {
// Folly's `.pc` file is missing the `fmt` and `gflags` dependencies. Find them here.
Config::new()
.statik(true)
.probe("fmt")
.expect("No `fmt` package found!");
Config::new()
.statik(true)
.probe("gflags")
.expect("No `gflags` package found!");

// Unfortunately, the `pkg-config` crate doesn't successfully parse some of Folly's
// dependencies, because it passes the raw `.so` files instead of using `-l` flags. So call
// `pkg-config` manually.
let mut lib_dirs = vec![];
let output = Command::new("pkg-config")
.args(&["--static", "--libs", "libfolly"])
.output()
.expect("Failed to execute `pkg-config` to find Folly!");
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") {
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);
}
}

// Unfortunately, just like `fmt` and `gflags`, Folly's `.pc` file doesn't contain a link flag
// for `boost_context`. What's worse, the name varies based on different systems
// (`libboost_context.a` vs. `libboost_context-mt.a`). So find that library manually. We assume
// it's in the same directory as the Folly installation itself.
let mut found_boost_context = false;
for lib_dir in &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 {
panic!(
"Could not find `boost_context`. Make sure either `libboost_context.a` or \
`libboost_context-mt.a` is located in the same directory as Folly."
)
}

let output = Command::new("pkg-config")
.args(&["--static", "--cflags", "libfolly"])
.output()
.expect("Failed to execute `pkg-config` to find Folly!");
let output = String::from_utf8(output.stdout).expect("`pkg-config --libs` wasn't UTF-8!");

let (mut include_dirs, mut other_cflags) = (vec![], vec![]);
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")
{
// Change any attempt to specify system headers from `-I` to `-isysroot`. `-I` is
// not the proper way to include a system header and will cause compilation failures
// on macOS Catalina.
//
// Pop off the trailing `usr/include`.
let sysroot = path.parent().unwrap().parent().unwrap();
other_cflags.push("-isysroot".to_owned());
other_cflags.push(sysroot.to_string_lossy().into_owned());
} else {
include_dirs.push(path.to_owned());
}
}
}
let folly = find_folly::probe_folly().expect("Couldn't find the Folly library!");

println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=include/folly_example.h");
Expand All @@ -114,9 +15,9 @@ fn main() {
.include("include")
.include("../common/include")
.include("../../cxx-async/include")
.includes(&include_dirs);
for other_cflag in other_cflags {
build.flag(&other_cflag);
.includes(&folly.include_paths);
for other_cflag in &folly.other_cflags {
build.flag(other_cflag);
}
build.compile("folly_example");
}

0 comments on commit 10dc5e2

Please sign in to comment.