summaryrefslogtreecommitdiff
path: root/build/parseSpec.c
blob: a3badbb18d4e3549d1686539793cdf90b41a3773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>

#include "header.h"
#include "rpmlib.h"
#include "part.h"
#include "spec.h"
#include "parse.h"
#include "read.h"
#include "misc.h"

static void setStandardMacros(Spec spec, char *arch, char *os);

int parseSpec(Spec *specp, char *specFile, char *buildRoot,
	      int inBuildArch, char *passPhrase, char *cookie)
{
    int parsePart = PART_PREAMBLE;
    int initialPackage = 1;
    char *name, *arch, *os;
    Package pkg;
    int x, index;
    Spec spec;
    
    /* Set up a new Spec structure with no packages. */
    spec = newSpec();

    spec->specFile = strdup(specFile);
    if (buildRoot) {
	spec->gotBuildRoot = 1;
	spec->buildRoot = strdup(buildRoot);
    }
    spec->docDir = strdup(rpmGetVar(RPMVAR_DEFAULTDOCDIR));
    spec->inBuildArchitectures = inBuildArch;
    if (passPhrase) {
	spec->passPhrase = strdup(passPhrase);
    }
    if (cookie) {
	spec->cookie = strdup(cookie);
    }

    if (rpmGetVar(RPMVAR_TIMECHECK)) {
	if (parseNum(rpmGetVar(RPMVAR_TIMECHECK), &(spec->timeCheck))) {
	    rpmError(RPMERR_BADSPEC, "Timecheck value must be an integer: %s",
		     rpmGetVar(RPMVAR_TIMECHECK));
	    freeSpec(spec);
	    return RPMERR_BADSPEC;
	}
    } else {
	spec->timeCheck = 0;
    }

    /* Add some standard macros */
    rpmGetArchInfo(&arch, NULL);
    rpmGetOsInfo(&os, NULL);
    setStandardMacros(spec, arch, os);

    /* All the parse*() functions expect to have a line pre-read */
    /* in the spec's line buffer.  Except for parsePreamble(),   */
    /* which handles the initial entry into a spec file.         */
    
    while (parsePart != PART_NONE) {
	switch (parsePart) {
	  case PART_PREAMBLE:
	    parsePart = parsePreamble(spec, initialPackage);
	    initialPackage = 0;
	    break;
	  case PART_PREP:
	    parsePart = parsePrep(spec);
	    break;
	  case PART_BUILD:
	  case PART_INSTALL:
	  case PART_CLEAN:
	    parsePart = parseBuildInstallClean(spec, parsePart);
	    break;
	  case PART_CHANGELOG:
	    parsePart = parseChangelog(spec);
	    break;
	  case PART_DESCRIPTION:
	    parsePart = parseDescription(spec);
	    break;
	  case PART_PRE:
	  case PART_POST:
	  case PART_PREUN:
	  case PART_POSTUN:
	  case PART_VERIFYSCRIPT:
	    parsePart = parseScript(spec, parsePart);
	    break;

	  case PART_FILES:
	    parsePart = parseFiles(spec);
	    break;

	  case PART_TRIGGERIN:
	  case PART_TRIGGERUN:
	    printf("Triggers are not supported yet.\n");
	    exit(1);
	    /*parsePart = parseTrigger(spec, parsePart);*/
	    break;
	}

	if (parsePart < 0) {
	    freeSpec(spec);
	    return parsePart;
	}

	if (parsePart == PART_BUILDARCHITECTURES) {
	    spec->buildArchitectureSpecs =
		malloc(sizeof(Spec) * spec->buildArchitectureCount);
	    x = 0;
	    index = 0;
	    while (x < spec->buildArchitectureCount) {
		if (rpmMachineScore(RPM_MACHTABLE_BUILDARCH,
				    spec->buildArchitectures[x])) {
		    rpmSetMachine(spec->buildArchitectures[x], NULL);
		    if (parseSpec(&(spec->buildArchitectureSpecs[index]),
				  specFile, buildRoot, 1,
				  passPhrase, cookie)) {
			spec->buildArchitectureCount = index;
			freeSpec(spec);
			return RPMERR_BADSPEC;
		    }
		    index++;
		}
		x++;
	    }
	    spec->buildArchitectureCount = index;
	    if (! index) {
		freeSpec(spec);
		rpmError(RPMERR_BADSPEC, "No buildable architectures");
		return RPMERR_BADSPEC;
	    }
	    closeSpec(spec);
	    *specp = spec;
	    return 0;
	}
    }

    /* Check for description in each package and add arch and os */
    pkg = spec->packages;
    while (pkg) {
	headerGetEntry(pkg->header, RPMTAG_NAME, NULL, (void **) &name, NULL);
	if (!headerIsEntry(pkg->header, RPMTAG_DESCRIPTION)) {
	    rpmError(RPMERR_BADSPEC, "Package has no %%description: %s", name);
	    freeSpec(spec);
	    return RPMERR_BADSPEC;
	}

	headerAddEntry(pkg->header, RPMTAG_OS, RPM_STRING_TYPE, os, 1);
	headerAddEntry(pkg->header, RPMTAG_ARCH, RPM_STRING_TYPE, arch, 1);

	pkg = pkg->next;
    }

    closeSpec(spec);
    *specp = spec;
    return 0;
}

static void setStandardMacros(Spec spec, char *arch, char *os)
{
    char buf[BUFSIZ];
    int x;

    addMacro(&spec->macros, "sourcedir", rpmGetVar(RPMVAR_SOURCEDIR));
    addMacro(&spec->macros, "builddir", rpmGetVar(RPMVAR_BUILDDIR));
    addMacro(&spec->macros, "optflags", rpmGetVar(RPMVAR_OPTFLAGS));
    addMacro(&spec->macros, "buildarch", arch);
    addMacro(&spec->macros, "buildos", os);
    
    x = 0;
    while (arch[x]) {
	buf[x] = tolower(arch[x]);
	x++;
    }
    buf[x] = '\0';
    addMacro(&spec->macros, "buildarch_lc", buf);
    x = 0;
    while (os[x]) {
	buf[x] = tolower(os[x]);
	x++;
    }
    buf[x] = '\0';
    addMacro(&spec->macros, "buildos_lc", buf);
}