git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #50246 from aszlig/libredirect-darwin

libredirect: Add support for Darwin

commitf7d5b76e03f4dc473357acdc2be6506bd0818dd5 authorEelco Dolstra <edolstra@gmail.com> dateMon 12 Nov 2018, 12:21 (8 years ago) parent7cedee334dd1c6
2 files changed+89−8
pkgs/build-support/libredirect/default.nix+30−8 pkgs/build-support/libredirect/test.c+59
pkgs/build-support/libredirect/default.nix+30−8
@@ -1,19 +1,41 @@
1−{ stdenv }:
1+{ stdenv, coreutils }:
22
33stdenv.mkDerivation {
44 name = "libredirect-0";
55
6− unpackPhase = "cp ${./libredirect.c} libredirect.c";
6+ unpackPhase = ''
7+ cp ${./libredirect.c} libredirect.c
8+ cp ${./test.c} test.c
9+ '';
710
8− buildPhase =
9− ''
10− gcc -Wall -std=c99 -O3 -shared libredirect.c -o libredirect.so -fPIC -ldl
11− '';
11+ shlibext = stdenv.targetPlatform.extensions.sharedLibrary;
12+
13+ buildPhase = ''
14+ $CC -Wall -std=c99 -O3 -shared libredirect.c \
15+ -o "libredirect$shlibext" -fPIC -ldl
16+
17+ if [ -n "$doInstallCheck" ]; then
18+ $CC -Wall -std=c99 -O3 test.c -o test
19+ fi
20+ '';
21+
22+ installPhase = ''
23+ install -vD "libredirect$shlibext" "$out/lib/libredirect$shlibext"
24+ '';
25+
26+ doInstallCheck = true;
1227
13− installPhase = "mkdir -p $out/lib; cp libredirect.so $out/lib";
28+ installCheckPhase = if stdenv.isDarwin then ''
29+ NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true" \
30+ DYLD_INSERT_LIBRARIES="$out/lib/libredirect$shlibext" \
31+ DYLD_FORCE_FLAT_NAMESPACE=1 ./test
32+ '' else ''
33+ NIX_REDIRECTS="/foo/bar/test=${coreutils}/bin/true" \
34+ LD_PRELOAD="$out/lib/libredirect$shlibext" ./test
35+ '';
1436
1537 meta = {
16− platforms = stdenv.lib.platforms.linux;
38+ platforms = stdenv.lib.platforms.unix;
1739 description = "An LD_PRELOAD library to intercept and rewrite the paths in glibc calls";
1840 longDescription = ''
1941 libredirect is an LD_PRELOAD library to intercept and rewrite the paths in
pkgs/build-support/libredirect/test.cnew file+59
@@ -0,0 +1,59 @@
1+#include <assert.h>
2+#include <fcntl.h>
3+#include <spawn.h>
4+#include <stdio.h>
5+#include <unistd.h>
6+
7+#include <sys/stat.h>
8+#include <sys/types.h>
9+#include <sys/wait.h>
10+
11+#define TESTPATH "/foo/bar/test"
12+
13+extern char **environ;
14+
15+void test_spawn(void) {
16+ pid_t pid;
17+ int ret;
18+ posix_spawn_file_actions_t file_actions;
19+ char *argv[] = {"true", NULL};
20+
21+ assert(posix_spawn_file_actions_init(&file_actions) == 0);
22+
23+ ret = posix_spawn(&pid, TESTPATH, &file_actions, NULL, argv, environ);
24+
25+ assert(ret == 0);
26+ assert(waitpid(pid, NULL, 0) != -1);
27+}
28+
29+void test_execv(void) {
30+ char *argv[] = {"true", NULL};
31+ assert(execv(TESTPATH, argv) == 0);
32+}
33+
34+int main(void)
35+{
36+ FILE *testfp;
37+ int testfd;
38+ struct stat testsb;
39+
40+ testfp = fopen(TESTPATH, "r");
41+ assert(testfp != NULL);
42+ fclose(testfp);
43+
44+ testfd = open(TESTPATH, O_RDONLY);
45+ assert(testfd != -1);
46+ close(testfd);
47+
48+ assert(access(TESTPATH, X_OK) == 0);
49+
50+ assert(stat(TESTPATH, &testsb) != -1);
51+
52+ test_spawn();
53+ test_execv();
54+
55+ /* If all goes well, this is never reached because test_execv() replaces
56+ * the current process.
57+ */
58+ return 0;
59+}