How to specify the macOS deployment target for a library?

Viewed 1564

I'm building a AU audio plugin for macOS that links against a static Rust library compiled with Cargo. I compile my plugin via CMake with a macOS deployment target set to 10.9. At link time, I get tons of errors like

ld: warning: object file (my/rust/libfoo.a (std-bd716fa574aff005.std.8vdpzfpj-cgu.15.rcgu.o)) was built for newer macOS version (10.15) than being linked (10.9)

A tester running MacOS 10.13 confirmed that loading the plug-in leads to a crash.

I need to compile the Rust library for a macOS deployment target of 10.9 too, but how do I do that?

The library I'm compiling is resvg, I modified the projects Cargo.toml to output a static library in my own fork, which you will find here.

I invoke cargo build --release and my static library is compiled.

Running

otool -l libresvg.a | rg LC_VERSION_MIN_MACOSX -A2 | sort | uniq

outputs

  cmdsize 16
  version 10.15
  version 10.7
1 Answers

Rust targets macOS 10.7 by default for x86 and x86_64 targets (11.0 for aarch64 targets):

% cargo new --lib demo

% cd demo

% echo '[lib]' >> Cargo.toml

% echo 'crate-type = ["staticlib"]' >> Cargo.toml

% unset MACOSX_DEPLOYMENT_TARGET

% cargo build

# Older versions of macOS and/or x86 and x86_64
% otool -l target/debug/libdemo.a | rg LC_VERSION_MIN_MACOSX -A2 | rg version | sort | uniq -c
 198   version 10.7

# Newer versions of macOS and/or aarch64
% otool -l target/debug/libdemo.a | rg LC_BUILD_VERSION -A4 | rg minos | sort | uniq -c

You can opt into supporting a newer version by specifying the MACOSX_DEPLOYMENT_TARGET environment variable:

% export MACOSX_DEPLOYMENT_TARGET=10.12

% cargo clean && cargo build

% otool -l target/debug/libdemo.a | rg LC_VERSION_MIN_MACOSX -A2 | rg version | sort | uniq -c
   2   version 10.12
 196   version 10.7

If you needed to target something older than 10.7, I'd expect that you'd have to recompile the Rust standard library itself.

Related