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.

Hi, I’m trying to write a CMake to include the luajit but it still does not work. Can you tell me why? Thanks a lot!

My CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(myapp LANGUAGES C CXX)
include(ExternalProject)
ExternalProject_Add(project_luajit
  #URL http://luajit.org/download/LuaJIT-2.0.1.tar.gz
  SOURCE_DIR "/users/weicuidi/cmake-v3/out/build/project_luajit-prefix/src/project_luajit"
  DOWNLOAD_COMMAND ""
  #PREFIX ${CMAKE_CURRENT_BINARY_DIR}/luajit-2.0.1
  CONFIGURE_COMMAND ""
  BUILD_COMMAND make
  BUILD_IN_SOURCE true
  INSTALL_COMMAND make install
)
ExternalProject_Get_Property(project_luajit SOURCE_DIR)
message("+++++" ${SOURCE_DIR})
#message("+++++install_dir:" ${install_dir})
add_library(luajit STATIC IMPORTED)
##./out/build/project_luajit-prefix/src/project_luajit/src/libluajit.a
set_property(TARGET luajit PROPERTY IMPORTED_LOCATION ${SOURCE_DIR}/src/libluajit.a)
add_dependencies(luajit project_luajit)
add_executable(${PROJECT_NAME} main.cpp)
include_directories(${SOURCE_DIR}/src)
target_link_libraries(${PROJECT_NAME} luajit)
target_link_libraries(${PROJECT_NAME} ${SOURCE_DIR}/src/libluajit.a)

I can cmake successfully but have an issue when I build. I have a very simple main.cpp which only calls the function print_usage() in luajit.h.
./build.sh
[ 80%] Built target project_luajit
[ 90%] Building CXX object CMakeFiles/myapp.dir/main.cpp.o
/users/wcd/cmake-v3/main.cpp: In function ‘int main()’:
/users/wcd/cmake-v3/main.cpp:12:5: error: ‘print_usage’ was not declared in this scope
12 | print_usage();
| ^~~~~~~~~~~
make[2]: *** [CMakeFiles/myapp.dir/build.make:63: CMakeFiles/myapp.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/myapp.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

Solved it by the following code.

ExternalProject_Add(project_luajit
  #URL http://luajit.org/download/LuaJIT-2.0.1.tar.gz
  SOURCE_DIR "/users/wcd/cmake-v3/out/build/project_luajit-prefix/src/project_luajit"
  DOWNLOAD_COMMAND ""
  #PREFIX ${CMAKE_CURRENT_BINARY_DIR}/luajit-2.0.1
  CONFIGURE_COMMAND ""
  BUILD_COMMAND make
  BUILD_IN_SOURCE true
  INSTALL_COMMAND make install
)
ExternalProject_Get_Property(project_luajit SOURCE_DIR)
message("+++++" ${SOURCE_DIR})
add_executable(${PROJECT_NAME} main.c)
target_link_libraries(${PROJECT_NAME} ${SOURCE_DIR}/src/libluajit.so)