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