2

Let's try to build lua via cmake!

Motivation: cmake is gaining more attention and support through IDEs like CLion or even Visual Studio 2017 (and newer).

This is great if you want to provide platform-independent open-sources and faciliate the entire build-process.

Now the problem is that creating a proper CMakeLists.txt isn't that straightforward in my opinion:

cmake_minimum_required(VERSION 3.16)
include(ExternalProject)

set(LUA_VERSION "lua-5.3.5")

ExternalProject_Add(lua
  URL https://www.lua.org/ftp/${LUA_VERSION}.tar.gz
  CONFIGURE_COMMAND ""
  BUILD_COMMAND make
  BUILD_ALWAYS true
)
add_library(liblua STATIC IMPORTED)

When you cmake ./ and make, this automatically downloads the .tar.gz-file, extracts it and tries to make (build) it, which is awesome.

But the build fails:

[ 75%] Performing build step for 'lua'
make[3]: *** No targets were specified and no "make" control file was found.  End.
CMakeFiles/lua.dir/build.make:113: recipe for target 'lua-prefix/src/lua-stamp/lua-build' failed

I feel that make/cmake is looking in the wrong folder. After the automatic download the folder structure looks like this:

CMakeLists.txt
…
lua-prefix/
   src/
     lua/
        doc/
        src/
           lua.c
           luac.c
           …
           Makefile
        Makefile
        README
     lua-build/
     lua-stamp/
       …
   tmp/

What is missing in the CMakeLists above? How would you do it in general?

user1511417
  • 1,880
  • 3
  • 20
  • 41
  • 2
    `BUILD_COMMAND` is executed from the **build** directory. By default, `ExternalProject_Add` performs **out-of-source** build: a build directory differs from the source one. If you want to perform **in-source** build, add `BUILD_IN_SOURCE ON` into your `ExternalProject_Add()` call. See [documentation](https://cmake.org/cmake/help/latest/module/ExternalProject.html) for more info. – Tsyvarev Dec 21 '19 at 16:56

2 Answers2

0

Tsyvarev's hint was useful! This CMakeLists.txt works now:

cmake_minimum_required(VERSION 3.10)
include(ExternalProject)

ExternalProject_Add(lua
   URL "https://www.lua.org/ftp/lua-5.3.5.tar.gz"
   CONFIGURE_COMMAND ""
   BUILD_COMMAND make generic
   BUILD_ALWAYS true
   BUILD_IN_SOURCE true
   INSTALL_COMMAND ""
)
add_library(liblua STATIC IMPORTED)
ExternalProject_Get_property(lua SOURCE_DIR)
message("liblua will be found at \'${SOURCE_DIR}/src\'")
user1511417
  • 1,880
  • 3
  • 20
  • 41
0

For anyone wanting to try a more native CMake build for Lua, please do try out this project I set up recently: https://gitlab.com/codelibre/lua/lua-cmake

It's up-to-date with the latest Lua release (5.4.6 at the time of writing).

This is a successor to previous work such as https://github.com/lubgr/lua-cmake (sadly no longer actively maintained).

There are also other alternatives to try out such as https://github.com/walterschell/Lua

Roger Leigh
  • 479
  • 4
  • 10