1# Makefile fragment - requires GNU make
2#
3# Copyright (c) 2019-2024, Arm Limited.
4# SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
5
6PLM := $(srcdir)/pl/math
7AOR := $(srcdir)/math
8B := build/pl/math
9
10pl-lib-srcs := $(wildcard $(PLM)/*.[cS])
11
12ifeq ($(WANT_SVE_MATH), 0)
13pl-lib-srcs := $(filter-out $(PLM)/sv_%, $(pl-lib-srcs))
14endif
15
16math-test-srcs := \
17	$(AOR)/test/mathtest.c \
18	$(AOR)/test/mathbench.c \
19	$(AOR)/test/ulp.c \
20
21math-test-host-srcs := $(wildcard $(AOR)/test/rtest/*.[cS])
22
23pl-includes := $(patsubst $(PLM)/%,build/pl/%,$(wildcard $(PLM)/include/*.h))
24pl-test-includes := $(patsubst $(PLM)/%,build/pl/include/%,$(wildcard $(PLM)/test/*.h))
25
26pl-libs := \
27	build/pl/lib/libmathlib.so \
28	build/pl/lib/libmathlib.a \
29
30math-tools := \
31	build/pl/bin/mathtest \
32	build/pl/bin/mathbench \
33	build/pl/bin/mathbench_libc \
34	build/pl/bin/runulp.sh \
35	build/pl/bin/ulp \
36
37math-host-tools := \
38	build/pl/bin/rtest \
39
40pl-lib-objs := $(patsubst $(PLM)/%,$(B)/%.o,$(basename $(pl-lib-srcs)))
41math-test-objs := $(patsubst $(AOR)/%,$(B)/%.o,$(basename $(math-test-srcs)))
42math-host-objs := $(patsubst $(AOR)/%,$(B)/%.o,$(basename $(math-test-host-srcs)))
43pl-target-objs := $(pl-lib-objs) $(math-test-objs)
44pl-objs := $(pl-target-objs) $(pl-target-objs:%.o=%.os) $(math-host-objs)
45
46pl/math-files := \
47	$(pl-objs) \
48	$(pl-libs) \
49	$(math-tools) \
50	$(math-host-tools) \
51	$(pl-includes) \
52	$(pl-test-includes) \
53
54all-pl/math: $(pl-libs) $(math-tools) $(pl-includes) $(pl-test-includes)
55
56$(pl-objs): $(pl-includes) $(pl-test-includes)
57$(pl-objs): CFLAGS_PL += $(math-cflags)
58$(B)/test/mathtest.o: CFLAGS_PL += -fmath-errno
59$(math-host-objs): CC = $(HOST_CC)
60$(math-host-objs): CFLAGS_PL = $(HOST_CFLAGS)
61
62$(B)/sv_%: CFLAGS_PL += $(math-sve-cflags)
63
64build/pl/include/test/ulp_funcs_gen.h: $(pl-lib-srcs)
65	# Replace PL_SIG
66	cat $^ | grep PL_SIG | $(CC) -xc - -o - -E "-DPL_SIG(v, t, a, f, ...)=_Z##v##t##a(f)" -P > $@
67
68build/pl/include/test/mathbench_funcs_gen.h: $(pl-lib-srcs)
69	# Replace PL_SIG macros with mathbench func entries
70	cat $^ | grep PL_SIG | $(CC) -xc - -o - -E "-DPL_SIG(v, t, a, f, ...)=_Z##v##t##a(f, ##__VA_ARGS__)" -P > $@
71
72build/pl/include/test/ulp_wrappers_gen.h: $(pl-lib-srcs)
73	# Replace PL_SIG macros with ULP wrapper declarations
74	cat $^ | grep PL_SIG | $(CC) -xc - -o - -E "-DPL_SIG(v, t, a, f, ...)=Z##v##N##t##a##_WRAP(f)" -P > $@
75
76$(B)/test/ulp.o: $(AOR)/test/ulp.h build/pl/include/test/ulp_funcs_gen.h build/pl/include/test/ulp_wrappers_gen.h
77$(B)/test/ulp.o: CFLAGS_PL += -I build/pl/include/test
78
79$(B)/test/mathbench.o: build/pl/include/test/mathbench_funcs_gen.h
80$(B)/test/mathbench.o: CFLAGS_PL += -I build/pl/include/test
81
82build/pl/lib/libmathlib.so: $(pl-lib-objs:%.o=%.os)
83	$(CC) $(CFLAGS_PL) $(LDFLAGS) -shared -o $@ $^
84
85build/pl/lib/libmathlib.a: $(pl-lib-objs)
86	rm -f $@
87	$(AR) rc $@ $^
88	$(RANLIB) $@
89
90$(math-host-tools): HOST_LDLIBS += -lm -lmpfr -lmpc
91$(math-tools): LDLIBS += $(math-ldlibs) -lm
92# math-sve-cflags should be empty if WANT_SVE_MATH is not enabled
93$(math-tools): CFLAGS_PL += $(math-sve-cflags)
94
95# Some targets to build pl/math/test from math/test sources
96build/pl/math/test/%.o: $(srcdir)/math/test/%.S
97	$(CC) $(CFLAGS_PL) -c -o $@ $<
98
99build/pl/math/test/%.o: $(srcdir)/math/test/%.c
100	$(CC) $(CFLAGS_PL) -c -o $@ $<
101
102build/pl/math/test/%.os: $(srcdir)/math/test/%.S
103	$(CC) $(CFLAGS_PL) -c -o $@ $<
104
105build/pl/math/test/%.os: $(srcdir)/math/test/%.c
106	$(CC) $(CFLAGS_PL) -c -o $@ $<
107
108# Some targets to build pl/ sources using appropriate flags
109build/pl/%.o: $(srcdir)/pl/%.S
110	$(CC) $(CFLAGS_PL) -c -o $@ $<
111
112build/pl/%.o: $(srcdir)/pl/%.c
113	$(CC) $(CFLAGS_PL) -c -o $@ $<
114
115build/pl/%.os: $(srcdir)/pl/%.S
116	$(CC) $(CFLAGS_PL) -c -o $@ $<
117
118build/pl/%.os: $(srcdir)/pl/%.c
119	$(CC) $(CFLAGS_PL) -c -o $@ $<
120
121build/pl/bin/rtest: $(math-host-objs)
122	$(HOST_CC) $(HOST_CFLAGS) $(HOST_LDFLAGS) -o $@ $^ $(HOST_LDLIBS)
123
124build/pl/bin/mathtest: $(B)/test/mathtest.o build/pl/lib/libmathlib.a
125	$(CC) $(CFLAGS_PL) $(LDFLAGS) -static -o $@ $^ $(LDLIBS)
126
127build/pl/bin/mathbench: $(B)/test/mathbench.o build/pl/lib/libmathlib.a
128	$(CC) $(CFLAGS_PL) $(LDFLAGS) -static -o $@ $^ $(LDLIBS)
129
130# This is not ideal, but allows custom symbols in mathbench to get resolved.
131build/pl/bin/mathbench_libc: $(B)/test/mathbench.o build/pl/lib/libmathlib.a
132	$(CC) $(CFLAGS_PL) $(LDFLAGS) -static -o $@ $< $(LDLIBS) -lc build/pl/lib/libmathlib.a -lm
133
134build/pl/bin/ulp: $(B)/test/ulp.o build/pl/lib/libmathlib.a
135	$(CC) $(CFLAGS_PL) $(LDFLAGS) -static -o $@ $^ $(LDLIBS)
136
137build/pl/include/%.h: $(PLM)/include/%.h
138	cp $< $@
139
140build/pl/include/test/%.h: $(PLM)/test/%.h
141	cp $< $@
142
143build/pl/bin/%.sh: $(PLM)/test/%.sh
144	cp $< $@
145
146pl-math-tests := $(wildcard $(PLM)/test/testcases/directed/*.tst)
147pl-math-rtests := $(wildcard $(PLM)/test/testcases/random/*.tst)
148
149check-pl/math-test: $(math-tools)
150	cat $(pl-math-tests) | $(EMULATOR) build/pl/bin/mathtest $(math-testflags)
151
152check-pl/math-rtest: $(math-host-tools) $(math-tools)
153	cat $(pl-math-rtests) | build/pl/bin/rtest | $(EMULATOR) build/pl/bin/mathtest $(math-testflags)
154
155ulp-input-dir=$(B)/test/inputs
156
157math-lib-lims = $(patsubst $(PLM)/%,$(ulp-input-dir)/%.ulp,$(basename $(pl-lib-srcs)))
158math-lib-fenvs = $(patsubst $(PLM)/%,$(ulp-input-dir)/%.fenv,$(basename $(pl-lib-srcs)))
159math-lib-itvs = $(patsubst $(PLM)/%,$(ulp-input-dir)/%.itv,$(basename $(pl-lib-srcs)))
160
161ulp-inputs = $(math-lib-lims) $(math-lib-fenvs) $(math-lib-itvs)
162
163$(ulp-inputs): CFLAGS_PL += -I$(PLM) -I$(PLM)/include $(math-cflags)
164
165$(ulp-input-dir)/%.ulp: $(PLM)/%.c
166	mkdir -p $(@D)
167	$(CC) -I$(PLM)/test $(CFLAGS_PL) $< -o - -E | { grep -o "PL_TEST_ULP [^ ]* [^ ]*" || true; } > $@
168
169$(ulp-input-dir)/%.fenv: $(PLM)/%.c
170	mkdir -p $(@D)
171	$(CC) -I$(PLM)/test $(CFLAGS_PL) $< -o - -E | { grep -o "PL_TEST_EXPECT_FENV_ENABLED [^ ]*" || true; } > $@
172
173$(ulp-input-dir)/%.itv: $(PLM)/%.c
174	mkdir -p $(dir $@)
175	$(CC) -I$(PLM)/test $(CFLAGS_PL) $< -o - -E | { grep "PL_TEST_INTERVAL " || true; } | sed "s/ PL_TEST_INTERVAL/\nPL_TEST_INTERVAL/g" > $@
176
177ulp-lims := $(ulp-input-dir)/limits
178$(ulp-lims): $(math-lib-lims)
179	cat $^ | sed "s/PL_TEST_ULP //g;s/^ *//g" > $@
180
181fenv-exps := $(ulp-input-dir)/fenv
182$(fenv-exps): $(math-lib-fenvs)
183	cat $^ | sed "s/PL_TEST_EXPECT_FENV_ENABLED //g;s/^ *//g" > $@
184
185ulp-itvs := $(ulp-input-dir)/intervals
186$(ulp-itvs): $(math-lib-itvs)
187	cat $^ | sort -u | sed "s/PL_TEST_INTERVAL //g" > $@
188
189check-pl/math-ulp: $(math-tools) $(ulp-lims) $(fenv-exps) $(ulp-itvs)
190	WANT_SVE_MATH=$(WANT_SVE_MATH) \
191	ULPFLAGS="$(math-ulpflags)" \
192	LIMITS=../../../$(ulp-lims) \
193	INTERVALS=../../../$(ulp-itvs) \
194	FENV=../../../$(fenv-exps) \
195	FUNC=$(func) \
196	build/pl/bin/runulp.sh $(EMULATOR)
197
198check-pl/math: check-pl/math-test check-pl/math-rtest check-pl/math-ulp
199
200$(DESTDIR)$(libdir)/pl/%.so: build/pl/lib/%.so
201	$(INSTALL) -D $< $@
202
203$(DESTDIR)$(libdir)/pl/%: build/pl/lib/%
204	$(INSTALL) -m 644 -D $< $@
205
206$(DESTDIR)$(includedir)/pl/%: build/pl/include/%
207	$(INSTALL) -m 644 -D $< $@
208
209install-pl/math: \
210 $(pl-libs:build/pl/lib/%=$(DESTDIR)$(libdir)/pl/%) \
211 $(pl-includes:build/pl/include/%=$(DESTDIR)$(includedir)/pl/%)
212
213clean-pl/math:
214	rm -f $(pl/math-files)
215
216.PHONY: all-pl/math check-pl/math-test check-pl/math-rtest check-pl/math-ulp check-pl/math install-pl/math clean-pl/math
217