From 72ffe91cf33b2afa7bb1996d1ede9d7b9624c7f0 Mon Sep 17 00:00:00 2001
From: Daniel Kolesa <daniel@octaforge.org>
Date: Sun, 26 Sep 2021 00:44:01 +0200
Subject: [PATCH] enforce system luajit

---
 CMakeLists.txt                | 192 ++--------------------------------
 vendor/luabins/CMakeLists.txt |   3 +-
 2 files changed, 13 insertions(+), 182 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4614f6d..0e7bc81 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,6 +3,10 @@ cmake_policy(SET CMP0074 NEW)
 
 project(Aegisub)
 
+find_package(PkgConfig)
+
+pkg_check_modules(LUAJIT REQUIRED luajit)
+
 list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
 
 # Explicitly set the build type to Release if no other type is specified
@@ -12,181 +16,6 @@ if(NOT CMAKE_BUILD_TYPE)
     set(CMAKE_BUILD_TYPE Release)
 endif()
 
-add_executable(luajit-minilua vendor/luajit/src/host/minilua.c)
-if(NOT WIN32)
-    target_link_libraries(luajit-minilua m)
-endif()
-if(WIN32)
-    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
-        add_custom_command(
-            OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/buildvm_arch.h"
-            COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen"
-            COMMAND luajit-minilua ../dynasm/dynasm.lua -LN -D WIN -D JIT -D FFI -D P64 -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/buildvm_arch.h" vm_x86.dasc
-            WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/luajit/src
-        )
-    else()
-        add_custom_command(
-            OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/buildvm_arch.h"
-            COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen"
-            COMMAND luajit-minilua ../dynasm/dynasm.lua -LN -D WIN -D JIT -D FFI -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/buildvm_arch.h" vm_x86.dasc
-            WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/luajit/src
-        )
-    endif()
-else()
-    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
-        add_custom_command(
-            OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/buildvm_arch.h"
-            COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen"
-            COMMAND luajit-minilua ../dynasm/dynasm.lua -D P64 -D JIT -D FFI -D FPU -D HFABI -D VER= -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/buildvm_arch.h" vm_x86.dasc
-            WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/luajit/src
-        )
-    else()
-        add_custom_command(
-            OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/buildvm_arch.h"
-            COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen"
-            COMMAND luajit-minilua ../dynasm/dynasm.lua -D JIT -D FFI -D FPU -D HFABI -D VER= -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/buildvm_arch.h" vm_x86.dasc
-            WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/luajit/src
-        )
-    endif()
-endif()
-
-add_executable(luajit-buildvm
-    vendor/luajit/src/host/buildvm.c
-    vendor/luajit/src/host/buildvm_asm.c
-    vendor/luajit/src/host/buildvm_peobj.c
-    vendor/luajit/src/host/buildvm_lib.c
-    vendor/luajit/src/host/buildvm_fold.c
-
-    "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/buildvm_arch.h"
-)
-target_compile_definitions(luajit-buildvm PRIVATE LUAJIT_ENABLE_LUA52COMPAT)
-target_include_directories(luajit-buildvm PRIVATE vendor/luajit/src "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen")
-if(UNIX)
-    add_custom_command(
-        OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/lj_vm.s"
-        COMMAND luajit-buildvm -m elfasm -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/lj_vm.s"
-    )
-elseif(MSVC)
-    add_custom_command(
-        OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/lj_vm.obj"
-        COMMAND luajit-buildvm -m peobj -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/lj_vm.obj"
-    )
-endif()
-add_custom_command(
-    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_ffdef.h"
-    COMMAND luajit-buildvm -m ffdef -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_ffdef.h" lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c
-    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/luajit/src
-)
-add_custom_command(
-    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_bcdef.h"
-    COMMAND luajit-buildvm -m bcdef -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_bcdef.h" lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c
-    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/luajit/src
-)
-add_custom_command(
-    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_folddef.h"
-    COMMAND luajit-buildvm -m folddef -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_folddef.h" lj_opt_fold.c
-    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/luajit/src
-)
-add_custom_command(
-    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_recdef.h"
-    COMMAND luajit-buildvm -m recdef -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_recdef.h" lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c
-    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/luajit/src
-)
-add_custom_command(
-    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_libdef.h"
-    COMMAND luajit-buildvm -m libdef -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_libdef.h" lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c
-    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/luajit/src
-)
-add_custom_command(
-    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/jit/vmdef.lua"
-    COMMAND luajit-buildvm -m vmdef -o "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/jit/vmdef.lua" lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c
-    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/vendor/luajit/src
-)
-
-add_library(luajit STATIC
-    vendor/luajit/src/lib_base.c
-    vendor/luajit/src/lib_math.c
-    vendor/luajit/src/lib_bit.c
-    vendor/luajit/src/lib_string.c
-    vendor/luajit/src/lib_table.c
-    vendor/luajit/src/lib_io.c
-    vendor/luajit/src/lib_os.c
-    vendor/luajit/src/lib_package.c
-    vendor/luajit/src/lib_debug.c
-    vendor/luajit/src/lib_jit.c
-    vendor/luajit/src/lib_ffi.c
-    vendor/luajit/src/lj_gc.c
-    vendor/luajit/src/lj_err.c
-    vendor/luajit/src/lj_char.c
-    vendor/luajit/src/lj_bc.c
-    vendor/luajit/src/lj_obj.c
-    vendor/luajit/src/lj_buf.c
-    vendor/luajit/src/lj_str.c
-    vendor/luajit/src/lj_tab.c
-    vendor/luajit/src/lj_func.c
-    vendor/luajit/src/lj_udata.c
-    vendor/luajit/src/lj_meta.c
-    vendor/luajit/src/lj_debug.c
-    vendor/luajit/src/lj_state.c
-    vendor/luajit/src/lj_dispatch.c
-    vendor/luajit/src/lj_vmevent.c
-    vendor/luajit/src/lj_vmmath.c
-    vendor/luajit/src/lj_strscan.c
-    vendor/luajit/src/lj_strfmt.c
-    vendor/luajit/src/lj_strfmt_num.c
-    vendor/luajit/src/lj_api.c
-    vendor/luajit/src/lj_profile.c
-    vendor/luajit/src/lj_lex.c
-    vendor/luajit/src/lj_parse.c
-    vendor/luajit/src/lj_bcread.c
-    vendor/luajit/src/lj_bcwrite.c
-    vendor/luajit/src/lj_load.c
-    vendor/luajit/src/lj_ir.c
-    vendor/luajit/src/lj_opt_mem.c
-    vendor/luajit/src/lj_opt_fold.c
-    vendor/luajit/src/lj_opt_narrow.c
-    vendor/luajit/src/lj_opt_dce.c
-    vendor/luajit/src/lj_opt_loop.c
-    vendor/luajit/src/lj_opt_split.c
-    vendor/luajit/src/lj_opt_sink.c
-    vendor/luajit/src/lj_mcode.c
-    vendor/luajit/src/lj_snap.c
-    vendor/luajit/src/lj_record.c
-    vendor/luajit/src/lj_crecord.c
-    vendor/luajit/src/lj_ffrecord.c
-    vendor/luajit/src/lj_asm.c
-    vendor/luajit/src/lj_trace.c
-    vendor/luajit/src/lj_gdbjit.c
-    vendor/luajit/src/lj_ctype.c
-    vendor/luajit/src/lj_cdata.c
-    vendor/luajit/src/lj_cconv.c
-    vendor/luajit/src/lj_ccall.c
-    vendor/luajit/src/lj_ccallback.c
-    vendor/luajit/src/lj_carith.c
-    vendor/luajit/src/lj_clib.c
-    vendor/luajit/src/lj_cparse.c
-    vendor/luajit/src/lj_lib.c
-    vendor/luajit/src/lj_alloc.c
-    vendor/luajit/src/lib_aux.c
-    vendor/luajit/src/lib_init.c
-
-    "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_ffdef.h"
-    "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_bcdef.h"
-    "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_folddef.h"
-    "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_recdef.h"
-    "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen/lj_libdef.h"
-    # "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/jit/vmdef.lua"
-)
-target_compile_definitions(luajit PRIVATE LUAJIT_ENABLE_LUA52COMPAT)
-target_include_directories(luajit PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/gen" PUBLIC "vendor/luajit/src")
-if(WIN32)
-    target_sources(luajit PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/lj_vm.obj")
-else()
-    target_sources(luajit PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/lj_vm.s")
-    set_property(SOURCE "${CMAKE_CURRENT_BINARY_DIR}/vendor/luajit/src/lj_vm.s" PROPERTY LANGUAGE C)
-    target_link_libraries(luajit ${CMAKE_DL_LIBS})
-endif()
-
 add_subdirectory(vendor/luabins)
 
 add_library(libaegisub STATIC
@@ -264,20 +93,20 @@ elseif(WIN32)
 endif()
 set_target_properties(libaegisub PROPERTIES PREFIX "")
 target_compile_definitions(libaegisub PRIVATE CMAKE_BUILD)
-target_include_directories(libaegisub PUBLIC "libaegisub/include")
+target_include_directories(libaegisub PUBLIC ${LUAJIT_INCLUDE_DIRS} "libaegisub/include")
 target_precompile_headers(libaegisub PRIVATE "libaegisub/lagi_pre.h")
-target_link_libraries(libaegisub PRIVATE luajit luabins)
+target_link_libraries(libaegisub PRIVATE ${LUAJIT_LIBRARIES} luabins)
 
 add_custom_command(
     OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/src/libresrc/default_config.cpp" "${CMAKE_CURRENT_BINARY_DIR}/src/libresrc/default_config.h"
     DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/src/libresrc/default_config_platform.json"
-    COMMAND luajit-minilua "${PROJECT_SOURCE_DIR}/tools/respack.lua" manifest.respack default_config.cpp default_config.h
+    COMMAND luajit "${PROJECT_SOURCE_DIR}/tools/respack.lua" manifest.respack default_config.cpp default_config.h
     WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/src/libresrc"
 )
 
 add_custom_command(
     OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/src/libresrc/bitmap.cpp" "${CMAKE_CURRENT_BINARY_DIR}/src/libresrc/bitmap.h"
-    COMMAND luajit-minilua ../../tools/respack.lua manifest.respack "${CMAKE_CURRENT_BINARY_DIR}/src/libresrc/bitmap.cpp" "${CMAKE_CURRENT_BINARY_DIR}/src/libresrc/bitmap.h"
+    COMMAND luajit ../../tools/respack.lua manifest.respack "${CMAKE_CURRENT_BINARY_DIR}/src/libresrc/bitmap.cpp" "${CMAKE_CURRENT_BINARY_DIR}/src/libresrc/bitmap.h"
     WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src/bitmaps
 )
 
@@ -451,8 +280,9 @@ add_executable(Aegisub WIN32
     src/video_slider.cpp
     src/visual_feature.cpp
 )
-target_link_libraries(Aegisub PRIVATE ${CMAKE_DL_LIBS} libaegisub luajit)
+target_link_libraries(Aegisub PRIVATE ${CMAKE_DL_LIBS} libaegisub ${LUAJIT_LIBRARIES})
 target_compile_definitions(Aegisub PRIVATE CMAKE_BUILD)
+target_include_directories(Aegisub PUBLIC ${LUAJIT_INCLUDE_DIRS})
 target_include_directories(Aegisub PRIVATE "src/libresrc" "${CMAKE_CURRENT_BINARY_DIR}/src/libresrc")
 target_precompile_headers(Aegisub PRIVATE "src/agi_pre.h")
 
@@ -776,7 +606,7 @@ message(STATUS "\n"
     "  FFTW3:                 ${WITH_FFTW3}\n"
     "  Hunspell:              ${WITH_HUNSPELL}\n"
     "  uchardet:              ${WITH_UCHARDET}\n"
-    "  LuaJIT:                bundled\n"
+    "  LuaJIT:                unfucked\n"
     "\n"
     "Options\n"
     "  Startup log:           ${WITH_STARTUPLOG}\n"
diff --git a/vendor/luabins/CMakeLists.txt b/vendor/luabins/CMakeLists.txt
index 1fe1d20..e68403c 100644
--- a/vendor/luabins/CMakeLists.txt
+++ b/vendor/luabins/CMakeLists.txt
@@ -36,4 +36,5 @@ add_library(luabins STATIC
     src/savebuffer.c
     src/write.c
 )
-target_link_libraries(luabins PRIVATE luajit)
\ No newline at end of file
+target_include_directories(luabins PUBLIC ${LUAJIT_INCLUDE_DIRS})
+target_link_libraries(luabins PRIVATE ${LUAJIT_LIBRARIES})
-- 
2.33.0

