2 * Parse spec file and build package.
16 /*@access rpmTransactionSet @*/ /* XXX compared with NULL @*/
17 /*@access rpmdb @*/ /* XXX compared with NULL @*/
18 /*@access FD_t @*/ /* XXX compared with NULL @*/
22 static int checkSpec(rpmTransactionSet ts, Header h)
23 /*@globals fileSystem, internalState @*/
24 /*@modifies ts, h, fileSystem, internalState @*/
29 if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
30 && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
33 rc = rpmtsAddPackage(ts, h, NULL, 0, NULL);
37 ps = rpmtsGetProblems(ts);
39 rpmMessage(RPMMESS_ERROR, _("Failed build dependencies:\n"));
40 printDepProblems(stderr, ps);
43 ps = rpmProblemSetFree(ps);
45 /* XXX nuke the added package. */
52 * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
55 /* XXX this is still a dumb test but at least it's i18n aware */
58 static int isSpecFile(const char * specfile)
59 /*@globals fileSystem @*/
60 /*@modifies fileSystem @*/
68 fd = Fopen(specfile, "r.ufdio");
69 if (fd == NULL || Ferror(fd)) {
70 rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
71 specfile, Fstrerror(fd));
74 count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
78 for (s = buf; count--; s++) {
83 /*@switchbreak@*/ break;
86 /*@switchbreak@*/ break;
88 if (checking && !(isprint(*s) || isspace(*s))) return 0;
89 /*@switchbreak@*/ break;
97 static int buildForTarget(rpmTransactionSet ts, const char * arg, BTA_t ba)
98 /*@globals rpmGlobalMacroContext,
99 fileSystem, internalState @*/
100 /*@modifies ts, rpmGlobalMacroContext,
101 fileSystem, internalState @*/
103 const char * passPhrase = ba->passPhrase;
104 const char * cookie = ba->cookie;
105 int buildAmount = ba->buildAmount;
106 const char * buildRootURL = NULL;
107 const char * specFile;
108 const char * specURL;
115 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
119 if (ba->buildRootOverride)
120 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
123 /*@-compmempass@*/ /* FIX: static zcmds heartburn */
124 if (ba->buildMode == 't') {
126 const char * specDir;
127 const char * tmpSpecFile;
129 rpmCompressedMagic res = COMPRESSED_OTHER;
130 /*@observer@*/ static const char *zcmds[] =
131 { "cat", "gunzip", "bunzip2", "cat" };
133 specDir = rpmGetPath("%{_specdir}", NULL);
135 /* XXX Using mkstemp is difficult here. */
136 /* XXX FWIW, default %{_specdir} is root.root 0755 */
138 strcpy(tfn, "rpm-spec.XXXXXX");
140 tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
144 (void) isCompressed(arg, &res);
146 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
147 sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
148 zcmds[res & 0x3], arg, tmpSpecFile);
149 if (!(fp = popen(cmd, "r"))) {
150 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
151 specDir = _free(specDir);
152 tmpSpecFile = _free(tmpSpecFile);
155 if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
159 sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
160 zcmds[res & 0x3], arg, tmpSpecFile);
161 if (!(fp = popen(cmd, "r"))) {
162 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
163 specDir = _free(specDir);
164 tmpSpecFile = _free(tmpSpecFile);
167 if (!fgets(buf, sizeof(buf) - 1, fp)) {
169 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
171 (void) unlink(tmpSpecFile);
172 specDir = _free(specDir);
173 tmpSpecFile = _free(tmpSpecFile);
180 while (*cmd != '\0') {
181 if (*cmd == '/') s = cmd + 1;
187 /* remove trailing \n */
188 s = cmd + strlen(cmd) - 1;
191 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
192 sprintf(s, "%s/%s", specDir, cmd);
193 res = rename(tmpSpecFile, s);
194 specDir = _free(specDir);
197 rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
199 (void) unlink(tmpSpecFile);
200 tmpSpecFile = _free(tmpSpecFile);
203 tmpSpecFile = _free(tmpSpecFile);
205 /* Make the directory which contains the tarball the source
206 directory for this run */
209 (void)getcwd(buf, BUFSIZ);
215 cmd = buf + strlen(buf) - 1;
216 while (*cmd != '/') cmd--;
219 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
225 specut = urlPath(specURL, &specFile);
226 if (*specFile != '/') {
227 char *s = alloca(BUFSIZ);
228 (void)getcwd(s, BUFSIZ);
234 if (specut != URL_IS_DASH) {
236 if (Stat(specURL, &st) < 0) {
237 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
241 if (! S_ISREG(st.st_mode)) {
242 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
248 /* Try to verify that the file is actually a specfile */
249 if (!isSpecFile(specURL)) {
250 rpmError(RPMERR_BADSPEC,
251 _("File %s does not appear to be a specfile.\n"), specURL);
257 /* Parse the spec file */
258 #define _anyarch(_f) \
259 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
260 if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
261 cookie, _anyarch(buildAmount), ba->force)) {
267 /* Assemble source header from parsed components */
268 initSourceHeader(spec);
270 /* Check build prerequisites */
271 if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
276 if (buildSpec(spec, buildAmount, ba->noBuild)) {
281 if (ba->buildMode == 't')
282 (void) Unlink(specURL);
286 spec = freeSpec(spec);
287 buildRootURL = _free(buildRootURL);
291 int build(rpmTransactionSet ts, const char * arg, BTA_t ba, const char * rcfile)
295 char * targets = ba->targets;
296 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
297 int cleanFlags = ba->buildAmount & buildCleanMask;
299 if (targets == NULL) {
300 rc = buildForTarget(ts, arg, ba);
304 /* parse up the build operators */
306 printf(_("Building target platforms: %s\n"), targets);
308 ba->buildAmount &= ~buildCleanMask;
309 for (t = targets; *t != '\0'; t = te) {
311 if ((te = strchr(t, ',')) == NULL)
313 target = alloca(te-t+1);
314 strncpy(target, t, (te-t));
318 else /* XXX Perform clean-up after last target build. */
319 ba->buildAmount |= cleanFlags;
321 printf(_("Building for target %s\n"), target);
323 /* Read in configuration for target. */
325 (void) rpmReadConfigFiles(rcfile, target);
326 rc = buildForTarget(ts, arg, ba);
332 /* Restore original configuration. */
334 (void) rpmReadConfigFiles(rcfile, NULL);