From c6d4fbc68e863b070a3f33db7c87d581569d1db5 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 20 Sep 2020 02:20:32 +0200 Subject: Build a static binary I recently tried installing the program via Homebrew on a machine that didn't have 'libgit2' installed. The Homebrew formula installs pre-built binaries. Running the binaries gave me an error saying that the 'libgit2' dynamic library couldn't be found. Change the build configuration to build a "static" binary. It still links to system dylibs, but I've removed all links to Homebrew dylibs. These were: * libgit2 * openssl Removed the 'openssl' link by adding the 'vendored-openssl' feature to the 'git2' dependency. In order to remove the 'libgit2' link, I had to do some more finagling. Accomplished this by setting an empty `PKG_CONFIG_LIBDIR` when building. While 'git2' doesn't have a Cargo feature to disable dynamic linking, we can force static linking by preventing `pkg-config` from finding the Homebrew-installed 'libgit2'. The 'libgit2-sys' crate's build script first checks for the library using `pkg-config`, and builds it statically if it doesn't exist: https://github.com/rust-lang/git2-rs/blob/43d583fd8b50fad0cfe0bbc334059ce965b6f3fc/libgit2-sys/build.rs#L15-L17 With these changes, the above packages will now be statically linked with the program's binaries. Upgrade 'git2' to the latest version (0.13.11) since we're changing its build configuration. Before: $ otool -L target/release/git-sug* target/release/git-sugapply: /System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 57740.60.18) /usr/local/opt/libgit2/lib/libgit2.1.0.dylib (compatibility version 1.0.0, current version 1.0.1) /usr/local/opt/openssl@1.1/lib/libssl.1.1.dylib (compatibility version 1.1.0, current version 1.1.0) /usr/local/opt/openssl@1.1/lib/libcrypto.1.1.dylib (compatibility version 1.1.0, current version 1.1.0) /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.8) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2) /usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0) target/release/git-sugapply.d: is not an object file target/release/git-sugdiff: /System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 57740.60.18) /usr/local/opt/libgit2/lib/libgit2.1.0.dylib (compatibility version 1.0.0, current version 1.0.1) /usr/local/opt/openssl@1.1/lib/libssl.1.1.dylib (compatibility version 1.1.0, current version 1.1.0) /usr/local/opt/openssl@1.1/lib/libcrypto.1.1.dylib (compatibility version 1.1.0, current version 1.1.0) /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.8) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2) /usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0) target/release/git-sugdiff.d: is not an object file After: $ otool -L target/release/git-sug* target/release/git-sugapply: /System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 57740.60.18) /usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0) /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1349.8.0) /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.8) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2) /usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0) target/release/git-sugapply.d: is not an object file target/release/git-sugdiff: /System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 57740.60.18) /usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0) /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1349.8.0) /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.8) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2) /usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0) target/release/git-sugdiff.d: is not an object file --- Cargo.lock | 26 ++++++++++++++++++-------- Cargo.toml | 2 +- Makefile | 9 ++++++++- github-suggestion/Cargo.toml | 2 +- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c9731da..eb593ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -258,9 +258,9 @@ dependencies = [ [[package]] name = "git2" -version = "0.13.8" +version = "0.13.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6ac22e49b7d886b6802c66662b12609452248b1bc9e87d6d83ecea3db96f557" +checksum = "1e094214efbc7fdbbdee952147e493b00e99a4e52817492277e98967ae918165" dependencies = [ "bitflags", "libc", @@ -481,9 +481,9 @@ checksum = "a2f02823cf78b754822df5f7f268fb59822e7296276d3e069d8e8cb26a14bd10" [[package]] name = "libgit2-sys" -version = "0.12.9+1.0.1" +version = "0.12.13+1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b33bf3d9d4c45b48ae1ea7c334be69994624dc0a69f833d5d9f7605f24b552b" +checksum = "069eea34f76ec15f2822ccf78fe0cdb8c9016764d0a12865278585a74dbdeae5" dependencies = [ "cc", "libc", @@ -495,9 +495,9 @@ dependencies = [ [[package]] name = "libssh2-sys" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eafa907407504b0e683786d4aba47acf250f114d37357d56608333fd167dd0fc" +checksum = "ca46220853ba1c512fc82826d0834d87b06bcd3c2a42241b7de72f3d2fe17056" dependencies = [ "cc", "libc", @@ -509,9 +509,9 @@ dependencies = [ [[package]] name = "libz-sys" -version = "1.0.25" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2eb5e43362e38e2bca2fd5f5134c4d4564a23a5c28e9b95411652021a8675ebe" +checksum = "602113192b08db8f38796c4e85c39e960c145965140e918018bcde1952429655" dependencies = [ "cc", "libc", @@ -648,6 +648,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" +[[package]] +name = "openssl-src" +version = "111.10.2+1.1.1g" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a287fdb22e32b5b60624d4a5a7a02dbe82777f730ec0dbc42a0554326fef5a70" +dependencies = [ + "cc", +] + [[package]] name = "openssl-sys" version = "0.9.58" @@ -657,6 +666,7 @@ dependencies = [ "autocfg", "cc", "libc", + "openssl-src", "pkg-config", "vcpkg", ] diff --git a/Cargo.toml b/Cargo.toml index 82632ee..e4ce989 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" [dependencies] exitcode = "1.1.2" getopts = "0.2.21" -git2 = "0.13.8" +git2 = { version = "0.13.11", features = ["vendored-openssl"] } regex = "1.3.9" thiserror = "1.0.20" url = "2.1.1" diff --git a/Makefile b/Makefile index a0f8c73..7778be3 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,13 @@ DIST := $(abspath dist) DIST_PRODUCTS := $(patsubst %,dist/%,$(PRODUCTS)) DIST_MAN_PAGES := $(patsubst doc/%,dist/%,$(MAN_PAGES)) +# Set STATIC=1 to build a static binary. +STATIC ?= 0 + +ifeq ($(STATIC), 1) +BUILD_VARS += PKG_CONFIG_LIBDIR='' +endif + .PHONY: doc doc: $(MAN_PAGES) @@ -38,7 +45,7 @@ doc/%.1: doc/%.1.txt $(RELEASE_PRODUCTS): $(SOURCES) - cargo build --release + $(BUILD_VARS) cargo build --release .PHONY: dist diff --git a/github-suggestion/Cargo.toml b/github-suggestion/Cargo.toml index 34e197c..8962adc 100644 --- a/github-suggestion/Cargo.toml +++ b/github-suggestion/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2018" [dependencies] -git2 = "0.13.8" +git2 = { version = "0.13.11", features = ["vendored-openssl"] } github-rs = "0.7.0" regex = "1.3.9" serde = { version = "1.0.114", features = ["derive"] } -- cgit v1.2.3