--- CMakeLists.txt.orig	2023-10-22 11:51:43 UTC
+++ CMakeLists.txt
@@ -14,7 +14,6 @@ if(CMAKE_BUILD_TYPE)
     string(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE_UC)
 endif()
 
-find_package(Git)
 if(Git_FOUND)
     execute_process(
         COMMAND ${GIT_EXECUTABLE} status
@@ -160,72 +159,18 @@ set(THIRDPARTY_DIR ${CMAKE_SOURCE_DIR}/vendor)
 
 include(ExternalProject)
 
+find_package(PkgConfig)
 ################################
 # LUA
 ################################
 
-set(LUA_DIR ${THIRDPARTY_DIR}/lua)
-set(LUA_SRC
-    ${LUA_DIR}/lapi.c
-    ${LUA_DIR}/lcode.c
-    ${LUA_DIR}/lctype.c
-    ${LUA_DIR}/ldebug.c
-    ${LUA_DIR}/ldo.c
-    ${LUA_DIR}/ldump.c
-    ${LUA_DIR}/lfunc.c
-    ${LUA_DIR}/lgc.c
-    ${LUA_DIR}/llex.c
-    ${LUA_DIR}/lmem.c
-    ${LUA_DIR}/lobject.c
-    ${LUA_DIR}/lopcodes.c
-    ${LUA_DIR}/lparser.c
-    ${LUA_DIR}/lstate.c
-    ${LUA_DIR}/lstring.c
-    ${LUA_DIR}/ltable.c
-    ${LUA_DIR}/ltm.c
-    ${LUA_DIR}/lundump.c
-    ${LUA_DIR}/lvm.c
-    ${LUA_DIR}/lzio.c
-    ${LUA_DIR}/lauxlib.c
-    ${LUA_DIR}/lbaselib.c
-    ${LUA_DIR}/lcorolib.c
-    ${LUA_DIR}/ldblib.c
-    ${LUA_DIR}/liolib.c
-    ${LUA_DIR}/lmathlib.c
-    ${LUA_DIR}/loslib.c
-    ${LUA_DIR}/lstrlib.c
-    ${LUA_DIR}/ltablib.c
-    ${LUA_DIR}/lutf8lib.c
-    ${LUA_DIR}/loadlib.c
-    ${LUA_DIR}/linit.c
-)
+pkg_check_modules(LUA REQUIRED lua-5.3 IMPORTED_TARGET GLOBAL)
 
-add_library(lua STATIC ${LUA_SRC})
-
-target_compile_definitions(lua PRIVATE LUA_COMPAT_5_2)
-target_include_directories(lua INTERFACE ${THIRDPARTY_DIR}/lua)
-
 if(N3DS)
     target_compile_definitions(lua PUBLIC LUA_32BITS)
 endif()
 
 ################################
-# LPEG
-################################
-
-set(LPEG_DIR ${THIRDPARTY_DIR}/lpeg)
-set(LPEG_SRC
-    ${LPEG_DIR}/lpcap.c
-    ${LPEG_DIR}/lpcode.c
-    ${LPEG_DIR}/lpprint.c
-    ${LPEG_DIR}/lptree.c
-    ${LPEG_DIR}/lpvm.c
-)
-
-add_library(lpeg STATIC ${LPEG_SRC})
-target_include_directories(lpeg PRIVATE ${LUA_DIR})
-
-################################
 # WREN
 ################################
 
@@ -341,62 +286,6 @@ target_include_directories(wasm PUBLIC ${THIRDPARTY_DI
 #target_include_directories(wasm PRIVATE ${THIRDPARTY_DIR}/wasm3/src/vm)
 
 ################################
-# SCHEME (S7)
-################################
-
-set(SCHEME_DIR ${THIRDPARTY_DIR}/s7)
-set(SCHEME_SRC
-    ${SCHEME_DIR}/s7.c
-)
-
-add_library(scheme STATIC ${SCHEME_SRC})
-set_target_properties(scheme PROPERTIES LINKER_LANGUAGE CXX)
-target_include_directories(scheme PUBLIC ${SCHEME_DIR})
-
-if (N3DS)
-    target_compile_definitions(scheme PRIVATE S7_N3DS)
-endif()
-
-if (BAREMETALPI)
-    target_compile_definitions(scheme PRIVATE S7_BAREMETALPI)
-endif()
-
-################################
-# SQUIRREL
-################################
-
-set(SQUIRREL_DIR ${THIRDPARTY_DIR}/squirrel)
-set(SQUIRREL_SRC
-    ${SQUIRREL_DIR}/squirrel/sqapi.cpp
-    ${SQUIRREL_DIR}/squirrel/sqbaselib.cpp
-    ${SQUIRREL_DIR}/squirrel/sqclass.cpp
-    ${SQUIRREL_DIR}/squirrel/sqcompiler.cpp
-    ${SQUIRREL_DIR}/squirrel/sqdebug.cpp
-    ${SQUIRREL_DIR}/squirrel/sqfuncstate.cpp
-    ${SQUIRREL_DIR}/squirrel/sqlexer.cpp
-    ${SQUIRREL_DIR}/squirrel/sqmem.cpp
-    ${SQUIRREL_DIR}/squirrel/sqobject.cpp
-    ${SQUIRREL_DIR}/squirrel/sqstate.cpp
-    ${SQUIRREL_DIR}/squirrel/sqtable.cpp
-    ${SQUIRREL_DIR}/squirrel/sqvm.cpp
-    ${SQUIRREL_DIR}/sqstdlib/sqstdaux.cpp
-    ${SQUIRREL_DIR}/sqstdlib/sqstdblob.cpp
-    ${SQUIRREL_DIR}/sqstdlib/sqstdio.cpp
-    ${SQUIRREL_DIR}/sqstdlib/sqstdmath.cpp
-    ${SQUIRREL_DIR}/sqstdlib/sqstdrex.cpp
-    ${SQUIRREL_DIR}/sqstdlib/sqstdstream.cpp
-    ${SQUIRREL_DIR}/sqstdlib/sqstdstring.cpp
-    ${SQUIRREL_DIR}/sqstdlib/sqstdsystem.cpp
-
-)
-
-add_library(squirrel STATIC ${SQUIRREL_SRC})
-set_target_properties(squirrel PROPERTIES LINKER_LANGUAGE CXX)
-target_include_directories(squirrel PUBLIC ${SQUIRREL_DIR}/include)
-target_include_directories(squirrel PRIVATE ${SQUIRREL_DIR}/squirrel)
-target_include_directories(squirrel PRIVATE ${SQUIRREL_DIR}/sqstdlib)
-
-################################
 # pocketpy (Python)
 ################################
 
@@ -417,27 +306,8 @@ if(EMSCRIPTEN)
     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fexceptions")
 endif()
 
-################################
-# GIFLIB
-################################
+find_package(GIF REQUIRED)
 
-set(GIFLIB_DIR ${THIRDPARTY_DIR}/giflib)
-set(GIFLIB_SRC
-    ${GIFLIB_DIR}/dgif_lib.c
-    ${GIFLIB_DIR}/egif_lib.c
-    ${GIFLIB_DIR}/gif_err.c
-    ${GIFLIB_DIR}/gif_font.c
-    ${GIFLIB_DIR}/gif_hash.c
-    ${GIFLIB_DIR}/gifalloc.c
-    ${GIFLIB_DIR}/openbsd-reallocarray.c
-)
-add_library(giflib STATIC ${GIFLIB_SRC})
-target_include_directories(giflib
-    PRIVATE ${GIFLIB_DIR}
-    INTERFACE 
-        ${THIRDPARTY_DIR}/giflib 
-        ${THIRDPARTY_DIR}/msf_gif)
-
 ################################
 # Blipbuf
 ################################
@@ -446,36 +316,6 @@ add_library(blipbuf STATIC ${THIRDPARTY_DIR}/blip-buf/
 target_include_directories(blipbuf INTERFACE ${THIRDPARTY_DIR}/blip-buf)
 
 ################################
-# QuickJS
-################################
-
-set(QUICKJS_DIR ${THIRDPARTY_DIR}/quickjs)
-
-file(STRINGS ${QUICKJS_DIR}/VERSION CONFIG_VERSION)
-
-add_library(quickjs STATIC
-    ${QUICKJS_DIR}/quickjs.c 
-    ${QUICKJS_DIR}/libregexp.c 
-    ${QUICKJS_DIR}/libunicode.c
-    ${QUICKJS_DIR}/cutils.c)
-
-target_compile_definitions(quickjs PUBLIC CONFIG_VERSION="${CONFIG_VERSION}")
-target_include_directories(quickjs INTERFACE ${QUICKJS_DIR})
-
-if(CMAKE_BUILD_TYPE STREQUAL "Debug")
-    target_compile_definitions(quickjs PRIVATE DUMP_LEAKS)
-endif()
-
-if(BAREMETALPI OR N3DS)
-    target_compile_definitions(quickjs PRIVATE POOR_CLIB) 
-endif()
-
-if(LINUX)
-    target_compile_definitions(quickjs PUBLIC _GNU_SOURCE _POSIX_C_SOURCE=200112)
-    target_link_libraries(quickjs PUBLIC m dl pthread)
-endif()
-
-################################
 # Janet
 ################################
 
@@ -514,9 +354,7 @@ if(BUILD_WITH_JANET)
         )
     endif()
 
-    add_library(janet ${THIRDPARTY_DIR}/janet/build/c/janet.c)
-    target_include_directories(janet PUBLIC ${THIRDPARTY_DIR}/janet/src/include)
-    target_include_directories(janet PUBLIC ${CMAKE_SOURCE_DIR}/build/janet/)
+    pkg_check_modules(JANET REQUIRED janet)
 endif()
 
 ################################
@@ -563,24 +401,26 @@ macro(MACRO_CORE SCRIPT DEFINE BUILD_DEPRECATED)
 
     target_include_directories(tic80core${SCRIPT}
         PRIVATE
+            ${THIRDPARTY_DIR}/msf_gif
             ${THIRDPARTY_DIR}/moonscript
             ${THIRDPARTY_DIR}/fennel
             ${POCKETPY_DIR}/src
+            ${LUA_INCLUDE_DIRS}
         PUBLIC
             ${CMAKE_SOURCE_DIR}/include
             ${CMAKE_SOURCE_DIR}/src)
 
     target_link_libraries(tic80core${SCRIPT}
-        lua
-        lpeg
+        ${LUA_LIBRARIES}
         wren
         wasm
         squirrel
+        sqstdlib
         python
-        scheme
+        s7
         quickjs
         blipbuf
-        zlib
+        z
     )
 
     if(BUILD_WITH_MRUBY)
@@ -593,7 +433,7 @@ macro(MACRO_CORE SCRIPT DEFINE BUILD_DEPRECATED)
 
     if(${BUILD_DEPRECATED})
         target_compile_definitions(tic80core${SCRIPT} PRIVATE BUILD_DEPRECATED)
-        target_link_libraries(tic80core${SCRIPT} giflib)
+        target_link_libraries(tic80core${SCRIPT} ${GIF_LIBRARIES})
     endif()
 
     if(LINUX)
@@ -641,6 +481,8 @@ endif()
 ################################
 if(BUILD_SDL AND NOT EMSCRIPTEN AND NOT RPI)
 
+    find_package(SDL2 REQUIRED)
+
     if(WIN32)
         set(HAVE_LIBC TRUE)
     endif()
@@ -650,10 +492,6 @@ if(BUILD_SDL AND NOT EMSCRIPTEN AND NOT RPI)
         set(SDL_STATIC_PIC ON CACHE BOOL "" FORCE)
     endif()
 
-    set(SDL_SHARED OFF CACHE BOOL "" FORCE)
-
-    add_subdirectory(${THIRDPARTY_DIR}/sdl2)
-
 endif()
 
 ################################
@@ -679,7 +517,7 @@ if(BUILD_SDL AND BUILD_PLAYER AND NOT RPI)
         target_link_options(player-sdl PRIVATE -static)
     endif()
 
-    target_link_libraries(player-sdl tic80core SDL2-static SDL2main)
+    target_link_libraries(player-sdl tic80core ${SDL2_LIBRARIES})
 endif()
 
 ################################
@@ -820,34 +658,8 @@ endif()
 # ZLIB
 ################################
 
-if (NOT N3DS)
+find_package(ZLIB)
 
-set(ZLIB_DIR ${THIRDPARTY_DIR}/zlib)
-set(ZLIB_SRC
-    ${ZLIB_DIR}/adler32.c
-    ${ZLIB_DIR}/compress.c
-    ${ZLIB_DIR}/crc32.c
-    ${ZLIB_DIR}/deflate.c
-    ${ZLIB_DIR}/inflate.c
-    ${ZLIB_DIR}/infback.c
-    ${ZLIB_DIR}/inftrees.c
-    ${ZLIB_DIR}/inffast.c
-    ${ZLIB_DIR}/trees.c
-    ${ZLIB_DIR}/uncompr.c
-    ${ZLIB_DIR}/zutil.c
-)
-
-add_library(zlib STATIC ${ZLIB_SRC})
-target_include_directories(zlib INTERFACE ${THIRDPARTY_DIR}/zlib)
-
-else ()
-
-add_library(zlib STATIC IMPORTED)
-set_target_properties( zlib PROPERTIES IMPORTED_LOCATION ${DEVKITPRO}/portlibs/3ds/lib/libz.a )
-target_include_directories(zlib INTERFACE ${DEVKITPRO}/portlibs/3ds/include)
-
-endif ()
-
 ################################
 # ZIP
 ################################
@@ -856,6 +668,12 @@ set(CMAKE_DISABLE_TESTING ON CACHE BOOL "" FORCE)
 add_subdirectory(${THIRDPARTY_DIR}/zip)
 
 ################################
+# PNG
+################################
+
+find_package(PNG REQUIRED)
+
+################################
 # bin2txt cart2prj prj2cart xplode wasmp2cart
 ################################
 
@@ -876,7 +694,7 @@ if(BUILD_DEMO_CARTS)
     target_link_libraries(wasmp2cart tic80core)
 
     add_executable(bin2txt ${TOOLS_DIR}/bin2txt.c)
-    target_link_libraries(bin2txt zlib)
+    target_link_libraries(bin2txt z)
 
     add_executable(xplode
         ${TOOLS_DIR}/xplode.c
@@ -982,7 +800,7 @@ if(USE_NAETT)
 
     if(WIN32)
         target_link_libraries(naett INTERFACE winhttp)
-    elseif(LINUX)
+    elseif(LINUX OR FREEBSD)
         find_package(CURL REQUIRED)
         target_include_directories(naett PRIVATE ${CURL_INCLUDE_DIRS})
         target_link_libraries(naett ${CURL_LIBRARIES})
@@ -990,40 +808,6 @@ if(USE_NAETT)
 endif()
 
 ################################
-# PNG
-################################
-
-set(LIBPNG_DIR ${THIRDPARTY_DIR}/libpng)
-set(LIBPNG_SRC
-    ${LIBPNG_DIR}/png.c
-    ${LIBPNG_DIR}/pngerror.c
-    ${LIBPNG_DIR}/pngget.c
-    ${LIBPNG_DIR}/pngmem.c
-    ${LIBPNG_DIR}/pngpread.c
-    ${LIBPNG_DIR}/pngread.c
-    ${LIBPNG_DIR}/pngrio.c
-    ${LIBPNG_DIR}/pngrtran.c
-    ${LIBPNG_DIR}/pngrutil.c
-    ${LIBPNG_DIR}/pngset.c
-    ${LIBPNG_DIR}/pngtrans.c
-    ${LIBPNG_DIR}/pngwio.c
-    ${LIBPNG_DIR}/pngwrite.c
-    ${LIBPNG_DIR}/pngwtran.c
-    ${LIBPNG_DIR}/pngwutil.c
-)
-
-configure_file(${LIBPNG_DIR}/scripts/pnglibconf.h.prebuilt ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h)
-
-add_library(png STATIC ${LIBPNG_SRC})
-
-target_compile_definitions(png PRIVATE PNG_ARM_NEON_OPT=0)
-
-target_include_directories(png
-    PUBLIC ${CMAKE_CURRENT_BINARY_DIR}
-    PRIVATE ${THIRDPARTY_DIR}/zlib
-    INTERFACE ${THIRDPARTY_DIR}/libpng)
-
-################################
 # TIC-80 studio
 ################################
 
@@ -1067,9 +851,9 @@ if(WIN32)
     target_include_directories(tic80studio PRIVATE ${THIRDPARTY_DIR}/dirent/include)
 endif()
 
-target_include_directories(tic80studio PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
+target_include_directories(tic80studio PUBLIC ${CMAKE_CURRENT_BINARY_DIR} PRIVATE ${LUA_INCLUDE_DIRS} ${THIRDPARTY_DIR}/msf_gif)
 
-target_link_libraries(tic80studio tic80core zip wave_writer argparse giflib png)
+target_link_libraries(tic80studio tic80core zip wave_writer argparse ${GIF_LIBRARIES} png)
 
 if(USE_NAETT)
     target_compile_definitions(tic80studio PRIVATE USE_NAETT)
@@ -1087,80 +871,6 @@ endif()
 target_compile_definitions(tic80studio PUBLIC BUILD_EDITORS)
 
 ################################
-# SDL GPU
-################################
-
-if(BUILD_SDLGPU)
-
-set(SDLGPU_DIR ${THIRDPARTY_DIR}/sdl-gpu/src)
-set(SDLGPU_SRC
-    ${SDLGPU_DIR}/renderer_GLES_2.c
-    ${SDLGPU_DIR}/SDL_gpu.c
-    ${SDLGPU_DIR}/SDL_gpu_matrix.c
-    ${SDLGPU_DIR}/SDL_gpu_renderer.c
-    ${SDLGPU_DIR}/externals/stb_image/stb_image.c
-    ${SDLGPU_DIR}/externals/stb_image_write/stb_image_write.c
-)
-
-if(NOT ANDROID)
-    list(APPEND SDLGPU_SRC
-        ${SDLGPU_DIR}/renderer_GLES_1.c
-        ${SDLGPU_DIR}/renderer_GLES_3.c
-        ${SDLGPU_DIR}/renderer_OpenGL_1.c
-        ${SDLGPU_DIR}/renderer_OpenGL_1_BASE.c
-        ${SDLGPU_DIR}/renderer_OpenGL_2.c
-        ${SDLGPU_DIR}/renderer_OpenGL_3.c
-        ${SDLGPU_DIR}/renderer_OpenGL_4.c
-        ${SDLGPU_DIR}/SDL_gpu_shapes.c
-        ${SDLGPU_DIR}/externals/glew/glew.c
-    )
-endif()
-
-add_library(sdlgpu STATIC ${SDLGPU_SRC})
-
-if(EMSCRIPTEN OR ANDROID)
-    target_compile_definitions(sdlgpu PRIVATE GLEW_STATIC SDL_GPU_DISABLE_GLES_1 SDL_GPU_DISABLE_GLES_3 SDL_GPU_DISABLE_OPENGL)
-else()
-    target_compile_definitions(sdlgpu PRIVATE GLEW_STATIC SDL_GPU_DISABLE_GLES SDL_GPU_DISABLE_OPENGL_3 SDL_GPU_DISABLE_OPENGL_4)
-endif()
-
-target_include_directories(sdlgpu PUBLIC ${THIRDPARTY_DIR}/sdl-gpu/include)
-target_include_directories(sdlgpu PRIVATE ${THIRDPARTY_DIR}/sdl-gpu/src/externals/glew)
-target_include_directories(sdlgpu PRIVATE ${THIRDPARTY_DIR}/sdl-gpu/src/externals/glew/GL)
-target_include_directories(sdlgpu PRIVATE ${THIRDPARTY_DIR}/sdl-gpu/src/externals/stb_image)
-target_include_directories(sdlgpu PRIVATE ${THIRDPARTY_DIR}/sdl-gpu/src/externals/stb_image_write)
-
-if(WIN32)
-    target_link_libraries(sdlgpu opengl32)
-endif()
-
-if(LINUX)
-    target_link_libraries(sdlgpu GL)
-endif()
-
-if(APPLE)
-    find_library(OPENGL_LIBRARY OpenGL)
-    target_link_libraries(sdlgpu ${OPENGL_LIBRARY})
-endif()
-
-if(ANDROID)
-    find_library( ANDROID_LOG_LIBRARY log )
-    find_library( ANDROID_GLES2_LIBRARY GLESv2 )
-    find_library( ANDROID_GLES1_LIBRARY GLESv1_CM )
-    target_link_libraries(sdlgpu
-        ${ANDROID_LOG_LIBRARY}
-        ${ANDROID_GLES2_LIBRARY}
-        ${ANDROID_GLES1_LIBRARY}
-    )
-endif()
-
-if(NOT EMSCRIPTEN)
-    target_link_libraries(sdlgpu SDL2-static)
-endif()
-
-endif()
-
-################################
 # TIC-80 app
 ################################
 
@@ -1201,7 +911,7 @@ if(BUILD_SDL)
         endif()
 
     elseif(NOT ANDROID)
-        target_link_libraries(tic80 SDL2main)
+        target_link_libraries(tic80 ${SDL2_LIBRARIES})
     endif()
 
     target_link_libraries(tic80 tic80studio)
@@ -1222,13 +932,13 @@ if(BUILD_SDL)
     endif()
 
     if(BUILD_SDLGPU)
-        target_link_libraries(tic80 sdlgpu)
+        target_link_libraries(tic80 SDL2_gpu)
     else()
         if(EMSCRIPTEN)
         elseif(RPI)
             target_link_libraries(tic80 libSDL2.a bcm_host)
         else()
-            target_link_libraries(tic80 SDL2-static)
+            target_link_libraries(tic80 ${SDL2_LIBRARIES})
         endif()
     endif()
 
@@ -1299,7 +1009,7 @@ if(N3DS)
         ${CMAKE_SOURCE_DIR}/src)
 
     target_link_directories(tic80_n3ds PRIVATE ${DEVKITPRO}/libctru/lib ${DEVKITPRO}/portlibs/3ds/lib)
-    target_link_libraries(tic80_n3ds tic80studio png citro3d)
+    target_link_libraries(tic80_n3ds tic80studio PNG::PNG citro3d)
 
     add_custom_command(TARGET tic80_n3ds
            POST_BUILD
@@ -1359,7 +1069,7 @@ if(BUILD_STUB)
             endif()
 
         else()
-            target_link_libraries(tic80${SCRIPT} SDL2main)
+            target_link_libraries(tic80${SCRIPT} ${SDL2_LIBRARIES})
         endif()
 
         target_link_libraries(tic80${SCRIPT} tic80core${SCRIPT} argparse)
@@ -1371,13 +1081,13 @@ if(BUILD_STUB)
         endif()
 
         if(BUILD_SDLGPU)
-            target_link_libraries(tic80${SCRIPT} sdlgpu)
+            target_link_libraries(tic80${SCRIPT} SDL2_gpu)
         else()
             if(EMSCRIPTEN)
             elseif(RPI)
                 target_link_libraries(tic80${SCRIPT} libSDL2.a bcm_host pthread dl)
             else()
-                target_link_libraries(tic80${SCRIPT} SDL2-static)
+                target_link_libraries(tic80${SCRIPT} ${SDL2_LIBRARIES})
             endif()
         endif()
 
