Преглед изворни кода

funchook: add package (#6765)

* add funchook.

* Update packages/f/funchook/xmake.lua

Co-authored-by: Saikari <[email protected]>

* Update packages/f/funchook/xmake.lua

Co-authored-by: Saikari <[email protected]>

* Update xmake.lua

* Update xmake.lua

* Update xmake.lua

* Update xmake.lua

* Update packages/f/funchook/xmake.lua

Co-authored-by: Saikari <[email protected]>

* Update packages/f/funchook/xmake.lua

Co-authored-by: Saikari <[email protected]>

* fix build system deps.

* update.

* update.

* add pkg-config dep.

* Use pkgconf on windows

* Check available platforms

* Update xmake.lua

* limit bsd, wasm, iphoneos, android

* Try support BSD

* Limit BSD back

* Improve dll defines

---------

Co-authored-by: Saikari <[email protected]>
Co-authored-by: star9029 <[email protected]>
RedbeanW пре 4 месеци
родитељ
комит
3725714e03

+ 149 - 0
packages/f/funchook/patches/fix-build-system-deps.patch

@@ -0,0 +1,149 @@
+From 676063535d82077dc47de1e5ef163ec765b1f682 Mon Sep 17 00:00:00 2001
+From: Redbeanw44602 <[email protected]>
+Date: Mon, 31 Mar 2025 23:09:52 +0800
+Subject: [PATCH] fix build system deps
+
+---
+ CMakeLists.txt | 91 +++++++++++++-------------------------------------
+ 1 file changed, 24 insertions(+), 67 deletions(-)
+
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index 1c3b2e9..0894f1d 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -86,25 +86,8 @@ endfunction(add_subdirectory_pic)
+ # capstone
+ #
+ if (DISASM_CAPSTONE)
+-  configure_file(cmake/capstone.cmake.in capstone-download/CMakeLists.txt)
+-  execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
+-      WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/capstone-download"
+-  )
+-  execute_process(COMMAND "${CMAKE_COMMAND}" --build .
+-      WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/capstone-download"
+-  )
+-
+-  string(TOUPPER ${FUNCHOOK_CPU} FUNCHOOK_CPU_UPPER)
+-
+-  set(CAPSTONE_BUILD_SHARED OFF CACHE BOOL "")
+-  set(CAPSTONE_BUILD_STATIC_RUNTIME OFF CACHE BOOL "")
+-  set(CAPSTONE_BUILD_TESTS OFF CACHE BOOL "")
+-  set(CAPSTONE_BUILD_CSTOOL OFF CACHE BOOL "")
+-  set(CAPSTONE_ARCHITECTURE_DEFAULT OFF CACHE BOOL "")
+-  set(CAPSTONE_${FUNCHOOK_CPU_UPPER}_SUPPORT ON CACHE BOOL "")
+-  add_subdirectory_pic(${CMAKE_CURRENT_BINARY_DIR}/capstone-src ${CMAKE_CURRENT_BINARY_DIR}/capstone-build)
+-
+-  list(APPEND FUNCHOOK_DEPS capstone-static)
++  find_package(capstone REQUIRED)
++  list(APPEND FUNCHOOK_DEPS capstone::capstone_static)
+   set(DISASM capstone)
+ endif ()
+ 
+@@ -112,55 +95,17 @@ endif ()
+ # distorm
+ #
+ if (DISASM_DISTORM)
+-  set(DISTORM_PATH distorm/)
+-  set(DISTORM_SRC_DIR ${DISTORM_PATH}/src/)
+-  set(DISTORM_SOURCES ${DISTORM_SRC_DIR}/decoder.c ${DISTORM_SRC_DIR}/distorm.c ${DISTORM_SRC_DIR}/instructions.c
+-      ${DISTORM_SRC_DIR}/insts.c ${DISTORM_SRC_DIR}/mnemonics.c ${DISTORM_SRC_DIR}/operands.c
+-      ${DISTORM_SRC_DIR}/prefix.c ${DISTORM_SRC_DIR}/textdefs.c)
+-  if (MSVC)
+-    # Suppress warning C4819:
+-    #   The file contains a character that cannot be represented in the current code
+-    #   page (number). Save the file in Unicode format to prevent data loss.
+-    #
+-    # prefix.c includes non-ascii chracters and C compiler warns C4819 on multibyte Windows
+-    # environment.
+-    set_source_files_properties(${DISTORM_SRC_DIR}/prefix.c PROPERTIES COMPILE_FLAGS /wd4819)
+-  endif ()
+-  add_library(distorm STATIC ${DISTORM_SOURCES})
+-  set_target_properties(distorm PROPERTIES POSITION_INDEPENDENT_CODE ON)
+-  target_include_directories(distorm PUBLIC ${DISTORM_PATH}/include)
+-  if (HAVE_FVISIBILITY_HIDDEN)
+-    target_compile_options(distorm PRIVATE -fvisibility=hidden)
+-  endif ()
+   set(DISASM distorm)
+-  list(APPEND FUNCHOOK_DEPS distorm)
+ endif ()
+ 
+ #
+ # zydis
+ #
+ if (DISASM_ZYDIS)
+-  include(ExternalProject)
+-  ExternalProject_Add(Zydis_src
+-    GIT_REPOSITORY    https://github.com/zyantific/zydis.git
+-    GIT_TAG           v3.1.0
+-    GIT_SHALLOW       TRUE
+-    CMAKE_ARGS        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
+-                      -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE}
+-                      -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
+-                      -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
+-                      -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}
+-                      -DCMAKE_POSITION_INDEPENDENT_CODE=ON
+-                      -DZYDIS_BUILD_SHARED_LIB=OFF
+-                      -DZYDIS_BUILD_EXAMPLES=OFF
+-                      -DZYDIS_BUILD_TOOLS=OFF
+-    INSTALL_COMMAND  ${CMAKE_COMMAND} --build . --target install && ${CMAKE_COMMAND} --build zycore --target install
+-  )
+-  file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/include)
+-  add_library(Zydis STATIC IMPORTED)
+-  set_property(TARGET Zydis PROPERTY IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}Zydis${CMAKE_STATIC_LIBRARY_SUFFIX})
+-  set_property(TARGET Zydis PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/include)
+-  list(APPEND FUNCHOOK_DEPS Zydis)
++  find_package(Zycore REQUIRED)
++  find_package(Zydis REQUIRED)
++  list(APPEND FUNCHOOK_DEPS Zydis::Zydis)
++  list(APPEND FUNCHOOK_DEPS Zycore::Zycore)
+   set(DISASM Zydis)
+ endif ()
+ 
+@@ -180,15 +125,9 @@ configure_file(src/cmake_config.h.in config.h)
+ 
+ function (add_funchook_library target_name target_type)
+   add_library(${target_name} ${target_type} ${FUNCHOOK_SOURCES})
+-  if (DISASM_ZYDIS)
+-    add_dependencies(${target_name} Zydis_src)
+-  endif ()
+   set_target_properties(${target_name} PROPERTIES ${FUNCHOOK_PROPERTIES})
+   target_include_directories(${target_name} PUBLIC include)
+   target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # to include config.h
+-  if (DISASM_CAPSTONE)
+-    target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/capstone-src/include)
+-  endif()
+   target_link_libraries(${target_name} PRIVATE ${FUNCHOOK_DEPS})
+   if (HAVE_FVISIBILITY_HIDDEN)
+     target_compile_options(${target_name} PRIVATE -fvisibility=hidden)
+@@ -206,10 +145,28 @@ if (FUNCHOOK_BUILD_SHARED)
+   if (MSVC)
+     set_target_properties(funchook-shared PROPERTIES IMPORT_SUFFIX _dll.lib)
+   endif ()
++  if (DISASM_DISTORM)
++    find_package(PkgConfig REQUIRED)
++    pkg_check_modules(DISTORM REQUIRED distorm)
++    target_include_directories(funchook-shared PRIVATE ${DISTORM_INCLUDE_DIRS}/distorm)
++    target_link_directories(funchook-shared PRIVATE ${DISTORM_LIBRARY_DIRS})
++    target_link_libraries(funchook-shared PRIVATE ${DISTORM_LIBRARIES})
++    target_compile_options(funchook-shared PRIVATE ${DISTORM_CFLAGS})
++    target_link_options(funchook-shared PRIVATE ${DISTORM_LDFLAGS})
++  endif ()
+ endif ()
+ 
+ if (FUNCHOOK_BUILD_STATIC)
+   add_funchook_library(funchook-static STATIC)
++  if (DISASM_DISTORM)
++    find_package(PkgConfig REQUIRED)
++    pkg_check_modules(DISTORM REQUIRED distorm)
++    target_include_directories(funchook-static PRIVATE ${DISTORM_INCLUDE_DIRS}/distorm)
++    target_link_directories(funchook-static PRIVATE ${DISTORM_LIBRARY_DIRS})
++    target_link_libraries(funchook-static PRIVATE ${DISTORM_LIBRARIES})
++    target_compile_options(funchook-static PRIVATE ${DISTORM_CFLAGS})
++    target_link_options(funchook-static PRIVATE ${DISTORM_LDFLAGS})
++  endif ()
+ endif ()
+ 
+ #
+-- 
+2.49.0
+

