Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ set(CMAKE_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_INCLUDE_PATH}
include(SetBuildParallelLevel)
include(SetHardwareArch)

if (ENABLE_LUAJIT_RANDOM_RA AND NOT USE_LUAJIT)
if (ENABLE_LUAJIT_RANDOM_RA AND NOT IS_LUAJIT)
message(FATAL_ERROR "Option ENABLE_LUAJIT_RANDOM_RA is LuaJIT-specific.")
endif (ENABLE_LUAJIT_RANDOM_RA AND NOT USE_LUAJIT)
endif()

if (USE_LUA AND NOT LUA_VERSION)
set(LUA_VERSION "master")
Expand All @@ -50,6 +50,20 @@ if (USE_LUA)
elseif (USE_LUAJIT)
include(BuildLuaJIT)
build_luajit(${LUA_VERSION})
set(IS_LUAJIT TRUE)
elseif (LUA_INCLUDE_DIR AND LUA_LIBRARIES AND LUA_EXECUTABLE)
message(STATUS "Lua library passed outside:")
message(STATUS "LUA_INCLUDE_DIR: ${LUA_INCLUDE_DIR}")
message(STATUS "LUA_LIBRARIES: ${LUA_LIBRARIES}")
message(STATUS "LUA_EXECUTABLE: ${LUA_EXECUTABLE}")

# When a path to a Lua library is passed outside, we should
# mimic a real CMake library to don't break code that depends on
# LUA_LIBRARIES.
add_library(bundled-liblua STATIC IMPORTED GLOBAL)
set_target_properties(bundled-liblua PROPERTIES
IMPORTED_LOCATION ${LUA_LIBRARIES})
set(LUA_LIBRARIES bundled-liblua)
else ()
message(FATAL_ERROR "No Lua is specified.")
endif ()
Expand All @@ -70,7 +84,7 @@ endif()
find_package(Protobuf)
if (NOT Protobuf_FOUND)
set(ENABLE_BUILD_PROTOBUF ON)
endif (NOT Protobuf_FOUND)
endif()

SetBuildParallelLevel(CMAKE_BUILD_PARALLEL_LEVEL)

Expand Down
11 changes: 8 additions & 3 deletions cmake/BuildLua.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ macro(build_lua LUA_VERSION)

include(ExternalProject)

set(LUA_LIBRARIES ${PROJECT_BINARY_DIR}/lua-${LUA_VERSION}/source/liblua.a)
set(LUA_LIBRARY ${PROJECT_BINARY_DIR}/lua-${LUA_VERSION}/source/liblua.a)
set(LUA_EXECUTABLE ${LUA_SOURCE_DIR}/lua)

ExternalProject_Add(patched-lua-${LUA_VERSION}
Expand All @@ -82,12 +82,17 @@ macro(build_lua LUA_VERSION)
LDFLAGS=${LDFLAGS}
INSTALL_COMMAND ""

BUILD_BYPRODUCTS ${LUA_LIBRARIES} ${LUA_EXECUTABLE}
BUILD_BYPRODUCTS ${LUA_LIBRARY} ${LUA_EXECUTABLE}
)

add_library(bundled-liblua STATIC IMPORTED GLOBAL)
set_target_properties(bundled-liblua PROPERTIES
IMPORTED_LOCATION ${LUA_LIBRARY})
add_dependencies(bundled-liblua patched-lua-${LUA_VERSION})

set(LUA_LIBRARIES bundled-liblua)
set(LUA_INCLUDE_DIR ${PROJECT_BINARY_DIR}/lua-${LUA_VERSION}/source/)
set(LUA_VERSION_STRING "PUC Rio Lua ${LUA_VERSION}")
set(LUA_TARGET patched-lua-${LUA_VERSION})

unset(LUA_BINARY_DIR)
unset(LUA_PATCH_PATH)
Expand Down
12 changes: 8 additions & 4 deletions cmake/BuildLuaJIT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ macro(build_luajit LJ_VERSION)

include(ExternalProject)

set(LUA_LIBRARIES ${LJ_SOURCE_DIR}/src/libluajit.a)
set(LUA_LIBRARY ${LJ_SOURCE_DIR}/src/libluajit.a)
set(LUA_EXECUTABLE ${LJ_SOURCE_DIR}/src/luajit)

ExternalProject_Add(patched-luajit-${LJ_VERSION}
Expand All @@ -110,13 +110,17 @@ macro(build_luajit LJ_VERSION)
-C src
INSTALL_COMMAND ""

BUILD_BYPRODUCTS ${LUA_LIBRARIES} ${LUA_EXECUTABLE}
BUILD_BYPRODUCTS ${LUA_LIBRARY} ${LUA_EXECUTABLE}
)

