00001
00005 #include "system.h"
00006
00007 #include <rpmcli.h>
00008 #include <rpmbuild.h>
00009
00010 #include "rpmps.h"
00011 #include "rpmte.h"
00012 #include "rpmts.h"
00013
00014 #include "build.h"
00015 #include "debug.h"
00016
00017
00018
00019
00020
00023 static int checkSpec(rpmts ts, Header h)
00024
00025
00026 {
00027 rpmps ps;
00028 int rc;
00029
00030 if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
00031 && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
00032 return 0;
00033
00034 rc = rpmtsAddInstallElement(ts, h, NULL, 0, NULL);
00035
00036 rc = rpmtsCheck(ts);
00037
00038 ps = rpmtsProblems(ts);
00039 if (rc == 0 && rpmpsNumProblems(ps) > 0) {
00040 rpmMessage(RPMMESS_ERROR, _("Failed build dependencies:\n"));
00041 rpmpsPrint(NULL, ps);
00042 rc = 1;
00043 }
00044 ps = rpmpsFree(ps);
00045
00046
00047 rpmtsClean(ts);
00048
00049 return rc;
00050 }
00051
00052
00053
00054
00055
00056
00059 static int isSpecFile(const char * specfile)
00060
00061
00062 {
00063 char buf[256];
00064 const char * s;
00065 FD_t fd;
00066 int count;
00067 int checking;
00068
00069 fd = Fopen(specfile, "r.ufdio");
00070 if (fd == NULL || Ferror(fd)) {
00071 rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
00072 specfile, Fstrerror(fd));
00073 return 0;
00074 }
00075 count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
00076 (void) Fclose(fd);
00077
00078 checking = 1;
00079 for (s = buf; count--; s++) {
00080 switch (*s) {
00081 case '\r':
00082 case '\n':
00083 checking = 1;
00084 break;
00085 case ':':
00086 checking = 0;
00087 break;
00088
00089 default:
00090 if (checking && !(isprint(*s) || isspace(*s))) return 0;
00091 break;
00092
00093 }
00094 }
00095 return 1;
00096 }
00097
00100
00101 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
00102
00103
00104 {
00105 const char * passPhrase = ba->passPhrase;
00106 const char * cookie = ba->cookie;
00107 int buildAmount = ba->buildAmount;
00108 const char * buildRootURL = NULL;
00109 const char * specFile;
00110 const char * specURL;
00111 int specut;
00112 char buf[BUFSIZ];
00113 Spec spec = NULL;
00114 int rc;
00115
00116 #ifndef DYING
00117 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
00118 #endif
00119
00120
00121 if (ba->buildRootOverride)
00122 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
00123
00124
00125
00126 if (ba->buildMode == 't') {
00127 FILE *fp;
00128 const char * specDir;
00129 char * tmpSpecFile;
00130 char * cmd, * s;
00131 rpmCompressedMagic res = COMPRESSED_OTHER;
00132 static const char *zcmds[] =
00133 { "cat", "gunzip", "bunzip2", "cat" };
00134
00135 specDir = rpmGetPath("%{_specdir}", NULL);
00136
00137 tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
00138 #if defined(HAVE_MKSTEMP)
00139 (void) close(mkstemp(tmpSpecFile));
00140 #else
00141 (void) mktemp(tmpSpecFile);
00142 #endif
00143
00144 (void) isCompressed(arg, &res);
00145
00146 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
00147 sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
00148 zcmds[res & 0x3], arg, tmpSpecFile);
00149 if (!(fp = popen(cmd, "r"))) {
00150 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
00151 specDir = _free(specDir);
00152 tmpSpecFile = _free(tmpSpecFile);
00153 return 1;
00154 }
00155 if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
00156
00157 (void) pclose(fp);
00158
00159 sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
00160 zcmds[res & 0x3], arg, tmpSpecFile);
00161 if (!(fp = popen(cmd, "r"))) {
00162 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
00163 specDir = _free(specDir);
00164 tmpSpecFile = _free(tmpSpecFile);
00165 return 1;
00166 }
00167 if (!fgets(buf, sizeof(buf) - 1, fp)) {
00168
00169 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
00170 arg);
00171 (void) unlink(tmpSpecFile);
00172 specDir = _free(specDir);
00173 tmpSpecFile = _free(tmpSpecFile);
00174 return 1;
00175 }
00176 }
00177 (void) pclose(fp);
00178
00179 cmd = s = buf;
00180 while (*cmd != '\0') {
00181 if (*cmd == '/') s = cmd + 1;
00182 cmd++;
00183 }
00184
00185 cmd = s;
00186
00187
00188 s = cmd + strlen(cmd) - 1;
00189 *s = '\0';
00190
00191 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
00192 sprintf(s, "%s/%s", specDir, cmd);
00193 res = rename(tmpSpecFile, s);
00194 specDir = _free(specDir);
00195
00196 if (res) {
00197 rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
00198 tmpSpecFile, s);
00199 (void) unlink(tmpSpecFile);
00200 tmpSpecFile = _free(tmpSpecFile);
00201 return 1;
00202 }
00203 tmpSpecFile = _free(tmpSpecFile);
00204
00205
00206
00207
00208 if (*arg != '/') {
00209 (void)getcwd(buf, BUFSIZ);
00210 strcat(buf, "/");
00211 strcat(buf, arg);
00212 } else
00213 strcpy(buf, arg);
00214
00215 cmd = buf + strlen(buf) - 1;
00216 while (*cmd != '/') cmd--;
00217 *cmd = '\0';
00218
00219 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
00220 } else {
00221 specURL = arg;
00222 }
00223
00224
00225 specut = urlPath(specURL, &specFile);
00226 if (*specFile != '/') {
00227 char *s = alloca(BUFSIZ);
00228 (void)getcwd(s, BUFSIZ);
00229 strcat(s, "/");
00230 strcat(s, arg);
00231 specURL = s;
00232 }
00233
00234 if (specut != URL_IS_DASH) {
00235 struct stat st;
00236 if (Stat(specURL, &st) < 0) {
00237 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
00238 rc = 1;
00239 goto exit;
00240 }
00241 if (! S_ISREG(st.st_mode)) {
00242 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
00243 specURL);
00244 rc = 1;
00245 goto exit;
00246 }
00247
00248
00249 if (!isSpecFile(specURL)) {
00250 rpmError(RPMERR_BADSPEC,
00251 _("File %s does not appear to be a specfile.\n"), specURL);
00252 rc = 1;
00253 goto exit;
00254 }
00255 }
00256
00257
00258 #define _anyarch(_f) \
00259 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
00260 if (parseSpec(ts, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
00261 cookie, _anyarch(buildAmount), ba->force))
00262 {
00263 rc = 1;
00264 goto exit;
00265 }
00266 #undef _anyarch
00267 if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
00268 rc = 1;
00269 goto exit;
00270 }
00271
00272
00273 initSourceHeader(spec);
00274
00275
00276 if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
00277 rc = 1;
00278 goto exit;
00279 }
00280
00281 if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
00282 rc = 1;
00283 goto exit;
00284 }
00285
00286 if (ba->buildMode == 't')
00287 (void) Unlink(specURL);
00288 rc = 0;
00289
00290 exit:
00291 spec = freeSpec(spec);
00292 buildRootURL = _free(buildRootURL);
00293 return rc;
00294 }
00295
00296
00297 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
00298 {
00299 char *t, *te;
00300 int rc = 0;
00301 char * targets = ba->targets;
00302 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
00303 int cleanFlags = ba->buildAmount & buildCleanMask;
00304 rpmVSFlags vsflags, ovsflags;
00305
00306 vsflags = rpmExpandNumeric("%{_vsflags_build}");
00307 if (ba->qva_flags & VERIFY_DIGEST)
00308 vsflags |= _RPMVSF_NODIGESTS;
00309 if (ba->qva_flags & VERIFY_SIGNATURE)
00310 vsflags |= _RPMVSF_NOSIGNATURES;
00311 if (ba->qva_flags & VERIFY_HDRCHK)
00312 vsflags |= RPMVSF_NOHDRCHK;
00313 ovsflags = rpmtsSetVSFlags(ts, vsflags);
00314
00315 if (targets == NULL) {
00316 rc = buildForTarget(ts, arg, ba);
00317 goto exit;
00318 }
00319
00320
00321
00322 printf(_("Building target platforms: %s\n"), targets);
00323
00324 ba->buildAmount &= ~buildCleanMask;
00325 for (t = targets; *t != '\0'; t = te) {
00326 char *target;
00327 if ((te = strchr(t, ',')) == NULL)
00328 te = t + strlen(t);
00329 target = alloca(te-t+1);
00330 strncpy(target, t, (te-t));
00331 target[te-t] = '\0';
00332 if (*te != '\0')
00333 te++;
00334 else
00335 ba->buildAmount |= cleanFlags;
00336
00337 printf(_("Building for target %s\n"), target);
00338
00339
00340 rpmFreeMacros(NULL);
00341 rpmFreeRpmrc();
00342 (void) rpmReadConfigFiles(rcfile, target);
00343 rc = buildForTarget(ts, arg, ba);
00344 if (rc)
00345 break;
00346 }
00347
00348 exit:
00349 vsflags = rpmtsSetVSFlags(ts, ovsflags);
00350
00351 rpmFreeMacros(NULL);
00352 rpmFreeRpmrc();
00353 (void) rpmReadConfigFiles(rcfile, NULL);
00354
00355 return rc;
00356 }