about summary refs log tree commit diff
path: root/pkgs/development/interpreters/luajit/default.nix
blob: 64aa0345e80b0d061ddcb1cc4778b18cdb467c36 (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
{ lib
, stdenv
, fetchFromGitHub
, buildPackages
, isStable
, version
, src
, extraMeta ? { }
, callPackage
, self
, packageOverrides ? (final: prev: {})
, pkgsBuildBuild
, pkgsBuildHost
, pkgsBuildTarget
, pkgsHostHost
, pkgsTargetTarget
, passthruFun
, enableFFI ? true
, enableJIT ? true
, enableJITDebugModule ? enableJIT
, enableGC64 ? true
, enable52Compat ? false
, enableValgrindSupport ? false
, valgrind ? null
, enableGDBJITSupport ? false
, enableAPICheck ? false
, enableVMAssertions ? false
, useSystemMalloc ? false
, luaAttr ? "luajit_${lib.versions.major version}_${lib.versions.minor version}"
} @ inputs:
assert enableJITDebugModule -> enableJIT;
assert enableGDBJITSupport -> enableJIT;
assert enableValgrindSupport -> valgrind != null;
let

  luaPackages = self.pkgs;

  XCFLAGS = with lib;
    optional (!enableFFI) "-DLUAJIT_DISABLE_FFI"
    ++ optional (!enableJIT) "-DLUAJIT_DISABLE_JIT"
    ++ optional enable52Compat "-DLUAJIT_ENABLE_LUA52COMPAT"
    ++ optional (!enableGC64) "-DLUAJIT_DISABLE_GC64"
    ++ optional useSystemMalloc "-DLUAJIT_USE_SYSMALLOC"
    ++ optional enableValgrindSupport "-DLUAJIT_USE_VALGRIND"
    ++ optional enableGDBJITSupport "-DLUAJIT_USE_GDBJIT"
    ++ optional enableAPICheck "-DLUAJIT_USE_APICHECK"
    ++ optional enableVMAssertions "-DLUAJIT_USE_ASSERT"
  ;
in
stdenv.mkDerivation rec {
  pname = "luajit";
  inherit version src;

  luaversion = "5.1";

  postPatch = ''
    substituteInPlace Makefile --replace ldconfig :
    if test -n "''${dontStrip-}"; then
      # CCDEBUG must be non-empty or everything will be stripped, -g being
      # passed by nixpkgs CC wrapper is insufficient on its own
      substituteInPlace src/Makefile --replace "#CCDEBUG= -g" "CCDEBUG= -g"
    fi

    {
      echo -e '
        #undef  LUA_PATH_DEFAULT
        #define LUA_PATH_DEFAULT "./share/lua/${luaversion}/?.lua;./?.lua;./?/init.lua"
        #undef  LUA_CPATH_DEFAULT
        #define LUA_CPATH_DEFAULT "./lib/lua/${luaversion}/?.so;./?.so;./lib/lua/${luaversion}/loadall.so"
      '
    } >> src/luaconf.h
  '';

  configurePhase = false;

  buildInputs = lib.optional enableValgrindSupport valgrind;

  buildFlags = [
    "amalg" # Build highly optimized version
  ];
  makeFlags = [
    "PREFIX=$(out)"
    "DEFAULT_CC=cc"
    "CROSS=${stdenv.cc.targetPrefix}"
    # TODO: when pointer size differs, we would need e.g. -m32
    "HOST_CC=${buildPackages.stdenv.cc}/bin/cc"
  ] ++ lib.optional enableJITDebugModule "INSTALL_LJLIBD=$(INSTALL_LMOD)";
  enableParallelBuilding = true;
  NIX_CFLAGS_COMPILE = XCFLAGS;

  postInstall = ''
    ( cd "$out/include"; ln -s luajit-*/* . )
    ln -s "$out"/bin/luajit-* "$out"/bin/lua
  '' + lib.optionalString (!isStable) ''
    ln -s "$out"/bin/luajit-* "$out"/bin/luajit
  '';

  LuaPathSearchPaths    = luaPackages.luaLib.luaPathList;
  LuaCPathSearchPaths   = luaPackages.luaLib.luaCPathList;

  setupHook = luaPackages.lua-setup-hook luaPackages.luaLib.luaPathList luaPackages.luaLib.luaCPathList;

  # copied from python
  passthru = let
    # When we override the interpreter we also need to override the spliced versions of the interpreter
    inputs' = lib.filterAttrs (n: v: ! lib.isDerivation v && n != "passthruFun") inputs;
    override = attr: let lua = attr.override (inputs' // { self = lua; }); in lua;
  in passthruFun rec {
    inherit self luaversion packageOverrides luaAttr;
    executable = "lua";
    luaOnBuildForBuild = override pkgsBuildBuild.${luaAttr};
    luaOnBuildForHost = override pkgsBuildHost.${luaAttr};
    luaOnBuildForTarget = override pkgsBuildTarget.${luaAttr};
    luaOnHostForHost = override pkgsHostHost.${luaAttr};
    luaOnTargetForTarget = if lib.hasAttr luaAttr pkgsTargetTarget then (override pkgsTargetTarget.${luaAttr}) else {};
  };

  meta = with lib; {
    description = "High-performance JIT compiler for Lua 5.1";
    homepage = "http://luajit.org";
    license = licenses.mit;
    platforms = platforms.linux ++ platforms.darwin;
    # See https://github.com/LuaJIT/LuaJIT/issues/628
    badPlatforms = [ "riscv64-linux" "riscv64-linux" ];
    maintainers = with maintainers; [ thoughtpolice smironov vcunat lblasc ];
  } // extraMeta;
}