- fix: remove rpmfi scareMem so that headers can be reloaded on ia64.
[tools/librpm-tizen.git] / build.c
1 /** \ingroup rpmcli
2  * Parse spec file and build package.
3  */
4
5 #include "system.h"
6
7 #include <rpmcli.h>
8 #include <rpmbuild.h>
9
10 #include "rpmps.h"
11 #include "rpmte.h"
12 #include "rpmts.h"
13
14 #include "build.h"
15 #include "debug.h"
16
17 /*@access rpmts @*/     /* XXX compared with NULL @*/
18 /*@access rpmdb @*/             /* XXX compared with NULL @*/
19 /*@access FD_t @*/              /* XXX compared with NULL @*/
20
21 /**
22  */
23 static int checkSpec(rpmts ts, Header h)
24         /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
25         /*@modifies ts, h, rpmGlobalMacroContext, fileSystem, internalState @*/
26 {
27     rpmps ps;
28     int rc;
29
30     if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
31      && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
32         return 0;
33
34     rc = rpmtsAddInstallElement(ts, h, NULL, 0, NULL);
35
36     rc = rpmtsCheck(ts);
37
38     ps = rpmtsProblems(ts);
39     if (rc == 0 && rpmpsNumProblems(ps) > 0) {
40         rpmMessage(RPMMESS_ERROR, _("Failed build dependencies:\n"));
41         rpmpsPrint(NULL, ps);
42         rc = 1;
43     }
44     ps = rpmpsFree(ps);
45
46     /* XXX nuke the added package. */
47     rpmtsClean(ts);
48
49     return rc;
50 }
51
52 /*
53  * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
54  * angielsku...
55  */
56 /* XXX this is still a dumb test but at least it's i18n aware */
57 /**
58  */
59 static int isSpecFile(const char * specfile)
60         /*@globals fileSystem, internalState @*/
61         /*@modifies fileSystem, internalState @*/
62 {
63     char buf[256];
64     const char * s;
65     FD_t fd;
66     int count;
67     int checking;
68
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));
73         return 0;
74     }
75     count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
76     (void) Fclose(fd);
77
78     checking = 1;
79     for (s = buf; count--; s++) {
80         switch (*s) {
81         case '\r':
82         case '\n':
83             checking = 1;
84             /*@switchbreak@*/ break;
85         case ':':
86             checking = 0;
87             /*@switchbreak@*/ break;
88 /*@-boundsread@*/
89         default:
90             if (checking && !(isprint(*s) || isspace(*s))) return 0;
91             /*@switchbreak@*/ break;
92 /*@=boundsread@*/
93         }
94     }
95     return 1;
96 }
97
98 /**
99  */
100 /*@-boundswrite@*/
101 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
102         /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
103         /*@modifies ts, rpmGlobalMacroContext, fileSystem, internalState @*/
104 {
105     const char * passPhrase = ba->passPhrase;
106     const char * cookie = ba->cookie;
107     int buildAmount = ba->buildAmount;
108     const char * buildRootURL = NULL;
109     const char * specFile;
110     const char * specURL;
111     int specut;
112     char buf[BUFSIZ];
113     Spec spec = NULL;
114     int rc;
115
116 #ifndef DYING
117     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
118 #endif
119
120     /*@-branchstate@*/
121     if (ba->buildRootOverride)
122         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
123     /*@=branchstate@*/
124
125     /*@-compmempass@*/ /* FIX: static zcmds heartburn */
126     if (ba->buildMode == 't') {
127         FILE *fp;
128         const char * specDir;
129         const char * tmpSpecFile;
130         char * cmd, * s;
131         rpmCompressedMagic res = COMPRESSED_OTHER;
132         /*@observer@*/ static const char *zcmds[] =
133                 { "cat", "gunzip", "bunzip2", "cat" };
134
135         specDir = rpmGetPath("%{_specdir}", NULL);
136
137         /* XXX Using mkstemp is difficult here. */
138         /* XXX FWIW, default %{_specdir} is root.root 0755 */
139         {   char tfn[64];
140             strcpy(tfn, "rpm-spec.XXXXXX");
141             /*@-unrecog@*/
142             tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
143             /*@=unrecog@*/
144         }
145
146         (void) isCompressed(arg, &res);
147
148         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
149         sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
150                         zcmds[res & 0x3], arg, tmpSpecFile);
151         if (!(fp = popen(cmd, "r"))) {
152             rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
153             specDir = _free(specDir);
154             tmpSpecFile = _free(tmpSpecFile);
155             return 1;
156         }
157         if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
158             /* Try again */
159             (void) pclose(fp);
160
161             sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
162                     zcmds[res & 0x3], arg, tmpSpecFile);
163             if (!(fp = popen(cmd, "r"))) {
164                 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
165                 specDir = _free(specDir);
166                 tmpSpecFile = _free(tmpSpecFile);
167                 return 1;
168             }
169             if (!fgets(buf, sizeof(buf) - 1, fp)) {
170                 /* Give up */
171                 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
172                         arg);
173                 (void) unlink(tmpSpecFile);
174                 specDir = _free(specDir);
175                 tmpSpecFile = _free(tmpSpecFile);
176                 return 1;
177             }
178         }
179         (void) pclose(fp);
180
181         cmd = s = buf;
182         while (*cmd != '\0') {
183             if (*cmd == '/') s = cmd + 1;
184             cmd++;
185         }
186
187         cmd = s;
188
189         /* remove trailing \n */
190         s = cmd + strlen(cmd) - 1;
191         *s = '\0';
192
193         specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
194         sprintf(s, "%s/%s", specDir, cmd);
195         res = rename(tmpSpecFile, s);
196         specDir = _free(specDir);
197         
198         if (res) {
199             rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
200                         tmpSpecFile, s);
201             (void) unlink(tmpSpecFile);
202             tmpSpecFile = _free(tmpSpecFile);
203             return 1;
204         }
205         tmpSpecFile = _free(tmpSpecFile);
206
207         /* Make the directory which contains the tarball the source 
208            directory for this run */
209
210         if (*arg != '/') {
211             (void)getcwd(buf, BUFSIZ);
212             strcat(buf, "/");
213             strcat(buf, arg);
214         } else 
215             strcpy(buf, arg);
216
217         cmd = buf + strlen(buf) - 1;
218         while (*cmd != '/') cmd--;
219         *cmd = '\0';
220
221         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
222     } else {
223         specURL = arg;
224     }
225     /*@=compmempass@*/
226
227     specut = urlPath(specURL, &specFile);
228     if (*specFile != '/') {
229         char *s = alloca(BUFSIZ);
230         (void)getcwd(s, BUFSIZ);
231         strcat(s, "/");
232         strcat(s, arg);
233         specURL = s;
234     }
235
236     if (specut != URL_IS_DASH) {
237         struct stat st;
238         if (Stat(specURL, &st) < 0) {
239             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
240             rc = 1;
241             goto exit;
242         }
243         if (! S_ISREG(st.st_mode)) {
244             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
245                 specURL);
246             rc = 1;
247             goto exit;
248         }
249
250         /* Try to verify that the file is actually a specfile */
251         if (!isSpecFile(specURL)) {
252             rpmError(RPMERR_BADSPEC,
253                 _("File %s does not appear to be a specfile.\n"), specURL);
254             rc = 1;
255             goto exit;
256         }
257     }
258     
259     /* Parse the spec file */
260 #define _anyarch(_f)    \
261 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
262     if (parseSpec(ts, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
263                 cookie, _anyarch(buildAmount), ba->force))
264     {
265         rc = 1;
266         goto exit;
267     }
268 #undef  _anyarch
269     if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
270         rc = 1;
271         goto exit;
272     }
273
274     /* Assemble source header from parsed components */
275     initSourceHeader(spec);
276
277     /* Check build prerequisites */
278     if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
279         rc = 1;
280         goto exit;
281     }
282
283     if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
284         rc = 1;
285         goto exit;
286     }
287     
288     if (ba->buildMode == 't')
289         (void) Unlink(specURL);
290     rc = 0;
291
292 exit:
293     spec = freeSpec(spec);
294     buildRootURL = _free(buildRootURL);
295     return rc;
296 }
297 /*@=boundswrite@*/
298
299 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
300 {
301     char *t, *te;
302     int rc = 0;
303     char * targets = ba->targets;
304 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
305     int cleanFlags = ba->buildAmount & buildCleanMask;
306     rpmVSFlags vsflags, ovsflags;
307
308     vsflags = rpmExpandNumeric("%{_vsflags_build}");
309     if (ba->qva_flags & VERIFY_DIGEST)
310         vsflags |= _RPMVSF_NODIGESTS;
311     if (ba->qva_flags & VERIFY_SIGNATURE)
312         vsflags |= _RPMVSF_NOSIGNATURES;
313     if (ba->qva_flags & VERIFY_HDRCHK)
314         vsflags |= RPMVSF_NOHDRCHK;
315     ovsflags = rpmtsSetVSFlags(ts, vsflags);
316
317     if (targets == NULL) {
318         rc =  buildForTarget(ts, arg, ba);
319         goto exit;
320     }
321
322     /* parse up the build operators */
323
324     printf(_("Building target platforms: %s\n"), targets);
325
326     ba->buildAmount &= ~buildCleanMask;
327     for (t = targets; *t != '\0'; t = te) {
328         char *target;
329         if ((te = strchr(t, ',')) == NULL)
330             te = t + strlen(t);
331         target = alloca(te-t+1);
332         strncpy(target, t, (te-t));
333         target[te-t] = '\0';
334         if (*te != '\0')
335             te++;
336         else    /* XXX Perform clean-up after last target build. */
337             ba->buildAmount |= cleanFlags;
338
339         printf(_("Building for target %s\n"), target);
340
341         /* Read in configuration for target. */
342         rpmFreeMacros(NULL);
343         (void) rpmReadConfigFiles(rcfile, target);
344         rc = buildForTarget(ts, arg, ba);
345         if (rc)
346             break;
347     }
348
349 exit:
350     vsflags = rpmtsSetVSFlags(ts, ovsflags);
351     /* Restore original configuration. */
352     rpmFreeMacros(NULL);
353     (void) rpmReadConfigFiles(rcfile, NULL);
354
355     return rc;
356 }