Search code examples
buildcmakelua

Download and build Lua with modern CMake


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?


Solution

  • 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\'")