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