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