Reorganize sources before implementing --repackage.
[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 "debug.h"
8
9 static int checkSpec(Header h)
10 {
11     char *rootdir = NULL;
12     rpmdb db = NULL;
13     int mode = O_RDONLY;
14     rpmTransactionSet ts;
15     struct rpmDependencyConflict * conflicts;
16     int numConflicts;
17     int rc;
18
19     if (!headerIsEntry(h, RPMTAG_REQUIREFLAGS))
20         return 0;
21
22     if (rpmdbOpen(rootdir, &db, mode, 0644)) {
23         const char *dn;
24         dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
25         rpmError(RPMERR_OPEN, _("cannot open rpm database in %s\n"), dn);
26         free((void *)dn);
27         exit(EXIT_FAILURE);
28     }
29     ts = rpmtransCreateSet(db, rootdir);
30
31     rc = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
32
33     rc = rpmdepCheck(ts, &conflicts, &numConflicts);
34     if (rc == 0 && conflicts) {
35         rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
36         printDepProblems(stderr, conflicts, numConflicts);
37         rpmdepFreeConflicts(conflicts, numConflicts);
38         rc = 1;
39     }
40
41     if (ts)
42         rpmtransFree(ts);
43     if (db)
44         rpmdbClose(db);
45
46     return rc;
47 }
48
49 /*
50  * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
51  * angielsku...
52  */
53 /* XXX this is still a dumb test but at least it's i18n aware */
54 static int isSpecFile(const char *specfile)
55 {
56     char buf[256];
57     const char * s;
58     FD_t fd;
59     int count;
60     int checking;
61
62     fd = Fopen(specfile, "r.ufdio");
63     if (fd == NULL || Ferror(fd)) {
64         rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
65                 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         /* XXX Using mkstemp is difficult here. */
119         /* XXX FWIW, default %{_specdir} is root.root 0755 */
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\n"));
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\n"));
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\n"),
151                         arg);
152                 unlink(tmpSpecFile);
153                 free((void *)specDir);
154                 free((void *)tmpSpecFile);
155                 return 1;
156             }
157         }
158         pclose(fp);
159
160         cmd = s = buf;
161         while (*cmd) {
162             if (*cmd == '/') s = cmd + 1;
163             cmd++;
164         }
165
166         cmd = s;
167
168         /* remove trailing \n */
169         s = cmd + strlen(cmd) - 1;
170         *s = '\0';
171
172         specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
173         sprintf(s, "%s/%s", specDir, cmd);
174         res = rename(tmpSpecFile, s);
175         free((void *)specDir);
176         
177         if (res) {
178             rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
179                         tmpSpecFile, s);
180             unlink(tmpSpecFile);
181             free((void *)tmpSpecFile);
182             return 1;
183         }
184         free((void *)tmpSpecFile);
185
186         /* Make the directory which contains the tarball the source 
187            directory for this run */
188
189         if (*arg != '/') {
190             (void)getcwd(buf, BUFSIZ);
191             strcat(buf, "/");
192             strcat(buf, arg);
193         } else 
194             strcpy(buf, arg);
195
196         cmd = buf + strlen(buf) - 1;
197         while (*cmd != '/') cmd--;
198         *cmd = '\0';
199
200         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
201     } else {
202         specURL = arg;
203     }
204
205     specut = urlPath(specURL, &specFile);
206     if (*specFile != '/') {
207         char *s = alloca(BUFSIZ);
208         (void)getcwd(s, BUFSIZ);
209         strcat(s, "/");
210         strcat(s, arg);
211         specURL = s;
212     }
213
214     if (specut != URL_IS_DASH) {
215         struct stat st;
216         if (Stat(specURL, &st) < 0) {
217             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
218             rc = 1;
219             goto exit;
220         }
221         if (! S_ISREG(st.st_mode)) {
222             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
223                 specURL);
224             rc = 1;
225             goto exit;
226         }
227
228         /* Try to verify that the file is actually a specfile */
229         if (!isSpecFile(specURL)) {
230             rpmError(RPMERR_BADSPEC,
231                 _("File %s does not appear to be a specfile.\n"), specURL);
232             rc = 1;
233             goto exit;
234         }
235     }
236     
237     /* Parse the spec file */
238 #define _anyarch(_f)    \
239 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
240     if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
241                 cookie, _anyarch(buildAmount), ba->force)) {
242         rc = 1;
243         goto exit;
244     }
245 #undef  _anyarch
246
247     /* Assemble source header from parsed components */
248     initSourceHeader(spec);
249
250     /* Check build prerequisites */
251     if (!ba->noDeps && checkSpec(spec->sourceHeader)) {
252         rc = 1;
253         goto exit;
254     }
255
256     if (buildSpec(spec, buildAmount, ba->noBuild)) {
257         rc = 1;
258         goto exit;
259     }
260     
261     if (ba->buildMode == 't') Unlink(specURL);
262     rc = 0;
263
264 exit:
265     if (spec)
266         freeSpec(spec);
267     if (buildRootURL)
268         free((void *)buildRootURL);
269     return rc;
270 }
271
272 int build(const char * arg, struct rpmBuildArguments * ba,
273         const char * passPhrase, char * cookie, const char * rcfile)
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, cookie);
283         goto exit;
284     }
285
286     /* parse up the build operators */
287
288     printf(_("Building target platforms: %s\n"), targets);
289
290     ba->buildAmount &= ~buildCleanMask;
291     for (t = targets; *t != '\0'; t = te) {
292         char *target;
293         if ((te = strchr(t, ',')) == NULL)
294             te = t + strlen(t);
295         target = alloca(te-t+1);
296         strncpy(target, t, (te-t));
297         target[te-t] = '\0';
298         if (*te)
299             te++;
300         else    /* XXX Perform clean-up after last target build. */
301             ba->buildAmount |= cleanFlags;
302
303         printf(_("Building for target %s\n"), target);
304
305         /* Read in configuration for target. */
306         rpmFreeMacros(NULL);
307         rpmReadConfigFiles(rcfile, target);
308         rc = buildForTarget(arg, ba, passPhrase, cookie);
309         if (rc)
310             break;
311     }
312
313 exit:
314     /* Restore original configuration. */
315     rpmFreeMacros(NULL);
316     rpmReadConfigFiles(rcfile, NULL);
317     return rc;
318 }