btrbk: add mainProgram (#356350)
[NixPkgs.git] / pkgs / development / interpreters / lua-5 / interpreter.nix
blob082fa879e2f7d700629c3ea2b4982f1c0118e897
1 { lib, stdenv, fetchurl, readline
2 , compat ? false
3 , makeWrapper
4 , self
5 , packageOverrides ? (final: prev: {})
6 , substituteAll
7 , pkgsBuildBuild
8 , pkgsBuildHost
9 , pkgsBuildTarget
10 , pkgsHostHost
11 , pkgsTargetTarget
12 , version
13 , hash
14 , passthruFun
15 , patches ? []
16 , postConfigure ? null
17 , postBuild ? null
18 , staticOnly ? stdenv.hostPlatform.isStatic
19 , luaAttr ? "lua${lib.versions.major version}_${lib.versions.minor version}"
20 } @ inputs:
22 stdenv.mkDerivation (finalAttrs:
23   let
24     luaPackages = self.pkgs;
26     luaversion = lib.versions.majorMinor finalAttrs.version;
28     plat = if (stdenv.hostPlatform.isLinux && lib.versionOlder self.luaversion "5.4") then "linux"
29           else if (stdenv.hostPlatform.isLinux && lib.versionAtLeast self.luaversion "5.4") then "linux-readline"
30           else if stdenv.hostPlatform.isDarwin then "macosx"
31           else if stdenv.hostPlatform.isMinGW then "mingw"
32           else if stdenv.hostPlatform.isFreeBSD then "freebsd"
33           else if stdenv.hostPlatform.isSunOS then "solaris"
34           else if stdenv.hostPlatform.isBSD then "bsd"
35           else if stdenv.hostPlatform.isUnix then "posix"
36           else "generic";
38     compatFlags = if (lib.versionOlder self.luaversion "5.3") then " -DLUA_COMPAT_ALL"
39                   else if (lib.versionOlder self.luaversion "5.4") then " -DLUA_COMPAT_5_1 -DLUA_COMPAT_5_2"
40                   else " -DLUA_COMPAT_5_3";
41   in
43   {
44   pname = "lua";
45   inherit version;
46   outputs = [ "out" "doc" ];
48   src = fetchurl {
49     url = "https://www.lua.org/ftp/lua-${finalAttrs.version}.tar.gz";
50     sha256 = hash;
51   };
53   LuaPathSearchPaths  = luaPackages.luaLib.luaPathList;
54   LuaCPathSearchPaths = luaPackages.luaLib.luaCPathList;
55   setupHook = builtins.toFile "lua-setup-hook" ''
56       source @out@/nix-support/utils.sh
57       addEnvHooks "$hostOffset" luaEnvHook
58       '';
60   nativeBuildInputs = [ makeWrapper ];
61   buildInputs = [ readline ];
63   inherit patches;
65   postPatch = ''
66       sed -i "s@#define LUA_ROOT[[:space:]]*\"/usr/local/\"@#define LUA_ROOT  \"$out/\"@g" src/luaconf.h
68       # abort if patching didn't work
69       grep $out src/luaconf.h
70   '' + lib.optionalString (!stdenv.hostPlatform.isDarwin && !staticOnly) ''
71     # Add a target for a shared library to the Makefile.
72     sed -e '1s/^/LUA_SO = liblua.so/' \
73         -e 's/ALL_T *= */&$(LUA_SO) /' \
74         -i src/Makefile
75     cat ${./lua-dso.make} >> src/Makefile
76   '' ;
78   # see configurePhase for additional flags (with space)
79   makeFlags = [
80     "INSTALL_TOP=${placeholder "out"}"
81     "INSTALL_MAN=${placeholder "out"}/share/man/man1"
82     "R=${version}"
83     "LDFLAGS=-fPIC"
84     "V=${luaversion}"
85     "PLAT=${plat}"
86     "CC=${stdenv.cc.targetPrefix}cc"
87     "RANLIB=${stdenv.cc.targetPrefix}ranlib"
88     # Lua links with readline wich depends on ncurses. For some reason when
89     # building pkgsStatic.lua it fails because symbols from ncurses are not
90     # found. Adding ncurses here fixes the problem.
91     "MYLIBS=-lncurses"
92   ];
94   configurePhase = ''
95     runHook preConfigure
97     makeFlagsArray+=(CFLAGS='-O2 -fPIC${lib.optionalString compat compatFlags} $(${
98       if lib.versionAtLeast luaversion "5.2" then "SYSCFLAGS" else "MYCFLAGS"})' )
99     makeFlagsArray+=(${lib.optionalString stdenv.hostPlatform.isDarwin "CC=\"$CC\""}${lib.optionalString (stdenv.buildPlatform != stdenv.hostPlatform) " 'AR=${stdenv.cc.targetPrefix}ar rcu'"})
101     installFlagsArray=( TO_BIN="lua luac" INSTALL_DATA='cp -d' \
102       TO_LIB="${if stdenv.hostPlatform.isDarwin then "liblua.${finalAttrs.version}.dylib"
103                 else ("liblua.a" + lib.optionalString (!staticOnly) " liblua.so liblua.so.${luaversion} liblua.so.${finalAttrs.version}" )}" )
105     runHook postConfigure
106   '';
107   inherit postConfigure;
109   inherit postBuild;
111   postInstall = ''
112     mkdir -p "$out/nix-support" "$out/share/doc/lua" "$out/lib/pkgconfig"
113     cp ${substituteAll {
114       src = ./utils.sh;
115       luapathsearchpaths=lib.escapeShellArgs finalAttrs.LuaPathSearchPaths;
116       luacpathsearchpaths=lib.escapeShellArgs finalAttrs.LuaCPathSearchPaths;
117     }} $out/nix-support/utils.sh
118     mv "doc/"*.{gif,png,css,html} "$out/share/doc/lua/"
119     rmdir $out/{share,lib}/lua/${luaversion} $out/{share,lib}/lua
120     mkdir -p "$out/lib/pkgconfig"
122     cat >"$out/lib/pkgconfig/lua.pc" <<EOF
123     prefix=$out
124     libdir=$out/lib
125     includedir=$out/include
126     INSTALL_BIN=$out/bin
127     INSTALL_INC=$out/include
128     INSTALL_LIB=$out/lib
129     INSTALL_MAN=$out/man/man1
131     Name: Lua
132     Description: An Extensible Extension Language
133     Version: ${finalAttrs.version}
134     Requires:
135     Libs: -L$out/lib -llua
136     Cflags: -I$out/include
137     EOF
138     ln -s "$out/lib/pkgconfig/lua.pc" "$out/lib/pkgconfig/lua-${luaversion}.pc"
139     ln -s "$out/lib/pkgconfig/lua.pc" "$out/lib/pkgconfig/lua${luaversion}.pc"
140     ln -s "$out/lib/pkgconfig/lua.pc" "$out/lib/pkgconfig/lua${lib.replaceStrings [ "." ] [ "" ] luaversion}.pc"
142     # Make documentation outputs of different versions co-installable.
143     mv $out/share/doc/lua $out/share/doc/lua-${finalAttrs.version}
144   '';
146   # copied from python
147   passthru = let
148     # When we override the interpreter we also need to override the spliced versions of the interpreter
149     inputs' = lib.filterAttrs (n: v: ! lib.isDerivation v && n != "passthruFun") inputs;
150     override = attr: let lua = attr.override (inputs' // { self = lua; }); in lua;
151   in passthruFun rec {
152     inherit self luaversion packageOverrides luaAttr;
153     executable = "lua";
154     luaOnBuildForBuild = override pkgsBuildBuild.${luaAttr};
155     luaOnBuildForHost = override pkgsBuildHost.${luaAttr};
156     luaOnBuildForTarget = override pkgsBuildTarget.${luaAttr};
157     luaOnHostForHost = override pkgsHostHost.${luaAttr};
158     luaOnTargetForTarget = lib.optionalAttrs (lib.hasAttr luaAttr pkgsTargetTarget) (override pkgsTargetTarget.${luaAttr});
159   };
161   meta = {
162     homepage = "https://www.lua.org";
163     description = "Powerful, fast, lightweight, embeddable scripting language";
164     longDescription = ''
165       Lua combines simple procedural syntax with powerful data
166       description constructs based on associative arrays and extensible
167       semantics. Lua is dynamically typed, runs by interpreting bytecode
168       for a register-based virtual machine, and has automatic memory
169       management with incremental garbage collection, making it ideal
170       for configuration, scripting, and rapid prototyping.
171     '';
172     mainProgram = "lua";
173     license = lib.licenses.mit;
174     platforms = lib.platforms.unix;
175   };