launchd.plist file directly in your
executable binary, brought to you by @NikolaiVazquez!
If you found this library useful, please consider sponsoring me on GitHub. ❤️
- Minimum Supported Rust Version
- Multi-Target Considerations
- Get Embedded Property Lists
- Accidental Reuse Protection
Certain programs require an embedded
launchd.plist file to
work correctly. For example:
Info.plistis needed to obtain certain permissions on macOS 10.15 and later.
launchd.plistis needed to make
launchddaemons and user agents.
Doing this manually with
include_bytes! is cumbersome. To understand
why, see the implementation. This library removes the
headache of doing this.
This library is available on crates.io and can be used by adding
the following to your project's
 = "1.2"
...and this to any source file in your crate:
!; // If making a daemon: embed_launchd_plist!; embed_info_plist
Done! It's that simple. 🙂
See implementation for details on this sorcery.
Minimum Supported Rust Version
This library targets 1.39 as its minimum supported Rust version (MSRV).
Requiring a newer Rust version is considered a breaking change and will
result in a "major" library version update. In other words:
1.y.z would become
This library only works for Mach-O
binaries. When building a cross-platform program, these macro calls should
be placed behind a
#[cfg] to prevent linker errors on other targets.
Get Embedded Property Lists
After using these macros, you can get their contents by calling
get_launchd_plist from anywhere in your program.
We can verify that the result is correct by checking it against reading the appropriate file at runtime:
!; let embedded_plist = get_info_plist; let read_plist = read?; assert_eq!; embed_info_plist
If the appropriate macro has not been called, each function creates a compile-time error by failing to reference the symbol defined by that macro:
// This fails to compile: let embedded_plist = get_info_plist;
Accidental Reuse Protection
Only one copy of
launchd.plist should exist in a binary.
Accidentally embedding them multiple times would break tools that read these
Fortunately, this library makes reuse a compile-time error! This protection works even if these macros are reused in different modules.
// This fails to compile: embed_info_plist!; embed_info_plist!;
This example produces the following error:
error: symbol `_EMBED_INFO_PLIST` is already defined --> src/main.rs:4:1 | 4 | embed_plist::embed_info_plist!("Info.plist"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to previous error
Warning: Although the name
_EMBED_INFO_PLISTcan be seen here, you should not reference this symbol with e.g. an
extern "C"block. I reserve the right to change this name in a SemVer-compatible update.
Files are read using
include_bytes!. This normally places data in
__TEXT,__const, where immutable data is kept. However, property list data
is expected to be in
section will explain how I accomplish that.
We begin by reading the file from disk:
const BYTES: & = include_bytes!;
A naïve approach is to do the following:
// Prevent optimizing out static PLIST: & = BYTES;
This doesn't work because only the slice's pointer and length that are
__TEXT,__info_plist. The referenced bytes are still placed in
Instead, we need to arrive at the following:
static PLIST: = *BYTES;
We can get
N by using
len. As of Rust 1.39, it is possible to get the
length of a slice within a
const N: usize = BYTES.len;
The next step is to dereference the bytes into a
There are two approaches:
This is not the approach used by this library because of concerns about compile performance. See the second approach for what this library does.
The following is all we need:
static PLIST: = *include_bytes!;
This works because
include_bytes!actually returns a
&[u8; N]. It's often used as a
&[u8]because we don't know the size when calling it.
Dereference our current bytes through pointer casting.
This is tricker than the first approach (and somewhat cursed). If you know me, then it's predictable I'd go this route.
We can get a pointer to the bytes via
as_ptr, which is usable in
const PTR: *const = BYTES.as_ptr as *const ;
Unfortunately, this pointer can't be directly dereferenced in Rust 1.39 (minimum supported version).
// This fails to compile: static PLIST: = unsafe ;
Instead, we must cast the pointer to a reference.
You may want to reach for
transmute, which was stabilized for use in
constin Rust 1.46. However, earlier versions need to be supported, so that is not an option for this library.
This bitwise cast can be accomplished with a
union Transmute const REF: & = unsafe ;
Finally, we can dereference our bytes:
static PLIST: = *REF;
This project is released under either:
at your choosing.