- transaction sets cerated in cli main.
[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 @*/
21         /*@modifies ts, h, fileSystem @*/
22 {
23     rpmDependencyConflict 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 = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
32
33     rc = rpmdepCheck(ts, &conflicts, &numConflicts);
34     /*@-branchstate@*/
35     if (rc == 0 && conflicts) {
36         rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
37         printDepProblems(stderr, conflicts, numConflicts);
38         conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
39         rc = 1;
40     }
41     /*@=branchstate@*/
42
43     return rc;
44 }
45
46 /*
47  * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
48  * angielsku...
49  */
50 /* XXX this is still a dumb test but at least it's i18n aware */
51 /**
52  */
53 static int isSpecFile(const char * specfile)
54         /*@globals fileSystem @*/
55         /*@modifies fileSystem @*/
56 {
57     char buf[256];
58     const char * s;
59     FD_t fd;
60     int count;
61     int checking;
62
63     fd = Fopen(specfile, "r.ufdio");
64     if (fd == NULL || Ferror(fd)) {
65         rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
66                 specfile, Fstrerror(fd));
67         return 0;
68     }
69     count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
70     (void) Fclose(fd);
71
72     checking = 1;
73     for (s = buf; count--; s++) {
74         switch (*s) {
75         case '\r':
76         case '\n':
77             checking = 1;
78             /*@switchbreak@*/ break;
79         case ':':
80             checking = 0;
81             /*@switchbreak@*/ break;
82         default:
83             if (checking && !(isprint(*s) || isspace(*s))) return 0;
84             /*@switchbreak@*/ break;
85         }
86     }
87     return 1;
88 }
89
90 /**
91  */
92 static int buildForTarget(rpmTransactionSet ts, const char * arg, BTA_t ba)
93         /*@globals rpmGlobalMacroContext,
94                 fileSystem, internalState @*/
95         /*@modifies ts, rpmGlobalMacroContext, fileSystem, internalState @*/
96 {
97     const char * passPhrase = ba->passPhrase;
98     const char * cookie = ba->cookie;
99     int buildAmount = ba->buildAmount;
100     const char * buildRootURL = NULL;
101     const char * specFile;
102     const char * specURL;
103     int specut;
104     char buf[BUFSIZ];
105     Spec spec = NULL;
106     int rc;
107
108 #ifndef DYING
109     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
110 #endif
111
112     /*@-branchstate@*/
113     if (ba->buildRootOverride)
114         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
115     /*@=branchstate@*/
116
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
217     specut = urlPath(specURL, &specFile);
218     if (*specFile != '/') {
219         char *s = alloca(BUFSIZ);
220         (void)getcwd(s, BUFSIZ);
221         strcat(s, "/");
222         strcat(s, arg);
223         specURL = s;
224     }
225
226     if (specut != URL_IS_DASH) {
227         struct stat st;
228         if (Stat(specURL, &st) < 0) {
229             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
230             rc = 1;
231             goto exit;
232         }
233         if (! S_ISREG(st.st_mode)) {
234             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
235                 specURL);
236             rc = 1;
237             goto exit;
238         }
239
240         /* Try to verify that the file is actually a specfile */
241         if (!isSpecFile(specURL)) {
242             rpmError(RPMERR_BADSPEC,
243                 _("File %s does not appear to be a specfile.\n"), specURL);
244             rc = 1;
245             goto exit;
246         }
247     }
248     
249     /* Parse the spec file */
250 #define _anyarch(_f)    \
251 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
252     if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
253                 cookie, _anyarch(buildAmount), ba->force)) {
254         rc = 1;
255         goto exit;
256     }
257 #undef  _anyarch
258
259     /* Assemble source header from parsed components */
260     initSourceHeader(spec);
261
262     /* Check build prerequisites */
263     if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
264         rc = 1;
265         goto exit;
266     }
267
268     if (buildSpec(spec, buildAmount, ba->noBuild)) {
269         rc = 1;
270         goto exit;
271     }
272     
273     if (ba->buildMode == 't')
274         (void) Unlink(specURL);
275     rc = 0;
276
277 exit:
278     spec = freeSpec(spec);
279     buildRootURL = _free(buildRootURL);
280     return rc;
281 }
282
283 int build(rpmTransactionSet ts, const char * arg, BTA_t ba, const char * rcfile)
284 {
285     char *t, *te;
286     int rc = 0;
287     char * targets = ba->targets;
288 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
289     int cleanFlags = ba->buildAmount & buildCleanMask;
290
291     if (targets == NULL) {
292         rc =  buildForTarget(ts, arg, ba);
293         goto exit;
294     }
295
296     /* parse up the build operators */
297
298     printf(_("Building target platforms: %s\n"), targets);
299
300     ba->buildAmount &= ~buildCleanMask;
301     for (t = targets; *t != '\0'; t = te) {
302         char *target;
303         if ((te = strchr(t, ',')) == NULL)
304             te = t + strlen(t);
305         target = alloca(te-t+1);
306         strncpy(target, t, (te-t));
307         target[te-t] = '\0';
308         if (*te != '\0')
309             te++;
310         else    /* XXX Perform clean-up after last target build. */
311             ba->buildAmount |= cleanFlags;
312
313         printf(_("Building for target %s\n"), target);
314
315         /* Read in configuration for target. */
316         rpmFreeMacros(NULL);
317         (void) rpmReadConfigFiles(rcfile, target);
318         rc = buildForTarget(ts, arg, ba);
319         if (rc)
320             break;
321     }
322
323 exit:
324     /* Restore original configuration. */
325     rpmFreeMacros(NULL);
326     (void) rpmReadConfigFiles(rcfile, NULL);
327
328     return rc;
329 }