make(1): adding sort modifiers

Marcel Moolenaar marcel at xcllnt.net
Tue Sep 16 23:51:29 PDT 2003


Gang,

Attached a patch that adds the functionality to make(1) to sort
the words in a variable. With this functionality and a small
change to bsd.subdir.mk (also attached), we automaticly have
sorted subdirectory recursion, even though it's impossible or
hard to do it in the makesfiles themselves. Is this too evil?

-- 
 Marcel Moolenaar	  USPA: A-39004		 marcel at xcllnt.net
-------------- next part --------------
Index: var.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/make/var.c,v
retrieving revision 1.42
diff -u -r1.42 var.c
--- var.c	15 Jan 2003 22:36:15 -0000	1.42
+++ var.c	17 Sep 2003 06:30:11 -0000
@@ -606,7 +606,38 @@
     return (str);
 }
 
+static char *
+VarSortWords(char *str, int (*cmp)(const void *, const void *))
+{
+	Buffer buf;
+	char **av;
+	int ac, i;
+
+	buf = Buf_Init(0);
+	av = brk_string(str, &ac, FALSE);
+	qsort((void*)(av + 1), ac - 1, sizeof(char*), cmp);
+	for (i = 1; i < ac; i++) {
+		Buf_AddBytes(buf, strlen(av[i]), (Byte *)av[i]);
+		Buf_AddByte(buf, (Byte)((i < ac - 1) ? ' ' : '\0'));
+	}
+	str = (char *)Buf_GetAll(buf, (int *)NULL);
+	Buf_Destroy(buf, FALSE);
+	return (str);
+}
+
+static int
+SortIncreasing(const void *l, const void *r)
+{
+	return (strcmp(*(char**)l, *(char**)r));
+}
+
+static int
+SortDecreasing(const void *l, const void *r)
+{
+	return (-strcmp(*(char**)l, *(char**)r));
+}
+
 /*-
  *-----------------------------------------------------------------------
  * VarGetPattern --
@@ -1448,6 +1479,22 @@
 		    free(pattern.matches);
 		    break;
 		}
+		case '<':
+		    if (tstr[1] == endc || tstr[1] == ':') {
+			newStr = VarSortWords(str, SortIncreasing);
+			cp = tstr + 1;
+			termc = *cp;
+			break;
+		    }
+		    /* FALLTHROUGH */
+		case '>':
+		    if (tstr[1] == endc || tstr[1] == ':') {
+			newStr = VarSortWords(str, SortDecreasing);
+			cp = tstr + 1;
+			termc = *cp;
+			break;
+		    }
+		    /* FALLTHROUGH */
 		case 'Q':
 		    if (tstr[1] == endc || tstr[1] == ':') {
 			newStr = VarQuote (str);
-------------- next part --------------
Index: bsd.subdir.mk
===================================================================
RCS file: /home/ncvs/src/share/mk/bsd.subdir.mk,v
retrieving revision 1.44
diff -u -r1.44 bsd.subdir.mk
--- bsd.subdir.mk	12 Jul 2002 15:09:35 -0000	1.44
+++ bsd.subdir.mk	17 Sep 2003 06:46:57 -0000
@@ -42,7 +42,7 @@
 
 _SUBDIR: .USE
 .if defined(SUBDIR) && !empty(SUBDIR) && !defined(NO_SUBDIR)
-	@for entry in ${SUBDIR}; do \
+	@for entry in ${SUBDIR:<}; do \
 		if test -d ${.CURDIR}/$${entry}.${MACHINE_ARCH}; then \
 			${ECHODIR} "===> ${DIRPRFX}$${entry}.${MACHINE_ARCH}"; \
 			edir=$${entry}.${MACHINE_ARCH}; \


More information about the freebsd-arch mailing list