2 # SPDX-License-Identifier: BSD-2-Clause
4 # Copyright (c) 2018-2021 Gavin D. Howard and contributors.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
9 # * Redistributions of source code must retain the above copyright notice, this
10 # list of conditions and the following disclaimer.
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.
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.
39 BC_ENABLED_NAME = BC_ENABLED
40 BC_ENABLED = %%BC_ENABLED%%
41 DC_ENABLED_NAME = DC_ENABLED
42 DC_ENABLED = %%DC_ENABLED%%
44 HEADERS = 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
45 BC_HEADERS = include/bc.h
46 DC_HEADERS = include/dc.h
47 HISTORY_HEADERS = include/history.h
48 EXTRA_MATH_HEADERS = include/rand.h
49 LIBRARY_HEADERS = include/bcl.h include/library.h
53 GEN_EXEC = $(GEN_DIR)/$(GEN)
54 GEN_C = $(GEN_DIR)/$(GEN).c
58 BC_LIB = $(GEN_DIR)/lib.bc
59 BC_LIB_C = $(GEN_DIR)/lib.c
60 BC_LIB_O = %%BC_LIB_O%%
61 BC_LIB_GCDA = $(GEN_DIR)/lib.gcda
62 BC_LIB_GCNO = $(GEN_DIR)/lib.gcno
64 BC_LIB2 = $(GEN_DIR)/lib2.bc
65 BC_LIB2_C = $(GEN_DIR)/lib2.c
66 BC_LIB2_O = %%BC_LIB2_O%%
67 BC_LIB2_GCDA = $(GEN_DIR)/lib2.gcda
68 BC_LIB2_GCNO = $(GEN_DIR)/lib2.gcno
70 BC_HELP = $(GEN_DIR)/bc_help.txt
71 BC_HELP_C = $(GEN_DIR)/bc_help.c
72 BC_HELP_O = %%BC_HELP_O%%
73 BC_HELP_GCDA = $(GEN_DIR)/bc_help.gcda
74 BC_HELP_GCNO = $(GEN_DIR)/bc_help.gcno
76 DC_HELP = $(GEN_DIR)/dc_help.txt
77 DC_HELP_C = $(GEN_DIR)/dc_help.c
78 DC_HELP_O = %%DC_HELP_O%%
79 DC_HELP_GCDA = $(GEN_DIR)/dc_help.gcda
80 DC_HELP_GCNO = $(GEN_DIR)/dc_help.gcno
84 EXEC_SUFFIX = %%EXECSUFFIX%%
85 EXEC_PREFIX = %%EXECPREFIX%%
89 BC_EXEC = $(BIN)/$(EXEC_PREFIX)$(BC)
90 DC_EXEC = $(BIN)/$(EXEC_PREFIX)$(DC)
92 BC_TEST_OUTPUTS = tests/bc_outputs
93 BC_FUZZ_OUTPUTS = tests/fuzzing/bc_outputs1 tests/fuzzing/bc_outputs2 tests/fuzzing/bc_outputs3
94 DC_TEST_OUTPUTS = tests/dc_outputs
95 DC_FUZZ_OUTPUTS = tests/fuzzing/dc_outputs
99 LIBBC = $(BIN)/$(LIB_NAME)
101 BCL_TEST = $(BIN)/$(BCL)
102 BCL_TEST_C = tests/$(BCL).c
105 BC_MANPAGE_NAME = $(EXEC_PREFIX)$(BC)$(EXEC_SUFFIX).1
106 BC_MANPAGE = $(MANUALS)/$(BC).1
107 BC_MD = $(BC_MANPAGE).md
108 DC_MANPAGE_NAME = $(EXEC_PREFIX)$(DC)$(EXEC_SUFFIX).1
109 DC_MANPAGE = $(MANUALS)/$(DC).1
110 DC_MD = $(DC_MANPAGE).md
111 BCL_MANPAGE_NAME = bcl.3
112 BCL_MANPAGE = $(MANUALS)/$(BCL_MANPAGE_NAME)
113 BCL_MD = $(BCL_MANPAGE).md
115 MANPAGE_INSTALL_ARGS = -Dm644
116 BINARY_INSTALL_ARGS = -Dm755
118 BCL_HEADER_NAME = bcl.h
119 BCL_HEADER = include/$(BCL_HEADER_NAME)
123 INCLUDEDIR = %%INCLUDEDIR%%
125 MAN1DIR = %%MAN1DIR%%
126 MAN3DIR = %%MAN3DIR%%
127 MAIN_EXEC = $(EXEC_PREFIX)$(%%MAIN_EXEC%%)$(EXEC_SUFFIX)
129 NLSPATH = %%NLSPATH%%
131 BC_ENABLE_LIBRARY = %%LIBRARY%%
133 BC_ENABLE_HISTORY = %%HISTORY%%
134 BC_ENABLE_EXTRA_MATH_NAME = BC_ENABLE_EXTRA_MATH
135 BC_ENABLE_EXTRA_MATH = %%EXTRA_MATH%%
136 BC_ENABLE_NLS = %%NLS%%
137 BC_ENABLE_PROMPT = %%PROMPT%%
138 BC_LONG_BIT = %%LONG_BIT%%
140 BC_ENABLE_AFL = %%FUZZ%%
141 BC_ENABLE_MEMCHECK = %%MEMCHECK%%
146 INSTALL = ./exec-install.sh
147 SAFE_INSTALL = ./safe-install.sh
149 MANPAGE = ./manpage.sh
150 KARATSUBA = ./karatsuba.py
151 LOCALE_INSTALL = ./locale_install.sh
152 LOCALE_UNINSTALL = ./locale_uninstall.sh
154 VALGRIND_ARGS = --error-exitcode=100 --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=all
156 TEST_STARS = "***********************************************************************"
158 BC_NUM_KARATSUBA_LEN = %%KARATSUBA_LEN%%
160 CPPFLAGS1 = -D$(BC_ENABLED_NAME)=$(BC_ENABLED) -D$(DC_ENABLED_NAME)=$(DC_ENABLED)
161 CPPFLAGS2 = $(CPPFLAGS1) -I./include/ -DVERSION=$(VERSION) %%LONG_BIT_DEFINE%%
162 CPPFLAGS3 = $(CPPFLAGS2) -DEXECPREFIX=$(EXEC_PREFIX) -DMAINEXEC=$(MAIN_EXEC)
163 CPPFLAGS4 = $(CPPFLAGS3) -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700
164 CPPFLAGS5 = $(CPPFLAGS4) -DBC_NUM_KARATSUBA_LEN=$(BC_NUM_KARATSUBA_LEN)
165 CPPFLAGS6 = $(CPPFLAGS5) -DBC_ENABLE_NLS=$(BC_ENABLE_NLS) -DBC_ENABLE_PROMPT=$(BC_ENABLE_PROMPT)
166 CPPFLAGS7 = $(CPPFLAGS6) -D$(BC_ENABLE_EXTRA_MATH_NAME)=$(BC_ENABLE_EXTRA_MATH)
167 CPPFLAGS8 = $(CPPFLAGS7) -DBC_ENABLE_HISTORY=$(BC_ENABLE_HISTORY) -DBC_ENABLE_LIBRARY=$(BC_ENABLE_LIBRARY)
168 CPPFLAGS = $(CPPFLAGS8) -DBC_ENABLE_MEMCHECK=$(BC_ENABLE_MEMCHECK) -DBC_ENABLE_AFL=$(BC_ENABLE_AFL)
169 CFLAGS = $(CPPFLAGS) %%CPPFLAGS%% %%CFLAGS%%
170 LDFLAGS = %%LDFLAGS%%
172 HOSTCFLAGS = %%HOSTCFLAGS%%
177 BC_LIB_C_ARGS = bc_lib bc_lib_name $(BC_ENABLED_NAME) 1
178 BC_LIB2_C_ARGS = bc_lib2 bc_lib2_name "$(BC_ENABLED_NAME) && $(BC_ENABLE_EXTRA_MATH_NAME)" 1
180 OBJS = $(DC_HELP_O) $(BC_HELP_O) $(BC_LIB_O) $(BC_LIB2_O) $(OBJ)
182 all: %%DEFAULT_TARGET%%
184 %%DEFAULT_TARGET%%: %%DEFAULT_TARGET_PREREQS%%
185 %%DEFAULT_TARGET_CMD%%
187 %%SECOND_TARGET%%: %%SECOND_TARGET_PREREQS%%
188 %%SECOND_TARGET_CMD%%
193 $(BC_LIB_C): $(GEN_EXEC) $(BC_LIB)
194 $(GEN_EMU) $(GEN_EXEC) $(BC_LIB) $(BC_LIB_C) $(BC_LIB_C_ARGS)
196 $(BC_LIB_O): $(BC_LIB_C)
197 $(CC) $(CFLAGS) -o $@ -c $<
199 $(BC_LIB2_C): $(GEN_EXEC) $(BC_LIB2)
200 $(GEN_EMU) $(GEN_EXEC) $(BC_LIB2) $(BC_LIB2_C) $(BC_LIB2_C_ARGS)
202 $(BC_LIB2_O): $(BC_LIB2_C)
203 $(CC) $(CFLAGS) -o $@ -c $<
205 $(BC_HELP_C): $(GEN_EXEC) $(BC_HELP)
206 $(GEN_EMU) $(GEN_EXEC) $(BC_HELP) $(BC_HELP_C) bc_help "" $(BC_ENABLED_NAME)
208 $(BC_HELP_O): $(BC_HELP_C)
209 $(CC) $(CFLAGS) -o $@ -c $<
211 $(DC_HELP_C): $(GEN_EXEC) $(DC_HELP)
212 $(GEN_EMU) $(GEN_EXEC) $(DC_HELP) $(DC_HELP_C) dc_help "" $(DC_ENABLED_NAME)
214 $(DC_HELP_O): $(DC_HELP_C)
215 $(CC) $(CFLAGS) -o $@ -c $<
223 @printf 'available targets:\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'
254 %%TIMECONST_ALL_TESTS%%
261 test_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'
264 test_bc_tests:%%BC_TESTS%%
266 test_bc_scripts:%%BC_SCRIPT_TESTS%%
269 @sh tests/stdin.sh bc %%BC_TEST_EXEC%%
272 @sh tests/read.sh bc %%BC_TEST_EXEC%%
275 @sh tests/errors.sh bc %%BC_TEST_EXEC%%
278 @sh tests/other.sh bc %%BC_TEST_EXEC%%
281 @printf '$(TEST_STARS)\n\nRunning bc tests...\n\n'
283 test_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'
286 test_dc_tests:%%DC_TESTS%%
288 test_dc_scripts:%%DC_SCRIPT_TESTS%%
291 @sh tests/stdin.sh dc %%DC_TEST_EXEC%%
294 @sh tests/read.sh dc %%DC_TEST_EXEC%%
297 @sh tests/errors.sh dc %%DC_TEST_EXEC%%
300 @sh tests/other.sh dc %%DC_TEST_EXEC%%
303 @printf '$(TEST_STARS)\n\nRunning dc tests...\n\n'
308 library_test: $(LIBBC)
309 $(CC) $(CFLAGS) $(BCL_TEST_C) $(LIBBC) -o $(BCL_TEST)
311 test_library: library_test
323 coverage:%%COVERAGE_PREREQS%%
326 @printf '%s' "$(VERSION)"
329 @printf '%s' "$(BC_LIB_C)"
332 @printf '%s' "$(BC_ENABLE_EXTRA_MATH)"
340 @$(RM) -f $(GEN_EXEC)
342 clean:%%CLEAN_PREREQS%%
343 @printf 'Cleaning files...\n'
348 @$(RM) -f $(LOCALES)/*.cat
349 @$(RM) -f $(BC_LIB_C) $(BC_LIB_O)
350 @$(RM) -f $(BC_LIB2_C) $(BC_LIB2_O)
351 @$(RM) -f $(BC_HELP_C) $(BC_HELP_O)
352 @$(RM) -f $(DC_HELP_C) $(DC_HELP_O)
353 @$(RM) -fr $(BC_TEST_OUTPUTS) $(DC_TEST_OUTPUTS)
354 @$(RM) -fr $(BC_FUZZ_OUTPUTS) $(DC_FUZZ_OUTPUTS)
357 @printf 'Cleaning config...\n'
359 @$(RM) -f $(BC_MD) $(DC_MD)
360 @$(RM) -f $(BC_MANPAGE) $(DC_MANPAGE)
363 @printf 'Cleaning coverage files...\n'
366 @$(RM) -f *.gcda *.gcno
368 @$(RM) -f $(GCDA) $(GCNO)
369 @$(RM) -f $(BC_GCDA) $(BC_GCNO)
370 @$(RM) -f $(DC_GCDA) $(DC_GCNO)
371 @$(RM) -f $(HISTORY_GCDA) $(HISTORY_GCNO)
372 @$(RM) -f $(RAND_GCDA) $(RAND_GCNO)
373 @$(RM) -f $(BC_LIB_GCDA) $(BC_LIB_GCNO)
374 @$(RM) -f $(BC_LIB2_GCDA) $(BC_LIB2_GCNO)
375 @$(RM) -f $(BC_HELP_GCDA) $(BC_HELP_GCNO)
376 @$(RM) -f $(DC_HELP_GCDA) $(DC_HELP_GCNO)
378 clean_tests: clean clean_config clean_coverage
379 @printf 'Cleaning test files...\n'
380 @$(RM) -f tests/bc/parse.txt tests/bc/parse_results.txt
381 @$(RM) -f tests/bc/print.txt tests/bc/print_results.txt
382 @$(RM) -f tests/bc/bessel.txt tests/bc/bessel_results.txt
383 @$(RM) -f tests/bc/scripts/bessel.txt
384 @$(RM) -f tests/bc/scripts/parse.txt
385 @$(RM) -f tests/bc/scripts/print.txt
386 @$(RM) -f tests/bc/scripts/add.txt
387 @$(RM) -f tests/bc/scripts/divide.txt
388 @$(RM) -f tests/bc/scripts/multiply.txt
389 @$(RM) -f tests/bc/scripts/subtract.txt
390 @$(RM) -f tests/dc/scripts/prime.txt tests/dc/scripts/stream.txt
392 @$(RM) -f .math.txt .results.txt .ops.txt
394 @$(RM) -f tags .gdbbreakpoints .gdb_history .gdbsetup
402 $(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(BC_MANPAGE) $(DESTDIR)$(MAN1DIR)/$(BC_MANPAGE_NAME)
405 $(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(DC_MANPAGE) $(DESTDIR)$(MAN1DIR)/$(DC_MANPAGE_NAME)
408 $(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(BCL_MANPAGE) $(DESTDIR)$(MAN3DIR)/$(BCL_MANPAGE_NAME)
411 $(SAFE_INSTALL) $(MANPAGE_INSTALL_ARGS) $(BCL_HEADER) $(DESTDIR)$(INCLUDEDIR)/$(BCL_HEADER_NAME)
414 $(INSTALL) $(DESTDIR)$(BINDIR) "$(EXEC_SUFFIX)"
417 $(SAFE_INSTALL) $(BINARY_INSTALL_ARGS) $(LIBBC) $(DESTDIR)$(LIBDIR)/$(LIB_NAME)
419 install:%%INSTALL_LOCALES_PREREQS%%%%INSTALL_MAN_PREREQS%%%%INSTALL_PREREQS%%
422 $(LOCALE_UNINSTALL) $(NLSPATH) $(MAIN_EXEC) $(DESTDIR)
424 uninstall_bc_manpage:
425 $(RM) -f $(DESTDIR)$(MAN1DIR)/$(BC_MANPAGE_NAME)
428 $(RM) -f $(DESTDIR)$(BINDIR)/$(EXEC_PREFIX)$(BC)$(EXEC_SUFFIX)
430 uninstall_dc_manpage:
431 $(RM) -f $(DESTDIR)$(MAN1DIR)/$(DC_MANPAGE_NAME)
434 $(RM) -f $(DESTDIR)$(BINDIR)/$(EXEC_PREFIX)$(DC)$(EXEC_SUFFIX)
437 $(RM) -f $(DESTDIR)$(LIBDIR)/$(LIB_NAME)
439 uninstall_bcl_header:
440 $(RM) -f $(DESTDIR)$(INCLUDEDIR)/$(BCL_HEADER_NAME)
442 uninstall_bcl_manpage:
443 $(RM) -f $(DESTDIR)$(MAN3DIR)/$(BCL_MANPAGE_NAME)
445 uninstall:%%UNINSTALL_LOCALES_PREREQS%%%%UNINSTALL_MAN_PREREQS%%%%UNINSTALL_PREREQS%%