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