working version, not debug version
[build.git] / parseSpec.c
1 /*
2  * Parses RPM spec file into Makefile fragment. See
3  *
4  * http://fedora.redhat.com/docs/drafts/rpm-guide-en/ch-programming-c.html
5  *
6  * Mark Huang <mlhuang@cs.princeton.edu>
7  * Copyright (C) 2006 The Trustees of Princeton University
8  *
9  * $Id: parseSpec.c,v 1.4 2006/11/28 21:28:11 mef Exp $
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <libgen.h>
16 #include <errno.h>
17 #include <rpm/rpmlib.h>
18 #include <rpm/rpmts.h>
19 #include <rpm/rpmcli.h>
20 #include <rpm/rpmbuild.h>
21 #include <rpm/rpmspec.h>
22
23 /* the structure describing the options we take and the defaults */
24 static struct poptOption optionsTable[] = {
25         { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
26           "Common options for all rpm modes and executables:",
27           NULL },
28
29         POPT_AUTOALIAS
30         POPT_AUTOHELP
31         POPT_TABLEEND
32 };
33
34 /* Stolen from rpm/build/spec.c:rpmspecQuery() */
35 Spec
36 rpmspecGet(rpmts ts, const char * arg)
37 {
38         char * buildRoot = NULL;
39         int recursing = 0;
40         char * passPhrase = "";
41         char *cookie = NULL;
42         int anyarch = 1;
43         int force = 1;
44
45         if (parseSpec(ts, arg, "/", buildRoot, recursing, passPhrase,
46                       cookie, anyarch, force)) {
47                 fprintf(stderr, "query of specfile %s failed, can't parse\n", arg);
48                 return NULL;
49         }
50
51         return rpmtsSpec(ts);
52 }
53
54 int
55 main(int argc, char *argv[])
56 {
57         poptContext context;
58         rpmts ts = NULL;
59         int ec = 0;
60         Spec spec;
61         struct Source *source;
62         Package pkg;
63         const char *name, *version, *release, *arch, *unused;
64
65         /* BEGIN: support to pull out --target from the args list */
66         int  alen, i;
67         char *target = NULL;
68         int args = 1;
69         int tlen = strlen("--target");
70
71         /* walk argv list looking for --target */
72         while ((args+1)<argc) {
73           if(strncmp(argv[args],"--target",tlen)==0){
74             char **dash;
75
76             /* get arch component of the --target option */
77             dash = (char**)strchr(argv[args+1],'-');
78             if (dash != NULL) *dash=NULL;
79
80             /* copy arch component of --target option to target */
81             alen = strnlen(argv[args+1],32);
82             target = (char*)malloc(alen);
83             if (target == NULL) return errno;
84             strncpy(target,argv[args+1],alen);
85
86             /* change argc, argv to take out the "--target xxx" */
87             for (i=args;i<argc;i++) argv[i]=argv[i+2];
88             argc-=2;
89
90             break;
91           }
92         }
93         argv[1]=argv[argc-1];
94         argv[2]=0;
95         /* END: support to pull out --target from the args list */
96
97         /* Parse common options for all rpm modes and executables */
98         context = rpmcliInit(argc, argv, optionsTable);
99
100         /* Create transaction state */
101         ts = rpmtsCreate();
102
103         /* Parse spec file. The rpmcli functions don't allow you to
104          * access the Spec structure directly, so we call our own
105          * version of rpmSpecQuery() directly. */
106         spec = rpmspecGet(ts, argv[1]);
107         if (!spec)
108                 goto done;
109
110         /* Print sources */
111         for (source = spec->sources; source; source = source->next) {
112                 char fullSource[PATH_MAX];
113
114                 strncpy(fullSource, source->fullSource, sizeof(fullSource));
115                 printf("SOURCES += SOURCES/%s\n", basename(fullSource));
116         }
117
118         /* Get SRPM name from name of first package */ 
119         pkg = spec->packages;
120         name = version = release = NULL;
121         (void) headerNVR(pkg->header, &name, &version, &release);
122         if (name && version && release)
123                 printf("SRPM := SRPMS/%s-%s-%s.src.rpm\n",
124                        name, version, release);
125
126         /* Print non-empty packages */
127         for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
128                 name = version = release = arch = NULL;
129                 (void) headerNEVRA(pkg->header, &name, &unused, &version, &release, &arch);
130                 if (name && version && release && arch) {
131                         if (!pkg->fileList)
132                                 printf("# Empty\n# ");
133
134                         if (target != NULL) {
135                           if (strcmp(arch,target)!=0) {
136                             arch=target;
137                           }
138                         }
139                         printf("RPMS += RPMS/%s/%s-%s-%s.%s.rpm\n",
140                                arch, name, version, release, arch);
141                 }
142         }
143
144         spec = freeSpec(spec);
145
146  done:
147         ts = rpmtsFree(ts);
148         context = rpmcliFini(context);
149         return ec;
150