Makefile revision 1.103
1#	$NetBSD: Makefile,v 1.103 2025/11/23 22:11:41 riastradh Exp $
2#
3
4NOSANITIZER=	# defined
5WARNS?=	5
6LIB=	pthread
7
8.include <bsd.own.mk>
9
10USE_SHLIBDIR=	yes
11
12.if defined(PTHREAD_MACHINE_ARCH) && !empty(PTHREAD_MACHINE_ARCH) && \
13    exists(${.CURDIR}/arch/${PTHREAD_MACHINE_ARCH})
14ARCHSUBDIR=	${PTHREAD_MACHINE_ARCH}
15.elif defined(PTHREAD_MACHINE_CPU) && !empty(PTHREAD_MACHINE_CPU) && \
16    exists(${.CURDIR}/arch/${PTHREAD_MACHINE_CPU})
17ARCHSUBDIR=	${PTHREAD_MACHINE_CPU}
18.elif exists(${.CURDIR}/arch/${MACHINE_ARCH})
19ARCHSUBDIR=	${MACHINE_ARCH}
20.elif exists(${.CURDIR}/arch/${MACHINE_CPU})
21ARCHSUBDIR=	${MACHINE_CPU}
22.else
23.BEGIN:
24	@echo "WARNING: no ARCHSUBDIR for ${MACHINE_ARCH}/${MACHINE_CPU}; skipping..."
25.endif
26
27INCS=	pthread.h pthread_types.h pthread_queue.h
28INCSDIR=/usr/include
29
30.if defined(ARCHSUBDIR)
31
32ARCHDIR=	${.CURDIR}/arch/${ARCHSUBDIR}
33.PATH:	${ARCHDIR}
34
35EXPSYM_SRCS+=	pthread_mi.expsym
36.  if ${MACHINE_ARCH} == "alpha" || ${MACHINE_ARCH} == "hppa"
37# PTHREAD__ASM_RASOPS
38EXPSYM_SRCS+=	pthread_md.expsym
39.  else
40# !PTHREAD__ASM_RASOPS
41# XXX These really shouldn't be exported.
42EXPSYM_SRCS+=	pthread_ras.expsym
43.  endif
44
45LIB_EXPSYM=	pthread.expsym
46pthread.expsym: ${EXPSYM_SRCS}
47	${_MKTARGET_CREATE}
48	LC_ALL=C sort -m ${.ALLSRC} >${.TARGET}.tmp && \
49	${MV} ${.TARGET}.tmp ${.TARGET}
50CLEANFILES+=	pthread.expsym pthread.expsym.tmp
51
52CPPFLAGS+=	-I${ARCHDIR} -I${.CURDIR} -I${.OBJDIR} -D_LIBC -D_REENTRANT
53CPPFLAGS+=	-I${.CURDIR}/../libc/include
54CPPFLAGS+=	-D__LIBPTHREAD_SOURCE__ -D__LIBC_THREAD_STUBS
55
56# Since <pthread.h> is not used, allow declaring pthread_create and
57# friends locally.
58CWARNFLAGS.clang+=	-Wno-builtin-requires-header
59
60# XXX: This crappy poke at libc's internals needs to be fixed.
61CPPFLAGS+=-I${NETBSDSRCDIR}/sys -I${.CURDIR}/../libc
62
63# providing alternative MI implementations for creating an lwp is
64# possible by setting PTHREAD_MAKELWP.  Currently, alternatives are
65# set by the rumprun software stacks (see https://github.com/rumpkernel/rumprun )
66PTHREAD_MAKELWP?=	pthread_makelwp_netbsd.c
67
68# Prevent unloading libpthread.
69#
70# libpthread provides strong definitions of some symbols defined weakly
71# by libc, such as pthread_mutex_lock (actually __libc_mutex_lock under
72# the hood), which are used in libraries that take locks for
73# thread-safety -- this way, in non-threaded applications,
74# pthread_mutex_lock is a cheap noop libc stub, while in threaded
75# applications, it's a real libpthread operation.  If libpthread is
76# dlopened, the symbol might be lazily resolved _after_ it is dlopened
77# to point at libpthread; then it cannot be safely unloaded, because
78# there's no way to roll back symbol resolution.
79#
80# PR lib/59784: dlopening and dlclosing libpthread is broken
81#
82LDADD+=		-Wl,-z,nodelete
83
84#
85# NOTE: When you create a new file for libpthread, make sure that pthread.c
86# gets a reference to a symbol in that file.  Otherwise, Unix's stupid static
87# library semantics will end up discarding potentially important objects.
88#
89SRCS=	pthread.c
90SRCS+=	pthread_attr.c
91SRCS+=	pthread_barrier.c
92# used by rumprun-posix to work around symbol collisions
93.if ${PTHREAD_CANCELSTUB:Uyes} != "no"
94SRCS+=	pthread_cancelstub.c
95.endif
96SRCS+=	pthread_cond.c
97SRCS+=	pthread_getcpuclockid.c
98SRCS+=	pthread_lock.c
99SRCS+=	${PTHREAD_MAKELWP}
100SRCS+=	pthread_misc.c
101SRCS+=	pthread_mutex.c
102SRCS+=	pthread_once.c
103SRCS+=	pthread_rwlock.c
104SRCS+=	pthread_specific.c
105SRCS+=	pthread_spin.c
106SRCS+=	pthread_tsd.c
107SRCS+=	res_state.c
108.PATH: ${.CURDIR}/../librt
109SRCS+=	sem.c
110# Architecture-dependent files
111.if exists(${ARCHDIR}/pthread_md.S)
112SRCS+= pthread_md.S
113.endif
114.if exists(${ARCHDIR}/Makefile.inc)
115.include "${ARCHDIR}/Makefile.inc"
116.endif
117
118# The PTHREAD__COMPAT flag builds a libpthread that can be dropped
119# into a NetBSD 2/3/4 chroot with a NetBSD 5 or later kernel.
120# This makes threading work in the chroot, no other modifications
121# required.
122#
123.if defined(PTHREAD__COMPAT)
124SRCS+=		pthread_compat.c
125.PATH.c:	${.CURDIR}/../../common/lib/libc/arch/${ARCHSUBDIR}/atomic
126.PATH.S:	${.CURDIR}/../../common/lib/libc/arch/${ARCHSUBDIR}/atomic
127.PATH.c:	${.CURDIR}/../../common/lib/libc/atomic
128.PATH.c:	${.CURDIR}/../libc/misc
129.include "../../common/lib/libc/arch/${ARCHSUBDIR}/atomic/Makefile.inc"
130.endif
131
132ALIGN_FUNCTIONS=	${${ACTIVE_CC} == "gcc":? -falign-functions=32 :}
133
134# The TSD routines are used in the implementation of profiling, and so
135# can't be profiled themselves.
136COPTS.pthread_specific.c+=	${ALIGN_FUNCTIONS}
137pthread_specific.po: pthread_specific.o
138	${_MKTARGET_CREATE}
139	cp pthread_specific.o pthread_specific.po
140
141COPTS.pthread.c += -Wno-stack-protector -Wno-format-nonliteral
142COPTS.pthread_attr.c += -Wno-format-nonliteral
143
144LINTFLAGS+=	-Ac11		# for _Atomic
145
146MAN+=	affinity.3 pthread.3 \
147	pthread_attr.3 \
148	pthread_attr_get_np.3 \
149	pthread_attr_getdetachstate.3 \
150	pthread_attr_getguardsize.3 \
151	pthread_attr_getinheritsched.3 \
152	pthread_attr_getname_np.3 \
153	pthread_attr_getschedparam.3 \
154	pthread_attr_getscope.3 \
155	pthread_attr_getstack.3 \
156	pthread_attr_setcreatesuspend_np.3 \
157	pthread_barrier.3 pthread_barrierattr.3 \
158	pthread_cancel.3 pthread_cleanup_push.3 \
159	pthread_cond.3 pthread_condattr.3 \
160	pthread_create.3 pthread_detach.3 pthread_equal.3 \
161	pthread_curcpu_np.3 \
162	pthread_exit.3 \
163	pthread_getcpuclockid.3 \
164	pthread_getname_np.3 \
165	pthread_getspecific.3 pthread_join.3 \
166	pthread_key_create.3 pthread_kill.3 \
167	pthread_mutex.3 pthread_mutexattr.3 \
168	pthread_once.3 pthread_rwlock.3 pthread_rwlockattr.3 \
169	pthread_schedparam.3 pthread_self.3 \
170	pthread_sigmask.3 pthread_spin.3 \
171	pthread_suspend_np.3 pthread_testcancel.3
172
173MLINKS+=	pthread_attr_get_np.3 pthread_getattr_np.3
174MLINKS+=	affinity.3 pthread_setaffinity_np.3
175MLINKS+=	affinity.3 pthread_getaffinity_np.3
176
177MLINKS+=	pthread_attr.3 pthread_attr_init.3
178MLINKS+=	pthread_attr.3 pthread_attr_destroy.3
179
180MLINKS+=	pthread_attr_getdetachstate.3 pthread_attr_setdetachstate.3
181MLINKS+=	pthread_attr_getguardsize.3 pthread_attr_setguardsize.3
182MLINKS+=	pthread_attr_getinheritsched.3 pthread_attr_setinheritsched.3
183MLINKS+=	pthread_attr_getname_np.3 pthread_attr_setname_np.3
184
185MLINKS+=	pthread_attr_getschedparam.3 pthread_attr_setschedparam.3 \
186		pthread_attr_getschedparam.3 pthread_attr_getschedpolicy.3 \
187		pthread_attr_getschedparam.3 pthread_attr_setschedpolicy.3
188
189MLINKS+=	pthread_attr_getscope.3 pthread_attr_setscope.3
190
191MLINKS+=	pthread_attr_getstack.3 pthread_attr_setstack.3 \
192		pthread_attr_getstack.3 pthread_attr_getstacksize.3 \
193		pthread_attr_getstack.3 pthread_attr_setstacksize.3 \
194		pthread_attr_getstack.3 pthread_attr_getstackaddr.3 \
195		pthread_attr_getstack.3 pthread_attr_setstackaddr.3
196
197MLINKS+=	pthread_cleanup_push.3 pthread_cleanup_pop.3
198
199MLINKS+=	pthread_barrier.3 pthread_barrier_init.3
200MLINKS+=	pthread_barrier.3 pthread_barrier_destroy.3
201MLINKS+=	pthread_barrier.3 pthread_barrier_wait.3
202
203MLINKS+=	pthread_barrierattr.3 pthread_barrierattr_init.3
204MLINKS+=	pthread_barrierattr.3 pthread_barrierattr_destroy.3
205MLINKS+=	pthread_barrierattr.3 pthread_barrierattr_getpshared.3
206MLINKS+=	pthread_barrierattr.3 pthread_barrierattr_setpshared.3
207
208MLINKS+=	pthread_cond.3 pthread_cond_init.3
209MLINKS+=	pthread_cond.3 pthread_cond_destroy.3
210MLINKS+=	pthread_cond.3 pthread_cond_broadcast.3
211MLINKS+=	pthread_cond.3 pthread_cond_wait.3
212MLINKS+=	pthread_cond.3 pthread_cond_signal.3
213MLINKS+=	pthread_cond.3 pthread_cond_timedwait.3
214
215MLINKS+=	pthread_condattr.3 pthread_condattr_init.3
216MLINKS+=	pthread_condattr.3 pthread_condattr_destroy.3
217MLINKS+=	pthread_condattr.3 pthread_condattr_setclock.3
218MLINKS+=	pthread_condattr.3 pthread_condattr_getclock.3
219MLINKS+=	pthread_condattr.3 pthread_condattr_getpshared.3
220MLINKS+=	pthread_condattr.3 pthread_condattr_setpshared.3
221
222MLINKS+=	pthread_getname_np.3 pthread_setname_np.3
223MLINKS+=	pthread_getspecific.3 pthread_setspecific.3
224MLINKS+=	pthread_key_create.3 pthread_key_delete.3
225
226MLINKS+=	pthread_mutex.3 pthread_mutex_init.3
227MLINKS+=	pthread_mutex.3 pthread_mutex_destroy.3
228MLINKS+=	pthread_mutex.3 pthread_mutex_lock.3
229MLINKS+=	pthread_mutex.3 pthread_mutex_trylock.3
230MLINKS+=	pthread_mutex.3 pthread_mutex_unlock.3
231MLINKS+=	pthread_mutex.3 pthread_mutex_timedlock.3
232MLINKS+=	pthread_mutex.3	pthread_mutex_getprioceiling.3
233MLINKS+=	pthread_mutex.3	pthread_mutex_setprioceiling.3
234
235MLINKS+=	pthread_mutexattr.3 pthread_mutexattr_init.3
236MLINKS+=	pthread_mutexattr.3 pthread_mutexattr_destroy.3
237MLINKS+=	pthread_mutexattr.3 pthread_mutexattr_getpshared.3
238MLINKS+=	pthread_mutexattr.3 pthread_mutexattr_setpshared.3
239MLINKS+=	pthread_mutexattr.3 pthread_mutexattr_settype.3
240MLINKS+=	pthread_mutexattr.3 pthread_mutexattr_gettype.3
241MLINKS+=	pthread_mutexattr.3 pthread_mutexattr_getprotocol.3
242MLINKS+=	pthread_mutexattr.3 pthread_mutexattr_setprotocol.3
243MLINKS+=	pthread_mutexattr.3 pthread_mutexattr_getprioceiling.3
244MLINKS+=	pthread_mutexattr.3 pthread_mutexattr_setprioceiling.3
245
246MLINKS+=	pthread_rwlock.3 pthread_rwlock_init.3
247MLINKS+=	pthread_rwlock.3 pthread_rwlock_destroy.3
248MLINKS+=	pthread_rwlock.3 pthread_rwlock_rdlock.3
249MLINKS+=	pthread_rwlock.3 pthread_rwlock_wrlock.3
250MLINKS+=	pthread_rwlock.3 pthread_rwlock_unlock.3
251
252MLINKS+=	pthread_rwlock.3 pthread_rwlock_timedrdlock.3
253MLINKS+=	pthread_rwlock.3 pthread_rwlock_timedwrlock.3
254MLINKS+=	pthread_rwlock.3 pthread_rwlock_tryrdlock.3
255MLINKS+=	pthread_rwlock.3 pthread_rwlock_trywrlock.3
256
257MLINKS+=	pthread_rwlockattr.3 pthread_rwlockattr_init.3
258MLINKS+=	pthread_rwlockattr.3 pthread_rwlockattr_destroy.3
259MLINKS+=	pthread_rwlockattr.3 pthread_rwlockattr_getpshared.3
260MLINKS+=	pthread_rwlockattr.3 pthread_rwlockattr_setpshared.3
261
262MLINKS+=	pthread_spin.3 pthread_spin_init.3
263MLINKS+=	pthread_spin.3 pthread_spin_destroy.3
264MLINKS+=	pthread_spin.3 pthread_spin_lock.3
265MLINKS+=	pthread_spin.3 pthread_spin_trylock.3
266MLINKS+=	pthread_spin.3 pthread_spin_unlock.3
267
268MLINKS+=	pthread_schedparam.3 pthread_setschedparam.3
269MLINKS+=	pthread_schedparam.3 pthread_getschedparam.3
270MLINKS+=	pthread_suspend_np.3 pthread_resume_np.3
271MLINKS+=	pthread_testcancel.3 pthread_setcancelstate.3
272MLINKS+=	pthread_testcancel.3 pthread_setcanceltype.3
273
274# ISO C threads (ISO/IEC 9899:2011)
275
276SRCS+=		call_once.c cnd.c mtx.c thrd.c tss.c
277MAN+=		threads.3 call_once.3 cnd.3 mtx.3 thrd.3 tss.3
278
279MLINKS+=	call_once.3 ONCE_FLAG_INIT.3
280MLINKS+=	call_once.3 once_flag.3
281
282MLINKS+=	cnd.3 cnd_broadcast.3
283MLINKS+=	cnd.3 cnd_destroy.3
284MLINKS+=	cnd.3 cnd_init.3
285MLINKS+=	cnd.3 cnd_signal.3
286MLINKS+=	cnd.3 cnd_timedwait.3
287MLINKS+=	cnd.3 cnd_wait.3
288
289MLINKS+=	mtx.3 mtx_destroy.3
290MLINKS+=	mtx.3 mtx_init.3
291MLINKS+=	mtx.3 mtx_lock.3
292MLINKS+=	mtx.3 mtx_timedlock.3
293MLINKS+=	mtx.3 mtx_trylock.3
294MLINKS+=	mtx.3 mtx_unlock.3
295
296MLINKS+=	thrd.3 thrd_create.3
297MLINKS+=	thrd.3 thrd_current.3
298MLINKS+=	thrd.3 thrd_detach.3
299MLINKS+=	thrd.3 thrd_equal.3
300MLINKS+=	thrd.3 thrd_exit.3
301MLINKS+=	thrd.3 thrd_join.3
302MLINKS+=	thrd.3 thrd_sleep.3
303MLINKS+=	thrd.3 thrd_yield.3
304
305MLINKS+=	tss.3 TSS_DTOR_ITERATIONS.3
306MLINKS+=	tss.3 tss_create.3
307MLINKS+=	tss.3 tss_delete.3
308MLINKS+=	tss.3 tss_get.3
309MLINKS+=	tss.3 tss_set.3
310
311INCS+=		threads.h
312
313# PR lib/54001: create libpthread.a as a single large object, with all the
314# symbols in one section. ensures that if any libpthread function is used,
315# you get all of them from libpthread, and not the libc stubs.
316#
317# This makes -lpthread equivalent to -Wl,--whole-archive -lpthread
318
319__archivebuild: .USE
320	${_MKTARGET_BUILD}
321	@rm -f ${.TARGET}
322	${LD} -r -o ${.TARGET}.o `NM=${NM} ${LORDER} ${.ALLSRC:M*o} | ${TSORT}`
323	${AR} ${_ARFL} ${.TARGET} ${.TARGET}.o
324
325CLEANFILES+=	${_LIBS:=.o}
326
327.include <bsd.lib.mk>
328
329.else
330
331.include <bsd.man.mk>
332.include <bsd.files.mk>
333.include <bsd.inc.mk>
334
335.endif
336
337# WARNS=2 sets -Wcast-qual. This causes problems for one of
338# pthread_setspecific() and pthread_getspecific(), since the constness
339# of the argument to setspecific() has to be discarded *somewhere*
340# before returning it from getspecific().
341CWARNFLAGS+= -Wno-cast-qual
342