Permit file objects in python header constructor
[platform/upstream/rpm.git] / build / build.c
index 60fc366..a4a271a 100644 (file)
 
 #include "system.h"
 
-#include <rpmio_internal.h>
-#include <rpmbuild.h>
+#include <rpm/rpmbuild.h>
+#include <rpm/rpmlog.h>
+#include <rpm/rpmfileutil.h>
 
 #include "debug.h"
 
-/*@unchecked@*/
 static int _build_debug = 0;
 
-/*@access StringBuf @*/
-/*@access urlinfo @*/          /* XXX compared with NULL */
-/*@access FD_t @*/
-
 /**
  */
-static void doRmSource(Spec spec)
-       /*@globals rpmGlobalMacroContext,
-               fileSystem@*/
-       /*@modifies fileSystem @*/
+rpmRC doRmSource(rpmSpec spec)
 {
     struct Source *p;
     Package pkg;
-    int rc;
+    int rc = 0;
     
-#if 0
-    rc = Unlink(spec->specFile);
-#endif
-
     for (p = spec->sources; p != NULL; p = p->next) {
        if (! (p->flags & RPMBUILD_ISNO)) {
-           const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
-           rc = Unlink(fn);
+           char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
+           rc = unlink(fn);
            fn = _free(fn);
+           if (rc) goto exit;
        }
     }
 
     for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
        for (p = pkg->icon; p != NULL; p = p->next) {
            if (! (p->flags & RPMBUILD_ISNO)) {
-               const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
-               rc = Unlink(fn);
+               char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
+               rc = unlink(fn);
                fn = _free(fn);
+               if (rc) goto exit;
            }
        }
     }
+exit:
+    return !rc ? RPMRC_OK : RPMRC_FAIL;
 }
 
 /*
  * @todo Single use by %%doc in files.c prevents static.
  */
