There are some open source tools to help build app bundles with dependent libraries for specific environments, for example, py2app for Python-based applications. If you don't find a more general one, perhaps you can adapt it to your needs.
The simplest solution is: create once an Xcode project without changing anything (i.e. keep the simple one-window app that Xcode creates for you), build it, and copy the bundle it created for you. Then, edit the files (notably the Info.plist) to suit your content, and put your own binary in the Contents/MacOS/ directory.
There are two ways to create an app bundle on MacOSX, the Easy and the Ugly.
The easy way is just to use XCode.
Done.
The problem is sometimes you can't.
In my case I'm building an app that builds other apps.
I can't assume the user has XCode installed.
I'm also using MacPorts to build the libraries my app depends on.
I need to make sure that these dylibs get bundled with the app before I distribute it.
Disclaimer: I'm totally unqualified to write this post, everything in
is has been gleamed from Apple docs, picking apart existing apps and
trial and error. It works for me, but is most likely wrong. Please
email me if you have any corrections.
First thing you should know is that an app bundle is just a directory.
Let's examine the structure of a hypothetical foo.app.
Info.plist is a plain XML file.
You can edit it with a text editor or the Property List Editor app that comes bundled with XCode.
(It's in /Developer/Applications/Utilities/ directory).
The key things you need to include are:
CFBundleName - The name of the app.
CFBundleIcon - An Icon file assumed to be in Contents/Resources dir.
Use the Icon Composer app to create the icon. (It's also in the /Developer/Applications/Utilities/ directory)
You can just drag and drop a png onto it's window and should automatically generate the mip-levels for you.
CFBundleExecutable - Name of the executable file assumed to be in Contents/MacOS/ sub-folder.
There are lots more options, the ones listed above are only the bare minimum.
Here's some Apple documentation on the
Info.plist
file and
App bundle structure.
In a perfect world you could just drop your executable into the
Contents/MacOS/ dir and be done. However, if your app has any
non-standard dylib dependencies it won't work. Like Windows, MacOS
comes with it's own special kind of DLL Hell.
If you're using MacPorts to build libraries that you link against, the locations of the the dylibs will be hard-coded into your executable.
If you run the app on a machine that has the dylibs in the exact same location, it will run fine.
However, most users won't have them installed; when they double-click your app it will just crash.
Before you distribute you executable you'll need to collect all the dylibs it loads and copy them into the app bundle.
You will also need to edit the executable so that it will look for the dylibs in the correct place. i.e. where you copied them to.
Hand editing an executable sounds dangerous right?
Luckily there are command line tools to help.
otool -L executable_name
This command will list all the dylibs that your app depends on.
If you see any that are NOT in the System/Library or usr/lib folder, those are the ones you'll need to copy into the app bundle.
Copy them into the /Contents/MacOS/ folder.
Next you'll need to edit the executable to use the new dylibs.
First, you need to make sure that you link using the -headerpad_max_install_names flag.
This just makes sure that if the new dylib path is longer then the previous one, there will be room for it.
Second, use the install_name_tool to change each dylib path.
Whew, we're almost done.
Now there's a small issue with the current working directory.
When you start your app the current directory will be the directory above where the application is located.
For example: If you place the foo.app in the /Applcations folder then the current directory when you launch the app will be the /Applications folder.
Not the /Applications/foo.app/Contents/MacOS/ as you might expect.
You can alter your app to account for this, or you can use this magic little launcher script that will change the current directory and launch your app.
#!/bin/bash
cd "${0%/*}"
./foo
Make sure you adjust the Info.plist file so that CFBundleExecutable points to the launch script and not to the previous executable.
Ok, all done now.
Luckily, once you know all this stuff you bury it in a build script.
I use this in my Makefile... It creates an app bundle. Read it and understand it, because you'll need a png icon file in a macosx/ folder along with the PkgInfo and Info.plist files i include here...
"it works on my computer"... I use this for multiple apps on Mavericks...
Here's my sketchy way of solving this problem using a Run script phase which is invoked every time I build a Release version of my app:
# this is an array of my dependencies' libraries paths
# which will be iterated in order to find those dependencies using otool -L
libpaths=("$NDNRTC_LIB_PATH" "$BOOST_LIB_PATH" "$NDNCHAT_LIB_PATH" "$NDNCPP_LIB_PATH" "/opt/local/lib")
frameworksDir=$BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH
executable=$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH
#echo "libpaths $libpaths"
bRecursion=0
lRecursion=0
# this function iterates through libpaths array
# and checks binary with "otool -L" command for containment
# of dependency which has "libpath" path
# if such dependency has been found, it will be copied to Frameworks
# folder and binary will be fixed with "install_name_tool -change" command
# to point to Frameworks/<dependency> library
# then, dependency is checked recursively with resolveDependencies function
function resolveDependencies()
{
local binfile=$1
local prefix=$2
local binname=$(basename $binfile)
local offset=$((lRecursion*20))
printf "%s :\t%s\n" $prefix "resolving $binname..."
for path in ${libpaths[@]}; do
local temp=$path
#echo "check lib path $path"
local pattern="$path/([A-z0-9.-]+\.dylib)"
while [[ "$(otool -L ${binfile})" =~ $pattern ]]; do
local libname=${BASH_REMATCH[1]}
otool -L ${binfile}
#echo "found match $libname"
printf "%s :\t%s\n" $prefix "fixing $libname..."
local libpath="${path}/$libname"
#echo "cp $libpath $frameworksDir"
${SRCROOT}/sudocp.sh $libpath $frameworksDir/$libname $(whoami)
local installLibPath="@rpath/$libname"
#echo "install_name_tool -change $libpath $installLibPath $binfile"
if [ "$libname" == "$binname" ]; then
install_name_tool -id "@rpath/$libname" $binfile
printf "%s :\t%s\n" $prefix "fixed id for $libname."
else
install_name_tool -change $libpath $installLibPath $binfile
printf "%s :\t%s\n" $prefix "$libname dependency resolved."
let lRecursion++
resolveDependencies "$frameworksDir/$libname" "$prefix>$libname"
resolveBoostDependencies "$frameworksDir/$libname" "$prefix>$libname"
let lRecursion--
fi
path=$temp
done # while
done # for
printf "%s :\t%s\n" $prefix "$(basename $binfile) resolved."
} # resolveDependencies
# for some reason, unlike other dependencies which maintain full path
# in "otool -L" output, boost libraries do not - they just appear
# as "libboost_xxxx.dylib" entries, without fully qualified path
# thus, resolveDependencies can't be used and a designated function is needed
# this function works pretty much in a similar way to resolveDependencies
# but targets only dependencies starting with "libboost_", copies them
# to the Frameworks folder and resolves them recursively
function resolveBoostDependencies()
{
local binfile=$1
local prefix=$2
local binname=$(basename $binfile)
local offset=$(((bRecursion+lRecursion)*20))
printf "%s :\t%s\n" $prefix "resolving Boost for $(basename $binfile)..."
local pattern="[[:space:]]libboost_([A-z0-9.-]+\.dylib)"
while [[ "$(otool -L ${binfile})" =~ $pattern ]]; do
local libname="libboost_${BASH_REMATCH[1]}"
#echo "found match $libname"
local libpath="${BOOST_LIB_PATH}/$libname"
#echo "cp $libpath $frameworksDir"
${SRCROOT}/sudocp.sh $libpath $frameworksDir/$libname $(whoami)
installLibPath="@rpath/$libname"
#echo "install_name_tool -change $libname $installLibPath $binfile"
if [ "$libname" == "$binname" ]; then
install_name_tool -id "@rpath/$libname" $binfile
printf "%s :\t%s\n" $prefix "fixed id for $libname."
else
install_name_tool -change $libname $installLibPath $binfile
printf "%s :\t%s\n" $prefix "$libname Boost dependency resolved."
let bRecursion++
resolveBoostDependencies "$frameworksDir/$libname" "$prefix>$libname"
let bRecursion--
fi
done # while
printf "%s :\t%s\n" $prefix "$(basename $binfile) resolved."
}
resolveDependencies $executable $(basename $executable)
resolveBoostDependencies $executable $(basename $executable)