Annotate function args.
[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, h_errno, 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 h_errno, 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, h_errno, 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         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         tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
138 #if defined(HAVE_MKSTEMP)
139         (void) close(mkstemp(tmpSpecFile));
140 #else
141         (void) mktemp(tmpSpecFile);
142 #endif
143
144         (void) isCompressed(arg, &res);
145
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);
153             return 1;
154         }
155         if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
156             /* Try again */
157             (void) pclose(fp);
158
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);
165                 return 1;
166             }
167             if (!fgets(buf, sizeof(buf) - 1, fp)) {
168                 /* Give up */
169                 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
170                         arg);
171                 (void) unlink(tmpSpecFile);
172                 specDir = _free(specDir);
173                 tmpSpecFile = _free(tmpSpecFile);
174                 return 1;
175             }
176         }
177         (void) pclose(fp);
178
179         cmd = s = buf;
180         while (*cmd != '\0') {
181             if (*cmd == '/') s = cmd + 1;
182             cmd++;
183         }
184
185         cmd = s;
186
187         /* remove trailing \n */
188         s = cmd + strlen(cmd) - 1;
189         *s = '\0';
190
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);
195         
196         if (res) {
197             rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
198                         tmpSpecFile, s);
199             (void) unlink(tmpSpecFile);
200             tmpSpecFile = _free(tmpSpecFile);
201             return 1;
202         }
203         tmpSpecFile = _free(tmpSpecFile);
204
205         /* Make the directory which contains the tarball the source 
206            directory for this run */
207
208         if (*arg != '/') {
209             (void)getcwd(buf, BUFSIZ);
210             strcat(buf, "/");
211             strcat(buf, arg);
212         } else 
213             strcpy(buf, arg);
214
215         cmd = buf + strlen(buf) - 1;
216         while (*cmd != '/') cmd--;
217         *cmd = '\0';
218
219         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
220     } else {
221         specURL = arg;
222     }
223     /*@=compmempass@*/
224
225     specut = urlPath(specURL, &specFile);
226     if (*specFile != '/') {
227         char *s = alloca(BUFSIZ);
228         (void)getcwd(s, BUFSIZ);
229         strcat(s, "/");
230         strcat(s, arg);
231         specURL = s;
232     }
233
234     if (specut != URL_IS_DASH) {
235         struct stat st;
236         if (Stat(specURL, &st) < 0) {
237             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
238             rc = 1;
239             goto exit;
240         }
241         if (! S_ISREG(st.st_mode)) {
242             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
243                 specURL);
244             rc = 1;
245             goto exit;
246         }
247
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);
252             rc = 1;
253             goto exit;
254         }
255     }
256     
257     /* Parse the spec file */
258 #define _anyarch(_f)    \
259 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
260     if (parseSpec(ts, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
261                 cookie, _anyarch(buildAmount), ba->force))
262     {
263         rc = 1;
264         goto exit;
265     }
266 #undef  _anyarch
267     if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
268         rc = 1;
269         goto exit;
270     }
271
272     /* Assemble source header from parsed components */
273     initSourceHeader(spec);
274
275     /* Check build prerequisites */
276     if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
277         rc = 1;
278         goto exit;
279     }
280
281     if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
282         rc = 1;
283         goto exit;
284     }
285     
286     if (ba->buildMode == 't')
287         (void) Unlink(specURL);
288     rc = 0;
289
290 exit:
291     spec = freeSpec(spec);
292     buildRootURL = _free(buildRootURL);
293     return rc;
294 }
295 /*@=boundswrite@*/
296
297 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
298 {
299     char *t, *te;
300     int rc = 0;
301     char * targets = ba->targets;
302 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
303     int cleanFlags = ba->buildAmount & buildCleanMask;
304     rpmVSFlags vsflags, ovsflags;
305
306     vsflags = rpmExpandNumeric("%{_vsflags_build}");
307     if (ba->qva_flags & VERIFY_DIGEST)
308         vsflags |= _RPMVSF_NODIGESTS;
309     if (ba->qva_flags & VERIFY_SIGNATURE)
310         vsflags |= _RPMVSF_NOSIGNATURES;
311     if (ba->qva_flags & VERIFY_HDRCHK)
312         vsflags |= RPMVSF_NOHDRCHK;
313     ovsflags = rpmtsSetVSFlags(ts, vsflags);
314
315     if (targets == NULL) {
316         rc =  buildForTarget(ts, arg, ba);
317         goto exit;
318     }
319
320     /* parse up the build operators */
321
322     printf(_("Building target platforms: %s\n"), targets);
323
324     ba->buildAmount &= ~buildCleanMask;
325     for (t = targets; *t != '\0'; t = te) {
326         char *target;
327         if ((te = strchr(t, ',')) == NULL)
328             te = t + strlen(t);
329         target = alloca(te-t+1);
330         strncpy(target, t, (te-t));
331         target[te-t] = '\0';
332         if (*te != '\0')
333             te++;
334         else    /* XXX Perform clean-up after last target build. */
335             ba->buildAmount |= cleanFlags;
336
337         printf(_("Building for target %s\n"), target);
338
339         /* Read in configuration for target. */
340         rpmFreeMacros(NULL);
341         (void) rpmReadConfigFiles(rcfile, target);
342         rc = buildForTarget(ts, arg, ba);
343         if (rc)
344             break;
345     }
346
347 exit:
348     vsflags = rpmtsSetVSFlags(ts, ovsflags);
349     /* Restore original configuration. */
350     rpmFreeMacros(NULL);
351     (void) rpmReadConfigFiles(rcfile, NULL);
352
353     return rc;
354 }