2 * Parse spec file and build package.
17 /*@access rpmts @*/ /* XXX compared with NULL @*/
18 /*@access rpmdb @*/ /* XXX compared with NULL @*/
19 /*@access FD_t @*/ /* XXX compared with NULL @*/
23 static int checkSpec(rpmts ts, Header h)
24 /*@globals fileSystem, internalState @*/
25 /*@modifies ts, h, fileSystem, internalState @*/
30 if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
31 && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
34 rc = rpmtsAddPackage(ts, h, NULL, 0, NULL);
38 ps = rpmtsGetProblems(ts);
40 rpmMessage(RPMMESS_ERROR, _("Failed build dependencies:\n"));
41 printDepProblems(stderr, ps);
46 /* XXX nuke the added package. */
53 * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
56 /* XXX this is still a dumb test but at least it's i18n aware */
59 static int isSpecFile(const char * specfile)
60 /*@globals fileSystem @*/
61 /*@modifies fileSystem @*/
69 fd = Fopen(specfile, "r.ufdio");
70 if (fd == NULL || Ferror(fd)) {
71 rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
72 specfile, Fstrerror(fd));
75 count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
79 for (s = buf; count--; s++) {
84 /*@switchbreak@*/ break;
87 /*@switchbreak@*/ break;
89 if (checking && !(isprint(*s) || isspace(*s))) return 0;
90 /*@switchbreak@*/ break;
98 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
99 /*@globals rpmGlobalMacroContext,
100 fileSystem, internalState @*/
101 /*@modifies ts, rpmGlobalMacroContext,
102 fileSystem, internalState @*/
104 const char * passPhrase = ba->passPhrase;
105 const char * cookie = ba->cookie;
106 int buildAmount = ba->buildAmount;
107 const char * buildRootURL = NULL;
108 const char * specFile;
109 const char * specURL;
116 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
120 if (ba->buildRootOverride)
121 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
124 /*@-compmempass@*/ /* FIX: static zcmds heartburn */
125 if (ba->buildMode == 't') {
127 const char * specDir;
128 const char * tmpSpecFile;
130 rpmCompressedMagic res = COMPRESSED_OTHER;
131 /*@observer@*/ static const char *zcmds[] =
132 { "cat", "gunzip", "bunzip2", "cat" };
134 specDir = rpmGetPath("%{_specdir}", NULL);
136 /* XXX Using mkstemp is difficult here. */
137 /* XXX FWIW, default %{_specdir} is root.root 0755 */
139 strcpy(tfn, "rpm-spec.XXXXXX");
141 tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
145 (void) isCompressed(arg, &res);
147 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
148 sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
149 zcmds[res & 0x3], arg, tmpSpecFile);
150 if (!(fp = popen(cmd, "r"))) {
151 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
152 specDir = _free(specDir);
153 tmpSpecFile = _free(tmpSpecFile);
156 if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
160 sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
161 zcmds[res & 0x3], arg, tmpSpecFile);
162 if (!(fp = popen(cmd, "r"))) {
163 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
164 specDir = _free(specDir);
165 tmpSpecFile = _free(tmpSpecFile);
168 if (!fgets(buf, sizeof(buf) - 1, fp)) {
170 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
172 (void) unlink(tmpSpecFile);
173 specDir = _free(specDir);
174 tmpSpecFile = _free(tmpSpecFile);
181 while (*cmd != '\0') {
182 if (*cmd == '/') s = cmd + 1;
188 /* remove trailing \n */
189 s = cmd + strlen(cmd) - 1;
192 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
193 sprintf(s, "%s/%s", specDir, cmd);
194 res = rename(tmpSpecFile, s);
195 specDir = _free(specDir);
198 rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
200 (void) unlink(tmpSpecFile);
201 tmpSpecFile = _free(tmpSpecFile);
204 tmpSpecFile = _free(tmpSpecFile);
206 /* Make the directory which contains the tarball the source
207 directory for this run */
210 (void)getcwd(buf, BUFSIZ);
216 cmd = buf + strlen(buf) - 1;
217 while (*cmd != '/') cmd--;
220 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
226 specut = urlPath(specURL, &specFile);
227 if (*specFile != '/') {
228 char *s = alloca(BUFSIZ);
229 (void)getcwd(s, BUFSIZ);
235 if (specut != URL_IS_DASH) {
237 if (Stat(specURL, &st) < 0) {
238 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
242 if (! S_ISREG(st.st_mode)) {
243 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
249 /* Try to verify that the file is actually a specfile */
250 if (!isSpecFile(specURL)) {
251 rpmError(RPMERR_BADSPEC,
252 _("File %s does not appear to be a specfile.\n"), specURL);
258 /* Parse the spec file */
259 #define _anyarch(_f) \
260 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
261 if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
262 cookie, _anyarch(buildAmount), ba->force)) {
268 /* Assemble source header from parsed components */
269 initSourceHeader(spec);
271 /* Check build prerequisites */
272 if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
277 if (buildSpec(spec, buildAmount, ba->noBuild)) {
282 if (ba->buildMode == 't')
283 (void) Unlink(specURL);
287 spec = freeSpec(spec);
288 buildRootURL = _free(buildRootURL);
292 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
296 char * targets = ba->targets;
297 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
298 int cleanFlags = ba->buildAmount & buildCleanMask;
300 if (targets == NULL) {
301 rc = buildForTarget(ts, arg, ba);
305 /* parse up the build operators */
307 printf(_("Building target platforms: %s\n"), targets);
309 ba->buildAmount &= ~buildCleanMask;
310 for (t = targets; *t != '\0'; t = te) {
312 if ((te = strchr(t, ',')) == NULL)
314 target = alloca(te-t+1);
315 strncpy(target, t, (te-t));
319 else /* XXX Perform clean-up after last target build. */
320 ba->buildAmount |= cleanFlags;
322 printf(_("Building for target %s\n"), target);
324 /* Read in configuration for target. */
326 (void) rpmReadConfigFiles(rcfile, target);
327 rc = buildForTarget(ts, arg, ba);
333 /* Restore original configuration. */
335 (void) rpmReadConfigFiles(rcfile, NULL);