macOS: make openconnect relocatable; drive installation through make install (#668)

Co-authored-by: Kevin Yue <[email protected]>
This commit is contained in:
Ihar HrachyshkaandKevin Yue authored and GitHub committed 2026-10-12 10:12:17 +08:00
1 parent f7649898fd
commit f9965f8a35
12 files changed
+427 -162

No files matched your search

+23
View File
@@ -93,6 +93,12 @@ fn sibling_binary_for_executable(executable: &Path, binary_name: &str) -> Option
if is_file(&binary) {
return Some(binary);
}
if matches!(binary_name, "gp-vpnc-script-installer" | "gp-hip-script-installer") {
let binary = bin_dir.parent()?.join("libexec/gpclient").join(binary_name);
if is_file(&binary) {
return Some(binary);
}
}
#[cfg(target_os = "macos")]
if let Some(binary) = macos::helper_path(&executable, binary_name)
&& is_file(&binary)
@@ -121,6 +127,23 @@ fn is_file(path: &Path) -> bool {
mod tests {
use super::*;
#[test]
fn script_installers_follow_the_installed_prefix() {
let directory = tempfile::tempdir().unwrap();
let bin = directory.path().join("bin");
let libexec = directory.path().join("libexec/gpclient");
std::fs::create_dir_all(&bin).unwrap();
std::fs::create_dir_all(&libexec).unwrap();
std::fs::write(bin.join("gpclient"), "").unwrap();
for name in ["gp-vpnc-script-installer", "gp-hip-script-installer"] {
std::fs::write(libexec.join(name), "").unwrap();
assert_eq!(
sibling_binary_for_executable(&bin.join("gpclient"), name),
Some(libexec.canonicalize().unwrap().join(name))
);
}
}
#[test]
#[cfg(target_os = "macos")]
fn desktop_resolves_helpers_in_the_actual_app_bundle_layout() {
+3
View File
@@ -15,3 +15,6 @@ cc = "1"
autotools = "0.2"
pkg-config = "0.3"
fs_extra = "1"
[dev-dependencies]
tempfile = "3"
+21
View File
@@ -137,6 +137,27 @@ fn build_openconnect(deps_dir: &PathBuf, out_dir: &PathBuf) -> PathBuf {
let lib_dir = dst.join("lib");
let include_dir = dst.join("include");
#[cfg(target_os = "macos")]
{
// Do not embed Cargo's temporary build directory in consumers of this library.
let library = std::fs::canonicalize(lib_dir.join("libopenconnect.dylib")).unwrap();
let library_name = library.file_name().unwrap().to_str().unwrap();
let status = std::process::Command::new("install_name_tool")
.arg("-id")
.arg(format!("@rpath/{library_name}"))
.arg(&library)
.status()
.expect("Failed to update the OpenConnect dylib install name");
assert!(status.success(), "Failed to update the OpenConnect dylib install name");
let status = std::process::Command::new("codesign")
.args(["--force", "--sign", "-"])
.arg(&library)
.status()
.expect("Failed to sign the OpenConnect dylib");
assert!(status.success(), "Failed to sign the OpenConnect dylib");
println!("cargo:lib_dir={}", lib_dir.display());
}
println!("cargo:rustc-link-search=native={}", lib_dir.display());
#[cfg(target_os = "macos")]
+34
View File
@@ -34,9 +34,23 @@ pub fn find_vpnc_script() -> Option<String> {
if let Some(path) = std::env::var_os("GP_VPNC_SCRIPT").filter(|path| !path.is_empty()) {
return configured_script(Path::new(&path));
}
if let Ok(executable) = std::env::current_exe()
&& let Some(script) = installed_vpnc_script(&executable)
{
return Some(script);
}
find_executable(VPNC_SCRIPT_LOCATIONS).map(str::to_owned)
}
fn installed_vpnc_script(executable: &Path) -> Option<String> {
let executable = executable.canonicalize().ok()?;
let bin = executable.parent()?;
if bin.file_name()? != "bin" {
return None;
}
configured_script(&bin.parent()?.join("libexec/gpclient/vpnc-script"))
}
fn configured_script(path: &Path) -> Option<String> {
if path.is_absolute() && path.is_file() && path.is_executable() {
return path.to_str().map(str::to_owned);
@@ -62,6 +76,26 @@ pub fn check_executable(file: &str) -> Result<(), io::Error> {
mod tests {
use super::*;
#[test]
fn installed_script_follows_the_executable_prefix_through_symlinks() {
let dir = tempfile::tempdir().unwrap();
let bin = dir.path().join("prefix/bin");
let libexec = dir.path().join("prefix/libexec/gpclient");
std::fs::create_dir_all(&bin).unwrap();
std::fs::create_dir_all(&libexec).unwrap();
std::fs::write(bin.join("gpclient"), "").unwrap();
let script = libexec.join("vpnc-script");
std::fs::write(&script, "#!/bin/sh\n").unwrap();
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
let link = dir.path().join("gpclient-link");
std::os::unix::fs::symlink(bin.join("gpclient"), &link).unwrap();
assert_eq!(
installed_vpnc_script(&link),
Some(script.canonicalize().unwrap().to_str().unwrap().to_owned())
);
}
#[test]
fn configured_script_requires_an_absolute_executable_path() {
assert_eq!(configured_script(Path::new("/bin/sh")), Some("/bin/sh".to_owned()));