00001
00005 #include "system.h"
00006
00007 #include <rpmcli.h>
00008 #include <rpmbuild.h>
00009
00010 #include "build.h"
00011 #include "debug.h"
00012
00013
00014
00015
00016
00019 static int checkSpec(Header h)
00020
00021 {
00022 const char * rootdir = NULL;
00023 rpmdb db = NULL;
00024 int mode = O_RDONLY;
00025 rpmTransactionSet ts;
00026 rpmDependencyConflict conflicts;
00027 int numConflicts;
00028 int rc;
00029
00030 if (!headerIsEntry(h, RPMTAG_REQUIREFLAGS))
00031 return 0;
00032
00033 if (rpmdbOpen(rootdir, &db, mode, 0644)) {
00034 const char * dn;
00035 dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
00036 rpmError(RPMERR_OPEN, _("cannot open rpm database in %s\n"), dn);
00037 dn = _free(dn);
00038 exit(EXIT_FAILURE);
00039 }
00040 ts = rpmtransCreateSet(db, rootdir);
00041
00042 rc = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
00043
00044 rc = rpmdepCheck(ts, &conflicts, &numConflicts);
00045 if (rc == 0 && conflicts) {
00046 rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
00047 printDepProblems(stderr, conflicts, numConflicts);
00048 conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
00049 rc = 1;
00050 }
00051
00052 ts = rpmtransFree(ts);
00053 if (db != NULL)
00054 (void) rpmdbClose(db);
00055
00056 return rc;
00057 }
00058
00059
00060
00061
00062
00063
00066 static int isSpecFile(const char * specfile)
00067
00068 {
00069 char buf[256];
00070 const char * s;
00071 FD_t fd;
00072 int count;
00073 int checking;
00074
00075 fd = Fopen(specfile, "r.ufdio");
00076 if (fd == NULL || Ferror(fd)) {
00077 rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
00078 specfile, Fstrerror(fd));
00079 return 0;
00080 }
00081 count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
00082 (void) Fclose(fd);
00083
00084 checking = 1;
00085 for (s = buf; count--; s++) {
00086 switch (*s) {
00087 case '\r':
00088 case '\n':
00089 checking = 1;
00090 break;
00091 case ':':
00092 checking = 0;
00093 break;
00094 default:
00095 if (checking && !(isprint(*s) || isspace(*s))) return 0;
00096 break;
00097 }
00098 }
00099 return 1;
00100 }
00101
00104 static int buildForTarget(const char * arg, BTA_t ba,
00105 const char * passPhrase, char * cookie)
00106
00107 {
00108 int buildAmount = ba->buildAmount;
00109 const char * buildRootURL = NULL;
00110 const char * specFile;
00111 const char * specURL;
00112 int specut;
00113 char buf[BUFSIZ];
00114 Spec spec = NULL;
00115 int rc;
00116
00117 #ifndef DYING
00118 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
00119 #endif
00120
00121 if (ba->buildRootOverride)
00122 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
00123
00124 if (ba->buildMode == 't') {
00125 FILE *fp;
00126 const char * specDir;
00127 const char * tmpSpecFile;
00128 char * cmd, * s;
00129 rpmCompressedMagic res = COMPRESSED_OTHER;
00130 static const char *zcmds[] =
00131 { "cat", "gunzip", "bunzip2", "cat" };
00132
00133 specDir = rpmGetPath("%{_specdir}", NULL);
00134
00135
00136
00137 { char tfn[64];
00138 strcpy(tfn, "rpm-spec.XXXXXX");
00139
00140 tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
00141
00142 }
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 specut = urlPath(specURL, &specFile);
00225 if (*specFile != '/') {
00226 char *s = alloca(BUFSIZ);
00227 (void)getcwd(s, BUFSIZ);
00228 strcat(s, "/");
00229 strcat(s, arg);
00230 specURL = s;
00231 }
00232
00233 if (specut != URL_IS_DASH) {
00234 struct stat st;
00235 if (Stat(specURL, &st) < 0) {
00236 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
00237 rc = 1;
00238 goto exit;
00239 }
00240 if (! S_ISREG(st.st_mode)) {
00241 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
00242 specURL);
00243 rc = 1;
00244 goto exit;
00245 }
00246
00247
00248 if (!isSpecFile(specURL)) {
00249 rpmError(RPMERR_BADSPEC,
00250 _("File %s does not appear to be a specfile.\n"), specURL);
00251 rc = 1;
00252 goto exit;
00253 }
00254 }
00255
00256
00257 #define _anyarch(_f) \
00258 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
00259 if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
00260 cookie, _anyarch(buildAmount), ba->force)) {
00261 rc = 1;
00262 goto exit;
00263 }
00264 #undef _anyarch
00265
00266
00267 initSourceHeader(spec);
00268
00269
00270 if (!ba->noDeps && checkSpec(spec->sourceHeader)) {
00271 rc = 1;
00272 goto exit;
00273 }
00274
00275 if (buildSpec(spec, buildAmount, ba->noBuild)) {
00276 rc = 1;
00277 goto exit;
00278 }
00279
00280 if (ba->buildMode == 't')
00281 (void) Unlink(specURL);
00282 rc = 0;
00283
00284 exit:
00285 spec = freeSpec(spec);
00286 buildRootURL = _free(buildRootURL);
00287 return rc;
00288 }
00289
00290 int build(const char * arg, BTA_t ba,
00291 const char * passPhrase, char * cookie, const char * rcfile)
00292 {
00293 char *t, *te;
00294 int rc = 0;
00295 char * targets = ba->targets;
00296 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
00297 int cleanFlags = ba->buildAmount & buildCleanMask;
00298
00299 if (targets == NULL) {
00300 rc = buildForTarget(arg, ba, passPhrase, cookie);
00301 goto exit;
00302 }
00303
00304
00305
00306 printf(_("Building target platforms: %s\n"), targets);
00307
00308 ba->buildAmount &= ~buildCleanMask;
00309 for (t = targets; *t != '\0'; t = te) {
00310 char *target;
00311 if ((te = strchr(t, ',')) == NULL)
00312 te = t + strlen(t);
00313 target = alloca(te-t+1);
00314 strncpy(target, t, (te-t));
00315 target[te-t] = '\0';
00316 if (*te != '\0')
00317 te++;
00318 else
00319 ba->buildAmount |= cleanFlags;
00320
00321 printf(_("Building for target %s\n"), target);
00322
00323
00324 rpmFreeMacros(NULL);
00325 (void) rpmReadConfigFiles(rcfile, target);
00326 rc = buildForTarget(arg, ba, passPhrase, cookie);
00327 if (rc)
00328 break;
00329 }
00330
00331 exit:
00332
00333 rpmFreeMacros(NULL);
00334 (void) rpmReadConfigFiles(rcfile, NULL);
00335 return rc;
00336 }