summary refs log tree commit diff
path: root/pkgs/top-level/builder-defs.nix
blob: 94f83b9c06fd4e5dfe7e183ded79e43c75793e0f (plain) (blame)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
args: with args; with stringsWithDeps; with lib;
(rec
{
	inherit writeScript; 



	forceShare = if args ? forceShare then args.forceShare else ["man" "doc" "info"];

	archiveType = s: 
		(if hasSuffixHack ".tar" s then "tar"
		else if (hasSuffixHack ".tar.gz" s) || (hasSuffixHack ".tgz" s) then "tgz" 
		else if (hasSuffixHack ".tar.bz2" s) || (hasSuffixHack ".tbz2" s) then "tbz2"
		else if (hasSuffixHack ".zip" s) || (hasSuffixHack ".ZIP" s) then "zip"
		else (abort "unknown archive type : ${s}"));

	defAddToSearchPath = FullDepEntry ("
		addToSearchPathWithCustomDelimiter() {
			local delimiter=\$1
			local varName=\$2
			local needDir=\$3
			local addDir=\${4:-\$needDir}
			local prefix=\$5
			if [ -d \$prefix\$needDir ]; then
				if [ -z \${!varName} ]; then
					eval export \${varName}=\${prefix}\$addDir
				else
					eval export \${varName}=\${!varName}\${delimiter}\${prefix}\$addDir
				fi
			fi
		}

		addToSearchPath()
		{
			addToSearchPathWithCustomDelimiter \"\${PATH_DELIMITER}\" \"\$@\"
		}
	") [defNest];

	defNest = noDepEntry ("
		nestingLevel=0

		startNest() {
			nestingLevel=\$((\$nestingLevel + 1))
				echo -en \"\\e[\$1p\"
		}

		stopNest() {
			nestingLevel=\$((\$nestingLevel - 1))
				echo -en \"\\e[q\"
		}

		header() {
			startNest \"\$2\"
				echo \"\$1\"
		}

		# Make sure that even when we exit abnormally, the original nesting
		# level is properly restored.
		closeNest() {
			while test \$nestingLevel -gt 0; do
				stopNest
					done
		}

		trap \"closeNest\" EXIT
	");

	minInit = FullDepEntry ("
		set -e
		NIX_GCC=${stdenv.gcc}
		export SHELL=${stdenv.shell}
		# Set up the initial path.
		PATH=
		for i in \$NIX_GCC ${toString stdenv.initialPath}; do
		    PATH=\$PATH\${PATH:+:}\$i/bin
		done
	" + (if ((stdenv ? preHook) && (stdenv.preHook != null) && 
			((toString stdenv.preHook) != "")) then 
		"
		param1=${stdenv.param1}
		param2=${stdenv.param2}
		param3=${stdenv.param3}
		param4=${stdenv.param4}
		param5=${stdenv.param5}
		source ${stdenv.preHook}

		export TZ=UTC

		prefix=${if args ? prefix then (toString args.prefix) else "\$out"}

		"
	else "")) [defNest defAddToSearchPath];
		
	addInputs = FullDepEntry ("
		# Recursively find all build inputs.
		findInputs()
		{
		    local pkg=\$1

		    case \$pkgs in
			*\\ \$pkg\\ *)
			    return 0
			    ;;
		    esac
		    
		    pkgs=\"\$pkgs \$pkg \"

			echo \$pkg
		    if test -f \$pkg/nix-support/setup-hook; then
			source \$pkg/nix-support/setup-hook
		    fi
		}

		pkgs=\"\"
		for i in \$NIX_GCC ${toString buildInputs}; do
		    findInputs \$i
		done


		# Set the relevant environment variables to point to the build inputs
		# found above.
		addToEnv()
		{
		    local pkg=\$1
		"+
		(if !((args ? ignoreFailedInputs) && (args.ignoreFailedInputs == 1)) then "
		    if [ -e \$1/nix-support/failed ]; then
			echo \"failed input \$1\" >&2
			fail
		    fi
		" else "")
		+"
		    if test -d \$1/bin; then
			export _PATH=\$_PATH\${_PATH:+:}\$1/bin
		    fi

		    for i in \"\${envHooks[@]}\"; do
			\$i \$pkg
		    done
		}

		for i in \$pkgs; do
		    addToEnv \$i
		done


		# Add the output as an rpath.
		if test \"\$NIX_NO_SELF_RPATH\" != \"1\"; then
		    export NIX_LDFLAGS=\"-rpath \$out/lib \$NIX_LDFLAGS\"
		fi

		PATH=\$_PATH\${_PATH:+:}\$PATH
	") [minInit];
	
	defEnsureDir = FullDepEntry ("
		# Ensure that the given directories exists.
		ensureDir() {
		    local dir
		    for dir in \"\$@\"; do
			if ! test -x \"\$dir\"; then mkdir -p \"\$dir\"; fi
		    done
		}
	") [minInit];

	toSrcDir = s : FullDepEntry ((if (archiveType s) == "tar" then "
		tar xvf '${s}'
		cd \"\$(tar tf '${s}' | head -1 | sed -e 's@/.*@@' )\"
	" else if (archiveType s) == "tgz" then "
		tar xvzf '${s}'
		cd \"\$(tar tzf '${s}' | head -1 | sed -e 's@/.*@@' )\"
	" else if (archiveType s) == "tbz2" then "
		tar xvjf '${s}'
		cd \"\$(tar tjf '${s}' | head -1 | sed -e 's@/.*@@' )\"
	" else if (archiveType s) == "zip" then "
		unzip '${s}'
		cd \"$( unzip -lqq '${s}' | tail -1 | 
			sed -e 's@^\\(\\s\\+[-0-9:]\\+\\)\\{3,3\\}\\s\\+\\([^/]\\+\\)/.*@\\2@' )\"
	" else (abort "unknown archive type : ${s}"))+
		(if args ? goSrcDir then args.goSrcDir else "")
	) [minInit];

	doConfigure = FullDepEntry ("
		./configure --prefix=\"\$prefix\" ${toString configureFlags}
	") [minInit addInputs doUnpack];

	doAutotools = FullDepEntry ("
		mkdir -p config
		libtoolize --copy --force
		aclocal --force
		#Some packages do not need this
		autoheader || true; 
		automake --add-missing --copy
		autoconf
	")[minInit addInputs doUnpack];

	doMake = FullDepEntry ("	
		make ${toString makeFlags}
	") [minInit addInputs doUnpack];

	doUnpack = toSrcDir (toString src);

	installPythonPackage = FullDepEntry ("
		python setup.py install --prefix=\"\$prefix\" 
		") [minInit addInputs doUnpack];

	doMakeInstall = FullDepEntry ("
		make ${toString (getAttr ["makeFlags"] "" args)} "+
			"${toString (getAttr ["installFlags"] "" args)} install") [doMake];

	doForceShare = FullDepEntry (" 
		ensureDir \"\$prefix/share\"
		for d in ${toString forceShare}; do
			if [ -d \"\$prefix/\$d\" -a ! -d \"\$prefix/share/\$d\" ]; then
				mv -v \"\$prefix/\$d\" \"\$prefix/share\"
				ln -sv share/\$d \"\$prefix\"
			fi;
		done;
	") [minInit defEnsureDir];

	doDump = n: noDepEntry "echo Dump number ${n}; set";

	patchFlags = if args ? patchFlags then args.patchFlags else "-p1";

	patches = getAttr ["patches"] [] args;

	toPatchCommand = s: "cat ${toString s} | patch ${toString patchFlags}";

	doPatch = FullDepEntry (concatStringsSep ";"
		(map toPatchCommand patches)
	) [minInit doUnpack];

	envAdderInner = s: x: if x==null then s else y: 
		a: envAdderInner (s+"echo export ${x}='\"'\"\$${x}:${y}\";'\"'\n") a;

	envAdder = envAdderInner "";

	envAdderList = l:  if l==[] then "" else 
	"echo export ${__head l}='\"'\"\\\$${__head l}:${__head (__tail l)}\"'\"';\n" +
		envAdderList (__tail (__tail l));

	wrapEnv = cmd: env: "
		mv \"${cmd}\" \"${cmd}-orig\";
		touch \"${cmd}\";
		chmod a+rx \"${cmd}\";
		(${envAdderList env}
		echo '\"'\"${cmd}-orig\"'\"' '\"'\\\$@'\"' \n)  > \"${cmd}\"";

	doWrap = cmd: FullDepEntry (wrapEnv cmd (getAttr ["wrappedEnv"] [] args)) [minInit];

	doPropagate = FullDepEntry ("
		ensureDir \$out/nix-support
		echo '${toString (getAttr ["propagatedBuildInputs"] [] args)}' >\$out/nix-support/propagated-build-inputs
	") [minInit defEnsureDir];

	/*debug = x:(__trace x x);
	debugX = x:(__trace (__toXML x) x);*/

	replaceScriptVar = file: name: value: ("sed -e 's`^${name}=.*`${name}='\\''${value}'\\''`' -i ${file}");
	replaceInScript = file: l: (concatStringsSep "\n" ((pairMap (replaceScriptVar file) l)));
	replaceScripts = l:(concatStringsSep "\n" (pairMap replaceInScript l));
	doReplaceScripts = FullDepEntry (replaceScripts (getAttr ["shellReplacements"] [] args)) [minInit];
	makeNest = x:(if x==defNest.text then x else "startNest\n" + x + "\nstopNest\n");
	textClosure = textClosureMap makeNest;

	inherit noDepEntry FullDepEntry PackEntry;

	defList = (getAttr ["defList"] [] args);
	getVal = getValue args defList;
	check = checkFlag args;
	reqsList = getAttr ["reqsList"] [] args;
	buildInputsNames = filter (x: (null != getVal x))
		(uniqList {inputList = 
		(concatLists (map 
		(x:(if (x==[]) then [] else builtins.tail x)) 
		reqsList));});
	configFlags = getAttr ["configFlags"] [] args;
	buildFlags = getAttr ["buildFlags"] [] args;
	nameSuffixes = getAttr ["nameSuffixes"] [] args;
	autoBuildInputs = assert (checkReqs args defList reqsList);
		filter (x: x!=null) (map getVal buildInputsNames);
	autoConfigureFlags = condConcat "" configFlags check;
	autoMakeFlags = condConcat "" buildFlags check;
	useConfig = getAttr ["useConfig"] false args;
	buildInputs = 
		lib.closePropagation ((if useConfig then 
			autoBuildInputs else 
			getAttr ["buildInputs"] [] args)++
			(getAttr ["propagatedBuildInputs"] [] args));
	configureFlags = if useConfig then autoConfigureFlags else 
	    getAttr ["configureFlags"] "" args;
	makeFlags = if useConfig then autoMakeFlags else getAttr ["makeFlags"] "" args;

	inherit lib;
}) // args