Explorar o código

quickjs: fix windows build (#3587)

* quickjs: fix windows build

* improve xmake.lua

* static link dep lib in shared kind

* add todo

* use gnu2mslib
star9029 hai 1 ano
pai
achega
7c5f205d15
Modificáronse 2 ficheiros con 117 adicións e 17 borrados
  1. 52 0
      packages/q/quickjs/patches/2024.01.13/msvc.patch
  2. 65 17
      packages/q/quickjs/xmake.lua

+ 52 - 0
packages/q/quickjs/patches/2024.01.13/msvc.patch

@@ -0,0 +1,52 @@
+From 5382b796b5b92901d492ec3f63c3447fe3beb1ad Mon Sep 17 00:00:00 2001
+From: SpringFestival <[email protected]>
+Date: Sat, 23 Mar 2024 16:28:15 +0800
+Subject: [PATCH] fix header to fit msvc
+
+---
+ quickjs.h | 13 +++++++++++--
+ 1 file changed, 11 insertions(+), 2 deletions(-)
+
+diff --git a/quickjs.h b/quickjs.h
+index 7199936..1da1999 100644
+--- a/quickjs.h
++++ b/quickjs.h
+@@ -215,9 +215,18 @@ typedef struct JSValue {
+ #define JS_VALUE_GET_FLOAT64(v) ((v).u.float64)
+ #define JS_VALUE_GET_PTR(v) ((v).u.ptr)
+ 
++#ifndef __MINGW__COMPILE__
++
++#define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ val }, tag }
++#define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ p }, tag }
++
++#else
++
+ #define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ .int32 = val }, tag }
+ #define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ .ptr = p }, tag }
+ 
++#endif
++
+ #define JS_TAG_IS_FLOAT64(tag) ((unsigned)(tag) == JS_TAG_FLOAT64)
+ 
+ #define JS_NAN (JSValue){ .u.float64 = JS_FLOAT64_NAN, JS_TAG_FLOAT64 }
+@@ -670,7 +679,7 @@ static inline JSValue JS_DupValue(JSContext *ctx, JSValueConst v)
+         JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
+         p->ref_count++;
+     }
+-    return (JSValue)v;
++    return v;
+ }
+ 
+ static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v)
+@@ -679,7 +688,7 @@ static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v)
+         JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
+         p->ref_count++;
+     }
+-    return (JSValue)v;
++    return v;
+ }
+ 
+ int JS_ToBool(JSContext *ctx, JSValueConst val); /* return -1 for JS_EXCEPTION */
+-- 
+2.40.0.windows.1

+ 65 - 17
packages/q/quickjs/xmake.lua

@@ -1,16 +1,15 @@
 package("quickjs")
-
     set_homepage("https://bellard.org/quickjs/")
     set_description("QuickJS is a small and embeddable Javascript engine")
 
+    add_urls("https://github.com/bellard/quickjs.git")
+    add_versions("2021.03.27", "b5e62895c619d4ffc75c9d822c8d85f1ece77e5b")
+    add_versions("2023.12.09", "daa35bc1e5d43192098af9b51caeb4f18f73f9f9")
+    add_versions("2024.01.13", "d6c7d169de6fb2c90cd2bd2226ba9dafdef883ce")
+
     if is_plat("windows") then
-        add_urls("https://github.com/xmake-mirror/quickjs.git")
-        add_versions("2021.03.27", "c83f82dce8988334be6c6a7b9b029eb963e7e310")
-    else
-        add_urls("https://github.com/bellard/quickjs.git")
-        add_versions("2021.03.27", "b5e62895c619d4ffc75c9d822c8d85f1ece77e5b")
-        add_versions("2023.12.09", "daa35bc1e5d43192098af9b51caeb4f18f73f9f9")
-        add_versions("2024.01.13", "d6c7d169de6fb2c90cd2bd2226ba9dafdef883ce")
+        add_patches("2024.01.13", "patches/2024.01.13/msvc.patch", "4e3b6a346a2b55455cba832d543876daba9f7fce6011ba9f858b2de31f0fd0e8")
+        add_configs("shared", {description = "Build shared library.", default = true, type = "boolean", readonly = true})
     end
 
     if is_plat("linux", "macosx", "iphoneos", "cross") then
