Thin out Universal binaries via a shell script

May 24, '06 07:30:00AM

Contributed by: trangised

This is a shell command to convert universal binaries to thin. Before you use it, here are a couple things to look out for:

  1. The lipo tool strips setuid and setgid bits.
  2. Frameworks and other linked code needs to be universal to support Rosetta.
The overall savings for this script is probably a few hundred megabytes, which probably would not matter to most people. If you're even thinking about trying this, back up your complete system first.

function strip_to_architecture() {
find "${1:-.}" -type f -perm +111 -not -perm +7000 -not -name '*.*' -exec lipo {} -thin "${2:-`arch`}" -output {} \; -print 2>/dev/null
}

The first argument to the function is the directory to search, if not the current directory. After that, the architecture could be specified (but defaults to the system architecture). The code must be run as root to affect most system directories. Here's a list of likely directories to thin:
/bin /sbin /usr/bin /usr/sbin /usr/libexec /Applications
On an Intel mac, do not strip any Frameworks folders if you want to leave Rosetta working. On PowerPC Macs that is not an issue. Any file which is not a universal binary is ignored. The -not -perm +7000 ignores any files with a set_id bit. If you want to save a few more megabytes for some reason, change the script to only find a specific set_id bit, then restore the bit after lipo clears it.

Alternately, you could use ditto to accomplish the same thing, but I do not think ditto preserves set_id bits either.

[robg adds: This script is potentially very dangerous. I have not tested it. As noted, if you are going to, please back up your system first. Proceed at your own risk. Look both ways before crossing the street. Always eat your vegetables...]

Comments (1)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20060519142400789