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
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 /* from f10 and up, Spec is renamed rpmSpec */
29 #include <linux/limits.h>
32 extern size_t strnlen(const char *s, size_t maxlen);
34 /* the structure describing the options we take and the defaults */
35 static struct poptOption optionsTable[] = {
36 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
37 "Common options for all rpm modes and executables:",
45 /* Stolen from rpm/build/spec.c:rpmspecQuery() */
47 rpmspecGet(rpmts ts, const char * arg)
49 char * buildRoot = NULL;
51 char * passPhrase = "";
56 if (parseSpec(ts, arg, "/", buildRoot, recursing, passPhrase,
57 cookie, anyarch, force)) {
58 fprintf(stderr, "query of specfile %s failed, can't parse\n", arg);
66 main(int argc, char *argv[])
72 struct Source *source;
74 const char *name, *version, *release, *arch, *unused;
75 const char *package_name;
77 /* BEGIN: support to pull out --target from the args list */
81 int tlen = strlen("--target");
84 /* walk argv list looking for --target */
85 while ((args+1)<argc) {
86 if(strncmp(argv[args],"--target",tlen)==0){
89 /* get arch component of the --target option */
90 dash = (char**)strchr(argv[args+1],'-');
91 if (dash != NULL) *dash=NULL;
93 /* copy arch component of --target option to target */
94 alen = strnlen(argv[args+1],32);
95 target = (char*)malloc(alen+1);
96 if (target == NULL) return errno;
97 strncpy(target,argv[args+1],alen);
100 /* change argc, argv to take out the "--target xxx" */
101 for (i=args;i<argc;i++) argv[i]=argv[i+2];
108 argv[1]=argv[argc-2];
109 argv[2]=argv[argc-1];
112 /* END: support to pull out --target from the args list */
114 /* Parse common options for all rpm modes and executables */
115 context = rpmcliInit(argc, argv, optionsTable);
117 /* Create transaction state */
120 /* Parse spec file. The rpmcli functions don't allow you to
121 * access the Spec structure directly, so we call our own
122 * version of rpmSpecQuery() directly. */
123 spec = rpmspecGet(ts, argv[1]);
124 package_name = argv[2];
131 for (source = spec->sources; source; source = source->next) {
132 char fullSource[PATH_MAX];
134 strncpy(fullSource, source->fullSource, sizeof(fullSource));
135 printf("%s.tarballs += SOURCES/%s\n", package_name, basename(fullSource));
136 /* computes the SOURCEDIR variable by removing .tar.gz or .tar.bz2 */
138 char *suffixes[] = {".tar.gz",".tgz",".tar.bz2", NULL};
142 for (suffix=suffixes ; *suffix ; suffix++) {
143 printf("# trying %s\n",*suffix);
144 suffix_index=strstr(fullSource,*suffix);
146 char sourcename[PATH_MAX];
147 size_t len = (size_t)(suffix_index-fullSource);
148 strncpy(sourcename,fullSource,len);
149 sourcename[len]='\0';
150 printf ("%s.source := SOURCES/%s\n",package_name,basename(sourcename));
158 /* Get SRPM name from name of first package */
159 pkg = spec->packages;
160 name = version = release = NULL;
161 (void) headerNVR(pkg->header, &name, &version, &release);
162 if (name && version && release)
163 printf("%s.srpm := SRPMS/%s-%s-%s.src.rpm\n",
164 package_name, name, version, release);
166 /* Print non-empty packages */
167 for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
168 name = version = release = arch = NULL;
169 (void) headerNEVRA(pkg->header, &name, &unused, &version, &release, &arch);
170 if (name && version && release && arch) {
171 if (target != NULL) {
172 if (strcmp(arch,target)!=0) {
176 /* skip empty packages */
178 /* attach (add) rpm path to package */
179 printf("%s.rpms += RPMS/%s/%s-%s-%s.%s.rpm\n",
180 package_name, arch, name, version, release, arch);
182 printf("%s.rpmnames += %s\n",
184 /* attach path to rpm name */
185 printf("%s.rpm-path := RPMS/%s/%s-%s-%s.%s.rpm\n",
186 name,arch, name, version, release, arch);
187 /* attach package to rpm name for backward resolution - should be unique */
188 printf("%s.package := %s\n",
194 /* export some macros to make */
195 /* note : this relies on pl-specific conventions and might be wrong */
197 char *macros[] = { "release" , "name" , "version" , "taglevel" , NULL } ;
199 char *macro=malloc(32);
200 for (nav=macros; *nav; nav++) {
201 sprintf(macro,"%%{%s}",*nav);
202 char *value = rpmExpand(macro,NULL);
203 printf ("%s.rpm-%s := %s\n",package_name,*nav,value);
208 printf ("%s.rpm-arch := %s\n",package_name,target);
210 spec = freeSpec(spec);
214 context = rpmcliFini(context);