@@ -18,7 +17,15 @@ package("quickjs")
     elseif is_plat("android") then
         add_syslinks("dl", "m")
     end
-    
+
+    on_load("windows", function (package)
+        if package:is_arch("x64") then
+            package:add("deps", "mingw-w64")
+        else
+            package:add("deps", "llvm-mingw")
+        end
+    end)
+
     on_install("linux", "macosx", "iphoneos", "android", "mingw", "cross", function (package)
         io.writefile("xmake.lua", ([[
             add_rules("mode.debug", "mode.release")
@@ -31,26 +38,67 @@ package("quickjs")
                 set_languages("c99")
                 add_defines("CONFIG_VERSION=\"%s\"", "_GNU_SOURCE")
                 add_defines("CONFIG_BIGNUM")
-                if is_plat("windows", "mingw") then
+                if is_plat("mingw") then
                     add_defines("__USE_MINGW_ANSI_STDIO")
                 end
         ]]):format(package:version_str()))
         local configs = {}
-        if package:config("shared") then
-            configs.kind = "shared"
-        end
         if package:is_plat("cross") then
             io.replace("quickjs.c", "#define CONFIG_PRINTF_RNDN", "")
         end
         import("package.tools.xmake").install(package, configs)
     end)
 
-    on_install("windows|x86", "windows|x64", function (package)
-        local configs = {}
+    on_install("windows", function (package)
+        io.writefile("xmake.lua", ([[
+            add_rules("mode.debug", "mode.release")
+            target("quickjs")
+                set_kind("$(kind)")
+                add_files("quickjs*.c", "cutils.c", "lib*.c")
+                add_headerfiles("quickjs-libc.h")
+                add_headerfiles("quickjs.h")
+                add_installfiles("*.js", {prefixdir = "share"})
+                set_languages("c99")
+                add_defines("CONFIG_VERSION=\"%s\"", "_GNU_SOURCE")
+                add_defines("CONFIG_BIGNUM","__USE_MINGW_ANSI_STDIO", "__MINGW__COMPILE__")
+                set_prefixname("")
+                if is_kind("shared") then
+                    add_shflags("-Wl,--output-def,quickjs.def")
+                    add_shflags("-static-libgcc", "-static-libstdc++")
+                    add_shflags("-Wl,-Bstatic -lpthread -Wl,-Bdynamic")
+                else
+                    set_extension(".lib")
+                    add_syslinks("pthread")
+                end
+        ]]):format(package:version_str()))
+
+        local arch_prev = package:arch()
+        local plat_prev = package:plat()
+        package:plat_set("mingw")
+        package:arch_set(os.arch())
+
+        import("package.tools.xmake").install(package)
+
+        package:plat_set(plat_prev)
+        package:arch_set(arch_prev)
+
         if package:config("shared") then
-            configs.kind = "shared"
+            import("utils.platform.gnu2mslib")
+
+            gnu2mslib("quickjs.lib", "quickjs.def", {plat = package:plat(), arch = package:arch()})
+            os.vcp("quickjs.lib", package:installdir("lib"))
+            os.rm(package:installdir("lib", "quickjs.dll.a"))
+        else
+            local mingw = import("detect.sdks.find_mingw")()
+            local bindir = mingw.bindir
+            if mingw and bindir then
+                os.vcp(path.join(bindir, "libgcc_s_seh-1.dll"), package:installdir("bin"))
+                os.vcp(path.join(bindir, "libwinpthread-1.dll"), package:installdir("bin"))
+                os.vcp(path.join(bindir, "libstdc++-6.dll"), package:installdir("bin"))
+            end
+
+            -- TODO: export .def and generate .lib
         end
-        import("package.tools.xmake").install(package, configs)
     end)
 
     on_test(function (package)