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 0
00091 if (checking && !(isprint(*s) || isspace(*s))) return 0;
00092 break;
00093 #else
00094 if (checking && !(isprint(*s) || isspace(*s)) && *(unsigned char *)s < 32) return 0;
00095 break;
00096 #endif
00097
00098 }
00099 }
00100 return 1;
00101 }
00102
00105
00106 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
00107
00108
00109 {
00110 const char * passPhrase = ba->passPhrase;
00111 const char * cookie = ba->cookie;
00112 int buildAmount = ba->buildAmount;
00113 const char * buildRootURL = NULL;
00114 const char * specFile;
00115 const char * specURL;
00116 int specut;
00117 char buf[BUFSIZ];
00118 Spec spec = NULL;
00119 int rc;
00120
00121 #ifndef DYING
00122 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
00123 #endif
00124
00125
00126 if (ba->buildRootOverride)
00127 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
00128
00129
00130
00131 if (ba->buildMode == 't') {
00132 FILE *fp;
00133 const char * specDir;
00134 char * tmpSpecFile;
00135 char * cmd, * s;
00136 rpmCompressedMagic res = COMPRESSED_OTHER;
00137 static const char *zcmds[] =
00138 { "cat", "gunzip", "bunzip2", "cat" };
00139
00140 specDir = rpmGetPath("%{_specdir}", NULL);
00141
00142 tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
00143 #if defined(HAVE_MKSTEMP)
00144 (void) close(mkstemp(tmpSpecFile));
00145 #else
00146 (void) mktemp(tmpSpecFile);
00147 #endif
00148
00149 (void) isCompressed(arg, &res);
00150
00151 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
00152 sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
00153 zcmds[res & 0x3], arg, tmpSpecFile);
00154 if (!(fp = popen(cmd, "r"))) {
00155 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
00156 specDir = _free(specDir);
00157 tmpSpecFile = _free(tmpSpecFile);
00158 return 1;
00159 }
00160 if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
00161
00162 (void) pclose(fp);
00163
00164 sprintf(cmd, "%s < %s | tar xOvf - --wildcards \\*.spec 2>&1 > %s",
00165 zcmds[res & 0x3], arg, tmpSpecFile);
00166 if (!(fp = popen(cmd, "r"))) {
00167 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
00168 specDir = _free(specDir);
00169 tmpSpecFile = _free(tmpSpecFile);
00170 return 1;
00171 }
00172 if (!fgets(buf, sizeof(buf) - 1, fp)) {
00173
00174 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
00175 arg);
00176 (void) unlink(tmpSpecFile);
00177 specDir = _free(specDir);
00178 tmpSpecFile = _free(tmpSpecFile);
00179 return 1;
00180 }
00181 }
00182 (void) pclose(fp);
00183
00184 cmd = s = buf;
00185 while (*cmd != '\0') {
00186 if (*cmd == '/') s = cmd + 1;
00187 cmd++;
00188 }
00189
00190 cmd = s;
00191
00192
00193 s = cmd + strlen(cmd) - 1;
00194 *s = '\0';
00195
00196 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
00197 sprintf(s, "%s/%s", specDir, cmd);
00198 res = rename(tmpSpecFile, s);
00199 specDir = _free(specDir);
00200
00201 if (res) {
00202 rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
00203 tmpSpecFile, s);
00204 (void) unlink(tmpSpecFile);
00205 tmpSpecFile = _free(tmpSpecFile);
00206 return 1;
00207 }
00208 tmpSpecFile = _free(tmpSpecFile);
00209
00210
00211
00212
00213 if (*arg != '/') {
00214 if (!getcwd(buf, BUFSIZ)) {
00215 rpmError(RPMERR_STAT, "getcwd failed: %m\n");
00216 return 1;
00217 }
00218 strcat(buf, "/");
00219 strcat(buf, arg);
00220 } else
00221 strcpy(buf, arg);
00222
00223 cmd = buf + strlen(buf) - 1;
00224 while (*cmd != '/') cmd--;
00225 *cmd = '\0';
00226
00227 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
00228 } else {
00229 specURL = arg;
00230 }
00231
00232
00233 specut = urlPath(specURL, &specFile);
00234 if (*specFile != '/') {
00235 char *s = alloca(BUFSIZ);
00236 if (!getcwd(s, BUFSIZ)) {
00237 rpmError(RPMERR_STAT, "getcwd failed: %m\n");
00238 rc = 1;
00239 goto exit;
00240 }
00241 strcat(s, "/");
00242 strcat(s, arg);
00243 specURL = s;
00244 }
00245
00246 if (specut != URL_IS_DASH) {
00247 struct stat st;
00248 if (Stat(specURL, &st) < 0) {
00249 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
00250 rc = 1;
00251 goto exit;
00252 }
00253 if (! S_ISREG(st.st_mode)) {
00254 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
00255 specURL);
00256 rc = 1;
00257 goto exit;
00258 }
00259
00260
00261 if (!isSpecFile(specURL)) {
00262 rpmError(RPMERR_BADSPEC,
00263 _("File %s does not appear to be a specfile.\n"), specURL);
00264 rc = 1;
00265 goto exit;
00266 }
00267 }
00268
00269
00270 #define _anyarch(_f) \
00271 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
00272 if (parseSpec(ts, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
00273 cookie, _anyarch(buildAmount), ba->force))
00274 {
00275 rc = 1;
00276 goto exit;
00277 }
00278 #undef _anyarch
00279 if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
00280 rc = 1;
00281 goto exit;
00282 }
00283
00284
00285 initSourceHeader(spec);
00286
00287
00288 if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
00289 rc = 1;
00290 goto exit;
00291 }
00292
00293 if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
00294 rc = 1;
00295 goto exit;
00296 }
00297
00298 if (ba->buildMode == 't')
00299 (void) Unlink(specURL);
00300 rc = 0;
00301
00302 exit:
00303 spec = freeSpec(spec);
00304 buildRootURL = _free(buildRootURL);
00305 return rc;
00306 }
00307
00308
00309 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
00310 {
00311 char *t, *te;
00312 int rc = 0;
00313 char * targets = ba->targets;
00314 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
00315 int cleanFlags = ba->buildAmount & buildCleanMask;
00316 rpmVSFlags vsflags, ovsflags;
00317
00318 vsflags = rpmExpandNumeric("%{_vsflags_build}");
00319 if (ba->qva_flags & VERIFY_DIGEST)
00320 vsflags |= _RPMVSF_NODIGESTS;
00321 if (ba->qva_flags & VERIFY_SIGNATURE)
00322 vsflags |= _RPMVSF_NOSIGNATURES;
00323 if (ba->qva_flags & VERIFY_HDRCHK)
00324 vsflags |= RPMVSF_NOHDRCHK;
00325 ovsflags = rpmtsSetVSFlags(ts, vsflags);
00326
00327 if (targets == NULL) {
00328 rc = buildForTarget(ts, arg, ba);
00329 goto exit;
00330 }
00331
00332
00333
00334 printf(_("Building target platforms: %s\n"), targets);
00335
00336 ba->buildAmount &= ~buildCleanMask;
00337 for (t = targets; *t != '\0'; t = te) {
00338 char *target;
00339 if ((te = strchr(t, ',')) == NULL)
00340 te = t + strlen(t);
00341 target = alloca(te-t+1);
00342 strncpy(target, t, (te-t));
00343 target[te-t] = '\0';
00344 if (*te != '\0')
00345 te++;
00346 else
00347 ba->buildAmount |= cleanFlags;
00348
00349 printf(_("Building for target %s\n"), target);
00350
00351
00352 rpmFreeMacros(NULL);
00353 rpmFreeRpmrc();
00354 (void) rpmReadConfigFiles(rcfile, target);
00355 rc = buildForTarget(ts, arg, ba);
00356 if (rc)
00357 break;
00358 }
00359
00360 exit:
00361 vsflags = rpmtsSetVSFlags(ts, ovsflags);
00362
00363 rpmFreeMacros(NULL);
00364 rpmFreeRpmrc();
00365 (void) rpmReadConfigFiles(rcfile, NULL);
00366
00367 return rc;
00368 }