summary refs log tree commit diff
path: root/pkgs/os-specific/linux/uclibc/default.nix
blob: 073927ba49a599d3f0cecb845402c59c6bc5f7e1 (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
{stdenv, fetchurl, linuxHeaders, libiconv, cross ? null, gccCross ? null,
extraConfig ? ""}:

assert stdenv.isLinux;
assert cross != null -> gccCross != null;

let
  configParser = ''
    function parseconfig {
        set -x
        while read LINE; do
            NAME=`echo "$LINE" | cut -d \  -f 1`
            OPTION=`echo "$LINE" | cut -d \  -f 2`

            if test -z "$NAME"; then
                continue
            fi

            if test "$NAME" == "CLEAR"; then
                echo "parseconfig: CLEAR"
                echo > .config
            fi

            echo "parseconfig: removing $NAME"
            sed -i /^$NAME=/d .config

            if test "$OPTION" != n; then
                echo "parseconfig: setting $NAME=$OPTION"
                echo "$NAME=$OPTION" >> .config
            fi
        done
        set +x
    }
  '';

  archMakeFlag = if (cross != null) then "ARCH=${cross.arch}" else "";
  crossMakeFlag = if (cross != null) then "CROSS=${cross.config}-" else "";

  nixConfig = ''
    RUNTIME_PREFIX "/"
    DEVEL_PREFIX "/"
    UCLIBC_HAS_WCHAR y
    UCLIBC_HAS_FTW y
    UCLIBC_HAS_RPC y
    DO_C99_MATH y
    UCLIBC_HAS_PROGRAM_INVOCATION_NAME y
    KERNEL_HEADERS "${linuxHeaders}/include"
  '';

in
stdenv.mkDerivation {
  name = "uclibc-0.9.31" + stdenv.lib.optionalString (cross != null)
    ("-" + cross.config);

  src = fetchurl {
    url = http://www.uclibc.org/downloads/uClibc-0.9.31.tar.bz2;
    sha256 = "1yk328fnz0abgh2vm2r68y65ckfkx97rdp8hbg4xvmx5s94kblw0";
  };

  # 'ftw' needed to build acl, a coreutils dependency
  configurePhase = ''
    make defconfig ${archMakeFlag}
    ${configParser}
    cat << EOF | parseconfig
    ${nixConfig}
    ${extraConfig}
    ${if cross != null then cross.uclibc.extraConfig else ""}
    $extraCrossConfig
    EOF
    make oldconfig
  '';

  # Cross stripping hurts.
  dontStrip = if (cross != null) then true else false;

  makeFlags = [ crossMakeFlag "VERBOSE=1" ];

  buildInputs = stdenv.lib.optional (gccCross != null) gccCross;

  installPhase = ''
    mkdir -p $out
    make PREFIX=$out VERBOSE=1 install ${crossMakeFlag}
    (cd $out/include && ln -s $(ls -d ${linuxHeaders}/include/* | grep -v "scsi$") .)
    sed -i s@/lib/@$out/lib/@g $out/lib/libc.so
  '';

  passthru = {
    # Derivations may check for the existance of this attribute, to know what to link to.
    inherit libiconv;
  };
  
  meta = {
    homepage = http://www.uclibc.org/;
    description = "A small implementation of the C library";
    license = "LGPLv2";
  };
}