+ 25 - 0
packages/f/funchook/patches/fix-function-visibility.patch

@@ -0,0 +1,25 @@
+From 256a68112ef5df7b0862efd145e4f5c101457e7b Mon Sep 17 00:00:00 2001
+From: Redbeanw44602 <[email protected]>
+Date: Sun, 30 Mar 2025 22:29:57 +0800
+Subject: [PATCH] try fix function visibility.
+
+---
+ include/funchook.h | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/include/funchook.h b/include/funchook.h
+index 0df118b..dfda840 100644
+--- a/include/funchook.h
++++ b/include/funchook.h
+@@ -41,7 +41,7 @@ extern "C" {
+ #ifdef FUNCHOOK_EXPORTS
+ #if defined(_WIN32)
+ #define FUNCHOOK_EXPORT __declspec(dllexport)
+-#elif defined(__GNUC__)
++#else
+ #define FUNCHOOK_EXPORT __attribute__((visibility("default")))
+ #endif
+ #endif /* FUNCHOOK_EXPORTS */
+-- 
+2.49.0
+

+ 81 - 0
packages/f/funchook/xmake.lua

@@ -0,0 +1,81 @@
+package("funchook")
+    set_homepage("https://github.com/kubo/funchook")
+    set_description("Hook function calls by inserting jump instructions at runtime.")
+    set_license("GPL-2.0-or-later")
+
+    add_urls("https://github.com/kubo/funchook/archive/refs/tags/$(version).tar.gz",
+             "https://github.com/kubo/funchook.git")
+
+    add_versions("v1.1.3", "4b0195e70524237e222dc34c53ac25e12677bb936e64eefe33189931688444c4")
+
+    add_patches("*", "patches/fix-build-system-deps.patch", "a39c8441e991851b3b0994088100f1b0751ccba4f4cc0ede73f95968ac873597")
+    add_patches("*", "patches/fix-function-visibility.patch", "5b505ad24332320f3970a6cb56b5f550b01b9c80aa14cea0fea74ac77f1fc8f3")
+
+    add_configs("disasm", {description = "Disassembler engine.", default = nil, type = "string", values = {"capstone", "distorm", "zydis"}})
+
+    if is_plat("windows", "mingw") then
+        add_syslinks("psapi")
+    else
+        add_syslinks("dl")
+    end
+
+    add_deps("cmake")
+    if is_subhost("windows") then
+        add_deps("pkgconf")
+    else
+        add_deps("pkg-config")
+    end
+
+    on_check(function (package)
+        if package:is_arch("arm.*") and not package:is_arch("aarch64", "arm64") then
+            raise("package(funchook): Unsupported arch.")
+        end
+    end)
+
+    on_load(function(package)
+        if not package:config("disasm") then
+            -- default disasm engine.
+            if package:is_arch("arm64") then
+                package:add("deps", "capstone")
+            else
+                package:add("deps", "distorm")
+            end
+        else
+            if package:config("disasm") == "zydis" then
+                -- the latest commit updated to 4.x, but we must use 3.x for the current version.
+                package:add("deps", "zydis 3.2.1")
+            else
+                package:add("deps", package:config("disasm"))
+            end
+        end
+    end)
+
+    on_install("windows", "linux", "macosx", "mingw", "msys", "cross", function (package)      
+        local configs = {"-DFUNCHOOK_BUILD_TESTS=OFF"}
+        table.insert(configs, "-DCMAKE_BUILD_TYPE="  .. (package:is_debug() and "Debug" or "Release"))
+        table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
+        table.insert(configs, "-DFUNCHOOK_BUILD_SHARED=" .. (package:config("shared") and "ON" or "OFF"))
+        table.insert(configs, "-DFUNCHOOK_BUILD_STATIC=" .. (package:config("shared") and "OFF" or "ON"))
+        if package:config("disasm") then
+            if package:is_arch("arm64") then
+                table.insert(configs, "-DFUNCHOOK_DISASM=capstone")
+            else
+                table.insert(configs, "-DFUNCHOOK_DISASM=" .. package:config("disasm"))
+            end
+        end
+        import("package.tools.cmake").install(package, configs)
+
+        if package:config("shared") and package:is_plat("windows") then
+            io.replace(path.join(package:installdir("include"), "funchook.h"),
+                "#define FUNCHOOK_EXPORT __declspec(dllexport)",
+                "#define FUNCHOOK_EXPORT __declspec(dllimport)", {plain = true})
+        end
+    end)
+
+    on_test(function (package)
+        assert(package:check_csnippets({test = [[
+            void test() {
+                funchook_t *funchook = funchook_create();
+            }
+        ]]}, {configs = {languages = "c99"}, includes = "funchook.h"}))
+    end)