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