2 * Parses RPM spec file into Makefile fragment. See
4 * http://fedora.redhat.com/docs/drafts/rpm-guide-en/ch-programming-c.html
6 * Mark Huang <mlhuang@cs.princeton.edu>
7 * Copyright (C) 2006 The Trustees of Princeton University
9 * $Id: parseSpec.c,v 1.7 2007/07/23 15:12:44 mef Exp $
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>
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:",
34 /* Stolen from rpm/build/spec.c:rpmspecQuery() */
36 rpmspecGet(rpmts ts, const char * arg)
38 char * buildRoot = NULL;
40 char * passPhrase = "";
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);
55 main(int argc, char *argv[])
61 struct Source *source;
63 const char *name, *version, *release, *arch, *unused;
65 /* BEGIN: support to pull out --target from the args list */
69 int tlen = strlen("--target");
71 /* walk argv list looking for --target */
72 while ((args+1)<argc) {
73 if(strncmp(argv[args],"--target",tlen)==0){
76 /* get arch component of the --target option */
77 dash = (char**)strchr(argv[args+1],'-');
78 if (dash != NULL) *dash=NULL;
80 /* copy arch component of --target option to target */
81 alen = strnlen(argv[args+1],32);
82 target = (char*)malloc(alen+1);
83 if (target == NULL) return errno;
84 strncpy(target,argv[args+1],alen);
87 /* change argc, argv to take out the "--target xxx" */
88 for (i=args;i<argc;i++) argv[i]=argv[i+2];
98 /* END: support to pull out --target from the args list */
100 /* Parse common options for all rpm modes and executables */
101 context = rpmcliInit(argc, argv, optionsTable);
103 /* Create transaction state */
106 /* Parse spec file. The rpmcli functions don't allow you to
107 * access the Spec structure directly, so we call our own
108 * version of rpmSpecQuery() directly. */
109 spec = rpmspecGet(ts, argv[1]);
114 for (source = spec->sources; source; source = source->next) {
115 char fullSource[PATH_MAX];
117 strncpy(fullSource, source->fullSource, sizeof(fullSource));
118 printf("SOURCES += SOURCES/%s\n", basename(fullSource));
121 /* Get SRPM name from name of first package */
122 pkg = spec->packages;
123 name = version = release = NULL;
124 (void) headerNVR(pkg->header, &name, &version, &release);
125 if (name && version && release)
126 printf("SRPM := SRPMS/%s-%s-%s.src.rpm\n",
127 name, version, release);
129 /* Print non-empty packages */
130 for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
131 name = version = release = arch = NULL;
132 (void) headerNEVRA(pkg->header, &name, &unused, &version, &release, &arch);
133 if (name && version && release && arch) {
135 printf("# Empty\n# ");
137 if (target != NULL) {
138 if (strcmp(arch,target)!=0) {
142 printf("RPMS += RPMS/%s/%s-%s-%s.%s.rpm\n",
143 arch, name, version, release, arch);
147 spec = freeSpec(spec);
151 context = rpmcliFini(context);