summaryrefslogtreecommitdiff
path: root/mk/bsd.buildlink.mk
blob: c75d4de7e98a6bcf4f1c86955bfc55f5e41fe0e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# $NetBSD: bsd.buildlink.mk,v 1.45 2001/10/24 19:01:00 jlam Exp $
#
# This Makefile fragment is included by package buildlink.mk files.  This
# file does the following things:
#
# (1) Defines a macro target, _BUILDLINK_USE, that symlinks package files
#     into a new hierarchy under ${BUILDLINK_DIR};
# (2) Defines private variables _BUILDLINK_CPPFLAGS and _BUILDLINK_LDFLAGS
#     containing the magic flags needed to find the buildlink'ed headers
#     files and the buildlink'ed libraries, respectively.
# (3) Prepends ${_BUILDLINK_CPPFLAGS} to CPPFLAGS, CFLAGS, and CXXFLAGS;
# (4) Prepends ${_BUILDLINK_LDFLAGS} to LDFLAGS.
# (5) Defines a macro target, _BUILDLINK_CONFIG_WRAPPER_USE, that generates
#     a wrapper script around GTK+-style config scripts that replaces
#     -I${LOCALBASE}/... and -L${LOCALBASE}/... with references into
#     ${BUILDLINK_DIR}.
#
# The variables required to be defined prior to including this file are
# listed below.  <pkgname> refers to the name of the package and should be
# used consistently throughout the buildlink.mk file.
#
# BUILDLINK_PREFIX.<pkgname>	installation prefix of the package
#
# BUILDLINK_FILES.<pkgname>	files relative to ${BUILDLINK_PREFIX.<pkgname>}
#				to be symlinked into ${BUILDLINK_DIR};
#				libtool archive files are automatically
#				filtered out and not linked
#
# BUILDLINK_TARGETS		targets to be invoked during pre-configure;
#				the targets should be appended to this variable
#				using +=
#
# The variables that may optionally be defined are:
#
# BUILDLINK_TRANSFORM.<pkgname>	sed arguments used to transform the name of
#				the source filename into a destination
#				filename
#
# BUILDLINK_CONFIG.<pkgname>	absolute path to GTK+-style config script
#
# BUILDLINK_CONFIG_WRAPPER.<pkgname>
#				absolute path for generated wrapper script;
#				this should be under ${BUILDLINK_DIR}/bin
#
# Additional variables used by bsd.buildlink.mk that may be defined in
# either the package Makefile or in buildlink.mk files:
#
# REPLACE_BUILDLINK		space-separated list of files on which we
#				run ${SED} ${REPLACE_BUILDLINK_SED}
#
# REPLACE_BUILDLINK_PATTERNS	space-separated list of shell glob patterns
#				representing files on which we run ${SED}
#				${REPLACE_BUILDLINK_SED}
#
# REPLACE_BUILDLINK_SED		sed expression used primarily to replace
#				references to directories in ${BUILDLINK_DIR}
#				into directories in ${LOCALBASE} and
#				${X11PREFIX}
#
# REPLACE_LIBNAMES		space-separated list of files on which we
#				run ${SED} ${REPLACE_LIBNAMES_SED}
#
# REPLACE_LIBNAMES_PATTERNS	space-separated list of shell glob patterns
#				representing files on which we run ${SED}
#				${REPLACE_LIBNAMES_SED}
#
# REPLACE_LIBNAMES_SED		sed expression used primarily to replace
#				references to library names within
#				${BUILDLINK_DIR} into the true library names
#				from the base system.
#
# BUILDLINK_CONFIG_WRAPPER_SED	sed expression used primarily to replace
#				references to directories in ${LOCALBASE}
#				and ${X11PREFIX} into directories in
#				${BUILDLINK_DIR} or ${BUILDLINK_X11_DIR} in
#				the buildlink config wrapper scripts
#
# The targets required to be defined prior to including this file are
# listed below.
#
# pre-configure			this target should have as dependencies any
#				targets to be invoked; this is just usually	#				<pkgname>-buildlink and possibly
#				<pkgname>-buildlink-config-wrapper
#
# <pkgname>-buildlink		this target should just invoke the
#				_BUILDLINK_USE macro target defined in this
#				file
#
# The targets that may optionally be defined are:
#
# <pkgname>-buildlink-config-wrapper
#				this target should just invoke the
#				_BUILDLINK_CONFIG_WRAPPER_USE macro target
#				defined in this file
#
# Example package buildlink.mk file:
#
# .include "../../mk/bsd.buildlink.mk"
#
# BUILDLINK_DEPENDS.foo?=	foo>=1.0
# DEPENDS+=			${BUILDLINK_DEPENDS.foo}:../../category/foo
#
# EVAL_PREFIX+=			BUILDLINK_PREFIX.foo=foo
# BUILDLINK_FILES.foo=		include/foo.h
# BUILDLINK_FILES.foo+=		include/bar.h
# BUILDLINK_FILES.foo+=		lib/libfoo.*
#
# # We need the libraries to be called "libbar.*".
# BUILDLINK_TRANSFORM.foo=	-e "s|libfoo|libbar|g"
#
# BUILDLINK_TARGETS+=		foo-buildlink
#
# pre-configure: foo-buildlink
# foo-buildlink: _BUILDLINK_USE

.if !defined(_BSD_BUILDLINK_MK)
_BSD_BUILDLINK_MK=	# defined

BUILDLINK_DIR?=		${WRKDIR}/.buildlink

.if !defined(_BUILDLINK_CPPFLAGS) || !defined(_BUILDLINK_LDFLAGS)
_BUILDLINK_CPPFLAGS=	-I${BUILDLINK_DIR}/include
_BUILDLINK_LDFLAGS=	-L${BUILDLINK_DIR}/lib

CFLAGS:=		${_BUILDLINK_CPPFLAGS} ${CFLAGS}
CXXFLAGS:=		${_BUILDLINK_CPPFLAGS} ${CXXFLAGS}
CPPFLAGS:=		${_BUILDLINK_CPPFLAGS} ${CPPFLAGS}
LDFLAGS:=		${_BUILDLINK_LDFLAGS} ${LDFLAGS}

CONFIGURE_ENV+=		BUILDLINK_DIR="${BUILDLINK_DIR}"
CONFIGURE_ENV+=		BUILDLINK_CPPFLAGS="${_BUILDLINK_CPPFLAGS}"
CONFIGURE_ENV+=		BUILDLINK_LDFLAGS="${_BUILDLINK_LDFLAGS}"

MAKE_ENV+=		BUILDLINK_DIR="${BUILDLINK_DIR}"
MAKE_ENV+=		BUILDLINK_CPPFLAGS="${_BUILDLINK_CPPFLAGS}"
MAKE_ENV+=		BUILDLINK_LDFLAGS="${_BUILDLINK_LDFLAGS}"
.endif

# Filter out libtool archives from the list of file to link into
# ${BUILDLINK_DIR}.
#
_LIBTOOL_ARCHIVE_FILTER=						\
	${SED}	-e 's|[^[:blank:]]*lib/[^[:blank:]]*.la$$||g'		\
		-e 's|[^[:blank:]]*lib/[^[:blank:]]*.la[[:blank:]]||g'

_BUILDLINK_USE: .USE
	${_PKG_SILENT}${_PKG_DEBUG}					\
	cookie=${BUILDLINK_DIR}/.${.TARGET:S/-buildlink//}_buildlink_done; \
	if [ ! -f $${cookie} ]; then					\
		${ECHO_MSG} "Linking ${.TARGET:S/-buildlink//} files into ${BUILDLINK_DIR}."; \
		${MKDIR} ${BUILDLINK_DIR};				\
		files="${BUILDLINK_FILES.${.TARGET:S/-buildlink//}:S/^/${BUILDLINK_PREFIX.${.TARGET:S/-buildlink//}}\//g}"; \
		files="`${ECHO} $${files} | ${_LIBTOOL_ARCHIVE_FILTER}`"; \
		for file in $${files}; do				\
			rel_file=`${ECHO} $${file} | ${SED} -e "s|${BUILDLINK_PREFIX.${.TARGET:S/-buildlink//}}/||"` ; \
			if [ -z "${BUILDLINK_TRANSFORM.${.TARGET:S/-buildlink//}:Q}" ]; then \
				dest=${BUILDLINK_DIR}/$${rel_file};	\
			else						\
				dest=`${ECHO} ${BUILDLINK_DIR}/$${rel_file} | ${SED} ${BUILDLINK_TRANSFORM.${.TARGET:S/-buildlink//}}`; \
			fi;						\
			if [ -f $${file} ]; then			\
				dir=`${DIRNAME} $${dest}`;		\
				if [ ! -d $${dir} ]; then		\
					${MKDIR} $${dir};		\
				fi;					\
				${RM} -f $${dest};			\
				${LN} -sf $${file} $${dest};		\
				if [ -z "${BUILDLINK_TRANSFORM.${.TARGET:S/-buildlink//}:Q}" ]; then \
					${ECHO} $${file} >> $${cookie};	\
				else					\
					${ECHO} "$${file} -> $${dest}" >> $${cookie}; \
				fi;					\
			else						\
				${ECHO} "$${file}: not found" >> $${cookie}; \
			fi;						\
		done;							\
		${TOUCH} ${TOUCH_FLAGS} $${cookie};			\
	fi

# If a package uses only buildlink.mk files, then automatically set
# USE_CONFIG_WRAPPER to use config script wrappers for packages that
# have them.
#
.if defined(USE_BUILDLINK_ONLY)
USE_CONFIG_WRAPPER=	# defined
.endif

BUILDLINK_CONFIG_WRAPPER_SED?=		# empty
BUILDLINK_CONFIG_WRAPPER_POST_SED+=					\
	-e "s|-I${LOCALBASE}/|-I${BUILDLINK_DIR}/|g"			\
	-e "s|-L${LOCALBASE}/|-L${BUILDLINK_DIR}/|g"

_BUILDLINK_CONFIG_WRAPPER_USE: .USE
	${_PKG_SILENT}${_PKG_DEBUG}					\
	cookie=${BUILDLINK_DIR}/.${.TARGET:S/-buildlink-config-wrapper//}_buildlink_config_wrapper_done; \
	if [ ! -f $${cookie} ]; then					\
		${ECHO_MSG} "Creating wrapper script ${BUILDLINK_CONFIG_WRAPPER.${.TARGET:S/-buildlink-config-wrapper//}}."; \
		${MKDIR} ${BUILDLINK_CONFIG_WRAPPER.${.TARGET:S/-buildlink-config-wrapper//}:H}; \
		(${ECHO} '#!/bin/sh';					\
		${ECHO} '';						\
		${ECHO} '${ECHO} "`${BUILDLINK_CONFIG.${.TARGET:S/-buildlink-config-wrapper//}} $$*`" | ${SED} \'; \
		${ECHO} '	${BUILDLINK_CONFIG_WRAPPER_SED} \';	\
		${ECHO} '	${BUILDLINK_CONFIG_WRAPPER_POST_SED} \'; \
		) > ${BUILDLINK_CONFIG_WRAPPER.${.TARGET:S/-buildlink-config-wrapper//}}; \
		${CHMOD} +x ${BUILDLINK_CONFIG_WRAPPER.${.TARGET:S/-buildlink-config-wrapper//}}; \
		${ECHO} ${BUILDLINK_CONFIG.${.TARGET:S/-buildlink-config-wrapper//}} >> $${cookie}; \
	fi

.include "../../mk/bsd.prefs.mk"

_CHECK_IS_TEXT_FILE=	${FILE_CMD} $${file} | ${GREP} "text" >/dev/null 2>&1

# _REPLACE_LIBNAMES_SCRIPT runs sed with ${REPLACE_LIBNAMES_SED} as the
# substitution expression on the files specified in $${replace_files}.
# The following variables need to be predefined:
#
#	cookie			cookie created after this command is run
#	replace_files		files on which to run the substitution
#	message			message to display
#
_REPLACE_LIBNAMES_SCRIPT=						\
	${MKDIR} ${BUILDLINK_DIR};					\
	if [ -n "$${replace_files}" -a -n "${REPLACE_LIBNAMES_SED:Q}" ]; then \
		${ECHO_MSG} "$${message}";				\
		cd ${WRKSRC};						\
		for file in $${replace_files}; do			\
			if ${_CHECK_IS_TEXT_FILE}; then			\
				${ECHO_MSG} "	$${file}";		\
				${SED}	${REPLACE_LIBNAMES_SED}		\
					$${file} > $${file}.fixed;	\
				if [ -x $${file} ]; then		\
					${CHMOD} +x $${file}.fixed;	\
				fi;					\
				${MV} -f $${file}.fixed $${file};	\
				${ECHO} $${file} >> $${cookie};		\
			fi;						\
		done;							\
	fi

MAKEFILE_PATTERNS+=	Makefile
MAKEFILE_PATTERNS+=	Makeconf
MAKEFILE_PATTERNS+=	*.mk

.if (${OBJECT_FMT} == "a.out")
REPLACE_LIBNAME_PATTERNS+=	${MAKEFILE_PATTERNS}
_REPLACE_LIBNAME_PATTERNS_FIND=	\
	${REPLACE_LIBNAME_PATTERNS:S/$/!/:S/^/-o -name !/:S/!/"/g:S/-o//1}

REPLACE_LIBNAMES+=	\
	`cd ${WRKSRC}; ${FIND} . ${_REPLACE_LIBNAME_PATTERNS_FIND} | ${SED} -e 's|^\./||' | ${SORT}`
.endif

.if defined(REPLACE_LIBNAMES) || defined(REPLACE_LIBNAME_PATTERNS)
.if defined(HAS_CONFIGURE) || defined(GNU_CONFIGURE)
pre-configure: replace-libnames-configure

# Fix linking on a.out platforms in configure scripts by changing library
# references to the true library names.
#
replace-libnames-configure:
	${_PKG_SILENT}${_PKG_DEBUG}					\
	cookie=${BUILDLINK_DIR}/.replace_libnames_configure_done;	\
	if [ ! -f $${cookie} ]; then					\
		replace_files="${CONFIGURE_SCRIPT}";			\
		message="Fixing library name references in configure scripts:"; \
		${_REPLACE_LIBNAMES_SCRIPT};				\
	fi
.endif	# HAS_CONFIGURE

post-configure: replace-libnames-makefiles

# Fix linking on a.out platforms by changing library references in Makefiles
# to the true library names.
#
replace-libnames-makefiles:
	${_PKG_SILENT}${_PKG_DEBUG}					\
	cookie=${BUILDLINK_DIR}/.replace_libnames_makefiles_done;	\
	if [ ! -f $${cookie} ]; then					\
		replace_files="${REPLACE_LIBNAMES}";			\
		message="Fixing library name references in Makefiles:";	\
		${_REPLACE_LIBNAMES_SCRIPT};				\
	fi
.endif	# REPLACE_LIBNAMES

REPLACE_RPATH_PATTERNS+=	${MAKEFILE_PATTERNS}
_REPLACE_RPATH_PATTERNS_FIND=	\
	${REPLACE_RPATH_PATTERNS:S/$/!/:S/^/-o -name !/:S/!/"/g:S/-o//1}

REPLACE_RPATH+=	\
	`cd ${WRKSRC}; ${FIND} . ${_REPLACE_RPATH_PATTERNS_FIND} | ${SED} -e 's|^\./||' | ${SORT}`

REPLACE_RPATH_SED+=	\
	-e "s|-R[ 	]*${BUILDLINK_DIR}/|-R${LOCALBASE}/|g"

post-configure: replace-rpath

replace-rpath:
	${_PKG_SILENT}${_PKG_DEBUG}					\
	cookie=${BUILDLINK_DIR}/.replace_rpath_done;			\
	if [ ! -f $${cookie} ]; then					\
		${MKDIR} ${BUILDLINK_DIR};				\
		replace_files="${REPLACE_RPATH}";			\
		if [ -n "$${replace_files}" ]; then			\
			${ECHO_MSG} "Removing rpath references to buildlink directories:"; \
			cd ${WRKSRC};					\
			for file in $${replace_files}; do		\
				if ${_CHECK_IS_TEXT_FILE}; then		\
					${ECHO_MSG} "	$${file}";	\
					${SED}	${REPLACE_RPATH_SED}	\
						$${file} > $${file}.fixed; \
					if [ -x $${file} ]; then	\
						${CHMOD} +x $${file}.fixed; \
					fi;				\
					${MV} -f $${file}.fixed $${file}; \
					${ECHO} $${file} >> $${cookie};	\
				fi;					\
			done;						\
		fi;							\
	fi

REPLACE_BUILDLINK_PATTERNS+=	*.lai
REPLACE_BUILDLINK_PATTERNS+=	*-config
REPLACE_BUILDLINK_PATTERNS+=	*Conf.sh
REPLACE_BUILDLINK_PATTERNS+=	*.pc
_REPLACE_BUILDLINK_PATTERNS_FIND=	\
	${REPLACE_BUILDLINK_PATTERNS:S/$/!/:S/^/-o -name !/:S/!/"/g:S/-o//1}

REPLACE_BUILDLINK+=	\
	`cd ${WRKSRC}; ${FIND} . ${_REPLACE_BUILDLINK_PATTERNS_FIND} | ${SED} -e 's|^\./||' | ${SORT}`

.if defined(REPLACE_BUILDLINK)
post-build: replace-buildlink

REPLACE_BUILDLINK_SED?=		# empty
REPLACE_BUILDLINK_POST_SED+=						\
	-e "s|-I${BUILDLINK_DIR}/|-I${LOCALBASE}/|g"			\
	-e "s|-L${BUILDLINK_DIR}/|-L${LOCALBASE}/|g"

REPLACE_BUILDLINK_POST_SED+=	${REPLACE_RPATH_SED}

# Fix files by removing buildlink directory references and library names.
replace-buildlink:
	${_PKG_SILENT}${_PKG_DEBUG}					\
	cookie=${BUILDLINK_DIR}/.replace_buildlink_done;		\
	if [ ! -f $${cookie} ]; then					\
		${MKDIR} ${BUILDLINK_DIR};				\
		replace_files="${REPLACE_BUILDLINK}";			\
		if [ -n "$${replace_files}" ]; then			\
			${ECHO_MSG} "Fixing directory references and library names:"; \
			cd ${WRKSRC};					\
			for file in $${replace_files}; do		\
				if ${_CHECK_IS_TEXT_FILE}; then		\
					${ECHO_MSG} "	$${file}";	\
					${SED}	${REPLACE_BUILDLINK_SED} \
						${REPLACE_BUILDLINK_POST_SED} \
						${REPLACE_LIBNAMES_SED}	\
						$${file} > $${file}.fixed; \
					if [ -x $${file} ]; then	\
						${CHMOD} +x $${file}.fixed; \
					fi;				\
					${MV} -f $${file}.fixed $${file}; \
					${ECHO} $${file} >> $${cookie};	\
				fi;					\
			done;						\
		fi;							\
	fi
.endif	# REPLACE_BUILDLINK

.endif	# _BSD_BUILDLINK_MK