1#	$NetBSD: bsd.lua.mk,v 1.10 2023/06/03 21:24:57 lukem Exp $
2#
3# Build rules and definitions for Lua modules
4
5#
6# Variables used
7#
8# LUA_VERSION	currently installed version of Lua
9# LUA_LIBDIR	${LIBDIR}/lua/${LUA_VERSION}
10#
11# LUA_MODULES	list of Lua modules to build/installi
12# LUA_DPLIBS	shared library dependencies as per LIBDPLIBS
13#
14# LUA_SRCS.mod	sources for each module (by default: "${mod:S/./_/g}.lua")
15#
16# DPADD		additional dependencies for building modules
17# DPADD.mod	additional dependencies for a specific module
18#
19#
20# HAVE_LUAC	if defined, .lua source files will be compiled with ${LUAC}
21#		and installed as precompiled chunks for faster loading. Note
22#		that the luac file format is not yet standardised and may be
23#		subject to change.
24#
25# LUAC		the luac compiler (by default: /usr/bin/luac)
26#
27#
28# Notes:
29#
30# currently make(depend) and make(tags) do not support .lua sources; We
31# add Lua sources to DPSRCS when HAVE_LUAC is defined and other language
32# sources to SRCS for <bsd.dep.mk>.
33#
34# other language support for other than C is incomplete
35#
36# C language sources are passed though lint, when MKLINT != "no"
37#
38# The Lua binary searches /usr/share/lua/5.4/ at this time and we could
39# install .lua modules there which would mean slightly less duplication
40# in compat builds. However, MKSHARE=no would prevent such modules from
41# being installed so we just install everything under /usr/lib/lua/5.4/
42#
43
44.if !defined(_BSD_LUA_MK_)
45_BSD_LUA_MK_=1
46
47.include <bsd.init.mk>
48.include <bsd.shlib.mk>
49.include <bsd.gcc.mk>
50
51##
52##### Basic targets
53realinstall:	.PHONY lua-install
54realall:	.PHONY lua-all
55lint:		.PHONY lua-lint
56
57lua-install:	.PHONY
58
59lua-all:	.PHONY
60
61lua-lint:	.PHONY
62
63CLEANFILES+= a.out [Ee]rrs mklog core *.core
64
65##
66##### Global variables
67LUA_VERSION?=	5.4
68LUA_LIBDIR?=	${LIBDIR}/lua/${LUA_VERSION}
69LUAC?=		/usr/bin/luac
70
71##
72##### Build rules
73
74# XX should these always be on?
75CFLAGS+=	-fPIC
76
77.SUFFIXES:	.lua .luac
78.lua.luac:
79	${_MKTARGET_COMPILE}
80	${LUAC} -o ${.TARGET} ${.IMPSRC}
81
82##
83##### Libraries that modules may depend upon.
84.for _lib _dir in ${LUA_DPLIBS}
85.if !defined(LIBDO.${_lib})
86LIBDO.${_lib}!=	cd "${_dir}" && ${PRINTOBJDIR}
87.MAKEOVERRIDES+=LIBDO.${_lib}
88.endif
89LDADD+=-L${LIBDO.${_lib}} -l${_lib}
90DPADD+=${LIBDO.${_lib}}/lib${_lib}.so
91.endfor
92
93##
94##### Lua Modules
95.for _M in ${LUA_MODULES}
96LUA_SRCS.${_M}?=${_M:S/./_/g}.lua
97LUA_DEST.${_M}=${LUA_LIBDIR}${_M:S/./\//g:S/^/\//:H}
98
99.if !empty(LUA_SRCS.${_M}:M*.lua)
100.if ${LUA_SRCS.${_M}:[\#]} > 1
101.error Module "${_M}" has too many source files
102.endif
103.if defined(HAVE_LUAC)
104##
105## The module has Lua source and needs to be compiled
106LUA_TARG.${_M}=${_M:S/./_/g}.luac
107LUA_NAME.${_M}=${_M:S/./\//g:T}.luac
108CLEANFILES+=${LUA_TARG.${_M}}
109DPSRCS+=${LUA_SRCS.${_M}}
110
111.NOPATH:		${LUA_TARG.${_M}}
112lua-all:		${LUA_TARG.${_M}}
113${LUA_TARG.${_M}}:	${LUA_SRCS.${_M}} ${DPADD} ${DPADD.${_M}}
114.else
115##
116## The module has Lua source and can be installed directly
117LUA_TARG.${_M}=${LUA_SRCS.${_M}}
118LUA_NAME.${_M}=${_M:S/./\//g:T}.lua
119.endif
120.else
121##
122## The module has other language source and we must build ${_M}.so
123LUA_OBJS.${_M}=${LUA_SRCS.${_M}:N*.lua:R:S/$/.o/g}
124LUA_LOBJ.${_M}=${LUA_SRCS.${_M}:M*.c:.c=.ln}
125LUA_TARG.${_M}=${_M:S/./_/g}.so
126LUA_NAME.${_M}=${_M:S/./\//g:T}.so
127CLEANFILES+=${LUA_OBJS.${_M}} ${LUA_LOBJ.${_M}} ${LUA_TARG.${_M}}
128DPSRCS+=${LUA_SRCS.${_M}}
129SRCS+=${LUA_SRCS.${_M}}
130
131LUA_LDOPTS=	-Wl,--warn-shared-textrel
132.if ${MKSTRIPSYM} != "no"
133LUA_LDOPTS+=	-Wl,-x
134.else
135LUA_LDOPTS+=	-Wl,-X
136.endif
137
138.NOPATH:		${LUA_OBJS.${_M}} ${LUA_LOBJ.${_M}} ${LUA_TARG.${_M}}
139.if ${MKLINT} != "no"
140${LUA_TARG.${_M}}:	${LUA_LOBJ.${_M}}
141.endif
142lua-lint:		${LUA_LOBJ.${_M}}
143lua-all:		${LUA_TARG.${_M}}
144${LUA_TARG.${_M}}:	${LUA_OBJS.${_M}} ${DPADD} ${DPADD.${_M}}
145	${_MKTARGET_BUILD}
146	rm -f ${.TARGET}
147	${CC} ${LUA_LDOPTS} -shared ${LUA_OBJS.${_M}} \
148	    -Wl,-soname,${LUA_NAME.${_M}} -o ${.TARGET} \
149	    ${LDADD} ${LDADD.${_M}} ${LDFLAGS} ${LDFLAGS.${_M}}
150
151.endif
152
153DPADD+=	${LIBLUA}
154LDADD+=	-llua
155
156##
157## module install rules
158lua-install:		${DESTDIR}${LUA_DEST.${_M}}/${LUA_NAME.${_M}}
159${DESTDIR}${LUA_DEST.${_M}}/${LUA_NAME.${_M}}! ${LUA_TARG.${_M}}
160	${_MKTARGET_INSTALL}
161	${INSTALL_FILE} -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE}	\
162	    ${.ALLSRC} ${.TARGET}
163
164.endfor
165##
166##### end of modules
167
168.include <bsd.clean.mk>
169.include <bsd.dep.mk>
170.include <bsd.inc.mk>
171.include <bsd.obj.mk>
172.include <bsd.sys.mk>
173.endif	# ! defined(_BSD_LUA_MK_)
174