Thursday, September 9, 2010

Building gfortran from Source Code on Mac OS X 10.6

The previous post outlined how to install gfortran on Mac OS X 10.6 using a binary installation. The fact that gfortran is a part of the open source gcc project means that building your own binary files directly from the source code is an option. This option is nice to have, especially since the gfortran developer community is continuously working to fix bug reports and add new features to the compiler.

When I say gcc, I am refering to the GNU Compiler Collection (http://gcc.gnu.org/), which is a collection of various types of compilers. The project currently, "includes front ends for C, C++, Objective-C, Fortran, Java, and Ada." The Fortran compiler of the gcc project is gfortran, which we will be installing, and which previous posts have refered to. Keep in mind that gfortran is just a part of gcc. We will selectively only install gfortran at this time.

Downloading the Source Code

Source code for the gcc project is provided in two ways:

  • Subversion (SVN) - A command line tool that automatically downloads the current source code
  • Tar-ball (.tar.gz) - a compressed file that contains a snapshot of the source code from a particular time in development

The gcc project provides snapshots on a weekly basis. These snapshots are working versions of the compilers, thus, they are considered experimental. Stable releases are also provided. These releases come every couple of months, so while they might be considered more stable, they might not have the current additions or bug-fixes that the development releases might have.

The tar-ball for either a stable release or a snapshot release can be downloaded from: http://gcc.gnu.org/mirrors.html Select any mirror site from the list provided. Some of the mirrors are more up to date than others, so keep that in mind if a particular version of gcc/gfortran is not available on the site you are browsing.

I am currently using Firefox to browse the ftp site of the mirror I chose from the list. I am not sure if other browsers can view ftp sites. You should see a list of folders on the mirror site:

  • infrastructure
  • libstdc++
  • old-releases
  • releases
  • snapshots
  • summit

We are interested in the releases and snapshots folders. The releases folder contains complete releases of gcc, major or minor versions. The snapshots folder contains snapshots of the source code in current development. For more information on the weekly snapshots, see: http://gcc.gnu.org/snapshots.html

For this example, we will go through the exercise of building a release of gfortran 4.5.1. Navigate to the releases folder, navigate down to the gcc-4.5.1 folder. Within this folder, we are going to download the tar-ball file: gcc-4.5.1.tar.gz, which is roughly 84MB.

Install Apple Developer Tools

If not already done, install the Apple Developer Tools either from the installation disc that came with your computer or download a .dmg file from Apple's website: http://developer.apple.com/technologies/xcode.html

Building the Source Code

There are limited instructions provided on the gcc wiki: http://gcc.gnu.org/wiki/GFortranSource. I will go through the installation instructions step-by-step below for building gfortran on Mac OS X 10.6.

Let's say that we downloaded gcc-4.5.1.tar.gz to the Desktop. On the Mac, double click on the downloaded file to unpack its contents. Unpacking should produce a folder named gcc-4.5.1. Before doing anything else, create a new, empty folder on the Desktop named gfortran-4.5.1-build, for example. It is important that this folder should be at the same "level" as the unpacked folder that contains the gcc source code. Our newly created gfortran-4.5.1-build folder will be our working directory. Launch Terminal to get started and enter the following on the command line:

$ cd desktop/gfortran-4.5.1-build
$ ../gcc-4.5.1/configure --prefix=$HOME/gfortran-4.5.1 --enable-languages=fortran --enable-checking=release --disable-bootstrap

A program will now run within Terminal that will configure a makefile. In all honesty, I am not entirely sure what the above options do, but here is a description of each:

--prefix=$HOME/gfortran-4.5.1

This option ensures that gfortran will be installed in the directory gfortran-4.5.1 at the location /users/userName on your hard drive (your home folder). You could specify any directory or location for the install.

--enable-languages=fortran

Fortran is the only language from gcc that we wish to install. If this option was omitted, all the compilers from the gcc project would be installed.

--enable-checking=release

From the gfortran wiki, “esp. useful for compile-time tests as it disables some consistency checks in the compile.” Not required.

--disable-bootstrap

From the gfortran wiki, “builds faster, but disables some consistency checks.” Not required.

Before moving on to the next step, enter the following command in Terminal, your result should be the same as mine, assuming the Apple Developer Tools were installed properly:

$ which gcc
/usr/bin/gcc

With the configure process completed, it is time to start building the source code. Please keep in mind that this process might take upwards of an hour and will take a lot of CPU resources. To begin, enter the following in Terminal:

$ make CC=/usr/bin/gcc

When the process finally finishes, hopefully without error, issue the following command in terminal:

$ make install

This final step completes the installation. The install directory was specified as “$HOME/gfortran-4.5.1”, thus, that is where the gfortran installation will be located. The folder gfortran-4.5.1 should contain the following folders:

  • bin
  • include
  • lib
  • libexec
  • share

Updating the PATH

The folder gfortran-4.5.1 can now be moved to someplace else on the hard drive (for example, /usr/local). Since /usr/local is a hidden folder, it can be opened in Finder with the following Terminal command:

$ open -a finder /usr/local

If the folder was copied to /usr/local, the PATH would have to be updated with the following:

sudo pico /etc/paths

Update the file with the following entry:

/usr/local/gfortran-4.5.1/bin

Close and reopen the terminal for the PATH changes to take effect. The above folder is the location of the gfortran executable that we have just built. It might also be a good idea to rename the gfortran executable with a version number in the name (example: gfortran-4.5.1) just in case there are multiple versions of gfortran in the PATH. Test that the newly built gfortran is working properly:

$ gfortran
gfortran: no input files
$ gfortran --version
GNU Fortran (GCC) 4.5.1 (stable)
Copyright (C) 2009 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING

Test your newly built gfortran by building some Fortran code of your own. See the post entitled "Hello!" for a quick, simple example.

3 comments:

  1. What does this line mean from the fortran --version command

    gfortran: warning: couldn’t understand kern.osversion ‘14.0.0

    ReplyDelete
  2. Hi, I have gcc installed on my machine. I do not want to disturb it (not personal machine) and want gfortran to be enabled which is currently not present. So, how can I just download gfortran source only and build it so that I can use gfortran command?
    Please guide. (gcc version 4.6.1)

    ReplyDelete
  3. Online gambling and sportsbook review - drmcd
    Our Online Gambling and Sportsbook review finds out 경산 출장마사지 all about online gambling in our 2021 NJ gambling experience. Read about the casino's 포천 출장안마 bonuses, betting 보령 출장마사지 options and more. Rating: 4 태백 출장마사지 · ‎Review 김해 출장샵 by Bill Grinstead

    ReplyDelete