FAQ - Help and questions

What is inside a macOS .app bundle, and how do I lay one out for a Rust program?

#macos#app-bundle#packaging#desktoppublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

A macOS app is a folder ending in .app. Inside, Contents holds an Info.plist naming the program, a MacOS folder with the executable and a Resources folder with the icon and data. Rust builds only the executable, so you create the rest yourself or with a bundling tool. The layout code was run on Windows.

Checked with rustc 1.93.0, Windows 11; the layout program was compiled and run on Windows and prints the folder tree it created. The bundle was NOT opened, signed or run on a Mac. Apple's pages could not be fetched as text, so the layout below follows Apple's bundle documentation as widely published; verify it on a Mac before shipping.

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Finder treats a folder named Something.app as a program. The standard structure is:

text
Notes.app/
  Contents/
    Info.plist          the bundle's description (name, identifier, version, executable name, icon)
    MacOS/              the executable
    Resources/          icon (.icns), data files, translations

cargo build gives you only the executable. A bundle needs the folders and an Info.plist. The program below creates this layout, so you can see exactly what goes where. It writes placeholder files for the executable and icon, so it checks the structure, not that macOS accepts the bundle.

src/main.rsrust
use std::fs;
use std::path::Path;

/// Lays out the folders and files of a macOS .app bundle. Only the layout is checked here (on Windows);
/// the bundle was not opened on a Mac.
fn main() -> std::io::Result<()> {
    let root = std::env::temp_dir().join("rs-forum-bundle-demo").join("Notes.app");
    let contents = root.join("Contents");
    fs::create_dir_all(contents.join("MacOS"))?;
    fs::create_dir_all(contents.join("Resources"))?;

    fs::write(contents.join("MacOS").join("notes"), b"(the compiled executable goes here)")?;
    fs::write(contents.join("Resources").join("AppIcon.icns"), b"(icon)")?;
    fs::write(
        contents.join("Info.plist"),
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleName</key><string>Notes</string>
    <key>CFBundleIdentifier</key><string>org.example.notes</string>
    <key>CFBundleExecutable</key><string>notes</string>
    <key>CFBundleIconFile</key><string>AppIcon</string>
    <key>CFBundlePackageType</key><string>APPL</string>
    <key>CFBundleShortVersionString</key><string>1.0.0</string>
    <key>CFBundleVersion</key><string>1</string>
</dict>
</plist>
"#,
    )?;

    print_tree(root.parent().unwrap(), root.parent().unwrap(), 0)?;
    fs::remove_dir_all(root.parent().unwrap())
}

fn print_tree(base: &Path, dir: &Path, depth: usize) -> std::io::Result<()> {
    let mut entries: Vec<_> = fs::read_dir(dir)?.flatten().collect();
    entries.sort_by_key(|e| e.file_name());
    for e in entries {
        println!("{}{}", "  ".repeat(depth), e.file_name().to_string_lossy());
        if e.path().is_dir() {
            print_tree(base, &e.path(), depth + 1)?;
        }
    }
    Ok(())
}
text
Notes.app
  Contents
    Info.plist
    MacOS
      notes
    Resources
      AppIcon.icns

Things to know:

  • CFBundleExecutable must match the file name in Contents/MacOS (notes above).
  • CFBundleIdentifier is a reverse-domain name that identifies the app to the system; choose one you control. org.example.notes is a placeholder.
  • The icon is an .icns file; CFBundleIconFile names it without the extension.
  • Build the bundle on a Mac. Windows file systems have no Unix permission bits, and the executable needs its execute permission, so assemble the real bundle on a Mac or a macOS CI runner.
  • Bundling tools exist so you do not write this by hand. cargo-bundle describes itself as generating "installers or app bundles for GUI executables built with cargo" and reads its settings from [package.metadata.bundle] in Cargo.toml; cargo-packager lists .app and .dmg among its macOS formats. Neither was run for this thread.

Common mistakes#

  • Putting writable data inside the bundle. Changing the bundle after signing breaks the signature; use the user's folders (see the settings thread).
  • Assembling the .app on Windows and expecting it to run on a Mac: see the note above.

Do I need a universal binary?#

That depends on whether you support both Intel and Apple silicon Macs; it is a build question, not a layout one.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet