xmake-io / xmake

🔥 A cross-platform build utility based on Lua
https://xmake.io
Apache License 2.0
10.14k stars 782 forks source link

xmake.lua脚本中,特定写法配置不生效的问题 #5517

Closed zhouqingyi123 closed 2 months ago

zhouqingyi123 commented 2 months ago

Xmake 版本

2.9.4

操作系统版本和架构

Windows 11 x64

描述问题

问题1:如下图所示,在is_plat("windows")的条件块中,通过set_config("vs", “2017”)设置vs的版本时不生效(我的电脑同时安装了2017和2022),还是使用的2022进行编译,但是写在条件块外面,是可以生效的(如上面注释部分)。

image

问题2:如下图所示,当我通过在on_load脚本域中写target:add("includedirs", xxx, {interface = true})来设置头文件目录时,依赖它的子target无法搜索到这个头文件目录,看上去add并没生效。但是我在描述域写add_inculdedirs时是可以生效的。

image

下面的demo1中包含了这两个问题。

demo1.zip

期待的结果

希望上图的两个场景,每个场景对应的出问题的写法能正常生效,当然,如果是因为用法不对导致的,请教一下正确的用法。

工程配置

set_project("demo1")
set_version("1.0.0")

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

-- 设置编译器
-- set_config("toolchain", "msvc")
-- set_config("vs", "2017")
if is_plat("windows") then
    set_config("toolchain", "msvc")
    set_config("vs", "2017")
    if is_mode("debug") then
        set_runtimes("MDd")
    else
        set_runtimes("MD")
    end
elseif is_plat("linux") then
    set_config("toolchain", "gcc")
end

target("version")
    add_rules("c++")
    set_kind("phony")
    add_configfiles("src/config.h.in")

    -- 有效
    -- add_includedirs("./$(buildir)/config", {interface = true})

    on_load(function (target)
        local configDir = "./$(buildir)/config"
        target:set("configdir", configDir)
        target:add("includedirs", configDir, {interface = true}) -- 无效
    end)

target("demo1")
    add_deps("version")
    set_kind("binary")
    add_files("src/*.cpp")

附加信息和错误日志

提交的demo1应该可以复现

Issues-translate-bot commented 2 months ago

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


Title: xmake.lua script, the specific writing configuration does not take effect.

waruqi commented 2 months ago
  1. see https://github.com/xmake-io/xmake/issues/5454#issuecomment-2282806877
  2. 脚本域不会自动处理相对路径,自己走 path.join(os.scriptdir(), "xxx"),或者 os.projectdir(), core.project.config/config.buildir() 拼下
zhouqingyi123 commented 2 months ago

@waruqi 感谢回复,第1个问题明白了,这种时候就只能用os.host()来获取当前主机的平台。但是第二个问题还是存在,麻烦看看我改的是否正确呢?通过打印这个绝对路径,我确认路径是没错的。

image

waruqi commented 2 months ago

不要用 $(buildir)

zhouqingyi123 commented 2 months ago

@waruqi 好的,改成用模块提供的函数来获取build路径进行设置确实没有问题了。这里再请教一下,这是什么原因呢?脚本域里使用$(buildir)得到的字符串通过print打印出来看也是正确的,感觉也能解析到这个内置变量,但就如上一个回复的图中那样,用它拼接而成的绝对路径就有问题。

Issues-translate-bot commented 2 months ago

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


@waruqi Thanks for the reply. I understand the first question. In this case, you can only use os.host() to get the platform of the current host. But the second problem still exists. Could you please check if I changed it correctly? By printing this absolute path, I confirmed that the path is correct.

image

Issues-translate-bot commented 2 months ago

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


Don’t use $(buildir)

waruqi commented 2 months ago

内置变量主要是给描述域的,脚本域上百个模块,上千上万个 api ,不可能每个接口,都给支持处理一遍

Issues-translate-bot commented 2 months ago

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


@waruqi Okay, changing to using the function provided by the module to obtain the build path for setting is indeed problematic. Let me ask you again, what is the reason for this? The string obtained by using $(builder) in the script field is correct when printed through print. It seems that this built-in variable can also be parsed, but as in the picture in the previous reply, the absolute path spliced ​​with it is question.

Issues-translate-bot commented 2 months ago

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


Built-in variables are mainly used in the description domain. There are hundreds of modules and thousands of APIs in the script domain. It is impossible to support and process every interface.

zhouqingyi123 commented 2 months ago

@waruqi 好的,感谢解惑

Issues-translate-bot commented 2 months ago

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


@waruqi Okay, thanks for clarifying