Add files from parent branch HEAD:
[pkgsrc.git] / mk / compiler.mk
1 # $NetBSD: compiler.mk,v 1.67 2008/10/20 18:33:50 ahoka Exp $
2 #
3 # This Makefile fragment implements handling for supported C/C++/Fortran
4 # compilers.
5 #
6 # The following variables may be set by the pkgsrc user in mk.conf:
7 #
8 # PKGSRC_COMPILER
9 #       A list of values specifying the chain of compilers to be used by
10 #       pkgsrc to build packages.
11 #
12 #       Valid values are:
13 #               ccc             Compaq C Compilers (Tru64)
14 #               ccache          compiler cache (chainable)
15 #               distcc          distributed C/C++ (chainable)
16 #               f2c             Fortran 77 to C compiler (chainable)
17 #               icc             Intel C++ Compiler (Linux)
18 #               ido             SGI IRIS Development Option cc (IRIX 5)
19 #               gcc             GNU
20 #               hp              HP-UX C/aC++ compilers
21 #               mipspro         Silicon Graphics, Inc. MIPSpro (n32/n64)
22 #               mipspro-ucode   Silicon Graphics, Inc. MIPSpro (o32)
23 #               pcc             Portable C Compiler
24 #               sunpro          Sun Microsystems, Inc. WorkShip/Forte/Sun
25 #                               ONE Studio
26 #               xlc             IBM's XL C/C++ compiler suite (Darwin/MacOSX)
27 #
28 #       The default is "gcc".  You can use ccache and/or distcc with
29 #       an appropriate PKGSRC_COMPILER setting, e.g. "ccache distcc
30 #       gcc".  You can also use "f2c" to overlay the lang/f2c package
31 #       over the C compiler instead of using the system Fortran
32 #       compiler.  The chain should always end in a real compiler.
33 #       This should only be set in /etc/mk.conf.
34 #
35 # USE_PKGSRC_GCC
36 #       Force using the appropriate version of GCC from pkgsrc based on
37 #       GCC_REQD instead of the native compiler.
38 #
39 # The following variables may be set by a package:
40 #
41 # GCC_REQD
42 #       A list of version numbers used to determine the minimum
43 #       version of GCC required by a package.  This value should only
44 #       be appended to by a package Makefile.
45 #
46 #       NOTE: Be conservative when setting GCC_REQD, as lang/gcc3 is
47 #       known not to build on some platforms, e.g. Darwin.  If gcc3 is
48 #       required, set GCC_REQD=3.0 so that we do not try to pull in
49 #       lang/gcc3 unnecessarily and have it fail.
50 #
51 # USE_LANGUAGES
52 #       Lists the languages used in the source code of the package,
53 #       and is used to determine the correct compilers to install.
54 #       Valid values are: c, c99, c++, fortran, java, objc.  The
55 #       default is "c".
56 #
57 # The following variables are defined, and available for testing in
58 # package Makefiles:
59 #
60 # CC_VERSION
61 #       The compiler and version being used, e.g.,
62 #
63 #       .include "../../mk/compiler.mk"
64 #
65 #       .if !empty(CC_VERSION:Mgcc-3*)
66 #       ...
67 #       .endif
68 #
69 # Keywords: compiler
70
71 .if !defined(BSD_COMPILER_MK)
72 BSD_COMPILER_MK=        defined
73
74 _VARGROUPS+=            compiler
75 _USER_VARS.compiler=    PKGSRC_COMPILER USE_PKGSRC_GCC ABI
76 _PKG_VARS.compiler=     USE_LANGUAGES GCC_REQD NOT_FOR_COMPILER ONLY_FOR_COMPILER
77 _SYS_VARS.compiler=     CC_VERSION
78
79 .include "bsd.fast.prefs.mk"
80
81 # Since most packages need a C compiler, this is the default value.
82 USE_LANGUAGES?= c
83
84 # Add c support if c99 is set
85 .if !empty(USE_LANGUAGES:Mc99)
86 USE_LANGUAGES+= c
87 .endif
88
89 # For environments where there is an external gcc too, but pkgsrc
90 # should use the pkgsrc one for consistency.
91 #
92 .if defined(USE_PKGSRC_GCC)
93 _USE_PKGSRC_GCC=        yes
94 .endif
95
96 _COMPILERS=             ccc gcc icc ido mipspro mipspro-ucode sunpro xlc hp pcc
97 _PSEUDO_COMPILERS=      ccache distcc f2c
98
99 .if defined(NOT_FOR_COMPILER) && !empty(NOT_FOR_COMPILER)
100 .  for _compiler_ in ${_COMPILERS}
101 .    if ${NOT_FOR_COMPILER:M${_compiler_}} == ""
102 _ACCEPTABLE_COMPILERS+= ${_compiler_}
103 .    endif
104 .  endfor
105 .elif defined(ONLY_FOR_COMPILER) && !empty(ONLY_FOR_COMPILER)
106 .  for _compiler_ in ${_COMPILERS}
107 .    if ${ONLY_FOR_COMPILER:M${_compiler_}} != ""
108 _ACCEPTABLE_COMPILERS+= ${_compiler_}
109 .    endif
110 .  endfor
111 .else
112 _ACCEPTABLE_COMPILERS+= ${_COMPILERS}
113 .endif
114
115 .if defined(_ACCEPTABLE_COMPILERS)
116 .  for _acceptable_ in ${_ACCEPTABLE_COMPILERS}
117 .    for _compiler_ in ${PKGSRC_COMPILER}
118 .      if !empty(_ACCEPTABLE_COMPILERS:M${_compiler_}) && !defined(_COMPILER)
119 _COMPILER=      ${_compiler_}
120 .      endif
121 .    endfor
122 .  endfor
123 .endif
124
125 .if !defined(_COMPILER)
126 PKG_FAIL_REASON+=       "No acceptable compiler found for ${PKGNAME}."
127 .endif
128
129 .for _compiler_ in ${PKGSRC_COMPILER}
130 .  if !empty(_PSEUDO_COMPILERS:M${_compiler_})
131 _PKGSRC_COMPILER:=      ${_compiler_} ${_PKGSRC_COMPILER}
132 .  endif
133 .endfor
134 _PKGSRC_COMPILER:=      ${_COMPILER} ${_PKGSRC_COMPILER}
135
136 _COMPILER_STRIP_VARS=   # empty
137
138 .for _compiler_ in ${_PKGSRC_COMPILER}
139 .  include "compiler/${_compiler_}.mk"
140 .endfor
141 .undef _compiler_
142
143 .if !defined(PKG_CPP)
144 PKG_CPP:=${CPP}
145 .endif
146
147 # Strip the leading paths from the toolchain variables since we manipulate
148 # the PATH to use the correct executable.
149 #
150 .for _var_ in ${_COMPILER_STRIP_VARS}
151 .  if empty(${_var_}:C/^/_asdf_/1:N_asdf_*)
152 ${_var_}:=      ${${_var_}:C/^/_asdf_/1:M_asdf_*:S/^_asdf_//:T}
153 .  else
154 ${_var_}:=      ${${_var_}:C/^/_asdf_/1:M_asdf_*:S/^_asdf_//:T} ${${_var_}:C/^/_asdf_/1:N_asdf_*}
155 .  endif
156 .endfor
157
158 .if defined(ABI) && !empty(ABI)
159 _WRAP_EXTRA_ARGS.CC+=   ${_COMPILER_ABI_FLAG.${ABI}}
160 _WRAP_EXTRA_ARGS.CXX+=  ${_COMPILER_ABI_FLAG.${ABI}}
161 _WRAP_EXTRA_ARGS.LD+=   ${_LINKER_ABI_FLAG.${ABI}}
162 .endif
163
164 # If the languages are not requested, force them not to be available
165 # in the generated wrappers.
166 #
167 _FAIL_WRAPPER.CC=       ${WRKDIR}/.compiler/bin/c-fail-wrapper
168 _FAIL_WRAPPER.CXX=      ${WRKDIR}/.compiler/bin/c++-fail-wrapper
169 _FAIL_WRAPPER.FC=       ${WRKDIR}/.compiler/bin/fortran-fail-wrapper
170
171 ${_FAIL_WRAPPER.CC}: fail-wrapper
172 ${_FAIL_WRAPPER.CXX}: fail-wrapper
173 ${_FAIL_WRAPPER.FC}: fail-wrapper
174
175 .PHONY: fail-wrapper
176 fail-wrapper: .USE
177         ${RUN}${MKDIR} ${.TARGET:H}
178         ${RUN}                                  \
179         exec 1>${.TARGET};                                              \
180         ${ECHO} '#!'${TOOLS_SHELL:Q};                                   \
181         ${ECHO} 'wrapperlog="$${TOOLS_WRAPPER_LOG-'${_TOOLS_WRAP_LOG:Q}'}"'; \
182         ${ECHO} 'lang="${.TARGET:T:S/-fail-wrapper//}"'; \
183         ${ECHO} 'msg="*** Please consider adding $$lang to USE_LANGUAGES in the package Makefile."'; \
184         ${ECHO} '${ECHO} "$$msg" >> $$wrapperlog'; \
185         ${ECHO} '${ECHO} "$$msg" > ${WARNING_DIR}/${.TARGET:T}'; \
186         ${ECHO} '${ECHO} "PKGSRC-WARNING: Something is trying to run the $$lang compiler," 1>&2'; \
187         ${ECHO} '${ECHO} "PKGSRC-WARNING: but it is not added to USE_LANGUAGES in the package Makefile." 1>&2'; \
188         ${ECHO} 'exit 1'
189         ${RUN}${CHMOD} +x ${.TARGET}
190
191 .if empty(USE_LANGUAGES:Mc) && empty(USE_LANGUAGES:Mobjc)
192 PKG_CC:=                ${_FAIL_WRAPPER.CC}
193 ALL_ENV+=               CPP=${CPP:Q}
194 override-tools: ${_FAIL_WRAPPER.CC}
195 .endif
196 .if empty(USE_LANGUAGES:Mc++)
197 PKG_CXX:=               ${_FAIL_WRAPPER.CXX}
198 ALL_ENV+=               CXXCPP=${CPP:Q} # to make some Autoconf scripts happy
199 override-tools: ${_FAIL_WRAPPER.CXX}
200 .endif
201 .if empty(USE_LANGUAGES:Mfortran)
202 PKG_FC:=                ${_FAIL_WRAPPER.FC}
203 override-tools: ${_FAIL_WRAPPER.FC}
204 .endif
205
206 .endif  # BSD_COMPILER_MK