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.2 2006/03/08 21:48:42 mlhuang Exp $
16 #include <rpm/rpmlib.h>
17 #include <rpm/rpmts.h>
18 #include <rpm/rpmcli.h>
19 #include <rpm/rpmbuild.h>
20 #include <rpm/rpmspec.h>
22 /* the structure describing the options we take and the defaults */
23 static struct poptOption optionsTable[] = {
24 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
25 "Common options for all rpm modes and executables:",
33 /* Stolen from rpm/build/spec.c:rpmspecQuery() */
35 rpmspecGet(rpmts ts, const char * arg)
37 char * buildRoot = NULL;
39 char * passPhrase = "";
44 if (parseSpec(ts, arg, "/", buildRoot, recursing, passPhrase,
45 cookie, anyarch, force)) {
46 fprintf(stderr, "query of specfile %s failed, can't parse\n", arg);
54 main(int argc, char *const argv[])
60 struct Source *source;
62 const char *name, *version, *release, *arch, *unused;
64 /* Parse common options for all rpm modes and executables */
65 context = rpmcliInit(argc, argv, optionsTable);
67 /* Create transaction state */
70 /* Parse spec file. The rpmcli functions don't allow you to
71 * access the Spec structure directly, so we call our own
72 * version of rpmSpecQuery() directly. */
73 spec = rpmspecGet(ts, argv[1]);
78 for (source = spec->sources; source; source = source->next) {
79 char fullSource[PATH_MAX];
81 strncpy(fullSource, source->fullSource, sizeof(fullSource));
82 printf("SOURCES += SOURCES/%s\n", basename(fullSource));
85 /* Get SRPM name from name of first package */
87 name = version = release = NULL;
88 (void) headerNVR(pkg->header, &name, &version, &release);
89 if (name && version && release)
90 printf("SRPM := SRPMS/%s-%s-%s.src.rpm\n",
91 name, version, release);
93 /* Print non-empty packages */
94 for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
95 name = version = release = arch = NULL;
96 (void) headerNEVRA(pkg->header, &name, &unused, &version, &release, &arch);
97 if (name && version && release && arch) {
99 printf("# Empty\n# ");
100 printf("RPMS += RPMS/%s/%s-%s-%s.%s.rpm\n",
101 arch, name, version, release, arch);
105 spec = freeSpec(spec);
109 context = rpmcliFini(context);