Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 13 Apr 2002 18:18:34 +0400
From:      Alex Semenyaka <alexs@ratmir.ru>
To:        freebsd-hackers@FreeBSD.ORG
Subject:   make(1) command-line variables
Message-ID:  <20020413141834.GA16339@snark.ratmir.ru>

next in thread | raw e-mail | index | archive | help
Dear Colleagues,

I need some help. Consider I have a Makefile for
application that can be build with different 
options. Some of them I need just to define
via -D flag of the ``make'', but other need
to be set to some specific values (for example,
it can be path to my temporary dir). So I
use

make -DFIRST MYTMPDIR=/special/tmp

And I want just to keep track of the parameters
used to build that application. Nothing difficult
to obtain the name of Makefile from .MAKEFILE, as well
as make flags of make (including those -D, defining
some variables) from the .MAKEFLAGS var. But I did
NOT found the normal way to obtain the list of
variables defined in the command line through the
VAR=VAL construction. Strange, right?

Man says that

     .MAKEFLAGS
                The environment variable MAKEFLAGS may contain anything that
                may be specified on make's command line.  Its contents are
                stored in make's .MAKEFLAGS variable.

That is wrong, .MAKEFLAGS does not contain anything.
It _is_ reasonable to cut the -f flag from there, for
example. But a bit strange to cut the value assignments,
I think. Mayby I am wrong so I would appreciate explanation why <g>

Actually I did a small patch to gather the information about
that variables (with quoted values where necessary) into the
new internal variable .MAKECMDVARS; if people will tell me that
this is the Good Thing or explain how to do the Good Thing from
it - I would glad to make final variant and to share it with other.
Just a note: I decided not to change the .MAKEFLAGS variable just for
the backward compatibility. Of course there is nothing special to
store same information there (as well or instead).

Here is my current patch:

diff -u /usr/src/usr.bin/make/main.c Make/main.c
--- /usr/src/usr.bin/make/main.c	Sun Mar  4 12:40:32 2001
+++ Make/main.c	Sat Apr 13 13:10:53 2002
@@ -113,6 +113,7 @@
 #endif	/* DEFMAXLOCAL */
 
 #define	MAKEFLAGS	".MAKEFLAGS"
+#define	MAKECMDVARS	".MAKECMDVARS"
 
 Lst			create;		/* Targets to be made */
 time_t			now;		/* Time at start of make */
@@ -171,8 +172,9 @@
 {
 	extern int optind;
 	extern char *optarg;
+	char *peq, *pval, *escvar;
 	char *p;
-	int c;
+	int c, offs;
 
 	optind = 1;	/* since we're called more than once */
 #ifdef REMOTE
@@ -369,8 +371,21 @@
 	 * on the end of the "create" list.
 	 */
 	for (argv += optind, argc -= optind; *argv; ++argv, --argc)
-		if (Parse_IsVar(*argv))
-			Parse_DoVar(*argv, VAR_CMD);
+		if (Parse_IsVar(*argv)) {
+			 peq  = strchr(*argv,'=') + 1;
+			 offs = (int) (peq - (*argv));
+			 pval = VarQuote(peq);
+
+			 escvar = malloc(offs + strlen(pval) + 1);
+			 strncpy(escvar, *argv, offs);
+			 strcat(escvar, pval);
+
+			 Var_Append(MAKECMDVARS, escvar, VAR_GLOBAL);
+
+			 free(pval); free(escvar);
+
+			 Parse_DoVar(*argv, VAR_CMD);
+			}
 		else {
 			if (!**argv)
 				Punt("illegal (null) argument.");
diff -u /usr/src/usr.bin/make/nonints.h Make/nonints.h
--- /usr/src/usr.bin/make/nonints.h	Sat Aug 28 05:03:35 1999
+++ Make/nonints.h	Sat Apr 13 12:54:41 2002
@@ -146,3 +146,4 @@
 void Var_Init __P((void));
 void Var_End __P((void));
 void Var_Dump __P((GNode *));
+char *VarQuote __P((char *));
diff -u /usr/src/usr.bin/make/var.c Make/var.c
--- /usr/src/usr.bin/make/var.c	Thu Feb 28 02:43:45 2002
+++ Make/var.c	Sat Apr 13 12:58:06 2002
@@ -188,7 +188,6 @@
 static Boolean VarSubstitute __P((char *, Boolean, Buffer, ClientData));
 static char *VarGetPattern __P((GNode *, int, char **, int, int *, int *,
 				VarPattern *));
-static char *VarQuote __P((char *));
 static char *VarModify __P((char *, Boolean (*)(char *, Boolean, Buffer,
 						ClientData),
 			    ClientData));
@@ -1411,7 +1410,7 @@
  *
  *-----------------------------------------------------------------------
  */
-static char *
+char *
 VarQuote(str)
 	char *str;
 {


Sincerely yours,
Alex Semenyaka.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020413141834.GA16339>