summaryrefslogtreecommitdiffstats
path: root/pkgs/development/interpreters/luajit/default.nix
blob: fede20472a1e5d9ad968e81cb913a2ed6bd5d77f (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
187
{
  lib,
  stdenv,
  buildPackages,
  version,
  src,
  replaceVars,
  extraMeta ? { },
  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,
  enableRegisterAllocationRandomization ? false,
  useSystemMalloc ? false,
  # Upstream generates randomized string id's by default for security reasons
  # https://github.com/LuaJIT/LuaJIT/issues/626. Deterministic string id's should
  # never be needed for correctness (that should be fixed in the lua code),
  # but may be helpful when you want to embed jit-compiled raw lua blobs in
  # binaries that you want to be reproducible.
  deterministicStringIds ? 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 =
    lib.optional (!enableFFI) "-DLUAJIT_DISABLE_FFI"
    ++ lib.optional (!enableJIT) "-DLUAJIT_DISABLE_JIT"
    ++ lib.optional enable52Compat "-DLUAJIT_ENABLE_LUA52COMPAT"
    ++ lib.optional (!enableGC64) "-DLUAJIT_DISABLE_GC64"
    ++ lib.optional useSystemMalloc "-DLUAJIT_USE_SYSMALLOC"
    ++ lib.optional enableValgrindSupport "-DLUAJIT_USE_VALGRIND"
    ++ lib.optional enableGDBJITSupport "-DLUAJIT_USE_GDBJIT"
    ++ lib.optional enableAPICheck "-DLUAJIT_USE_APICHECK"
    ++ lib.optional enableVMAssertions "-DLUAJIT_USE_ASSERT"
    ++ lib.optional enableRegisterAllocationRandomization "-DLUAJIT_RANDOM_RA"
    ++ lib.optional deterministicStringIds "-DLUAJIT_SECURITY_STRID=0";

  # LuaJIT requires build for 32bit architectures to be build on x86 not x86_64
  # TODO support also other build architectures. The ideal way would be to use
  # stdenv_32bit but that doesn't work due to host platform mismatch:
  # https://github.com/NixOS/nixpkgs/issues/212494
  buildStdenv =
    if buildPackages.stdenv.hostPlatform.isx86_64 && stdenv.hostPlatform.is32bit then
      buildPackages.pkgsi686Linux.buildPackages.stdenv
    else
      buildPackages.stdenv;

in
stdenv.mkDerivation (finalAttrs: {
  pname = "luajit";
  inherit version src;

  luaversion = "5.1";

  postPatch = ''
    substituteInPlace Makefile --replace-fail 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-fail "#CCDEBUG= -g" "CCDEBUG= -g"
    fi
  '';

  dontConfigure = true;

  buildInputs = lib.optional enableValgrindSupport valgrind;

  buildFlags = [
    "amalg" # Build highly optimized version
  ];
  makeFlags = [
    "PREFIX=$(out)"
    "DEFAULT_CC=cc"
    "CROSS=${stdenv.cc.targetPrefix}"
    "HOST_CC=${buildStdenv.cc}/bin/cc"
  ]
  # LuaJIT's build system needs an explicit target on MinGW, otherwise it can
  # emit ELF-specific assembler directives (e.g. .hidden/.type/.size) that the
  # PE/COFF toolchain doesn't accept.
  ++ lib.optionals stdenv.hostPlatform.isMinGW [
    "TARGET_SYS=Windows"
  ]
  ++ lib.optional enableJITDebugModule "INSTALL_LJLIBD=$(INSTALL_LMOD)"
  ++ lib.optional stdenv.hostPlatform.isStatic "BUILDMODE=static";
  enableParallelBuilding = true;
  env.NIX_CFLAGS_COMPILE = toString XCFLAGS;

  # The LuaJIT build produces `src/luajit.exe` on Windows targets, but the
  # upstream install rule expects `src/luajit`. Provide a compatibility copy.
  preInstall = lib.optionalString stdenv.hostPlatform.isMinGW ''
    if [[ -e src/luajit.exe && ! -e src/luajit ]]; then
      cp -p src/luajit.exe src/luajit
    fi
  '';

  postInstall = ''
    mkdir -p $out/nix-support
    cp ${
      replaceVars ../lua-5/utils.sh {
        luapathsearchpaths = lib.escapeShellArgs finalAttrs.LuaPathSearchPaths;
        luacpathsearchpaths = lib.escapeShellArgs finalAttrs.LuaCPathSearchPaths;
      }
    } $out/nix-support/utils.sh
    ( cd "$out/include"; ln -s luajit-*/* . )
    ln -s "$out"/bin/luajit-* "$out"/bin/lua
    if [[ ! -e "$out"/bin/luajit ]]; then
      ln -s "$out"/bin/luajit* "$out"/bin/luajit
    fi
  '';

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

  setupHook = builtins.toFile "lua-setup-hook" ''
    source @out@/nix-support/utils.sh
    addEnvHooks "$hostOffset" luaEnvHook
  '';

  # 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 packageOverrides luaAttr;
      inherit (finalAttrs) luaversion;
      executable = "lua";
      luaOnBuildForBuild = override pkgsBuildBuild.${luaAttr};
      luaOnBuildForHost = override pkgsBuildHost.${luaAttr};
      luaOnBuildForTarget = override pkgsBuildTarget.${luaAttr};
      luaOnHostForHost = override pkgsHostHost.${luaAttr};
      luaOnTargetForTarget = lib.optionalAttrs (lib.hasAttr luaAttr pkgsTargetTarget) (
        override pkgsTargetTarget.${luaAttr}
      );
    };

  meta =

    {
      description = "High-performance JIT compiler for Lua 5.1";
      homepage = "https://luajit.org/";
      license = lib.licenses.mit;
      # MSYS2 ships LuaJIT for mingw-w64, and nixpkgs consumers (like phosphor)
      # need it in Windows cross builds.
      platforms = lib.platforms.linux ++ lib.platforms.darwin ++ lib.platforms.windows;
      badPlatforms = [
        "loongarch64-linux" # See https://github.com/LuaJIT/LuaJIT/issues/1278
        "riscv64-linux" # See https://github.com/LuaJIT/LuaJIT/issues/628
        # `#error "No support for PPC64"`
        "powerpc64-linux"
        "powerpc64le-linux"
      ];
      mainProgram = "lua";
      maintainers = with lib.maintainers; [
        thoughtpolice
        vcunat
        lblasc
      ];
    }
    // extraMeta;
})