- cpio mappings carry dirname/basename, not absolute path.
[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"),
66                 specfile, Fstrerror(fd));
67         return 0;
68     }
69     count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
70     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             break;
79         case ':':
80             checking = 0;
81             break;
82         default:
83             if (checking && !(isprint(*s) || isspace(*s))) return 0;
84             break;
85         }
86     }
87     return 1;
88 }
89
90 static int buildForTarget(const char *arg, struct rpmBuildArguments *ba,
91         const char *passPhrase, char *cookie)
92 {
93     int buildAmount = ba->buildAmount;
94     const char *buildRootURL = NULL;
95     const char * specFile;
96     const char * specURL;
97     int specut;
98     char buf[BUFSIZ];
99     Spec spec = NULL;
100     int rc;
101
102 #ifndef DYING
103     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
104 #endif
105
106     if (ba->buildRootOverride)
107         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
108
109     if (ba->buildMode == 't') {
110         FILE *fp;
111         const char *specDir;
112         const char * tmpSpecFile;
113         char * cmd, *s;
114         rpmCompressedMagic res = COMPRESSED_OTHER;
115         static const char *zcmds[] = { "cat", "gunzip", "bunzip2", "cat" };
116
117         specDir = rpmGetPath("%{_specdir}", NULL);
118
119         /* XXX Using mkstemp is difficult here. */
120         /* XXX FWIW, default %{_specdir} is root.root 0755 */
121         {   char tfn[64];
122             strcpy(tfn, "rpm-spec.XXXXXX");
123             tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
124         }
125
126         isCompressed(arg, &res);
127
128         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
129         sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
130                         zcmds[res & 0x3], arg, tmpSpecFile);
131         if (!(fp = popen(cmd, "r"))) {
132             rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
133             free((void *)specDir);
134             free((void *)tmpSpecFile);
135             return 1;
136         }
137         if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
138             /* Try again */
139             pclose(fp);
140
141             sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
142                     zcmds[res & 0x3], arg, tmpSpecFile);
143             if (!(fp = popen(cmd, "r"))) {
144                 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
145                 free((void *)specDir);
146                 free((void *)tmpSpecFile);
147                 return 1;
148             }
149             if (!fgets(buf, sizeof(buf) - 1, fp)) {
150                 /* Give up */
151                 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
152                         arg);
153                 unlink(tmpSpecFile);
154                 free((void *)specDir);
155                 free((void *)tmpSpecFile);
156                 return 1;
157             }
158         }
159         pclose(fp);
160
161         cmd = s = buf;
162         while (*cmd) {
163             if (*cmd == '/') s = cmd + 1;
164             cmd++;
165         }
166
167         cmd = s;
168
169         /* remove trailing \n */
170         s = cmd + strlen(cmd) - 1;
171         *s = '\0';
172
173         specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
174         sprintf(s, "%s/%s", specDir, cmd);
175         res = rename(tmpSpecFile, s);
176         free((void *)specDir);
177         
178         if (res) {
179             rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
180                         tmpSpecFile, s);
181             unlink(tmpSpecFile);
182             free((void *)tmpSpecFile);
183             return 1;
184         }
185         free((void *)tmpSpecFile);
186
187         /* Make the directory which contains the tarball the source 
188            directory for this run */
189
190         if (*arg != '/') {
191             (void)getcwd(buf, BUFSIZ);
192             strcat(buf, "/");
193             strcat(buf, arg);
194         } else 
195             strcpy(buf, arg);
196
197         cmd = buf + strlen(buf) - 1;
198         while (*cmd != '/') cmd--;
199         *cmd = '\0';
200
201         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
202     } else {
203         specURL = arg;
204     }
205
206     specut = urlPath(specURL, &specFile);
207     if (*specFile != '/') {
208         char *s = alloca(BUFSIZ);
209         (void)getcwd(s, BUFSIZ);
210         strcat(s, "/");
211         strcat(s, arg);
212         specURL = s;
213     }
214
215     if (specut != URL_IS_DASH) {
216         struct stat st;
217         if (Stat(specURL, &st) < 0) {
218             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
219             rc = 1;
220             goto exit;
221         }
222         if (! S_ISREG(st.st_mode)) {
223             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
224                 specURL);
225             rc = 1;
226             goto exit;
227         }
228
229         /* Try to verify that the file is actually a specfile */
230         if (!isSpecFile(specURL)) {
231             rpmError(RPMERR_BADSPEC,
232                 _("File %s does not appear to be a specfile.\n"), specURL);
233             rc = 1;
234             goto exit;
235         }
236     }
237     
238     /* Parse the spec file */
239 #define _anyarch(_f)    \
240 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
241     if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
242                 cookie, _anyarch(buildAmount), ba->force)) {
243         rc = 1;
244         goto exit;
245     }
246 #undef  _anyarch
247
248     /* Assemble source header from parsed components */
249     initSourceHeader(spec);
250
251     /* Check build prerequisites */
252     if (!ba->noDeps && checkSpec(spec->sourceHeader)) {
253         rc = 1;
254         goto exit;
255     }
256
257     if (buildSpec(spec, buildAmount, ba->noBuild)) {
258         rc = 1;
259         goto exit;
260     }
261     
262     if (ba->buildMode == 't') Unlink(specURL);
263     rc = 0;
264
265 exit:
266     if (spec)
267         freeSpec(spec);
268     if (buildRootURL)
269         free((void *)buildRootURL);
270     return rc;
271 }
272
273 int build(const char * arg, struct rpmBuildArguments * ba,
274         const char * passPhrase, char * cookie, const char * rcfile)
275 {
276     char *t, *te;
277     int rc = 0;
278     char *targets = ba->targets;
279 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
280     int cleanFlags = ba->buildAmount & buildCleanMask;
281
282     if (targets == NULL) {
283         rc =  buildForTarget(arg, ba, passPhrase, cookie);
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, cookie);
310         if (rc)
311             break;
312     }
313
314 exit:
315     /* Restore original configuration. */
316     rpmFreeMacros(NULL);
317     rpmReadConfigFiles(rcfile, NULL);
318     return rc;
319 }