| 162 | |
| 163 | == Providing compiler variants == #gcc |
| 164 | |
| 165 | By default, a port will compile using Apple's gcc compiler. |
| 166 | For most ports this is fine, but some require a newer version of gcc, or are for some reason incompatible with Apple's version. |
| 167 | In these cases you can use `configure.compiler` to specify an alternate compiler, for example one provided by a MacPorts gcc port. |
| 168 | More commonly, a port specifies such a compiler because it needs gcj or gfortran, which Apple does not provide any version of at all. |
| 169 | |
| 170 | On the one hand, such ports should prefer to use the newest suitable stable version of gcc they can. |
| 171 | On the other hand, a user may already have an older gcc port installed and may not want to spend the time to compile a newer one right now, or may have other reasons for preferring a particular version of gcc. |
| 172 | Therefore, ports that need to use a gcc port, but aren't picky about exactly which one, are encouraged to offer variants: |
| 173 | |
| 174 | {{{ |
| 175 | variant gcc42 conflicts gcc43 gcc44 description {Compile with gcc42} { |
| 176 | configure.compiler macports-gcc-4.2 |
| 177 | depends_lib-append port:gcc42 |
| 178 | } |
| 179 | |
| 180 | variant gcc43 conflicts gcc42 gcc44 description {Compile with gcc43} { |
| 181 | configure.compiler macports-gcc-4.3 |
| 182 | depends_lib-append port:gcc43 |
| 183 | } |
| 184 | |
| 185 | variant gcc44 conflicts gcc42 gcc43 description {Compile with gcc44} { |
| 186 | configure.compiler macports-gcc-4.4 |
| 187 | depends_lib-append port:gcc44 |
| 188 | } |
| 189 | |
| 190 | if {![variant_isset gcc42] && ![variant_isset gcc43] && ![variant_isset gcc44]} { |
| 191 | default_variants +gcc44 |
| 192 | } |
| 193 | }}} |
| 194 | |
| 195 | Note that the variants are all marked as conflicting with one another, and that the newest one is chosen by default if the user has not picked one. |
| 196 | Note also that the compiler dependencies are library dependencies because programs compiled using these compilers will generally end up linked to at least one of the compiler's libraries (i.e. libgcc_s.dylib, libgfortran.dylib, etc.). |
| 197 | |
| 198 | Setting `configure.compiler` changes the values MacPorts puts in variables like ${configure.cc}, ${configure.cxx}, ${configure.f77}, etc., which MacPorts automatically sets as environment variables during the configure phase. |
| 199 | If the software in question doesn't use the configure phase, and you therefore need to pass these variables to the build phase, you must do so in a pre-build block; |
| 200 | if you try to do so directly in the portfile body, you'll pick up the original values, before the variant changed them. |
| 201 | |
| 202 | {{{ |
| 203 | pre-build { |
| 204 | build.args CC=${configure.cc} \ |
| 205 | CXX=${configure.cxx} |
| 206 | } |
| 207 | }}} |
| 208 | |
| 209 | Another reason to want compiler variants is if the software installs a library, or uses a library built with a different gcc. |
| 210 | Subtle and difficult-to-find errors can occur if a library and the program using it are not both compiled with the same compiler. |