How to install and use autotools on Mac OS X

In this article we’ll learn how to install and configure autotools on Mac OS X so that we can start to compile Open Source Software on the Mac.

Premise

To follow this article you need to have some confidence using Mac OS terminal application and some basic Mac command line experience. If you don’t have these requirements, please make some practice first or you may end up messing with your Mac. This is a how-to article so I assume you know how to use Mac OS X terminal and commands like sudo.

Requirements

First of all let’s install the basic requirements:

Mac has two important “porting” projects where 2 communities of developers are constantly porting OSS (Open Source Software) on the Mac platform:

  1. MacPorts
    1. The MacPorts Project is an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the OS X operating system.
    2. I use it since the beginning of this project and recommend to install it to all OSS developers who wants to work on OSS software using their Macs, more info on Mac Ports here.
  2. Homebrew
    1. Homebrew is a free/open source software package management system that simplifies the installation of software on the Mac OS X operating system.
    2. I find Homebrew really useful and easy to use, so I definitely recommend to install it to all novice (or advanced) developers, it will boost all the tedious activities of properly install/configure OSS libraries on the Mac! More info about Homebrew here.

Can MacPorts and Homebrew coexist on the same system?

I have used both of them for quite a long time now without having any issue, but the two solutions are NOT developed to work together, so you need to pay some attention.

Let’s get started!

First of all let’s install MacPorts:

  • To install MacPorts you first need to install Apple Xcode on your Mac, you can install Xcode using the Apple store application on your Mac. Once you’ve installed Xcode, run Mac OS X terminal and type:
$ sudo xcode-select --install

  This will install the required command line developer tools.

  • At this point accept Xcode license by executing:
$ sudo xcodebuild -license
  • Now, download MacPorts Package file correspondent to the Mac OS X version installed on your Mac, you can download the correct Pkg from here: https://distfiles.macports.org/MacPorts/
  • Once you’ve downloaded the correct Pkg file (or dmg file for some version of Mac OS X), install it as your would install a normal Pkg file or dmg file. (So double click on the file icon that will be located in the directory where you downloaded the file and follow instructions from there).
  • Time to synch up your MacPorts installation, to do so get back at your terminal window and type:
$ sudo port -v selfupdate

   This update will take a while, depending on your internet connection and your Mac speed into computing all the received info.

  • Now you have MacPorts installed and updated, but you still need some little hack to make sure it will not interfere with Homebrew later.
  • A possible hack to achieve this is to create the following script to use instead of the standard MacPort syntax to run MacPort installations and everything. I saved this script into my ~/ and called it “macport”, you can call it as you prefer.
#!/bin/bash

# Script wrapper for Macport commands
# this script is meant to help resolving
# conflicts between Homebrew and Macport
# no warranty is provided with it.
# Please note that is is NOT meant to be
# a comprensive solution to the issues 
# between Homebrew and Macport
# Also please note I keep tweaking this
# script if I find more issues, so consider
# this alpha code quality.

# If no parameters are provided then show
# usage:
if [ "$#" -le 0 ]; then
  echo "macport usage: $0 command [arg1, arg2, ...]" >&2
  exit 1
fi

# Check for Macport path and wrap it
if [[ -z $MACPORTS_PREFIX ]]; then
  MACPORTS_PREFIX='/opt/local'
fi
export PATH="$MACPORTS_PREFIX/bin:$MACPORTS_PREFIX/sbin:$PATH"
export CPATH="$MACPORTS_PREFIX/include:$CPATH"

# Get the Macport command line
macport_cmd=$1
shift

# Execute it
exec $macport_cmd $*

Don’t forget to give the script executable permissions using “chmod +x” and don’t forget to execute your macport commands using this script to run them!

  • Now let’s install GCC, this because on recent releases of Mac OS X (from Maverick AFAIR) clang is used also when invoking specifically gcc. While this works fine for Mac native code, it won’t work for most of Open Source projects usually created around GCC. To install gcc on Mac using Macports and our previous script:
$ sudo ./macport port install gcc49
  • At this point you should be done.

Now let’s install Homebrew:

  • Homebrew requires only one line of instructions to be installed, so get back at terminal windows and type:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

   This will take a bit of time, depending on your internet connection and your Mac speed.

   Because we installed MacPorts before, homebrew may ask you to confirm changing permission to /opt/local and other directories in there to install OSS libraries you’ll need in the future, you can just press enter to confirm this.

  • To make sure that home-brew is ok please execute
$ brew doctor

And follow the instructions to fix any found issue. Obviously brew doctor will warn you that it has found Macport installed and that this is not an optimal situation…

Now let’s start to have some fun!

At this point all the requirements are in place so we can start installing the libraries we need to make autotools work fine on Mac:

  • First let install autotool, to do this, from terminal window, type:
$ brew install autoconf
  • And finally automake
$ brew install automake
  •  That’s it! You are ready to go 🙂

A little extra help…

By experience I saw that many autogen files (autogen.sh) do not consider little differences with Mac platform compared to other BSD based system, so you may have to add something like this to your autogen.sh file in order for it to work properly when generating the configure file:

case `uname` in Darwin
                      *) glibtoolize --copy ;;
                      *) libtoolize --copy ;;
esac

This because some autogen.sh may still find it difficult to find libtoolize on the Mac.

Also on some Makefile you may want to add Macport GCC specific path, to make it easy for Make to find the right compiler, here is an example of how to do that, open your Makefile and edit the line where the variable CC is defined or add this line if no line with CC is defined in the Makefile:

CC=/opt/local/bin/gcc-mp-4.9

That’s it! Now you should be able to run all the classic autogen.sh, configure and make/make install for your Open Source Projects on the Mac, enjoy!

Thanks for reading and, if you enjoyed this post, please support my blog by visiting my on-line hacking and engineering merchandise shop on redbubble.com by clicking here, thank you! 🙂

Advertisements

3 thoughts on “How to install and use autotools on Mac OS X

  1. Pingback: C code snippet to measure function execution time for both Linux and Mac OS X | Paolo Fabio Zaino's Blog

    • Thanks for reading the article Humberto.

      I did the job on Yosemite and then updated my Mac to El Captain and it seems to be still working. However the initial setup was done on Yosemite and with the releases of Homebrew and MacPorts of the time, so I would recommend to do the job on a Virtual Machine run Mac OS X Yosemite first and test it properly.

      I shared the info because I have seen many people were struggling to properly setup Mac with Autotools at the time, so, another recommendation is to check first if NOW either Homebrew OR MacPorts have all the pieces together so you don’t need to use a combinations of the two, but, instead, you are able to use only one of the two ports repository (that should be the best way of doing it).

      Hope this helps!
      Cheers 🙂

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s