lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi folks,

I saw that there were some existing projects which have worked on adding CMake support for Lua.  The main one seems to be https://github.com/LuaDist/lua, but this seems to no longer be active for some time.

https://github.com/lubgr/lua-cmake is a more recent take, but it's very simple and it's tracking https://github.com/lua/lua as a submodule which is missing luac.c -- I never saw any reply to my question asking why this is missing from the GitHub mirror.
https://github.com/bencsikandrei/lua-cmake is again more recent and has some nice features, but it's not maintained and is missing some details

As a result, I've started this project recently: https://gitlab.com/codelibre/lua/lua-cmake

I have tried to do a much more comprehensive job in supporting all of the build options provided by Lua, including:

- Autodetects all supported platforms
- Supports all modes of building shared libraries
- Supports all defines for selecting languages and language behaviours
- Autodetects and supports line editing (Readline or Editline)

To Do: library versioning, exported configuration, installation, packaging, CI testing.

The aim was to pick up where LuaDist left off, working with Lua 5.4.4 and with much of the logic and options being inspired by their work.  It's not quite yet at feature parity, but will catch up quickly once those todo items are completed.

If this is of interest to anyone and you would like to give it a try and provide some feedback, I'd be very grateful.

One issue I have found is that luac can't be linked when building lua as a DLL or shared library:

Windows:

"C:\Program Files\CMake\bin\cmake.exe" --build C:\Users\rleigh\code\lua-cmake\cmake-build-debug-visual-studio-2022-shared --target luac -j 12
[1/2] Linking C shared library src\lua.dll
[2/2] Linking C executable src\luac.exe
FAILED: src/luac.exe 
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=src\CMakeFiles\luac.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests  -- C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1435~1.322\bin\Hostx64\x64\link.exe /nologo src\CMakeFiles\luac.dir\luac.c.obj  /out:src\luac.exe /implib:src\luac.lib /pdb:src\luac.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console  src\lua.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cmd.exe /C "cd /D C:\Users\rleigh\code\lua-cmake\cmake-build-debug-visual-studio-2022-shared\src && C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Bypass -file E:/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary C:/Users/rleigh/code/lua-cmake/cmake-build-debug-visual-studio-2022-shared/src/luac.exe -installedDir E:/vcpkg/installed/x64-windows/debug/bin -OutVariable out""
LINK Pass 1: command "C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1435~1.322\bin\Hostx64\x64\link.exe /nologo src\CMakeFiles\luac.dir\luac.c.obj /out:src\luac.exe /implib:src\luac.lib /pdb:src\luac.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console src\lua.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:src\CMakeFiles\luac.dir/intermediate.manifest src\CMakeFiles\luac.dir/manifest.res" failed (exit code 1120) with the following output:
luac.c.obj : error LNK2019: unresolved external symbol luaM_free_ referenced in function combine
luac.c.obj : error LNK2019: unresolved external symbol luaG_getfuncline referenced in function PrintCode
luac.c.obj : error LNK2019: unresolved external symbol luaU_dump referenced in function pmain
src\luac.exe : fatal error LNK1120: 3 unresolved externals
ninja: build stopped: subcommand failed.
Unix:

/usr/local/bin/cmake --build /tmp/tmp.5Aqibooi/cmake-build-debug-freebsd-cxx --target luac -v
[1/2] /usr/bin/c++  -I/tmp/tmp.5Aqibooi/src -I/tmp/tmp.5Aqibooi/cmake-build-debug-freebsd-cxx/src -g -fcolor-diagnostics -MD -MT src/CMakeFiles/luac.dir/luac.cpp.o -MF src/CMakeFiles/luac.dir/luac.cpp.o.d -o src/CMakeFiles/luac.dir/luac.cpp.o -c /tmp/tmp.5Aqibooi/cmake-build-debug-freebsd-cxx/src/luac.cpp
[2/2] : && /usr/bin/c++ -g  src/CMakeFiles/luac.dir/luac.cpp.o -o src/luac  -Wl,-rpath,/tmp/tmp.5Aqibooi/cmake-build-debug-freebsd-cxx/src  src/liblua.so && :
FAILED: src/luac 
: && /usr/bin/c++ -g  src/CMakeFiles/luac.dir/luac.cpp.o -o src/luac  -Wl,-rpath,/tmp/tmp.5Aqibooi/cmake-build-debug-freebsd-cxx/src  src/liblua.so && :
ld: error: undefined hidden symbol: luaU_dump(lua_State*, Proto const*, int (*)(lua_State*, void const*, unsigned long, void*), void*, int)
>>> referenced by luac.cpp:190 (src/luac.cpp:190)
>>>               src/CMakeFiles/luac.dir/luac.cpp.o:(pmain(lua_State*))