set(LUA_SOURCE_DIR ${LJ_SOURCE_DIR})
add_library(bundled-liblua STATIC IMPORTED GLOBAL)
set_target_properties(bundled-liblua PROPERTIES
IMPORTED_LOCATION ${LUA_LIBRARY})
add_dependencies(bundled-liblua patched-luajit-${LJ_VERSION})

set(LUA_LIBRARIES bundled-liblua)
set(LUA_INCLUDE_DIR ${LJ_SOURCE_DIR}/src/)
set(LUA_VERSION_STRING "LuaJIT ${LJ_VERSION}")
set(LUA_TARGET patched-luajit-${LJ_VERSION})

unset(LJ_SOURCE_DIR)
unset(LJ_BINARY_DIR)
Expand Down
8 changes: 4 additions & 4 deletions cmake/CodeCoverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ if(USE_LUA)
set(GCOVR_OPTIONS ${GCOVR_OPTIONS} --object-directory ${LUA_SOURCE_DIR})
endif ()

if(USE_LUAJIT)
if(IS_LUAJIT)
# Exclude DynASM files, that contain a low-level VM code for CPUs.
set(GCOVR_OPTIONS ${GCOVR_OPTIONS} --exclude ".*\.dasc")
# Exclude buildvm source code, it's a project's build infrastructure.
set(GCOVR_OPTIONS ${GCOVR_OPTIONS} --exclude ".*/host/")
set(GCOVR_OPTIONS ${GCOVR_OPTIONS} --object-directory ${LUA_SOURCE_DIR}/src)
endif (USE_LUAJIT)
endif ()

