- fix: QUERY_FOR_LIST file count clobbered.
[platform/upstream/rpm.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 #include <rpmurl.h>
10
11 #include "build.h"
12 #include "debug.h"
13
14 /*@access rpmTransactionSet @*/ /* XXX compared with NULL @*/
15 /*@access rpmdb @*/             /* XXX compared with NULL @*/
16 /*@access FD_t @*/              /* XXX compared with NULL @*/
17
18 /**
19  */
20 static int checkSpec(Header h)
21         /*@modifies h, fileSystem @*/
22 {
23     const char * rootdir = NULL;
24     rpmdb db = NULL;
25     int mode = O_RDONLY;
26     rpmTransactionSet ts;
27     rpmDependencyConflict conflicts;
28     int numConflicts;
29     int rc;
30
31     if (!headerIsEntry(h, RPMTAG_REQUIREFLAGS))
32         return 0;
33
34     if (rpmdbOpen(rootdir, &db, mode, 0644)) {
35         const char * dn;
36         dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
37         rpmError(RPMERR_OPEN, _("cannot open rpm database in %s\n"), dn);
38         dn = _free(dn);
39         exit(EXIT_FAILURE);
40     }
41     ts = rpmtransCreateSet(db, rootdir);
42
43     rc = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
44
45     rc = rpmdepCheck(ts, &conflicts, &numConflicts);
46     if (rc == 0 && conflicts) {
47         rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
48         printDepProblems(stderr, conflicts, numConflicts);
49         conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
50         rc = 1;
51     }
52
53     if (ts != NULL)
54         rpmtransFree(ts);
55     if (db != NULL)
56         (void) rpmdbClose(db);
57
58     return rc;
59 }
60
61 /*
62  * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
63  * angielsku...
64  */
65 /* XXX this is still a dumb test but at least it's i18n aware */
66 /**
67  */
68 static int isSpecFile(const char * specfile)
69         /*@modifies fileSystem @*/
70 {
71     char buf[256];
72     const char * s;
73     FD_t fd;
74     int count;
75     int checking;
76
77     fd = Fopen(specfile, "r.ufdio");
78     if (fd == NULL || Ferror(fd)) {
79         rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
80                 specfile, Fstrerror(fd));
81         return 0;
82     }
83     count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
84     (void) Fclose(fd);
85
86     checking = 1;
87     for (s = buf; count--; s++) {
88         switch (*s) {
89         case '\r':
90         case '\n':
91             checking = 1;
92             break;
93         case ':':
94             checking = 0;
95             break;
96         default:
97             if (checking && !(isprint(*s) || isspace(*s))) return 0;
98             break;
99         }
100     }
101     return 1;
102 }
103
104 /**
105  */
106 static int buildForTarget(const char * arg, BTA_t ba,
107                 const char * passPhrase, char * cookie)
108         /*@modifies fileSystem @*/
109 {
110     int buildAmount = ba->buildAmount;
111     const char * buildRootURL = NULL;
112     const char * specFile;
113     const char * specURL;
114     int specut;
115     char buf[BUFSIZ];
116     Spec spec = NULL;
117     int rc;
118
119 #ifndef DYING
120     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
121 #endif
122
123     if (ba->buildRootOverride)
124         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
125
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
226     specut = urlPath(specURL, &specFile);
227     if (*specFile != '/') {
228         char *s = alloca(BUFSIZ);
229         (void)getcwd(s, BUFSIZ);
230         strcat(s, "/");
231         strcat(s, arg);
232         specURL = s;
233     }
234
235     if (specut != URL_IS_DASH) {
236         struct stat st;
237         if (Stat(specURL, &st) < 0) {
238             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
239             rc = 1;
240             goto exit;
241         }
242         if (! S_ISREG(st.st_mode)) {
243             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
244                 specURL);
245             rc = 1;
246             goto exit;
247         }
248
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);
253             rc = 1;
254             goto exit;
255         }
256     }
257     
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)) {
263         rc = 1;
264         goto exit;
265     }
266 #undef  _anyarch
267
268     /* Assemble source header from parsed components */
269     initSourceHeader(spec);
270
271     /* Check build prerequisites */
272     if (!ba->noDeps && checkSpec(spec->sourceHeader)) {
273         rc = 1;
274         goto exit;
275     }
276
277     if (buildSpec(spec, buildAmount, ba->noBuild)) {
278         rc = 1;
279         goto exit;
280     }
281     
282     if (ba->buildMode == 't')
283         (void) Unlink(specURL);
284     rc = 0;
285
286 exit:
287     spec = freeSpec(spec);
288     buildRootURL = _free(buildRootURL);
289     return rc;
290 }
291
292 int build(const char * arg, BTA_t ba,
293         const char * passPhrase, char * cookie, const char * rcfile)
294 {
295     char *t, *te;
296     int rc = 0;
297     char * targets = ba->targets;
298 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
299     int cleanFlags = ba->buildAmount & buildCleanMask;
300
301     if (targets == NULL) {
302         rc =  buildForTarget(arg, ba, passPhrase, cookie);
303         goto exit;
304     }
305
306     /* parse up the build operators */
307
308     printf(_("Building target platforms: %s\n"), targets);
309
310     ba->buildAmount &= ~buildCleanMask;
311     for (t = targets; *t != '\0'; t = te) {
312         char *target;
313         if ((te = strchr(t, ',')) == NULL)
314             te = t + strlen(t);
315         target = alloca(te-t+1);
316         strncpy(target, t, (te-t));
317         target[te-t] = '\0';
318         if (*te != '\0')
319             te++;
320         else    /* XXX Perform clean-up after last target build. */
321             ba->buildAmount |= cleanFlags;
322
323         printf(_("Building for target %s\n"), target);
324
325         /* Read in configuration for target. */
326         rpmFreeMacros(NULL);
327         (void) rpmReadConfigFiles(rcfile, target);
328         rc = buildForTarget(arg, ba, passPhrase, cookie);
329         if (rc)
330             break;
331     }
332
333 exit:
334     /* Restore original configuration. */
335     rpmFreeMacros(NULL);
336     (void) rpmReadConfigFiles(rcfile, NULL);
337     return rc;
338 }