ld: error: undefined hidden symbol: luaM_free_(lua_State*, void*, unsigned long)
>>> referenced by luac.cpp:158 (src/luac.cpp:158)
>>>               src/CMakeFiles/luac.dir/luac.cpp.o:(combine(lua_State*, int))

ld: error: undefined hidden symbol: luaG_getfuncline(Proto const*, int)
>>> referenced by luac.cpp:353 (src/luac.cpp:353)
>>>               src/CMakeFiles/luac.dir/luac.cpp.o:(PrintCode(Proto const*))
c++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Is this a known problem?  Looking at the code, it is using private symbols which are marked as internal-use only and so they aren't exported for use outside the library.

I see others working around the limitation, e.g. https://github.com/bencsikandrei/lua-cmake/blob/main/CMakeLists.txt#L192

Examples of using it:

Windows (from the CLion IDE with vcpkg to provide GNU Readline):

"C:\Program Files\CMake\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=C:/Users/rleigh/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-1/223.8836.42/bin/ninja/win/x64/ninja.exe -DCMAKE_TOOLCHAIN_FILE=E:\vcpkg\scripts\buildsystems\vcpkg.cmake -DBUILD_SHARED_LIBS=OFF -G Ninja -S C:\Users\rleigh\code\lua-cmake -B C:\Users\rleigh\code\lua-cmake\cmake-build-debug-visual-studio-2022-static
-- The C compiler identification is MSVC 19.35.32215.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Found Readline: E:/vcpkg/installed/x64-windows/debug/lib/readline.lib (found version "5.0") 
-- Failed to find Editline (missing: Editline_LIBRARY) (found version "5.0")
-- Looking for pow
-- Looking for pow - found
-- Found CMath: TRUE  
-- 
-- Lua is now configured for Windows-10.0.22621
-- 
--   Platform:                               Windows (Autodetect requested)
--   C compiler:                             C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe
--   C++ compiler:                           
--   Language for compilation:               C
--   Use longjmp in place of C++ exceptions: OFF
--   Restrict C to C89:                      OFF
--   Additional debug consistency checks:    OFF
--   Build shared libraries:                 OFF
--   Library loader support:                 WIN32 LoadLibrary (ON requested)
--   Line editor support:                    GNU Readline (Autodetect requested)
--   Installation directory:                 
-- Configuring done (4.3s)
-- Generating done (0.0s)
-- Build files have been written to: C:/Users/rleigh/code/lua-cmake/cmake-build-debug-visual-studio-2022-static

Unix:

/usr/local/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=cc -DCMAKE_CXX_COMPILER=c++ -G Ninja -DLUA_LINE_EDITOR=Editline -DLUA_LANGUAGE=CXX -S /tmp/tmp.5Aqibooi -B /tmp/tmp.5Aqibooi/cmake-build-debug-freebsd-cxx
-- The C compiler identification is Clang 13.0.0
-- The CXX compiler identification is Clang 13.0.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for dlopen
-- Looking for dlopen - found
-- Found Editline: /usr/lib/libedit.so (found version "4.2") 
-- Looking for pow
-- Looking for pow - not found
-- Looking for pow
-- Looking for pow - found
-- Found CMath: TRUE  
-- 
-- Lua is now configured for FreeBSD-13.1-RELEASE-p6
-- 
--   Platform:                               POSIX (Autodetect requested)
--   C compiler:                             /usr/bin/cc
--   C++ compiler:                           /usr/bin/c++
--   Language for compilation:               C++
--   Use longjmp in place of C++ exceptions: OFF
--   Restrict C to C89:                      OFF
--   Additional debug consistency checks:    OFF
--   Build shared libraries:                 OFF
--   Library loader support:                 POSIX dlopen (ON requested)
--   Line editor support:                    BSD Editline (Editline requested)
--   Installation directory:                 
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/tmp.5Aqibooi/cmake-build-debug-freebsd-cxx


Kind regards,
Roger