file(MAKE_DIRECTORY ${COVERAGE_DIR})
add_custom_target(${target_name})
Expand All @@ -56,12 +56,12 @@ add_custom_command(TARGET ${target_name}
# object files built with the GCC -fprofile-arcs option is executed.
# https://gcc.gnu.org/onlinedocs/gcc/Gcov-Data-Files.html
set(GCDA_FILES "${LUA_SOURCE_DIR}/*.gcda")
if(USE_LUAJIT)
if(IS_LUAJIT)
# Files 'src/host/*.gcda' are not removed, because
# CMake cannot remove recursively by globbing.
# Files 'src/host/*.gcda' are not used for building coverage report.
set(GCDA_FILES "${LUA_SOURCE_DIR}/src/*.gcda")
endif(USE_LUAJIT)
endif()
add_custom_target(coverage-reset
COMMENT "Reset code coverage counters"
COMMAND ${CMAKE_COMMAND} -E rm -f ${GCDA_FILES}
Expand Down
10 changes: 10 additions & 0 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ function(lua_source varname filename symbolname)
set(var ${${varname}})
set(${varname} ${var} ${dstfile} PARENT_SCOPE)
endfunction()

macro(AppendFlags flags)
foreach(flag ${ARGN})
if (${flags})
set(${flags} "${${flags}} ${flag}")
else()
set(${flags} "${flag}")
endif()
endforeach()
endmacro()
6 changes: 3 additions & 3 deletions libluamut/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ if (ENABLE_COV)
-fcoverage-mapping -ftest-coverage)
set(LDFLAGS ${LDFLAGS} -fprofile-instr-generate -fprofile-arcs
-fcoverage-mapping -ftest-coverage)
endif (ENABLE_COV)
endif()

set(LIB_LUA_MUTATE lua_mutate)
add_library(${LIB_LUA_MUTATE} STATIC mutate.c)
target_link_libraries(${LIB_LUA_MUTATE} PRIVATE ${LUA_LIBRARIES} ${LDFLAGS})
target_include_directories(${LIB_LUA_MUTATE} PRIVATE ${LUA_INCLUDE_DIR})
target_compile_options(${LIB_LUA_MUTATE} PRIVATE ${CFLAGS})
add_dependencies(${LIB_LUA_MUTATE} ${LUA_TARGET})
add_dependencies(${LIB_LUA_MUTATE} ${LUA_LIBRARIES})

set(LIB_LUA_CROSSOVER lua_crossover)
add_library(${LIB_LUA_CROSSOVER} STATIC crossover.c)
target_link_libraries(${LIB_LUA_CROSSOVER} PRIVATE ${LUA_LIBRARIES} ${LDFLAGS})
target_include_directories(${LIB_LUA_CROSSOVER} PRIVATE ${LUA_INCLUDE_DIR})
target_compile_options(${LIB_LUA_CROSSOVER} PRIVATE ${CFLAGS})
add_dependencies(${LIB_LUA_CROSSOVER} ${LUA_TARGET})
add_dependencies(${LIB_LUA_CROSSOVER} ${LUA_LIBRARIES})

if (ENABLE_INTERNAL_TESTS)
add_subdirectory(tests)
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_subdirectory(capi)
if (ENABLE_BONUS_TESTS)
add_subdirectory(lapi)
endif (ENABLE_BONUS_TESTS)
endif()
41 changes: 25 additions & 16 deletions tests/capi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,30 @@ target_link_libraries(
>
)

# The following condition looks unnecessary, it was added to fix
# an issue for Sydr: in the Lua external project in the build
# system the explicit build command is used:
# BUILD_COMMAND cd <SOURCE_DIR> && make -j CC=${CMAKE_C_COMPILER} CFLAGS=${CFLAGS} LDFLAGS=${LDFLAGS},
# which use only flags for Lua build, which were explicitly
# declared earlier, so we need to add `-g` to CFLAGS by ourselves.
# See [1].
#
# 1. https://github.com/ligurio/lua-c-api-tests/pull/6#discussion_r1185003511
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(LDFLAGS "${LDFLAGS} ${CMAKE_C_FLAGS_DEBUG}")
endif (CMAKE_BUILD_TYPE STREQUAL "Debug")
AppendFlags(LDFLAGS ${CMAKE_C_FLAGS_DEBUG})
endif()

if (ENABLE_ASAN)
set(LDFLAGS "${LDFLAGS} -fsanitize=address")
endif (ENABLE_ASAN)
AppendFlags(LDFLAGS -fsanitize=address)
endif()

if (ENABLE_UBSAN)
set(LDFLAGS "${LDFLAGS} -fsanitize=undefined")
endif (ENABLE_UBSAN)
AppendFlags(LDFLAGS -fsanitize=undefined)
endif()

if (ENABLE_COV)
set(LDFLAGS "${LDFLAGS} -fprofile-instr-generate -fcoverage-mapping")
endif (ENABLE_COV)
AppendFlags(LDFLAGS -fprofile-instr-generate -fcoverage-mapping)
endif()

set(DEFAULT_RUNS_NUMBER 5)

Expand Down Expand Up @@ -71,13 +80,13 @@ function(create_test)
target_link_libraries(${test_name} PUBLIC fuzzer_config ${FUZZ_LIBRARIES} ${LUA_LIBRARIES} ${LDFLAGS})
target_include_directories(${test_name} PRIVATE ${LUA_INCLUDE_DIR})
target_compile_options(${test_name} PRIVATE -Wall -Wextra -Wpedantic -Wno-unused-parameter -g)
add_dependencies(${test_name} ${LUA_TARGET})
add_dependencies(${test_name} ${LUA_LIBRARIES})
string(REPLACE "_test" "" test_prefix ${test_name})
set(LIBFUZZER_OPTS "${LIBFUZZER_OPTS} -artifact_prefix=${test_name}_")
if (USE_LUAJIT AND (${test_name} STREQUAL "lua_dump_test"))
if (IS_LUAJIT AND (${test_name} STREQUAL "lua_dump_test"))
set(LIBFUZZER_OPTS "${LIBFUZZER_OPTS} -only_ascii=1")
endif ()
if (USE_LUAJIT AND (${test_name} STREQUAL "lua_load_test"))
if (IS_LUAJIT AND (${test_name} STREQUAL "lua_load_test"))
set(LIBFUZZER_OPTS "${LIBFUZZER_OPTS} -only_ascii=1")
endif ()
set(dict_path ${PROJECT_SOURCE_DIR}/corpus/${test_name}.dict)
Expand All @@ -96,14 +105,14 @@ function(create_test)
set_tests_properties(${test_name} PROPERTIES
ENVIRONMENT "ASAN_OPTIONS='detect_invalid_pointer_pairs=2'"
)
endif (USE_LUA)
endif()
set_tests_properties(${test_name} PROPERTIES
LABELS capi
)

if (USE_LUAJIT)
if (IS_LUAJIT)
target_compile_definitions(${test_name} PUBLIC LUAJIT)
endif (USE_LUAJIT)
endif()
endfunction()

# These Lua C functions are unsupported by LuaJIT.
Expand All @@ -121,7 +130,7 @@ list(APPEND LUAJIT_BLACKLIST_TESTS "luaL_loadstring_test")
file(GLOB tests LIST_DIRECTORIES false ${CMAKE_CURRENT_SOURCE_DIR} *.c *.cc)
foreach(filename ${tests})
get_filename_component(test_name ${filename} NAME_WE)
if (USE_LUAJIT AND (${test_name} IN_LIST LUAJIT_BLACKLIST_TESTS))
if (IS_LUAJIT AND (${test_name} IN_LIST LUAJIT_BLACKLIST_TESTS))
continue()
endif ()
if ((${test_name} IN_LIST BLACKLIST_TESTS))
Expand All @@ -134,6 +143,6 @@ endforeach()

include(ProtobufMutator)
add_subdirectory(luaL_loadbuffer_proto)
if(USE_LUAJIT)
if(IS_LUAJIT)
add_subdirectory(ffi_cdef_proto)
endif ()
Loading