1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2018-2021 Gavin D. Howard and contributors.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are met:
8#
9# * Redistributions of source code must retain the above copyright notice, this
10#   list of conditions and the following disclaimer.
11#
12# * Redistributions in binary form must reproduce the above copyright notice,
13#   this list of conditions and the following disclaimer in the documentation
14#   and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27#
28# %%WARNING%%
29#
30.POSIX:
31
32SRC = %%SRC%%
33OBJ = %%OBJ%%
34GCDA = %%GCDA%%
35GCNO = %%GCNO%%
36
37BC_ENABLED_NAME = BC_ENABLED
38BC_ENABLED = %%BC_ENABLED%%
39DC_ENABLED_NAME = DC_ENABLED
40DC_ENABLED = %%DC_ENABLED%%
41
42HEADERS = include/args.h include/file.h include/lang.h include/lex.h include/num.h include/opt.h include/parse.h include/program.h include/read.h include/status.h include/vector.h include/vm.h
43BC_HEADERS = include/bc.h
44DC_HEADERS = include/dc.h
45HISTORY_HEADERS = include/history.h
46EXTRA_MATH_HEADERS = include/rand.h
47LIBRARY_HEADERS = include/bcl.h include/library.h
48
49GEN_DIR = gen
50GEN = %%GEN%%
51GEN_EXEC = $(GEN_DIR)/$(GEN)
52GEN_C = $(GEN_DIR)/$(GEN).c
53
54GEN_EMU = %%GEN_EMU%%
55
56BC_LIB = $(GEN_DIR)/lib.bc
57BC_LIB_C = $(GEN_DIR)/lib.c
58BC_LIB_O = %%BC_LIB_O%%
59BC_LIB_GCDA = $(GEN_DIR)/lib.gcda
60BC_LIB_GCNO = $(GEN_DIR)/lib.gcno
61
62BC_LIB2 = $(GEN_DIR)/lib2.bc
63BC_LIB2_C = $(GEN_DIR)/lib2.c
64BC_LIB2_O = %%BC_LIB2_O%%
65BC_LIB2_GCDA = $(GEN_DIR)/lib2.gcda
66BC_LIB2_GCNO = $(GEN_DIR)/lib2.gcno
67
68BC_HELP = $(GEN_DIR)/bc_help.txt
69BC_HELP_C = $(GEN_DIR)/bc_help.c
70BC_HELP_O = %%BC_HELP_O%%
71BC_HELP_GCDA = $(GEN_DIR)/bc_help.gcda
72BC_HELP_GCNO = $(GEN_DIR)/bc_help.gcno
73
74DC_HELP = $(GEN_DIR)/dc_help.txt
75DC_HELP_C = $(GEN_DIR)/dc_help.c
76DC_HELP_O = %%DC_HELP_O%%
77DC_HELP_GCDA = $(GEN_DIR)/dc_help.gcda
78DC_HELP_GCNO = $(GEN_DIR)/dc_help.gcno
79
80BIN = bin
81LOCALES = locales
82EXEC_SUFFIX = %%EXECSUFFIX%%
83EXEC_PREFIX = %%EXECPREFIX%%
84
85BC = bc
86DC = dc
87BC_EXEC = $(BIN)/$(EXEC_PREFIX)$(BC)
88DC_EXEC = $(BIN)/$(EXEC_PREFIX)$(DC)
89
90BC_TEST_OUTPUTS = tests/bc_outputs
91BC_FUZZ_OUTPUTS = tests/fuzzing/bc_outputs1 tests/fuzzing/bc_outputs2 tests/fuzzing/bc_outputs3
92DC_TEST_OUTPUTS = tests/dc_outputs
93DC_FUZZ_OUTPUTS = tests/fuzzing/dc_outputs
94
95LIB = libbcl
96LIB_NAME = $(LIB).a
97LIBBC = $(BIN)/$(LIB_NAME)
98BCL = bcl
99BCL_TEST = $(BIN)/$(BCL)
100BCL_TEST_C = tests/$(BCL).c
101
102MANUALS = manuals
103BC_MANPAGE_NAME = $(EXEC_PREFIX)$(BC)$(EXEC_SUFFIX).1
104BC_MANPAGE = $(MANUALS)/$(BC).1
105BC_MD = $(BC_MANPAGE).md
106DC_MANPAGE_NAME = $(EXEC_PREFIX)$(DC)$(EXEC_SUFFIX).1
107DC_MANPAGE = $(MANUALS)/$(DC).1
108DC_MD = $(DC_MANPAGE).md
109BCL_MANPAGE_NAME = bcl.3
110BCL_MANPAGE = $(MANUALS)/$(BCL_MANPAGE_NAME)
111BCL_MD = $(BCL_MANPAGE).md
112
113MANPAGE_INSTALL_ARGS = -Dm644
114BINARY_INSTALL_ARGS = -Dm755
115
116BCL_HEADER_NAME = bcl.h
117BCL_HEADER = include/$(BCL_HEADER_NAME)
118
119%%DESTDIR%%
120BINDIR = %%BINDIR%%
121INCLUDEDIR = %%INCLUDEDIR%%
122LIBDIR = %%LIBDIR%%
123MAN1DIR = %%MAN1DIR%%
124MAN3DIR = %%MAN3DIR%%
125MAIN_EXEC = $(EXEC_PREFIX)$(%%MAIN_EXEC%%)$(EXEC_SUFFIX)
126EXEC = $(%%EXEC%%)
127NLSPATH = %%NLSPATH%%
128
129BC_BUILD_TYPE = %%BUILD_TYPE%%
130
131BC_ENABLE_LIBRARY = %%LIBRARY%%
132
133BC_ENABLE_HISTORY = %%HISTORY%%
134BC_ENABLE_EXTRA_MATH_NAME = BC_ENABLE_EXTRA_MATH
135BC_ENABLE_EXTRA_MATH = %%EXTRA_MATH%%
136BC_ENABLE_NLS = %%NLS%%
137BC_ENABLE_PROMPT = %%PROMPT%%
138BC_LONG_BIT = %%LONG_BIT%%
139
140BC_ENABLE_AFL = %%FUZZ%%
141BC_ENABLE_MEMCHECK = %%MEMCHECK%%
142
143RM = rm
144MKDIR = mkdir
145
146INSTALL = ./exec-install.sh
147SAFE_INSTALL = ./safe-install.sh
148LINK = ./link.sh
149MANPAGE = ./manpage.sh
150KARATSUBA = ./karatsuba.py
151LOCALE_INSTALL = ./locale_install.sh
152LOCALE_UNINSTALL = ./locale_uninstall.sh
153
154VALGRIND_ARGS = --error-exitcode=100 --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=all
155
156TEST_STARS = "***********************************************************************"
157
158BC_NUM_KARATSUBA_LEN = %%KARATSUBA_LEN%%
159
160CPPFLAGS1 = -D$(BC_ENABLED_NAME)=$(BC_ENABLED) -D$(DC_ENABLED_NAME)=$(DC_ENABLED)
161CPPFLAGS2 = $(CPPFLAGS1) -I./include/ -DBUILD_TYPE=$(BC_BUILD_TYPE) %%LONG_BIT_DEFINE%%
162CPPFLAGS3 = $(CPPFLAGS2) -DEXECPREFIX=$(EXEC_PREFIX) -DMAINEXEC=$(MAIN_EXEC)
163CPPFLAGS4 = $(CPPFLAGS3) -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700
164CPPFLAGS5 = $(CPPFLAGS4) -DBC_NUM_KARATSUBA_LEN=$(BC_NUM_KARATSUBA_LEN)
165CPPFLAGS6 = $(CPPFLAGS5) -DBC_ENABLE_NLS=$(BC_ENABLE_NLS) -DBC_ENABLE_PROMPT=$(BC_ENABLE_PROMPT)
166CPPFLAGS7 = $(CPPFLAGS6) -D$(BC_ENABLE_EXTRA_MATH_NAME)=$(BC_ENABLE_EXTRA_MATH)
167CPPFLAGS8 = $(CPPFLAGS7) -DBC_ENABLE_HISTORY=$(BC_ENABLE_HISTORY) -DBC_ENABLE_LIBRARY=$(BC_ENABLE_LIBRARY)
168CPPFLAGS = $(CPPFLAGS8) -DBC_ENABLE_MEMCHECK=$(BC_ENABLE_MEMCHECK) -DBC_ENABLE_AFL=$(BC_ENABLE_AFL)
169CFLAGS = $(CPPFLAGS) %%CPPFLAGS%% %%CFLAGS%%
170LDFLAGS = %%LDFLAGS%%
171
172HOSTCFLAGS = %%HOSTCFLAGS%%
173
174CC = %%CC%%
175HOSTCC = %%HOSTCC%%
176
177BC_LIB_C_ARGS = bc_lib bc_lib_name $(BC_ENABLED_NAME) 1
178BC_LIB2_C_ARGS = bc_lib2 bc_lib2_name "$(BC_ENABLED_NAME) && $(BC_ENABLE_EXTRA_MATH_NAME)" 1
179
180OBJS = $(DC_HELP_O) $(BC_HELP_O) $(BC_LIB_O) $(BC_LIB2_O) $(OBJ)
181
182all: %%DEFAULT_TARGET%%
183
184%%DEFAULT_TARGET%%: %%DEFAULT_TARGET_PREREQS%%
185	%%DEFAULT_TARGET_CMD%%
186
187%%SECOND_TARGET%%: %%SECOND_TARGET_PREREQS%%
188	%%SECOND_TARGET_CMD%%
189
190$(GEN_EXEC):
191	%%GEN_EXEC_TARGET%%
192
193$(BC_LIB_C): $(GEN_EXEC) $(BC_LIB)
194	$(GEN_EMU) $(GEN_EXEC) $(BC_LIB) $(BC_LIB_C) $(BC_LIB_C_ARGS)
195
196$(BC_LIB_O): $(BC_LIB_C)
197	$(CC) $(CFLAGS) -o $@ -c $<
198
199$(BC_LIB2_C): $(GEN_EXEC) $(BC_LIB2)
200	$(GEN_EMU) $(GEN_EXEC) $(BC_LIB2) $(BC_LIB2_C) $(BC_LIB2_C_ARGS)
201
202$(BC_LIB2_O): $(BC_LIB2_C)
203	$(CC) $(CFLAGS) -o $@ -c $<
204
205$(BC_HELP_C): $(GEN_EXEC) $(BC_HELP)
206	$(GEN_EMU) $(GEN_EXEC) $(BC_HELP) $(BC_HELP_C) bc_help "" $(BC_ENABLED_NAME)
207
208$(BC_HELP_O): $(BC_HELP_C)
209	$(CC) $(CFLAGS) -o $@ -c $<
210
211$(DC_HELP_C): $(GEN_EXEC) $(DC_HELP)
212	$(GEN_EMU) $(GEN_EXEC) $(DC_HELP) $(DC_HELP_C) dc_help "" $(DC_ENABLED_NAME)
213
214$(DC_HELP_O): $(DC_HELP_C)
215	$(CC) $(CFLAGS) -o $@ -c $<
216
217$(BIN):
218	$(MKDIR) -p $(BIN)
219
220headers: %%HEADERS%%
221
222help:
223	@printf 'available targets:\n'
224	@printf '\n'
225	@printf '    all (default)   builds %%EXECUTABLES%%\n'
226	@printf '    check           alias for `make test`\n'
227	@printf '    clean           removes all build files\n'
228	@printf '    clean_config    removes all build files as well as the generated Makefile\n'
229	@printf '    clean_tests     removes all build files, the generated Makefile,\n'
230	@printf '                    and generated tests\n'
231	@printf '    install         installs binaries to "%s%s"\n' "$(DESTDIR)" "$(BINDIR)"
232	@printf '                    and (if enabled) manpages to "%s%s"\n' "$(DESTDIR)" "$(MAN1DIR)"
233	@printf '    karatsuba       runs the karatsuba script (requires Python 3)\n'
234	@printf '    karatsuba_test  runs the karatsuba script while running tests\n'
235	@printf '                    (requires Python 3)\n'
236	@printf '    uninstall       uninstalls binaries from "%s%s"\n' "$(DESTDIR)" "$(BINDIR)"
237	@printf '                    and (if enabled) manpages from "%s%s"\n' "$(DESTDIR)" "$(MAN1DIR)"
238	@printf '    test            runs the test suite\n'
239	@printf '    test_bc         runs the bc test suite, if bc has been built\n'
240	@printf '    test_dc         runs the dc test suite, if dc has been built\n'
241	@printf '    time_test       runs the test suite, displaying times for some things\n'
242	@printf '    time_test_bc    runs the bc test suite, displaying times for some things\n'
243	@printf '    time_test_dc    runs the dc test suite, displaying times for some things\n'
244	@printf '    timeconst       runs the test on the Linux timeconst.bc script,\n'
245	@printf '                    if it exists and bc has been built\n'
246	@printf '    valgrind        runs the test suite through valgrind\n'
247	@printf '    valgrind_bc     runs the bc test suite, if bc has been built,\n'
248	@printf '                    through valgrind\n'
249	@printf '    valgrind_dc     runs the dc test suite, if dc has been built,\n'
250	@printf '                    through valgrind\n'
251
252run_all_tests:
253	%%BC_ALL_TESTS%%
254	%%TIMECONST_ALL_TESTS%%
255	%%DC_ALL_TESTS%%
256
257check: test
258
259test: %%TESTS%%
260
261test_bc: test_bc_header test_bc_tests test_bc_scripts test_bc_stdin test_bc_read test_bc_errors test_bc_other
262	@printf '\nAll bc tests passed.\n\n$(TEST_STARS)\n'
263
264test_bc_tests:%%BC_TESTS%%
265
266test_bc_scripts:%%BC_SCRIPT_TESTS%%
267
268test_bc_stdin:
269	@sh tests/stdin.sh bc %%BC_TEST_EXEC%%
270
271test_bc_read:
272	@sh tests/read.sh bc %%BC_TEST_EXEC%%
273
274test_bc_errors:
275	@sh tests/errors.sh bc %%BC_TEST_EXEC%%
276
277test_bc_other:
278	@sh tests/other.sh bc %%BC_TEST_EXEC%%
279
280test_bc_header:
281	@printf '$(TEST_STARS)\n\nRunning bc tests...\n\n'
282
283test_dc: test_dc_header test_dc_tests test_dc_scripts test_dc_stdin test_dc_read test_dc_errors test_dc_other
284	@printf '\nAll dc tests passed.\n\n$(TEST_STARS)\n'
285
286test_dc_tests:%%DC_TESTS%%
287
288test_dc_scripts:%%DC_SCRIPT_TESTS%%
289
290test_dc_stdin:
291	@sh tests/stdin.sh dc %%DC_TEST_EXEC%%
292
293test_dc_read:
294	@sh tests/read.sh dc %%DC_TEST_EXEC%%
295
296test_dc_errors:
297	@sh tests/errors.sh dc %%DC_TEST_EXEC%%
298
299test_dc_other:
300	@sh tests/other.sh dc %%DC_TEST_EXEC%%
301
302test_dc_header:
303	@printf '$(TEST_STARS)\n\nRunning dc tests...\n\n'
304
305timeconst:
306	%%TIMECONST%%
307
308library_test: $(LIBBC)
309	$(CC) $(CFLAGS) $(BCL_TEST_C) $(LIBBC) -o $(BCL_TEST)
310
311test_library: library_test
312	$(BCL_TEST)
313
314karatsuba:
315	%%KARATSUBA%%
316
317karatsuba_test:
318	%%KARATSUBA_TEST%%
319
320coverage_output:
321	%%COVERAGE_OUTPUT%%
322
323coverage:%%COVERAGE_PREREQS%%
324
325libcname:
326	@printf '%s' "$(BC_LIB_C)"
327
328extra_math:
329	@printf '%s' "$(BC_ENABLE_EXTRA_MATH)"
330
331manpages:
332	$(MANPAGE) bc
333	$(MANPAGE) dc
334	$(MANPAGE) bcl
335
336clean_gen:
337	@$(RM) -f $(GEN_EXEC)
338
339clean:%%CLEAN_PREREQS%%
340	@printf 'Cleaning files...\n'
341	@$(RM) -f src/*.tmp gen/*.tmp
342	@$(RM) -f $(OBJ)
343	@$(RM) -f $(BC_EXEC)
344	@$(RM) -f $(DC_EXEC)
345	@$(RM) -fr $(BIN)
346	@$(RM) -f $(LOCALES)/*.cat
347	@$(RM) -f $(BC_LIB_C) $(BC_LIB_O)
348	@$(RM) -f $(BC_LIB2_C) $(BC_LIB2_O)
349	@$(RM) -f $(BC_HELP_C) $(BC_HELP_O)
350	@$(RM) -f $(DC_HELP_C) $(DC_HELP_O)
351	@$(RM) -fr $(BC_TEST_OUTPUTS) $(DC_TEST_OUTPUTS)
352	@$(RM) -fr $(BC_FUZZ_OUTPUTS) $(DC_FUZZ_OUTPUTS)
353	@$(RM) -fr Debug/ Release/
354
355clean_config: clean
356	@printf 'Cleaning config...\n'
357	@$(RM) -f Makefile
358	@$(RM) -f $(BC_MD) $(DC_MD)
359	@$(RM) -f $(BC_MANPAGE) $(DC_MANPAGE)
360
361clean_coverage:
362	@printf 'Cleaning coverage files...\n'
363	@$(RM) -f *.gcov
364	@$(RM) -f *.html
365	@$(RM) -f *.gcda *.gcno
366	@$(RM) -f *.profraw
367	@$(RM) -f $(GCDA) $(GCNO)
368	@$(RM) -f $(BC_GCDA) $(BC_GCNO)
369	@$(RM) -f $(DC_GCDA) $(DC_GCNO)
370	@$(RM) -f $(HISTORY_GCDA) $(HISTORY_GCNO)
371	@$(RM) -f $(RAND_GCDA) $(RAND_GCNO)
372	@$(RM) -f $(BC_LIB_GCDA) $(BC_LIB_GCNO)
373	@$(RM) -f $(BC_LIB2_GCDA) $(BC_LIB2_GCNO)
374	@$(RM) -f $(BC_HELP_GCDA) $(BC_HELP_GCNO)
375	@$(RM) -f $(DC_HELP_GCDA) $(DC_HELP_GCNO)
376
377clean_tests: clean clean_config clean_coverage
378	@printf 'Cleaning test files...\n'
379	@$(RM) -f tests/bc/parse.txt tests/bc/parse_results.txt
380	@$(RM) -f tests/bc/print.txt tests/bc/print_results.txt
381	@$(RM) -f tests/bc/bessel.txt tests/bc/bessel_results.txt
382	@$(RM) -f tests/bc/scripts/bessel.txt
383	@$(RM) -f tests/bc/scripts/parse.txt
384	@$(RM) -f tests/bc/scripts/print.txt
385	@$(RM) -f tests/bc/scripts/add.txt
386	@$(RM) -f tests/bc/scripts/divide.txt
387	@$(RM) -f tests/bc/scripts/multiply.txt
388	@$(RM) -f tests/bc/scripts/subtract.txt
389	@$(RM) -f tests/dc/scripts/prime.txt tests/dc/scripts/stream.txt
390	@$(RM) -f .log_*.txt
391	@$(RM) -f .math.txt .results.txt .ops.txt
392	@$(RM) -f .test.txt
393	@$(RM) -f tags .gdbbreakpoints .gdb_history .gdbsetup
394	@$(RM) -f cscope.*
395	@$(RM) -f bc.old
396
397install_locales:
398	%%INSTALL_LOCALES%%
399
400install_bc_manpage:
401	$(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(BC_MANPAGE) $(DESTDIR)$(MAN1DIR)/$(BC_MANPAGE_NAME)
402
403install_dc_manpage:
404	$(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(DC_MANPAGE) $(DESTDIR)$(MAN1DIR)/$(DC_MANPAGE_NAME)
405
406install_bcl_manpage:
407	$(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(BCL_MANPAGE) $(DESTDIR)$(MAN3DIR)/$(BCL_MANPAGE_NAME)
408
409install_bcl_header:
410	$(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(BCL_HEADER) $(DESTDIR)$(INCLUDEDIR)/$(BCL_HEADER_NAME)
411
412install_execs:
413	$(INSTALL) $(DESTDIR)$(BINDIR) "$(EXEC_SUFFIX)"
414
415install_library:
416	$(SAFE_INSTALL) $(BINARY_INSTALL_ARGS) $(LIBBC) $(DESTDIR)$(LIBDIR)/$(LIB_NAME)
417
418install:%%INSTALL_LOCALES_PREREQS%%%%INSTALL_MAN_PREREQS%%%%INSTALL_PREREQS%%
419
420uninstall_locales:
421	$(LOCALE_UNINSTALL) $(NLSPATH) $(MAIN_EXEC) $(DESTDIR)
422
423uninstall_bc_manpage:
424	$(RM) -f $(DESTDIR)$(MAN1DIR)/$(BC_MANPAGE_NAME)
425
426uninstall_bc:
427	$(RM) -f $(DESTDIR)$(BINDIR)/$(EXEC_PREFIX)$(BC)$(EXEC_SUFFIX)
428
429uninstall_dc_manpage:
430	$(RM) -f $(DESTDIR)$(MAN1DIR)/$(DC_MANPAGE_NAME)
431
432uninstall_dc:
433	$(RM) -f $(DESTDIR)$(BINDIR)/$(EXEC_PREFIX)$(DC)$(EXEC_SUFFIX)
434
435uninstall_library:
436	$(RM) -f $(DESTDIR)$(LIBDIR)/$(LIB_NAME)
437
438uninstall_bcl_header:
439	$(RM) -f $(DESTDIR)$(INCLUDEDIR)/$(BCL_HEADER_NAME)
440
441uninstall_bcl_manpage:
442	$(RM) -f $(DESTDIR)$(MAN3DIR)/$(BCL_MANPAGE_NAME)
443
444uninstall:%%UNINSTALL_LOCALES_PREREQS%%%%UNINSTALL_MAN_PREREQS%%%%UNINSTALL_PREREQS%%
445