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