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