How to build the lastest Vim for MacOSX

[Updated: 12/03/2011: Updated for Xcode 4 and Vim 7.3.x]

How to build the latest Vim from Mercurial (or you can grab it right from my SourceForge project page MacOSX Vim)

When compiling on Snow Leopard, make sure to have the MacOSX10.4u.sdk package installed (Source: optionals install package) if you want to build an universal binary.
Carbon is only available for i386 and thus a x86_64 build is not possible.
An universal Vim can only be built using Xcode 3.x.

Xcode 3.x

  • universal binary
  • export CC=/usr/bin/gcc-4.0
    export CFLAGS=”-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386 -mmacosx-version-min=10.4″
    export LDFLAGS=”-Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386 -mmacosx-version-min=10.4″

  • intel only, 10.5.x compatible
  • export CFLAGS=”-isysroot /Developer/SDKs/MacOSX10.5.sdk – arch i386 -mmacosx-version-min=10.5″
    export LDFLAGS=”-Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -arch i386 -mmacosx-version-min=10.5″

  • intel only, 10.6.x compatible
  • export CFLAGS=”-isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -mmacosx-version-min=10.6″
    export LDFLAGS=”-Wl,-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -arch i386 -mmacosx-version-min=10.6″

Xcode 4.x

  • To build Mercurial, an ‘as’ binary for ppc is required
  • Follow these steps once to provide the ‘as’ binary

    sudo bash
    mkdir /usr/libexec/gcc/darwin/ppc/
    cd /usr/libexec/gcc/darwin/ppc/
    cp /Developer-old/usr/libexec/gcc/darwin/ppc/as .

  • universal binary
  • Both the compiler and the SDK provided by Xcode 3.x must be used

    export CC=”/Developer-old/usr/bin/gcc-4.0″
    export CFLAGS=”-isysroot /Developer-old/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386 -mmacosx-version-min=10.4″
    export LDFLAGS=”-Wl,-syslibroot,/Developer-old/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386 -mmacosx-version-min=10.4″

  • intel only, 10.6.x compatible
  • export CFLAGS=”-isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -mmacosx-version-min=10.6″
    export LDFLAGS=”-Wl,-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -arch i386 -mmacosx-version-min=10.6″

Here we go, with some additional commentary :)

Create a 512MB Ramdisk (this can speed up things quite a bit)

