Start separating build options.
[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 buildplatform(char *arg, int buildAmount, char *passPhrase,
9                   char *buildRoot, int fromTarball, int test, char *cookie,
10                   force);
11 #endif
12
13 int buildplatform(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 * arch, char * os, 
160           char *buildplatforms, int force)
161 {
162     char *platform, *t;
163     int rc;
164
165     if (buildplatforms == NULL) {
166         rc =  buildplatform(arg, buildAmount, passPhrase, buildRoot,
167                 fromTarball, test, cookie, force);
168         return rc;
169     }
170
171     /* parse up the build operators */
172
173     printf("building these platforms: %s\n", buildplatforms);
174
175     t = buildplatforms;
176     while((platform = strtok(t, ",")) != NULL) {
177         t = NULL;
178         printf("building %s\n", platform);
179
180         rpmSetVar(RPMVAR_BUILDPLATFORM,platform);
181         rpmReadConfigFiles(rcfile, arch, os, 1, platform);
182         rc = buildplatform(arg, buildAmount, passPhrase, buildRoot,
183             fromTarball, test, cookie, force);
184         if (rc)
185             return rc;
186     }
187
188     return 0;
189 }
190
191 #define POPT_USECATALOG         1000
192 #define POPT_NOLANG             1001
193 #define POPT_RMSOURCE           1002
194 #define POPT_RMBUILD            1003
195 #define POPT_BUILDROOT          1004
196
197 extern int noLang;
198 static int useCatalog = 0;
199
200 static void buildArgCallback(poptContext con, enum poptCallbackReason reason,
201                              const struct poptOption * opt, const char * arg,
202                              struct rpmBuildArguments * data)
203 {
204     switch (opt->val) {
205     case POPT_USECATALOG: data->useCatalog = 1; break;
206     case POPT_NOLANG: data->noLang = 1; break;
207     case POPT_RMSOURCE: data->buildAmount |= RPMBUILD_RMSOURCE; break;
208     case POPT_RMBUILD: data->buildAmount |= RPMBUILD_RMBUILD; break;
209     case POPT_BUILDROOT:
210         if (data->buildRootOverride) {
211             fprintf(stderr, _("buildroot already specified"));
212             exit(EXIT_FAILURE);
213         }
214         data->buildRootOverride = strdup(arg);
215         break;
216     }
217 }
218
219 struct poptOption rpmBuildPoptTable[] = {
220         { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA,
221                 buildArgCallback, 0, NULL, NULL },
222         { "buildroot", '\0', POPT_ARG_STRING, 0,  POPT_BUILDROOT,
223                 N_("override build root"), "DIRECTORY" },
224         { "clean", '\0', 0, 0, POPT_RMBUILD,
225                 N_("remove build tree when done"), NULL},
226         { "nolang", '\0', 0, &noLang, POPT_NOLANG,
227                 N_("do not accept I18N msgstr's from specfile"), NULL},
228         { "rmsource", '\0', 0, 0, POPT_RMSOURCE,
229                 N_("remove sources and specfile when done"), NULL},
230         { "usecatalog", '\0', 0, &useCatalog, POPT_USECATALOG,
231                 N_("lookup I18N strings in specfile catalog"), NULL},
232         { 0, 0, 0, 0, 0,        NULL, NULL }
233 };
234