gcc-4.7.1-native-mac-osx-lion Log in to Edit

Building gcc-4.7.1 with Ada on Mac OSX Lion

  • Author: Warren Gay
  • Date: July 25, 2012, updated September 3, 2012
  • OS: Mac OS X, Lion

Before you can build a cross compiler for AVR-Ada, you first need a native Ada compiler for your platform. This page contains notes for building a native gcc-4.7.1 with Ada support on the Mac OS X Lion platform.

The Ada compiler within the gcc collection requires an Ada compiler to build it. So this puts you into a catch-22 situation. There are two general solutions for this for the Mac:

  • Use the adacore compiler built for Mac OS X (libre.adacore.com)
  • Use a 3rd party built compiler

AdaCore.com Compiler

Initially I tried using the AdaCore.com compiler. The binary package installs nicely and seems to work. But when trying to build another compiler for the Mac, I ran into problems.

The first problem was that configure couldn't determine the size of a long long type. This is overcome by editing the configuration script to remove the -fkeep-inline-functions compiler option. But even after that fix, you will later encounter linking errors at a later stage of the build along these lines:

Undefined symbols for architecture x86_64:
  "___gnat_rcheck_CE_Explicit_Raise", referenced from:
      _ada__exceptions__exception_message in a-except.o
      _ada__exceptions__exception_name__2 in a-except.o
      _ada__exceptions__exception_data__append_info_exception_nameXn in a-except.o
      ___gnat_append_info_e_msg in a-except.o
      _ada__exceptions__exception_information in a-except.o
      _par__prag.3204 in par.o
      _system__storage_elements__Omod in s-stoele.o
      ...
  "___gnat_rcheck_SE_Explicit_Raise", referenced from:
      _alfa__alfa_xref_table__reallocate in alfa.o
      _alfa__alfa_scope_table__reallocate in alfa.o
      _alfa__alfa_file_table__reallocate in alfa.o
      _prep__symbol_table__reallocate in prep.o
      _scos__sco_table__reallocate in scos.o
      _scos__sco_unit_table__reallocate in scos.o
      _sinput__alloc_line_tables in sinput.o
      ...
  "___gnat_rcheck_CE_Overflow_Check", referenced from:
      _ali__scan_ali in ali.o
      _osint__add_file in osint.o
      _restrict__check_restriction__update_restrictions.4149 in restrict.o
      _targparm__get_target_parameters in targparm.o===== Headline =====

  "___gnat_rcheck_PE_Explicit_Raise", referenced from:
      _atree__paren_count in atree.o
      _checks__build_discriminant_checks__aggregate_discriminant_val.5976 in checks.o
      _checks__check_needed in checks.o
      _checks__null_exclusion_static_checks in checks.o
      _einfo__set_invariant_procedure in einfo.o
      _einfo__set_predicate_function in einfo.o
      _einfo__subtype_kind in einfo.o
      ...
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

If you solve that mystery, you might succeed. I decided to move on.

3rd Party Bootstrap

In my google travels, I chanced upon this page:

There some issues were identified and discussed. But the main nugget of information is this: a binary distribution of a 4.6.0 gnat compiler is found here:

Download and install this set of binaries into the directory:

  • /opt/gcc-4.6.0-x86_64

Once you set your PATH variable, you can use this Ada compiler as a bootstrap.

Prerequisites

The gcc compiler obviously has several dependencies. The major requirements are listed below. This section is borrowed from gcc-4.7.1's own documentation included with the compiler (and abbreviated):

  • ISO C90 compiler
  • /bin/sh, /bin/ksh or /bin/bash (not zsh)
  • awk or gawk
  • GNU binutils
  • gzip version 1.2.4 or later, or bzip2 version 1.0.2 or later
  • GNU make version 3.80 or later (it must be GNU make).
  • GNU tar version 1.14 or later
  • Perl version 5.6.1 or later.
  • zip and unzip - Necessary to build libgcj, the GCJ runtime.
  • GMP - GNU Multiple Precision Library (GMP) version 4.3.2 or later
  • MPFR - MPFR Library version 2.4.2 or later
  • Parma Polyhedra Library (PPL) version 0.11 - Necessary to build GCC with the Graphite loop optimizations.

There are also several tools including flex and bison, which you'll need if you plan on modifying the compiler (see the gcc documentation).

The gcc-4.7.1 requirements were all satisfied in this build attempt using the mac ports collection. These are the gcc related mac ports that I used for this build:

  • autoconf @2.69_0 (active)
  • automake @1.12.2_0 (active)
  • bison @2.5.1_0+universal (active)
  • bzip2 @1.0.6_0+universal (active)
  • coreutils @8.17_0+universal (active)
  • dejagnu @1.4.4_0+universal (active)
  • flex @2.5.35_1+universal (active)
  • gmake @3.82_0+universal (active)
  • gmp @5.0.4_1+universal (active)
  • gnutls @2.12.19_0+universal (active)
  • gzip @1.4_0+universal (active)
  • libmpc @0.9_0+universal (active)
  • libtool @2.4.2_2+universal (active)
  • m4 @1.4.16_0+universal (active)
  • makedepend @1.0.4_0+universal (active)
  • mpc @0.22_0+universal (active)
  • mpfr @3.1.0-p3_1+universal (active)
  • perl5.12 @5.12.4_1+universal (active)
  • zlib @1.2.7_0+universal (active)

The version of iconv that I had installed, which was problematic, was this one:

  • libiconv @1.14_0+universal (active)

iconv & libintl Work-around

Before you can start building your compiler, there is some hacking you have to do in order to build gcc-4.7.1. There is some problem with using the mac ports version of iconv and libintl.

So what you need to do is to rename both the header files and the libraries, so that the Mac OSX libraries are used instead. Failure to move the header files out of the way, will seem to work but lead to problems much later in the build.

$ su -
# cd /opt/local/include
# mv iconv.h    was.iconv.h
# mv libintl.h  was.libintl.h

# cd /opt/local/lib
# mv libiconv.2.dylib was.libiconv.2.dylib
# mv libiconv.a       was.libiconv.a
# mv libiconv.dylib   was.libiconv.dylib
# mv libiconv.la      was.libiconv.la
# mv libintl.8.dylib  was.libintl.8.dylib
# mv libintl.a        was.libintl.a
# mv libintl.dylib    was.libintl.dylib
# mv libintl.la       was.libintl.la

On Mac OSX Lion, there is no equivalent. When I scanned my system, I only
came up with these files:

/Library/Frameworks/Mono.framework/Versions/2.10.5/lib/libintl.8.dylib
/opt/local/include/libintl.h
/opt/local/lib/libintl.8.dylib
/opt/local/lib/libintl.a
/opt/local/lib/libintl.la
/opt/local/share/gettext/intl/libintl.rc

The idea is to hide the presence of libintl and libiconv from the
compiler configuration step.

./gcc-4.7.1

Change to the gcc source directory and perform the following:

$ mkdir obj
$ cd ./obj

./obj/setPath

Getting your environment correct is crucial. I prefer to establish this in a text file, so that I can easily redo it later (if necessary).

$ cat setPath
export PATH="/opt/gcc-4.6.0-x86_64/bin:/opt/local/sbin:/opt/local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin"
export LDFLAGS="-L/opt/local/lib -L/usr/local/lib"

I probably should have included a CFLAGS variable to point to the /opt/local/include header files. Yet, the configure script seemed to find them ok, so this was not an issue with this build (on a failed build, it did find /usr/local/include/iconv.h for example).

Then make it so, with the command:

$ . ./setPath

./obj/conf

Likewise, the configuration is complex enough that I like to spell it all out in a script before hand. Once again, if you need to do this more than once, it helps to guarantee that all the options were specified as required.

$ cat conf
set -x
../configure \
    --prefix=/opt/gcc-4.7.1 \
    --enable-languages=ada,c,c++,fortran,java,objc,obj-c++ \
    --disable-multilib \
    --with-gmp=/opt/local \
    --with-mpfr=/opt/local \
    --with-mpc=/opt/local \
    --build=x86_64-apple-darwin11   
set +x
~~~~~~

  * In the above, if you are only concerned about an Ada compiler, abbreviate the language list to ada,c,c++
  * Due to the placement of the Mac ports libraries, don't leave out the –with-gmp= etc. options

## Restarting a Build ##

If you've had one or more failed builds, you should clean up after it before beginning a new one:

$ cd ./obj
$ sudo make distclean # I M P O R T A N T
$ rm -fr ./stage_final # see below

  * The make distclean eliminates most of the fluff, but you may still have left overs. Look for them and eliminate them.
  * You may need to run 'make distclean' as root, if you have done a 'make install' as root (some objects will be owned by root).

## Configure gcc ##

Reminder: have you done a configure before?  Be sure to do a 'make distclean' as outlined in the previous section. Otherwise, some configuration may linger from a prior attempt.

From your ./obj directory, invoke the configure script as follows:

$ . ./conf

  * Do not build the compiler as root!
  * Here I destined the compiler for /opt/gcc-4.7.1.
  * If you downloaded the binary bootstrap compiler above, it will be found in /opt/gcc-4.6.0-x86_64

If you have an error message telling you to see config.log, then keep in mind that there is more than one of these. For example, the message you may be interested in may actually be in ./gcc/config.log instead of ./config.log.

## Build ##

If the configure succeeded (didn't complain), then you should be able to do the following (NOT as root):

$ cd ./obj
$ make 2>&1 | tee make.out

The tee command lets you see what is happening, while capturing all the messages flying by to log make.out. On my MacBook Pro with it's 2.8 Ghz Intel Core i7 processor (4GB 1333 MHz RAM), the build for all languages (as listed) takes 1 hour and 37 minutes to complete. If you leave out all but Ada,c,c++, the time required shrinks considerably.

## Install ##

If the compile completed with no errors, then you can install it:

$ sudo make install

In my configuration, this installed it all under /opt/gcc-4.7.1.

## Final Loose Ends ##

Don't forget to undo the changes you made to your mac ports libraries:

$ su -

cd /opt/include

mv was.iconv.h iconv.h

mv was.libintl.h libintl.h

cd /opt/local/lib

mv was.libiconv.2.dylib libiconv.2.dylib

mv was.libiconv.a libiconv.a

mv was.libiconv.dylib libiconv.dylib

mv was.libiconv.la libiconv.la

mv was.libintl.8.dylib libintl.8.dylib

mv was.libintl.a libintl.a

mv was.libintl.dylib libintl.dylib

mv was.libintl.la libintl.la

~~~~~