-int doScript(Spec spec, int what, const char *name, StringBuf sb, int test)
+rpmRC doScript(rpmSpec spec, rpmBuildFlags what, const char *name, StringBuf sb, int test)
 {
-    const char * rootURL = spec->rootURL;
-    const char * rootDir;
-    const char *scriptName = NULL;
-    const char * buildDirURL = rpmGenPath(rootURL, "%{_builddir}", "");
-    const char * buildScript;
-    const char * buildCmd = NULL;
-    const char * buildTemplate = NULL;
-    const char * buildPost = NULL;
+    const char * rootDir = spec->rootDir;
+    char *scriptName = NULL;
+    char * buildDir = rpmGenPath(rootDir, "%{_builddir}", "");
+    char * buildCmd = NULL;
+    char * buildTemplate = NULL;
+    char * buildPost = NULL;
     const char * mTemplate = NULL;
+    const char * mCmd = NULL;
     const char * mPost = NULL;
     int argc = 0;
     const char **argv = NULL;
     FILE * fp = NULL;
-    urlinfo u = NULL;
 
     FD_t fd;
     FD_t xfd;
-    int child;
+    pid_t pid;
+    pid_t child;
     int status;
-    int rc;
+    rpmRC rc;
     
     switch (what) {
-      case RPMBUILD_PREP:
+    case RPMBUILD_PREP:
        name = "%prep";
        sb = spec->prep;
        mTemplate = "%{__spec_prep_template}";
        mPost = "%{__spec_prep_post}";
+       mCmd = "%{__spec_prep_cmd}";
        break;
-      case RPMBUILD_BUILD:
+    case RPMBUILD_BUILD:
        name = "%build";
        sb = spec->build;
        mTemplate = "%{__spec_build_template}";
        mPost = "%{__spec_build_post}";
+       mCmd = "%{__spec_build_cmd}";
        break;
-      case RPMBUILD_INSTALL:
+    case RPMBUILD_INSTALL:
        name = "%install";
        sb = spec->install;
        mTemplate = "%{__spec_install_template}";
        mPost = "%{__spec_install_post}";
+       mCmd = "%{__spec_install_cmd}";
+       break;
+    case RPMBUILD_CHECK:
+       name = "%check";
+       sb = spec->check;
+       mTemplate = "%{__spec_check_template}";
+       mPost = "%{__spec_check_post}";
+       mCmd = "%{__spec_check_cmd}";
        break;
-      case RPMBUILD_CLEAN:
+    case RPMBUILD_CLEAN:
        name = "%clean";
        sb = spec->clean;
        mTemplate = "%{__spec_clean_template}";
        mPost = "%{__spec_clean_post}";
+       mCmd = "%{__spec_clean_cmd}";
        break;
-      case RPMBUILD_RMBUILD:
+    case RPMBUILD_RMBUILD:
        name = "--clean";
        mTemplate = "%{__spec_clean_template}";
        mPost = "%{__spec_clean_post}";
+       mCmd = "%{__spec_clean_cmd}";
        break;
-      case RPMBUILD_STRINGBUF:
-      default:
+    case RPMBUILD_STRINGBUF:
+    default:
        mTemplate = "%{___build_template}";
        mPost = "%{___build_post}";
+       mCmd = "%{___build_cmd}";
        break;
     }
+    if (name == NULL)  /* XXX shouldn't happen */
+       name = "???";
 
     if ((what != RPMBUILD_RMBUILD) && sb == NULL) {
-       rc = 0;
+       rc = RPMRC_OK;
        goto exit;
     }
     
-    if (makeTempFile(rootURL, &scriptName, &fd) || fd == NULL || Ferror(fd)) {
-       rpmError(RPMERR_SCRIPT, _("Unable to open temp file.\n"));
-       rc = RPMERR_SCRIPT;
+    fd = rpmMkTempFile(rootDir, &scriptName);
+    if (fd == NULL || Ferror(fd)) {
+       rpmlog(RPMLOG_ERR, _("Unable to open temp file.\n"));
+       rc = RPMRC_FAIL;
        goto exit;
     }
 
-#ifdef HAVE_FCHMOD
-    switch (rootut) {
-    case URL_IS_PATH:
-    case URL_IS_UNKNOWN:
-       (void)fchmod(Fileno(fd), 0600);
-       break;
-    default:
-       break;
-    }
-#endif
-
-    if (fdGetFp(fd) == NULL)
+    if (fdGetFILE(fd) == NULL)
        xfd = Fdopen(fd, "w.fpio");
     else
        xfd = fd;
-    if ((fp = fdGetFp(xfd)) == NULL) {
-       rc = RPMERR_SCRIPT;
+
+    if ((fp = fdGetFILE(xfd)) == NULL) {
+       rc = RPMRC_FAIL;
        goto exit;
     }
     
-    (void) urlPath(rootURL, &rootDir);
     if (*rootDir == '\0') rootDir = "/";
 
-    (void) urlPath(scriptName, &buildScript);
-
     buildTemplate = rpmExpand(mTemplate, NULL);
     buildPost = rpmExpand(mPost, NULL);
 
     (void) fputs(buildTemplate, fp);
 
     if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
-       fprintf(fp, "cd %s\n", spec->buildSubdir);
+       fprintf(fp, "cd '%s'\n", spec->buildSubdir);
 
     if (what == RPMBUILD_RMBUILD) {
        if (spec->buildSubdir)
-           fprintf(fp, "rm -rf %s\n", spec->buildSubdir);
-    } else
+           fprintf(fp, "rm -rf '%s'\n", spec->buildSubdir);
+    } else if (sb != NULL)
        fprintf(fp, "%s", getStringBuf(sb));
 
     (void) fputs(buildPost, fp);
@@ -169,90 +164,59 @@ int doScript(Spec spec, int what, const char *name, StringBuf sb, int test)
     (void) Fclose(xfd);
 
     if (test) {
-       rc = 0;
+       rc = RPMRC_OK;
        goto exit;
     }
     
 if (_build_debug)
-fprintf(stderr, "*** rootURL %s buildDirURL %s\n", rootURL, buildDirURL);
-    if (buildDirURL && buildDirURL[0] != '/' &&
-       (urlSplit(buildDirURL, &u) != 0)) {
-       rc = RPMERR_SCRIPT;
+fprintf(stderr, "*** rootDir %s buildDir %s\n", rootDir, buildDir);
+    if (buildDir && buildDir[0] != '/') {
+       rc = RPMRC_FAIL;
        goto exit;
     }
-    if (u != NULL) {
-       switch (u->urltype) {
-       case URL_IS_FTP:
-if (_build_debug)
-fprintf(stderr, "*** addMacros\n");
-           addMacro(spec->macros, "_remsh", NULL, "%{__remsh}", RMIL_SPEC);
-           addMacro(spec->macros, "_remhost", NULL, u->host, RMIL_SPEC);
-           if (strcmp(rootDir, "/"))
-               addMacro(spec->macros, "_remroot", NULL, rootDir, RMIL_SPEC);
-           break;
-       case URL_IS_HTTP:
-       default:
-           break;
-       }
-    }
 
-    buildCmd = rpmExpand("%{___build_cmd}", " ", buildScript, NULL);
+    buildCmd = rpmExpand(mCmd, " ", scriptName, NULL);
     (void) poptParseArgvString(buildCmd, &argc, &argv);
 
-    rpmMessage(RPMMESS_NORMAL, _("Executing(%s): %s\n"), name, buildCmd);
+    rpmlog(RPMLOG_NOTICE, _("Executing(%s): %s\n"), name, buildCmd);
     if (!(child = fork())) {
 
        errno = 0;
        (void) execvp(argv[0], (char *const *)argv);
 
-       rpmError(RPMERR_SCRIPT, _("Exec of %s failed (%s): %s\n"),
+       rpmlog(RPMLOG_ERR, _("Exec of %s failed (%s): %s\n"),
                scriptName, name, strerror(errno));
 
-       _exit(-1);
+       _exit(127); /* exit 127 for compatibility with bash(1) */
     }
 
-    rc = waitpid(child, &status, 0);
+    pid = waitpid(child, &status, 0);
 
     if (!WIFEXITED(status) || WEXITSTATUS(status)) {
-       rpmError(RPMERR_SCRIPT, _("Bad exit status from %s (%s)\n"),
+       rpmlog(RPMLOG_ERR, _("Bad exit status from %s (%s)\n"),
                 scriptName, name);
-       rc = RPMERR_SCRIPT;
+       rc = RPMRC_FAIL;
     } else
-       rc = 0;
+       rc = RPMRC_OK;
     
 exit:
     if (scriptName) {
-       if (!rc)
-           (void) Unlink(scriptName);
+       if (rc == RPMRC_OK)
+           (void) unlink(scriptName);
        scriptName = _free(scriptName);
     }
-    if (u != NULL) {
-       switch (u->urltype) {
-       case URL_IS_FTP:
-       case URL_IS_HTTP:
-if (_build_debug)
-fprintf(stderr, "*** delMacros\n");
-           delMacro(spec->macros, "_remsh");
-           delMacro(spec->macros, "_remhost");
-           if (strcmp(rootDir, "/"))
-               delMacro(spec->macros, "_remroot");
-           break;
-       default:
-           break;
-       }
-    }
     argv = _free(argv);
     buildCmd = _free(buildCmd);
     buildTemplate = _free(buildTemplate);
     buildPost = _free(buildPost);
-    buildDirURL = _free(buildDirURL);
+    buildDir = _free(buildDir);
 
     return rc;
 }
 
-int buildSpec(Spec spec, int what, int test)
+rpmRC buildSpec(rpmts ts, rpmSpec spec, int what, int test)
 {
-    int rc = 0;
+    rpmRC rc = RPMRC_OK;
 
     if (!spec->recursing && spec->BACount) {
        int x;
@@ -260,7 +224,7 @@ int buildSpec(Spec spec, int what, int test)
        /* packaging on the first run, and skip RMSOURCE altogether */
        if (spec->BASpecs != NULL)
        for (x = 0; x < spec->BACount; x++) {
-           if ((rc = buildSpec(spec->BASpecs[x],
+           if ((rc = buildSpec(ts, spec->BASpecs[x],
                                (what & ~RPMBUILD_RMSOURCE) |
                                (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
                                test))) {
@@ -280,6 +244,10 @@ int buildSpec(Spec spec, int what, int test)
            (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
                goto exit;
 
+       if ((what & RPMBUILD_CHECK) &&
+           (rc = doScript(spec, RPMBUILD_CHECK, NULL, NULL, test)))
+               goto exit;
+
        if ((what & RPMBUILD_PACKAGESOURCE) &&
            (rc = processSourceFiles(spec)))
                goto exit;
@@ -310,11 +278,11 @@ int buildSpec(Spec spec, int what, int test)
        doRmSource(spec);
 
     if (what & RPMBUILD_RMSPEC)
-       (void) Unlink(spec->specFile);
+       (void) unlink(spec->specFile);
 
 exit:
-    if (rc && rpmlogGetNrecs() > 0) {
-       rpmMessage(RPMMESS_NORMAL, _("\n\nRPM build errors:\n"));
+    if (rc != RPMRC_OK && rpmlogGetNrecs() > 0) {
+       rpmlog(RPMLOG_NOTICE, _("\n\nRPM build errors:\n"));
        rpmlogPrint(NULL);
     }