Files
git/contrib/libgit-sys/build.rs
Josh Steadmon e7f8bf125c libgit-sys: introduce Rust wrapper for libgit.a
Introduce libgit-sys, a Rust wrapper crate that allows Rust code to call
functions in libgit.a. This initial patch defines build rules and an
interface that exposes user agent string getter functions as a proof of
concept. This library can be tested with `cargo test`. In later commits,
a higher-level library containing a more Rust-friendly interface will be
added at `contrib/libgit-rs`.

Symbols in libgit can collide with symbols from other libraries such as
libgit2. We avoid this by first exposing library symbols in
public_symbol_export.[ch]. These symbols are prepended with "libgit_" to
avoid collisions and set to visible using a visibility pragma. In
build.rs, Rust builds contrib/libgit-rs/libgit-sys/libgitpub.a, which also
contains libgit.a and other dependent libraries, with
-fvisibility=hidden to hide all symbols within those libraries that
haven't been exposed with a visibility pragma.

Co-authored-by: Kyle Lippincott <spectral@google.com>
Co-authored-by: Calvin Wan <calvinwan@google.com>
Signed-off-by: Calvin Wan <calvinwan@google.com>
Signed-off-by: Kyle Lippincott <spectral@google.com>
Signed-off-by: Josh Steadmon <steadmon@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2025-01-28 14:45:47 -08:00

36 lines
1.1 KiB
Rust

use std::env;
use std::path::PathBuf;
pub fn main() -> std::io::Result<()> {
let ac = autocfg::new();
ac.emit_has_path("std::ffi::c_char");
let crate_root = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let git_root = crate_root.join("../..");
let dst = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let make_output = make_cmd::gnu_make()
.env("DEVELOPER", "1")
.env_remove("PROFILE")
.current_dir(git_root.clone())
.args([
"INCLUDE_LIBGIT_RS=YesPlease",
"contrib/libgit-sys/libgitpub.a",
])
.output()
.expect("Make failed to run");
if !make_output.status.success() {
panic!(
"Make failed:\n stdout = {}\n stderr = {}\n",
String::from_utf8(make_output.stdout).unwrap(),
String::from_utf8(make_output.stderr).unwrap()
);
}
std::fs::copy(crate_root.join("libgitpub.a"), dst.join("libgitpub.a"))?;
println!("cargo:rustc-link-search=native={}", dst.display());
println!("cargo:rustc-link-lib=gitpub");
println!("cargo:rerun-if-changed={}", git_root.display());
Ok(())
}