xmake-io / xmake-repo

📦 An official xmake package repository
https://xrepo.xmake.io
Apache License 2.0
673 stars 395 forks source link

msvc的quickjs运行似乎有问题,直接分发二进制是否可行 #3586

Closed SpringFesti closed 6 months ago

SpringFesti commented 6 months ago

Xmake 版本

xmake 2.8.6

操作系统版本和架构

Windows11 22000.1574 x64

描述问题

使用xrepo内的quickjs的时候,会在JS_NewContext函数发生错误 测试代码如下

#include <quickjs.h>

using namespace std;

int main(int argc, char** argv)
{
    JSRuntime* rt = JS_NewRuntime() ;
    JSContext* ctx = JS_NewContext(rt) ;

    JS_FreeContext(ctx) ;
    JS_FreeRuntime(rt) ;
    return 0;
}

The test code is as above. When I use the quickjs package in xmake-repo ,an error exists after calling the function JS_NewContext

期待的结果

正常运行退出,无输出结果

runs nomally, without output

工程配置

add_rules("mode.debug", "mode.release")
add_requires("quickjs")

target("test")
    set_kind("binary")
    add_files("src/*.cpp")
    add_packages("quickjs")

附加信息和错误日志

由于quickjs本身不能在msvc上编译,所以我想能否直接分发用mingw编译的二进制,再导出lib给msvc使用。所以我做了这么一个改进https://github.com/SpringFesti/xmake-repo/blob/dev/packages/q/quickjs/xmake.lua ,在我的电脑(Win11 x64)上测试是能跑得起来。不过我不太清楚直接这样分发dll库会不会有些不太方便。

Given that quickjs could not be compiled by msvc, so I want to distribute the dynamic lib compiled by mingw and export a lib that msvc could use. I make a demo in my repository https://github.com/SpringFesti/xmake-repo/blob/dev/packages/q/quickjs/xmake.lua, and I had tested it on my computer(Win11 x64). However, I do not know if there would exist some inconveniences if I do so .

waruqi commented 6 months ago

二进制的话,跟随版本更新维护不方便。

可以参考 ffmpeg 在 windows 的编译方式,引入 msys/mingw 包,直接调用 mingw 出包 https://github.com/xmake-io/xmake-repo/blob/e663848c66407ff42561178d8a40cbb68cee620b/packages/f/ffmpeg/xmake.lua#L104

Issues-translate-bot commented 6 months ago

Bot detected the issue body's language is not English, translate it automatically.


Title: There seems to be a problem with the quickjs operation of msvc. Is it feasible to distribute the binary directly?

Issues-translate-bot commented 6 months ago

Bot detected the issue body's language is not English, translate it automatically.


In the case of binary, it is inconvenient to update and maintain according to the version.

You can refer to the compilation method of ffmpeg in windows, introduce the msys/mingw package, and directly call mingw to export the package. https://github.com/xmake-io/xmake-repo/blob/e663848c66407ff42561178d8a40cbb68cee620b/packages/f/ffmpeg/xmake.lua#L104

SpringFesti commented 6 months ago

二进制的话,跟随版本更新维护不方便。

可以参考 ffmpeg 在 windows 的编译方式,引入 msys/mingw 包,直接调用 mingw 出包

https://github.com/xmake-io/xmake-repo/blob/e663848c66407ff42561178d8a40cbb68cee620b/packages/f/ffmpeg/xmake.lua#L104

主要是感觉为了一个很小的包拉了一条本身就有完整编译项目能力的工具链过来对用户看起来有些怪了

I feel that it seems strange to pull a huge toolchain to compile a small package, and the toolchain could be used to compile the whole project by itself.

star-hengxing commented 6 months ago

这个包怎么打,感觉 xmake 内置辅助一下比较好。目前这样是可行的:

add_requires("foo", {plat = "mingw"})

target("test")
    set_kind("binary")
    add_files("src/*.cpp")
    add_packages("foo")

package("foo")
    on_install(function (package)
        io.writefile("xmake.lua", [[
            add_rules("mode.debug", "mode.release")
            target("foo")
                set_kind("$(kind)")
                add_files("foo.c")
                set_prefixname("")
                set_extension(".lib")
        ]])
        import("package.tools.xmake").install(package, configs)
    end)
Issues-translate-bot commented 6 months ago

Bot detected the issue body's language is not English, translate it automatically.


How to build this package? I feel it would be better to use xmake’s built-in assistance. This is currently possible:

add_requires("foo", {plat = "mingw"})

target("test")
    set_kind("binary")
    add_files("src/*.cpp")
    add_packages("foo")

package("foo")
    on_install(function (package)
        io.writefile("xmake.lua", [[
            add_rules("mode.debug", "mode.release")
            target("foo")
                set_kind("$(kind)")
                add_files("foo.c")
                set_prefixname("")
                set_extension(".lib")
        ]])
        import("package.tools.xmake").install(package, configs)
    end)
star-hengxing commented 6 months ago

ref vulkan-hpp

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("linux", "macosx", "iphoneos", "cross") then
        add_syslinks("pthread", "dl", "m")
    elseif is_plat("android") then
        add_syslinks("dl", "m")
    end

    if is_plat("windows") then
        add_deps("mingw-w64")
    end

    on_install("windows|x86", "windows|x64", "linux", "macosx", "iphoneos", "android", "mingw", "cross", function (package)
        local arch_prev
        local plat_prev
        if package:is_plat("windows") then
            arch_prev = package:arch()
            plat_prev = package:plat()
            package:plat_set("mingw")
            package:arch_set(os.arch())
        end

        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")
                if is_plat("windows", "mingw") then
                    add_defines("__USE_MINGW_ANSI_STDIO")
                end
        ]]):format(package:version_str()))
        local configs = {}
        if package:is_plat("cross") then
            io.replace("quickjs.c", "#define CONFIG_PRINTF_RNDN", "")
        end
        import("package.tools.xmake").install(package, configs)

        if arch_prev and plat_prev then
            package:plat_set(plat_prev)
            package:arch_set(arch_prev)

            local lib = package:installdir("lib", "libquickjs")
            os.mv(lib .. ".a", lib .. ".lib")
        end
    end)

    on_test(function (package)
        assert(package:has_cfuncs("JS_NewRuntime", {includes = "quickjs.h"}))
    end)