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