From d2b14f02fbf6b8420abb4c77870d64df596fc0e9 Mon Sep 17 00:00:00 2001 From: Alessandro Ghedini Date: Tue, 1 Feb 2022 11:36:54 +0000 Subject: [PATCH] build: fix pkg-config output path When building the tarball from crates.io rather than from the git repository, the target directory for the pkg-config file doesn't exist. Note that when explicitly passing the --target option to cargo, this changes the output directory from "target//quiche.pc" (e.g. "target/debug/quiche.pc") to "target///quiche.pc" (e.g. "target/x86_64-unknown-linux-gnu/debug). Fixes #1142. --- quiche/src/build.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/build.rs b/src/build.rs index ce299cd49..d1ef4f4ee 100644 --- a/src/build.rs +++ b/src/build.rs @@ -173,11 +173,10 @@ fn get_boringssl_cmake_config() -> cmake::Config { fn write_pkg_config() { use std::io::prelude::*; - let profile = std::env::var("PROFILE").unwrap(); let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); - let target_dir = format!("{}/../target/{}", manifest_dir, profile); + let target_dir = target_dir_path(); - let out_path = std::path::Path::new(&target_dir).join("quiche.pc"); + let out_path = target_dir.as_path().join("quiche.pc"); let mut out_file = std::fs::File::create(&out_path).unwrap(); let include_dir = format!("{}/include", manifest_dir); @@ -196,12 +195,27 @@ Version: {} Libs: -Wl,-rpath,${{libdir}} -L${{libdir}} -lquiche Cflags: -I${{includedir}} ", - include_dir, target_dir, version + include_dir, + target_dir.to_str().unwrap(), + version ); out_file.write_all(output.as_bytes()).unwrap(); } +fn target_dir_path() -> std::path::PathBuf { + let out_dir = std::env::var("OUT_DIR").unwrap(); + let out_dir = std::path::Path::new(&out_dir); + + for p in out_dir.ancestors() { + if p.ends_with("build") { + return p.parent().unwrap().to_path_buf(); + } + } + + unreachable!(); +} + fn main() { if cfg!(feature = "boringssl-vendored") && !cfg!(feature = "boring-sys") { let bssl_dir = std::env::var("QUICHE_BSSL_PATH").unwrap_or_else(|_| {