$ DISK_ID=$(hdid -nomount ram://26214400)
$ newfs_hfs -v Ramdisk ${DISK_ID}
$ diskutil mount ${DISK_ID}

Create the target environment for the compilation

$ BUILD_SANDBOX="/Volumes/Ramdisk"
$ DMG_SRCDIR=release
$ DMG_SANDBOX="${BUILD_SANDBOX}/${DMG_SRCDIR}"
$ cd ${BUILD_SANDBOX}
$ mkdir ${DMG_SANDBOX}

Get Mercurial

$ cd ${BUILD_SANDBOX}
$ curl -s -O http://mercurial.selenic.com/release/mercurial-1.6.tar.gz
$ tar xzpf mercurial-1.6.tar.gz
$ cd mercurial-1.6
$ make all
$ make install PREFIX="${BUILD_SANDBOX}/sw"
$ export   PYTHONPATH="${BUILD_SANDBOX}/sw/lib/python2.6/site-packages/"

Grab the source from the mercurial repository

$ cd ${BUILD_SANDBOX}
$ export PATH=${BUILD_SANDBOX}/sw/bin:$PATH
$ hg clone https://vim.googlecode.com/hg/ vim
$ cd vim/src
$ hg update default 
$ BASEDIR=$(pwd)

Fix some odd problem with clip_mch reference

$ cd ${BASEDIR}
$ perl -p -i -e  "s/^clip_mch/goo_clip_mch/g" $(find . -type f -name os_macosx.m)

Depending on what version of MacOSX you are running, choose the correct compiler

$ osxver=$(sw_vers -productVersion | cut -d"." -f2)
$ if [ $osxver -gt 5 ]; then export CC=/usr/bin/gcc-4.0; fi

These compiler flags are important if you want to build a 10.4.x compatible version
(plus you get both architectures)

$ export CFLAGS="-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386 -mmacosx-version-min=10.4"
$ export LDFLAGS="-Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386 -mmacosx-version-min=10.4"

Now things may get a little tricky. If you have Xcode 4.x installed, Vim will build but only as an i386 binary and will only run on MacOSX 10.6

$ if [ -e /Developer-old ]
> then 
> export CC=/Developer-old/usr/bin/gcc-4.0
> export CFLAGS="-isysroot /Developer-old/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386 -mmacosx-version-min=10.4" 
> export LDFLAGS="-Wl,-syslibroot,/Developer-old/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386 -mmacosx-version-min=10.4" 
> fi

Run ./configure, no bells and whistles

$ ./configure --prefix=${DMG_SANDBOX} --with-compiledby="swiv@users.sourceforge.net" --enable-gui=carbon --enable-multibyte
$ make -j 2

Obtain the Vim version information by using awk

$ VIMVOLNAMESION=$(src/Vim.app/Contents/MacOS/Vim --version | awk '/^VIM/ {print $5}' )
$ PATCHLEVEL=$(src/Vim.app/Contents/MacOS/Vim --version | awk -F"-" '/patches/ { print $2 }' )
$ if [ -z ${PATCHLEVEL} ]; then PATCHLEVEL="000"; fi
$ VIMFULLVER="${VIMVOLNAMESION}.${PATCHLEVEL}"
$ VIMVOLNAME="Vim-${VIMFULLVER}"

Add custom spellchecker and colour files

$ cd ${BASEDIR}
$ cd src/Vim.app/Contents/Resources/vim/runtime
$ cp ${HOME}/tmptmp/vimstuff/*.vim colors/
$ cp ${HOME}/tmptmp/vimstuff/de.* spell/

Create and move the HTML documentation

$ cd ${BASEDIR}
$ cd src/Vim.app/Contents/Resources/vim/runtime/doc/
$ make --quiet html
$ mkdir        ${DMG_SANDBOX}/Documentation
$ mv *.html    ${DMG_SANDBOX}/Documentation

Copy the Vim licence

$ cd ${BASEDIR}
$ cp src/Vim.app/Contents/Resources/vim/runtime/doc/uganda.txt ${DMG_SANDBOX}/Licence.txt

Create a PDF version of the Vim man page

$ cd ${BASEDIR}
$ cd src/Vim.app/Contents/Resources/vim/runtime/doc/
$ /usr/bin/groff -Tps  -mandoc -c vim.1 | pstopdf -i -o "${DMG_SANDBOX}/Vim Manpage.pdf"

Install to the target environment (Ramdisk)

$ cd ${BASEDIR}
$ make install

Creating a read-writable Apple Disk Image

$ cd ${BUILD_SANDBOX}
$ hdiutil create -volname ${VIMVOLNAME} -srcfolder ${DMG_SRCDIR} ${VIMVOLNAME}
$ cd ${BUILD_SANDBOX}
$ hdiutil convert ${VIMVOLNAME}.dmg -format UDRW -o ${VIMVOLNAME}.rw
$ rm ${VIMVOLNAME}.dmg
$ hdiutil attach ${VIMVOLNAME}.rw.dmg

“Fix” the Vim version shown in the Info dialog of MacOSX

$ cd /Volumes/${VIMVOLNAME}
$ cd Vim.app/Contents
$ /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${VIMFULLVER}" Info.plist
$ /usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${VIMFULLVER}" Info.plist

And finalise the Apple Disk Image

$ cd ${BUILD_SANDBOX}
$ hdiutil detach /Volumes/${VIMVOLNAME}
$ cd ${BUILD_SANDBOX}
$ hdiutil convert ${VIMVOLNAME}.rw.dmg -format UDBZ -o ${VIMVOLNAME}
$ cp ${VIMVOLNAME}.dmg ${HOME}/Desktop/

Cleanup :)

$ cd ${HOME}
$ diskutil unmount /Volumes/Ramdisk