[Rpm-maint] [PATCH] rpmbuild: clean up buildroot handling, set a default buildroot

Bill Nottingham notting at redhat.com
Mon Feb 12 20:37:23 UTC 2007


The attached:

- moves buildroot setting into a single function, called when needed
- cleans up some of the noise
- allows setting of %{buildroot} to work in spec files without BuildRoot:
- sets a default buildroot if there isn't one set elsewhere

AKA, the 'stop the silly buildroot debates' patch.

Bill
-------------- next part --------------
diff -ru rpm-4.4.2-dist/build/parsePreamble.c rpm-4.4.2/build/parsePreamble.c
--- rpm-4.4.2-dist/build/parsePreamble.c	2005-07-12 06:44:26.000000000 -0400
+++ rpm-4.4.2/build/parsePreamble.c	2007-02-12 15:23:48.000000000 -0500
@@ -551,50 +551,17 @@
 	break;
     case RPMTAG_BUILDROOT:
 	SINGLE_TOKEN_ONLY;
-      {	const char * buildRoot = NULL;
-	const char * buildRootURL = spec->buildRootURL;
-
-	/*
-	 * Note: rpmGenPath should guarantee a "canonical" path. That means
-	 * that the following pathologies should be weeded out:
-	 *          //bin//sh
-	 *          //usr//bin/
-	 *          /.././../usr/../bin//./sh
-	 */
-	if (buildRootURL == NULL) {
-	    buildRootURL = rpmGenPath(NULL, "%{?buildroot:%{buildroot}}", NULL);
-	    if (strcmp(buildRootURL, "/")) {
-		spec->buildRootURL = buildRootURL;
-		macro = NULL;
-	    } else {
-		const char * specURL = field;
-
-		buildRootURL = _free(buildRootURL);
-		(void) urlPath(specURL, (const char **)&field);
-		/*@-branchstate@*/
-		if (*field == '\0') field = "/";
-		/*@=branchstate@*/
-		buildRootURL = rpmGenPath(spec->rootURL, field, NULL);
-		spec->buildRootURL = buildRootURL;
-		field = (char *) buildRootURL;
-	    }
-	    spec->gotBuildRootURL = 1;
-	} else {
-	    macro = NULL;
-	}
-	buildRootURL = rpmGenPath(NULL, spec->buildRootURL, NULL);
-	(void) urlPath(buildRootURL, &buildRoot);
-	/*@-branchstate@*/
-	if (*buildRoot == '\0') buildRoot = "/";
-	/*@=branchstate@*/
-	if (!strcmp(buildRoot, "/")) {
-	    rpmError(RPMERR_BADSPEC,
-		     _("BuildRoot can not be \"/\": %s\n"), spec->buildRootURL);
-	    buildRootURL = _free(buildRootURL);
-	    return RPMERR_BADSPEC;
+	if (spec->buildRootURL == NULL) {
+	    int err;
+	    
+	    err = setBuildRoot(spec, field);
+	    
+	    if (err)
+	        return err;
 	}
-	buildRootURL = _free(buildRootURL);
-      }	break;
+	/* already set by setBuildRoot */
+	macro = NULL;
+	break;
     case RPMTAG_PREFIXES:
 	addOrAppendListEntry(pkg->header, tag, field);
 	xx = hge(pkg->header, tag, &type, (void **)&array, &num);
diff -ru rpm-4.4.2-dist/build/parseSpec.c rpm-4.4.2/build/parseSpec.c
--- rpm-4.4.2-dist/build/parseSpec.c	2004-10-09 14:23:00.000000000 -0400
+++ rpm-4.4.2/build/parseSpec.c	2007-02-12 15:25:18.000000000 -0500
@@ -429,6 +429,28 @@
     }
 }
 
+/*@=boundswrite@*/
+int setBuildRoot(Spec spec, const char *buildRootURL)
+{
+    char *buildRoot = NULL;
+    
+    if (!buildRootURL)
+        return 0;
+    (void) urlPath(buildRootURL, (const char **)&buildRoot);
+    
+    if (*buildRoot == '\0') {
+        rpmError(RPMERR_BADSPEC,
+                 _("BuildRoot can not be \"/\": %s\n"), buildRootURL);
+        return RPMERR_BADSPEC;
+    }
+    
+    spec->gotBuildRootURL = 1;
+    spec->buildRootURL = xstrdup(buildRootURL);
+    addMacro(spec->macros, "buildroot", NULL, buildRoot, RMIL_SPEC);
+    
+    return 0;
+}
+
 /*@-redecl@*/
 /*@unchecked@*/
 extern int noLang;		/* XXX FIXME: pass as arg */
@@ -461,20 +483,23 @@
     spec->specFile = rpmGetPath(specFile, NULL);
     spec->fileStack = newOpenFileInfo();
     spec->fileStack->fileName = xstrdup(spec->specFile);
+	
+    
+    if (!buildRootURL) {
+        const char *tmpURL = rpmGenPath(NULL, "%{?buildroot:%{buildroot}}", NULL);
+        
+        if (strcmp(tmpURL,"/")) {
+            buildRootURL = tmpURL;
+        }
+    }
+    
     if (buildRootURL) {
-	const char * buildRoot;
-	(void) urlPath(buildRootURL, &buildRoot);
-	/*@-branchstate@*/
-	if (*buildRoot == '\0') buildRoot = "/";
-	/*@=branchstate@*/
-	if (!strcmp(buildRoot, "/")) {
-            rpmError(RPMERR_BADSPEC,
-                     _("BuildRoot can not be \"/\": %s\n"), buildRootURL);
-            return RPMERR_BADSPEC;
+	int err;    
+	    
+	err = setBuildRoot(spec, buildRootURL);
+	if (err) {
+	    return err;
         }
-	spec->gotBuildRootURL = 1;
-	spec->buildRootURL = xstrdup(buildRootURL);
-	addMacro(spec->macros, "buildroot", NULL, buildRoot, RMIL_SPEC);
     }
     addMacro(NULL, "_docdir", NULL, "%{_defaultdocdir}", RMIL_SPEC);
     spec->recursing = recursing;
@@ -621,6 +646,13 @@
     }
     /*@=infloops@*/	/* LCL: parsePart is modified @*/
 
+    /* If buildroot is not set, set to a default value. */
+    if (!spec->gotBuildRootURL) {
+        int err = setBuildRoot(spec, "%{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)");
+        
+        if (err)
+            return err;
+    }
     /* Check for description in each package and add arch and os */
   {
 #ifdef	DYING
diff -ru rpm-4.4.2-dist/build/rpmbuild.h rpm-4.4.2/build/rpmbuild.h
--- rpm-4.4.2-dist/build/rpmbuild.h	2004-10-09 17:58:33.000000000 -0400
+++ rpm-4.4.2/build/rpmbuild.h	2007-02-12 15:20:57.000000000 -0500
@@ -231,6 +231,15 @@
 		rpmGlobalMacroContext, fileSystem, internalState @*/;
 
 /** \ingroup rpmbuild
+ * Parse BuildRoot: tags and/or settings
+ * @param spec		spec file control structure
+ * @param buildRoot	buildRoot to set
+ * @return		0 on success, RPMERR_BADSPEC on failure
+ */
+int setBuildRoot(Spec spec, const char *buildRoot)
+	/*@modifies spec->buildRootURL, spec->gotBuildRootURL @*/;
+
+/** \ingroup rpmbuild
  * Parse %%changelog section of a spec file.
  * @param spec		spec file control structure
  * @return		>= 0 next rpmParseState, < 0 on error


More information about the Rpm-maint mailing list