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