Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configvar_check 能否依赖另一个 configvar_check #5944

Closed
zeromake opened this issue Dec 7, 2024 · 6 comments
Closed

configvar_check 能否依赖另一个 configvar_check #5944

zeromake opened this issue Dec 7, 2024 · 6 comments

Comments

@zeromake
Copy link
Contributor

zeromake commented Dec 7, 2024

你在什么场景下需要该功能?

cmake 里有一些 check 可以直接获得到检查结果

# 检查是否有某个头文件,有加入到 CMAKE_EXTRA_INCLUDE_FILES
CHECK_INCLUDE_FILES (winsock2.h            HAVE_WINSOCK2_H)
IF (HAVE_WINSOCK2_H)
LIST (APPEND CMAKE_EXTRA_INCLUDE_FILES winsock2.h)
ELSE()
LIST (APPEND CMAKE_EXTRA_INCLUDE_FILES sys/socket.h)
ENDIF ()
# 再根据命中的头文件检查
CHECK_SYMBOL_EXISTS (freeaddrinfo    "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_FREEADDRINFO)

在转换到 xmake 时,在 on_config 里需要不停的 target:check 和判断非常的麻烦

target("xx")
  on_config(function (target)
    local net = {}
    if target:has_cincludes({"windows.h", "winsock2.h"}) then
      table.insert(net, "winsock2.h")
    else
      table.insert(net, "sys/socket.h")
    end
    if target:has_cfuncs("freeaddrinfo", {includes=net}) then
      target:add("defines", "HAVE_FREEADDRINFO=1")
    end
  end

描述可能的解决方案

local check_net_includes = {
HAVE_SYS_SOCKET_H = {"sys/socket.h"},
HAVE_WINSOCK2_H = {"windows.h", "winsock2.h"},
}

target("xxx")
  for k, v in ipairs(check_net_includes) do
    configvar_check_cincludes(k, v)
  end
  on_check_extra(function (target)
    -- 执行该方法后再一次 _do_check,这种方式还能派生出其他设置其他 configvar 值
    local configvar = target:get("configvar") or {}
    local net = {}
    for k, v in ipairs(check_net_includes) do
      if configvar[k] then
        table.join2(net, v)
      end
    end
    configvar_check_cfuncs("HAVE_FREEADDRINFO", "freeaddrinfo", {includes=net})
  end)

描述你认为的候选方案

上面是多次执行 _instance:_do_check() 的想法,不知道是否描述清楚了,如果有更好方案请指出

其他信息

No response

@Issues-translate-bot
Copy link

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


Title: Can configvar_check depend on another configvar_check?

@waruqi
Copy link
Member

waruqi commented Dec 7, 2024

在转换到 xmake 时,在 on_config 里需要不停的 target:check 和判断非常的麻烦

如果要互相依赖,只有这种方式,而且也没见多麻烦,下面你提的解决方式,也没见简单多少,好像更复杂了。

脚本域,你随意搞,觉得繁琐,自己走 function 或者 module 封装复用下就行了。

@Issues-translate-bot
Copy link

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


When converting to xmake, it is very troublesome to constantly perform target:check and judgment in on_config

If you want to rely on each other, this is the only way, and it's not too troublesome. The solution you mentioned below is not much simpler, it seems to be more complicated.

You can do whatever you want with the script domain. If it seems cumbersome, just use function or module to encapsulate and reuse it.

@zeromake
Copy link
Contributor Author

zeromake commented Dec 7, 2024

@waruqi
想了一下好像也是,我自己实现一个在 check 之后在 configfile 前就可以做到了吧,我明天试试看,没问题就把示例贴在这里

@Issues-translate-bot
Copy link

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


@waruqi
After thinking about it, it seems to be the case. I can implement one myself and do it before the configfile after checking. I will try it tomorrow. If there is no problem, I will post the example here.

@zeromake
Copy link
Contributor Author

zeromake commented Dec 8, 2024

尝试了一下,on_config 正好就在 option 的 check 之后,generate_configfiles 之前,直接在 on_config 里取 configvar 并设置新的 configvar 就能影响到 configfile 的生成,后面我自己封装一下和外部的 configvar_check_xxx 一致就能达到我想要的效果了

xmake.lua

includes("@builtin/check")

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

configvar_check_cincludes("HAVE_SYS_SOCKET_H", "sys/socket.h")
configvar_check_cincludes("HAVE_WINSOCK2_H", "winsock2.h")
configvar_check_cincludes("HAVE_WS2TCPIP_H", "ws2tcpip.h")

target("xxx")
    add_files("main.c")
    add_configfiles("config.h.in")

    on_config(function (target)
        local variables = target:get("configvar") or {}
        for _, opt in ipairs(target:orderopts()) do
            for name, value in pairs(opt:get("configvar")) do
                if variables[name] == nil then
                    variables[name] = table.unwrap(value)
                    variables["__extraconf_" .. name] = opt:extraconf("configvar." .. name, value)
                end
            end
        end
        print(variables)
        local common_includes = {}
        if variables["HAVE_SYS_SOCKET_H"] then
            table.insert(common_includes, "sys/socket.h")
        end
        if variables["HAVE_WINSOCK2_H"] then
            table.insert(common_includes, "winsock2.h")
        end
        if variables["HAVE_WS2TCPIP_H"] then
            table.insert(common_includes, "ws2tcpip.h")
        end
        if target:has_cfuncs("freeaddrinfo", {includes = common_includes}) then
            target:set("configvar", "HAVE_FREEADDRINFO", 1)
        end
    end)

config.h.in

${define HAVE_SYS_SOCKET_H}
${define HAVE_WINSOCK2_H}
${define HAVE_FREEADDRINFO}
${define HAVE_WS2TCPIP_H}

windows 下的效果

> checking for c includes(winsock2.h, ws2tcpip.h)
> checking for c funcs(freeaddrinfo)
> checking for c snippet(has_cfuncs)
generating config.h.in to build\config.h ..
checking for git ... ok
  > replace HAVE_SYS_SOCKET_H -> /* #undef HAVE_SYS_SOCKET_H */
  > replace HAVE_WINSOCK2_H -> #define HAVE_WINSOCK2_H 1
  > replace HAVE_FREEADDRINFO -> #define HAVE_FREEADDRINFO 1
  > replace HAVE_WS2TCPIP_H -> #define HAVE_WS2TCPIP_H 1

@zeromake zeromake closed this as completed Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants