Changes between Initial Version and Version 1 of UsingTheRightCompiler


Ignore:
Timestamp:
Jun 4, 2009, 5:20:05 AM (15 years ago)
Author:
ryandesign (Ryan Carsten Schmidt)
Comment:

new page explaining the rationale behind ${configure.compiler}

Legend:

Unmodified
Added
Removed
Modified
  • UsingTheRightCompiler

    v1 v1  
     1= Introduction = #introduction
     2
     3Most software is built using a C compiler called gcc,
     4and the various versions of Xcode for Mac OS X come with different versions of gcc.
     5For example, with Xcode 2.5 on Mac OS X 10.4, gcc 3.3 and 4.0 are available, with 4.0 being the default.
     6With Xcode 3.1 on Mac OS X 10.5, gcc 4.2 is also available;
     7this might become the default in a future version of Mac OS X.
     8
     9
     10= The problem with the default compiler = #default-compiler
     11
     12The default version of gcc can be run simply as "`gcc`".
     13But this default can be changed using the "`gcc_select`" command.
     14Some users may have used this command to change their default gcc,
     15for example on Leopard to test new functionality in gcc 4.2,
     16or on Tiger to downgrade to gcc 3.3 to compile old software that is not compatible with gcc 4.
     17
     18Software by default builds using "`gcc`" and this can be a problem if the user has changed what "`gcc`" is.
     19On the one hand, gcc 4 may be too new to compile some very old software,
     20but on the other hand gcc 3.3 is probably too old to compile a lot of modern software.
     21The problem is further complicated by the various gcc ports which can be installed using MacPorts,
     22which have different capabilities than the Apple versions of gcc.
     23For example, only the Apple versions can create universal binaries in a single step.
     24The gcc_select port can be used to make any of them the default compiler.
     25So we cannot rely on "`gcc`" being any particular version of gcc with any particular capabilities.
     26
     27Most port authors will not have used "`gcc_select`"
     28and will therefore have the usual default version of gcc on their machine,
     29and will not have tested to see what happens if a different gcc is selected.
     30To remove this testing burden from maintainers,
     31and to prevent users from running into unanticipated problems,
     32MacPorts arranges for ports to always compile using the correct default version for the current operating system,
     33and does not use the unpredictable "`gcc`".
     34It does this by specifying the desired compiler's complete path in the CC environment variable
     35during the port's configure phase.
     36For example, on Mac OS X 10.4 and 10.5, CC is set to the value "`/usr/bin/gcc-4.0`"
     37so that there is no ambiguity.
     38
     39In fact there are more variables than just CC:
     40there's also CXX for the C++ compiler and CPP for the C pre-processor.
     41During the configure phase,
     42MacPorts sets each of these variables to the right value for the user's OS.
     43It does this through the use of a number of similarly-named Tcl variables:
     44${configure.cc}, ${configure.cxx} and ${configure.cpp}.
     45
     46
     47= Selecting a different compiler = #configure-compiler
     48
     49For most ports,
     50the default compiler chosen by MacPorts is the one that should be used,
     51unless that compiler doesn't work with that port for some reason.
     52In that case,
     53you can select a different compiler for that port by overwriting ${configure.cc} and friends,
     54but what you probably want to do instead is overwrite the ${configure.compiler} variable,
     55which sets everything for you as a group.
     56MacPorts knows about a handful of compilers:
     57
     58 * gcc
     59 * gcc-3.3
     60 * gcc-4.0
     61 * gcc-4.2
     62 * llvm-gcc-4.2
     63 * clang
     64 * apple-gcc-3.3
     65 * apple-gcc-4.0
     66 * apple-gcc-4.2
     67 * macports-gcc-3.3
     68 * macports-gcc-3.4
     69 * macports-gcc-4.0
     70 * macports-gcc-4.1
     71 * macports-gcc-4.2
     72 * macports-gcc-4.3
     73 * macports-gcc-4.4
     74
     75Compiler names beginning with "macports" use ports in MacPorts
     76(e.g. "macports-gcc-4.2" corresponds to the gcc42 port).
     77So do those whose names begin with "apple"
     78(e.g. "apple-gcc-4.0" corresponds to the apple-gcc40 port).
     79The remaining compiler names refer to compilers installed by Xcode
     80(e.g. "gcc-4.0" is the gcc 4.0.1 compiler installed by Xcode
     81which is the default compiler on Mac OS X 10.4 and 10.5).
     82Note that if you set ${configure.compiler} to a compiler provided by a MacPorts port,
     83you must also declare a build dependency on that port.
     84
     85For the MacPorts gcc 4 compilers,
     86additional environment variables FC, F77 and F90 are set to the path of the Fortran compiler.
     87These variables are not set for the other compilers because they do not include a Fortran compiler.
     88
     89
     90= Ports with nonstandard or nonexistent configure scripts = #nonstandard-ports
     91
     92Setting the CC, CXX and CPP environment variables at configure time
     93is all that most software needs in order to use the compiler we want.
     94But some ports have unusual configure scripts that don't obey these settings.
     95Some ports don't have a configure phase at all.
     96For such ports, it can be necessary to set the variables at build time:
     97
     98{{{
     99build.env-append        CC=${configure.cc} \
     100                        CXX=${configure.cxx} \
     101                        CPP=${configure.cpp}
     102}}}
     103
     104Some ports' Makefiles do not use the CC variable and always try to run "`gcc`" or "`cc`".
     105In these cases, patches are needed.
     106For example, "`gcc`" or "`cc`" can be replaced with "$(CC)" in the Makefile,
     107possibly in combination with setting ${build.env} as above.
     108Such patches should usually be sent upstream for inclusion in the next version of the software.