aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2024-05-22 08:58:13 +0100
committerRon Yorston <rmy@pobox.com>2024-05-22 08:58:13 +0100
commiteaa8695b76849d8156826a8919a591555c81a3dd (patch)
tree3725f00acff066c1a217705b4df6be52448a6957
parenta643b5541b71ce5413739a93a560e097947afece (diff)
downloadbusybox-w32-eaa8695b76849d8156826a8919a591555c81a3dd.tar.gz
busybox-w32-eaa8695b76849d8156826a8919a591555c81a3dd.tar.bz2
busybox-w32-eaa8695b76849d8156826a8919a591555c81a3dd.zip
make: add support for CURDIR macro
Austin Group defect report 1626 introduced support for the CURDIR macro: https://www.austingroupbugs.net/view.php?id=1626 Implement this as a POSIX 202X feature. Adds 160-176 bytes.
-rw-r--r--miscutils/make.c33
-rwxr-xr-xtestsuite/make.tests25
2 files changed, 53 insertions, 5 deletions
diff --git a/miscutils/make.c b/miscutils/make.c
index fca00d8e3..f594e196b 100644
--- a/miscutils/make.c
+++ b/miscutils/make.c
@@ -2681,9 +2681,10 @@ expand_makeflags(void)
2681} 2681}
2682 2682
2683// These macros require special treatment 2683// These macros require special treatment
2684#define MAKEFLAGS_SHELL "MAKEFLAGS\0SHELL\0" 2684#define SPECIAL_MACROS "MAKEFLAGS\0SHELL\0CURDIR\0"
2685#define MAKEFLAGS 0 2685#define MAKEFLAGS 0
2686#define SHELL 1 2686#define SHELL 1
2687#define CURDIR 2
2687 2688
2688/* 2689/*
2689 * Instantiate all macros in an argv-style array of pointers. Stop 2690 * Instantiate all macros in an argv-style array of pointers. Stop
@@ -2697,7 +2698,7 @@ process_macros(char **argv, int level)
2697 char *p; 2698 char *p;
2698 2699
2699 for (; *argv; argv++) { 2700 for (; *argv; argv++) {
2700 int immediate = 0; 2701 int idx, immediate = 0;
2701 2702
2702 if (!(p = strchr(*argv, '='))) { 2703 if (!(p = strchr(*argv, '='))) {
2703 // Skip targets on the command line 2704 // Skip targets on the command line
@@ -2714,8 +2715,15 @@ process_macros(char **argv, int level)
2714 immediate = M_IMMEDIATE; 2715 immediate = M_IMMEDIATE;
2715 p[-2] = '\0'; 2716 p[-2] = '\0';
2716 } else 2717 } else
2717 *p = '\0'; 2718 *p = '\0';
2718 if (level != 3 || index_in_strings(MAKEFLAGS_SHELL, *argv) < 0) { 2719
2720 /* We want to process _most_ macro assignments.
2721 * There are exceptions for particular values from the
2722 * environment (level 3). */
2723 idx = index_in_strings(SPECIAL_MACROS, *argv);
2724 if (!(level == 3 &&
2725 (idx == MAKEFLAGS || idx == SHELL ||
2726 (idx == CURDIR && !useenv && !POSIX_2017)))) {
2719 if (immediate) { 2727 if (immediate) {
2720 char *exp = expand_macros(p + 1, FALSE); 2728 char *exp = expand_macros(p + 1, FALSE);
2721 setmacro(*argv, exp, level | immediate); 2729 setmacro(*argv, exp, level | immediate);
@@ -2724,6 +2732,7 @@ process_macros(char **argv, int level)
2724 setmacro(*argv, p + 1, level); 2732 setmacro(*argv, p + 1, level);
2725 } 2733 }
2726 } 2734 }
2735
2727 *p = '='; 2736 *p = '=';
2728 if (immediate) 2737 if (immediate)
2729 p[-2] = ':'; 2738 p[-2] = ':';
@@ -2766,7 +2775,7 @@ update_makeflags(void)
2766 for (i = 0; i < HTABSIZE; ++i) { 2775 for (i = 0; i < HTABSIZE; ++i) {
2767 for (mp = macrohead[i]; mp; mp = mp->m_next) { 2776 for (mp = macrohead[i]; mp; mp = mp->m_next) {
2768 if (mp->m_level == 1 || mp->m_level == 2) { 2777 if (mp->m_level == 1 || mp->m_level == 2) {
2769 int idx = index_in_strings(MAKEFLAGS_SHELL, mp->m_name); 2778 int idx = index_in_strings(SPECIAL_MACROS, mp->m_name);
2770 if (idx == MAKEFLAGS) 2779 if (idx == MAKEFLAGS)
2771 continue; 2780 continue;
2772 macro = xzalloc(strlen(mp->m_name) + 2 * strlen(mp->m_val) + 1); 2781 macro = xzalloc(strlen(mp->m_name) + 2 * strlen(mp->m_val) + 1);
@@ -2960,6 +2969,20 @@ int make_main(int argc UNUSED_PARAM, char **argv)
2960 2969
2961 setmacro("SHELL", DEFAULT_SHELL, 4); 2970 setmacro("SHELL", DEFAULT_SHELL, 4);
2962 setmacro("MAKE", path, 4); 2971 setmacro("MAKE", path, 4);
2972 if (!POSIX_2017) {
2973 char *cwd = xrealloc_getcwd_or_warn(NULL);
2974
2975 if (cwd) {
2976 if (!useenv) {
2977 // Export cwd to environment, if necessary
2978 char *envcwd = getenv("CURDIR");
2979 if (envcwd && strcmp(cwd, envcwd) != 0)
2980 setenv("CURDIR", cwd, 1);
2981 }
2982 setmacro("CURDIR", cwd, 4);
2983 }
2984 free(cwd);
2985 }
2963 free((void *)newpath); 2986 free((void *)newpath);
2964 2987
2965 if (!makefiles) { // Look for a default Makefile 2988 if (!makefiles) { // Look for a default Makefile
diff --git a/testsuite/make.tests b/testsuite/make.tests
index 546dca7d5..fdbb4ccfc 100755
--- a/testsuite/make.tests
+++ b/testsuite/make.tests
@@ -377,6 +377,31 @@ target:
377b} 377b}
378' 378'
379 379
380# The CURDIR macro is supported in POSIX 202X.
381testing "make CURDIR macro" \
382 "make -f -" \
383 "OK\n" "" '
384target:
385 @test "$(CURDIR)" = "$$(pwd)" && echo OK
386'
387# The CURDIR environment variable doesn't affect the macro
388export CURDIR=/you/are/here
389testing "make CURDIR macro not affected by environment" \
390 "make -f -" \
391 "OK\n" "" '
392target:
393 @test "$(CURDIR)" != "/you/are/here" && echo OK
394'
395
396# The -e option makes the CURDIR macro match the environment
397testing "make with -e CURDIR macro is affected by the environment" \
398 "make -e -f -" \
399 "/you/are/here\n" "" '
400target:
401 @echo $(CURDIR)
402'
403unset CURDIR
404
380# POSIX 202X permits additional characters in macro and target names 405# POSIX 202X permits additional characters in macro and target names
381testing "make allow - and / in target names, - in macro names" \ 406testing "make allow - and / in target names, - in macro names" \
382 "make -f -" \ 407 "make -f -" \