add per-target macrofiles to search path.
[platform/upstream/rpm.git] / build.c
1 #include "system.h"
2
3 #include "build/rpmbuild.h"
4 #include "popt/popt.h"
5 #include "build.h"
6
7 static int buildForTarget(const char *arg, int buildAmount, const char *passPhrase,
8                   const char *buildRoot, int fromTarball, int test, char *cookie,
9                   int force)
10 {
11
12     FILE *f;
13     const char * specfile;
14     int res = 0;
15     struct stat statbuf;
16     char * s;
17     int count, fd;
18     char buf[BUFSIZ];
19     Spec spec = NULL;
20
21     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
22
23     if (fromTarball) {
24         const char *specDir;
25         const char * tmpSpecFile;
26         char * cmd, *s;
27
28         specDir = rpmGetPath("%{_specdir}", NULL);
29
30         {   char tfn[64];
31             strcpy(tfn, "rpm-spec.XXXXXX");
32             tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
33         }
34
35         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
36         sprintf(cmd, "gunzip < %s | tar xOvf - Specfile 2>&1 > %s",
37                         arg, tmpSpecFile);
38         if (!(f = popen(cmd, "r"))) {
39             fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
40                         strerror(errno));
41             xfree(specDir);
42             xfree(tmpSpecFile);
43             return 1;
44         }
45         if ((!fgets(buf, sizeof(buf) - 1, f)) || !strchr(buf, '/')) {
46             /* Try again */
47             pclose(f);
48
49             sprintf(cmd, "gunzip < %s | tar xOvf - \\*.spec 2>&1 > %s", arg,
50                     tmpSpecFile);
51             if (!(f = popen(cmd, "r"))) {
52                 fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
53                         strerror(errno));
54                 xfree(specDir);
55                 xfree(tmpSpecFile);
56                 return 1;
57             }
58             if (!fgets(buf, sizeof(buf) - 1, f)) {
59                 /* Give up */
60                 fprintf(stderr, _("Failed to read spec file from %s\n"), arg);
61                 unlink(tmpSpecFile);
62                 xfree(specDir);
63                 xfree(tmpSpecFile);
64                 return 1;
65             }
66         }
67         pclose(f);
68
69         cmd = s = buf;
70         while (*cmd) {
71             if (*cmd == '/') s = cmd + 1;
72             cmd++;
73         }
74
75         cmd = s;
76
77         /* remove trailing \n */
78         s = cmd + strlen(cmd) - 1;
79         *s = '\0';
80
81         s = alloca(strlen(specDir) + strlen(cmd) + 5);
82         sprintf(s, "%s/%s", specDir, cmd);
83         
84         if (rename(tmpSpecFile, s)) {
85             fprintf(stderr, _("Failed to rename %s to %s: %s\n"),
86                     tmpSpecFile, s, strerror(errno));
87             unlink(tmpSpecFile);
88             xfree(specDir);
89             xfree(tmpSpecFile);
90             return 1;
91         }
92
93         /* Make the directory which contains the tarball the source 
94            directory for this run */
95
96         if (*arg != '/') {
97             (void)getcwd(buf, BUFSIZ);
98             strcat(buf, "/");
99             strcat(buf, arg);
100         } else 
101             strcpy(buf, arg);
102
103         cmd = buf + strlen(buf) - 1;
104         while (*cmd != '/') cmd--;
105         *cmd = '\0';
106
107         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
108         xfree(specDir);
109         xfree(tmpSpecFile);
110         specfile = s;
111     } else if (arg[0] == '/') {
112         specfile = arg;
113     } else {
114         char *s = alloca(BUFSIZ);
115         (void)getcwd(s, BUFSIZ);
116         strcat(s, "/");
117         strcat(s, arg);
118         specfile = s;
119     }
120
121     stat(specfile, &statbuf);
122     if (! S_ISREG(statbuf.st_mode)) {
123         fprintf(stderr, _("File is not a regular file: %s\n"), specfile);
124         return 1;
125     }
126     
127     if ((fd = open(specfile, O_RDONLY)) < 0) {
128         fprintf(stderr, _("Unable to open spec file: %s\n"), specfile);
129         return 1;
130     }
131     count = read(fd, buf, sizeof(buf) < 128 ? sizeof(buf) : 128);
132     close(fd);
133     s = buf;
134     while(count--) {
135         if (! (isprint(*s) || isspace(*s))) {
136             fprintf(stderr, _("File contains non-printable characters(%c): %s\n"), *s,
137                     specfile);
138             return 1;
139         }
140         s++;
141     }
142
143 #define _anyarch(_f)    \
144 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
145     if (parseSpec(&spec, specfile, buildRoot, 0, passPhrase, cookie,
146         _anyarch(buildAmount), force)) {
147             return 1;
148     }
149 #undef  _anyarch
150
151     if (buildSpec(spec, buildAmount, test)) {
152         freeSpec(spec);
153         return 1;
154     }
155     
156     if (fromTarball) unlink(specfile);
157
158     freeSpec(spec);
159     
160     return res;
161 }
162
163 int build(const char *arg, int buildAmount, const char *passPhrase,
164           const char *buildRoot, int fromTarball, int test, char *cookie,
165           const char * rcfile, char *targets, int force)
166 {
167     char *t, *te;
168     int rc;
169
170     if (targets == NULL) {
171         rc =  buildForTarget(arg, buildAmount, passPhrase, buildRoot,
172                 fromTarball, test, cookie, force);
173         return rc;
174     }
175
176     /* parse up the build operators */
177
178     printf("Building target platforms: %s\n", targets);
179
180     for (t = targets; *t != '\0'; t = te) {
181         char *target;
182         if ((te = strchr(t, ',')) == NULL)
183             te = t + strlen(t);
184         target = alloca(te-t+1);
185         strncpy(target, t, (te-t));
186         target[te-t] = '\0';
187         printf("Building for target %s\n", target);
188
189         rpmReadConfigFiles(rcfile, target);
190         rc = buildForTarget(arg, buildAmount, passPhrase, buildRoot,
191             fromTarball, test, cookie, force);
192         if (rc)
193             return rc;
194
195         freeMacros(NULL);
196     }
197
198     return 0;
199 }
200
201 #define POPT_USECATALOG         1000
202 #define POPT_NOLANG             1001
203 #define POPT_RMSOURCE           1002
204 #define POPT_RMBUILD            1003
205 #define POPT_BUILDROOT          1004
206 #define POPT_BUILDARCH          1005
207 #define POPT_BUILDOS            1006
208 #define POPT_TARGETPLATFORM     1007
209 #define POPT_NOBUILD            1008
210 #define POPT_SHORTCIRCUIT       1009
211
212 extern int noLang;
213 static int noBuild = 0;
214 static int useCatalog = 0;
215
216 static void buildArgCallback(poptContext con, enum poptCallbackReason reason,
217                              const struct poptOption * opt, const char * arg,
218                              struct rpmBuildArguments * data)
219 {
220     switch (opt->val) {
221     case POPT_USECATALOG: data->useCatalog = 1; break;
222     case POPT_NOBUILD: data->noBuild = 1; break;
223     case POPT_NOLANG: data->noLang = 1; break;
224     case POPT_SHORTCIRCUIT: data->shortCircuit = 1; break;
225     case POPT_RMSOURCE: data->buildAmount |= RPMBUILD_RMSOURCE; break;
226     case POPT_RMBUILD: data->buildAmount |= RPMBUILD_RMBUILD; break;
227     case POPT_BUILDROOT:
228         if (data->buildRootOverride) {
229             fprintf(stderr, _("buildroot already specified"));
230             exit(EXIT_FAILURE);
231         }
232         data->buildRootOverride = strdup(arg);
233         break;
234     case POPT_BUILDARCH:
235         fprintf(stderr, _("--buildarch has been obsoleted.  Use the --target option instead.\n")); 
236         exit(EXIT_FAILURE);
237         break;
238     case POPT_BUILDOS:
239         fprintf(stderr, _("--buildos has been obsoleted.  Use the --target option instead.\n")); 
240         exit(EXIT_FAILURE);
241         break;
242     case POPT_TARGETPLATFORM:
243         if (data->targets) {
244             int len = strlen(data->targets) + strlen(arg) + 2;
245             data->targets = realloc(data->targets, len);
246             strcat(data->targets, ",");
247         } else {
248             data->targets = malloc(strlen(arg) + 1);
249             data->targets[0] = '\0';
250         }
251         strcat(data->targets, arg);
252         break;
253     }
254 }
255
256 struct poptOption rpmBuildPoptTable[] = {
257         { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA,
258                 buildArgCallback, 0, NULL, NULL },
259         { "buildarch", '\0', POPT_ARG_STRING, 0,  POPT_BUILDARCH,
260                 N_("override build architecture"), "ARCH" },
261         { "buildos", '\0', POPT_ARG_STRING, 0,  POPT_BUILDOS,
262                 N_("override build operating system"), "OS" },
263         { "buildroot", '\0', POPT_ARG_STRING, 0,  POPT_BUILDROOT,
264                 N_("override build root"), "DIRECTORY" },
265         { "clean", '\0', 0, 0, POPT_RMBUILD,
266                 N_("remove build tree when done"), NULL},
267         { "nobuild", '\0', 0, &noBuild,  POPT_NOBUILD,
268                 N_("do not execute any stages of the build"), NULL },
269         { "nolang", '\0', 0, &noLang, POPT_NOLANG,
270                 N_("do not accept I18N msgstr's from specfile"), NULL},
271         { "rmsource", '\0', 0, 0, POPT_RMSOURCE,
272                 N_("remove sources and specfile when done"), NULL},
273         { "short-circuit", '\0', 0, 0,  POPT_SHORTCIRCUIT,
274                 N_("skip straight to specified stage (only for c,i)"), NULL },
275         { "target", '\0', POPT_ARG_STRING, 0,  POPT_TARGETPLATFORM,
276                 N_("override target platform"), "CPU-VENDOR-OS" },
277         { "usecatalog", '\0', 0, &useCatalog, POPT_USECATALOG,
278                 N_("lookup I18N strings in specfile catalog"), NULL},
279         { 0, 0, 0, 0, 0,        NULL, NULL }
280 };