External project using makefile

Hi all,

I’m incorporating lua-jit into a larger cross-compilation project, I’d like to invoke the Makefile of lua-jit using some cross platform environment variables.

The lua-jit source code is already present in my repository.

As a first step I’ve added a CMakeLists.txt file to the root folder of lua-jit, so I could do add_subdirectory() from the larger project.

I’m not very familiar with cmake so any help will be appreciated.

Source
Using “SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}”

Build

Build command “make -C <SOURCE_DIR>…”
Mainly I’m looking to override 2 environment variables:
HOST_CC - lua-jit is building some compilation utils, how can I set this to the default compiler in the system?
CC - should pick the toolchain compiler that is used by the larger project, how can I get the toolchain compiler path?

Install
Where should I install the lua-jit library, I simply want to link with it, how can I do this?

Thanks!

I think there are two ways to go about this:

  • adding a CMakeLists.txt to LuaJIT and using it directly (i.e., making your own add_library calls) (note that others may have already done this and searching GitHub for such examples may be of use); or
  • using FetchContent (which does the ExternalProject for you) and should make LuaJIT available for use in the rest of your project.

@craig.scott is way more versed in these bits than I am, so he likely would have more useful information beyond just these pointers.

Maybe something like this (untested, but should get you close):

ExternalProject_Add(lua-jit
    SOURCE_DIR /path/to/wherever/you/put/it    # This is not likely to be CMAKE_CURRENT_LIST_DIR
    INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/install   # Just an example
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ${CMAKE_COMMAND} -E env
        HOST_CC=...   # See below
        CC=${CMAKE_C_COMPILER}
        make -C <SOURCE_DIR>
    BUILD_IN_SOURCE TRUE
    INSTALL_COMMAND make DESTDIR=<INSTALL_DIR> install
)

There’s a number of assumptions baked into the above to keep the example simple:

  • The command make is always available on the host system, it reachable on the PATH and is the appropriate make tool to use.
  • make DESTDIR=<somewhere> install does the expected thing and installs the things you want under the location you specify with DESTDIR.
  • The CMAKE_C_COMPILER variable holds only the executable to run the compiler and no additional command-line arguments (a feature that is only supported for CMake 3.19 or later anyway, from what I can tell).
  • No configure step is required.
  • The lua-jit project should be built in-source. This is usually something you want to avoid, but I don’t know if luajit supports an out-of-source build.

I can’t quite tell what you expect for the HOST_CC compiler. You want to specify to use the default without actually working out/knowing what that default is? You will need to work out what to specify for that yourself and put it at the place shown in the example.

With the above, you then have to work out where the library you want to use in the main project gets installed to by ExternalProject. The following is just a guess:

add_executable(someApp ....)

target_link_libraries(someApp ${CMAKE_CURRENT_BINARY_DIR}/install/lib/liblui-jit.a)
add_dependencies(someApp lua-jit)

Again, the above is untested and makes assumptions about what kind of library is built and where it gets installed. You will need to investigate yourself and work out the kinks from here.