Tutorial: Open Source on iOS (Part 1): Build Your Tools

This the first in a series of tutorials explaining how to compile popular open source libraries for iOS. In this tutorial you will download and compile a number of tools used in the GNU Build System used by many open source projects, specifically autoconf, automake and libtool. Since each of these tools is built with the GNU Build System it also provide a useful introduction to the system itself. It should be noted that many projects can be built without having these tools installed, but when things go wrong it is useful to have them around.

Open source builds use the command line extensively. If you are familiar with bash you can probably skip most of my commentary about commands and scripting, if not, I hope I provide enough information for you to understand what is going on. If you ever forget how to use a command you can always type man command_name where command_name is the name of the command you are trying to use. The system will provide you with a manual page for the command.

Creating the Directory Structure

To get started open the terminal application and type the following:

Let’s look at this a little more closely. The command cd changes directory; the tilde ‘~’ character is a shorthand for your home directory. (If you ever lose track of the directory you are in you can type pwd to print the current working directory). The command mkdir idz_build creates a new directory named idz_build. The following command changes into this newly created directory. This directory will be the root directory containing all your source, builds and scripts for your open source builds.

The remaining commands create directories for each of the tools you are about to build. As time goes on you may need to build updated versions so a sub-directory is created for each version. The -p flag instructs mkdir to make all directories in the path.

Building autoconf

The first tool you will build is autoconf. Type the following command.

The command pushd is similar to cd in that it changes directory but it also pushes the directory you were in onto a directory stack. To pop a directory off the stack and change to it use the popd command.

Now that you are in the appropriate directory you download and decompress the source for autoconf using the following commands.

The command curl is a command line equivalent of downloading a file via your web browser. The -O argument tells the command to create an output file with the basename of the URL; in this case autoconf-2.69.tar.gz.

The tar command is the command line equivalent of uncompressing an archive file. The argument xvfz means extract, verbose, file, unzip.

To test that the build has been successful you can run (WARNING: on my MacBook Air this took quite some time. You can skip this step):

To install the results of the build you type:

This command will prompt you for your password. Before you type it you should understand what is going on here. Your computer has a concept of a superuser (a.k.a. root). The sudo command executes whatever follows it as if it were superuser. The make install command needs to be executes as root because it will install programs in a global directory (in this case /usr/local).

If you want to test that your install has been successful you can type (WARNING: on my MacBook Air this took quite some time. You can skip this step):

To return to the top-level idz_build directory type:

To make sure your system is using your newly build autoconf type:

It is quite possible that your system may not find you newly installed version or no version may exist.

To correct this problem type the following commands:

The echo command simply prints its arguments, items in single quotes are printed explicitly, but the >> operator causes the output to appended to a file, in this case, a special, hidden file called .profile. The .profile file is used to set variables for your command line terminal (shell). The PATH special environment variable is used to tell the shell where to look for executable commands. The export command makes the changes to PATH globally available. The assignment PATH=/usr/local/bin:$PATH prepends /usr/local/bin to the search path /usr/local/bin first when looking for commands to execute.

Building automake

In the last section, to explain the steps involved in compiling a GNU tool, I suggested typing commands one by one. To repeat this process would be tedious, therefore I suggest gathering these commands into a shell script. A shell script is a sequence of commands gathered into a file.

Let’s review the basic steps:

So essentially all we need to do supply suitable values for the variables, that is, the values prefixed with the $ sign.

It turns out that these values can be easily derived from the program name and version.

Putting all this together we can create the following minimal script:

In this script that #!/bin/bash is just some magic to make a script run as a program and the $1 and $2 refer to the first and second arguments given to the program. So, executed in the correct directory, idz_gnu_tool automake 1.12 should download, build and install automake version 1.12.

This is a minimal script because it does not error checking. The following is a more complete script:

This is the first of a number of scripts you will be creating to avoid repetitive tasks. Create a directory to hold scripts and change into it:

You can now either download and decompress the above script:

or if you prefer copy and paste the script using your favorite text editor. If you do not have a favorite text editor the following will create and empty file and open it in the TextEdit app:

The script file needs to be made executable to allow it to run. Issue the following command:

This command adds (+) execute permission for the user that owns the file.

Finally you need to make sure that shell can find this file. Modify the last lines of your ~/.profile as follows:

Line 1 creates a new environment variable IDZ_BUILD_ROOT which you will use in later scripts. Line 2 is a modification of the change you made after compiling autoconf that adds you new script directory to places the shell searches for executables.

If all has gone well you should now be able to type the following commands:

This will download, build and install automake.

Compiling libtool

Compiling libtool is now trivial:

Verifying Tool Versions

You can check all the tools are successfully installed and that the shell is finding the correct versions by typing:

You will need all of these tools to be correctly installed before continuing to the next tutorial “Compiling libogg for iOS”.


About idz

A professional software engineer dabbling in iOS app development.
This entry was posted in Tutorial and tagged , , , , . Bookmark the permalink.

5 Responses to Tutorial: Open Source on iOS (Part 1): Build Your Tools

  1. Pingback: Tutorial: Open Source on iOS (Part 2): Compiling libogg on iOS | iOS Developer Zone

  2. Pingback: Google Speech Recognition on iOS | DEVCHAPEL

  3. xooorx says:

    Typo:
    idz_gnu_build automate 1.12 <— should say automake?

  4. Pingback: Attack the block! (iOS AIR native extensions) « Pavel Langweil's blog

Leave a Reply