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

批量处理target的定义 #5243

Closed wagshiwei closed 4 months ago

wagshiwei commented 4 months ago

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

多target需要统一定义不同的处理

描述可能的解决方案

target("hello1")
    set_kind("binary")
    add_files("*.cpp","*.c")     ----为什么不能添加 h 文件

target("hello2")
    set_kind("binary")
    add_files("*.cpp","*.c")
-- on_load
    on_config(function (target)
        import("core.project.task")
        task.run("test")
    end)

task("test")
on_run(function ()
        import("core.project.project")
        import("core.base.option")
        for _, target0 in pairs(project.targets()) do
            local targetname = target0:name()
            print("target name:" .. targetname)
            target0:add_defines("MODULE_"..targetname)   ----报错
        end
     end)
Issues-translate-bot commented 4 months ago

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


Title: Definition of batch processing target

In what scenario do you need this function?

Multiple targets require unified definition of different processes.

Describe possible solutions

target("hello1")
    set_kind("binary")
    add_files("*.cpp","*.c")
target("hello2")
    set_kind("binary")
    add_files("*.cpp","*.c")
-- on_load
    on_config(function (target)
        import("core.project.task")
        task.run("test")
    end)

task("test")
on_run(function ()
        import("core.project.project")
        import("core.base.option")
        for _, target0 in pairs(project.targets()) do
            local targetname = target0:name()
            print("target name:" .. targetname)
            target0:add_defines("module_"..targetname) ----error report
        end
     end)

Describe your alternatives

No response

other information

No response

star-hengxing commented 4 months ago
rule("module")
    on_load(function (target)
        target:set("kind", "static")
        target:add("defines", target:name())
    end)
rule_end()

add_rules("module")

target("a")
    add_files("a.cpp")

target("b")
    add_files("b.cpp")
wagshiwei commented 4 months ago

我用的xcode,xcode是不是不支持生成workspace,只能生成单个target的proj文件,但是单个项目文件里也没有增加新的宏定义。CMakeLists.txt文件里也没有统一添加的宏定义

Issues-translate-bot commented 4 months ago

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


The xcode I use does not support generating workspace. It can only generate a proj file for a single target, but no new macro definitions are added to a single project file. There is no uniformly added macro definition in the CMakeLists.txt file.

wagshiwei commented 4 months ago

似乎是缓存问题,需要删掉CMakeLists.txt才会重新生成

Issues-translate-bot commented 4 months ago

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


It seems to be a cache problem. You need to delete CMakeLists.txt before it can be regenerated.

waruqi commented 4 months ago
add_files("*.cpp","*.c")     ----为什么不能添加 h 文件

添加安装头文件用 add_headerfiles ,添加搜索路径用 add_includedirs 。。为什么要加到 add_files ,那里面要参与编译生成 obj 的。。.h 不会编译到 obj

另外 test 是内置 task xmake test,不要去改写,换个名。

Issues-translate-bot commented 4 months ago

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


add_files(".cpp",".c") ----Why can't I add h files?

Use add_headerfiles to add installation header files and add_includedirs to add search paths. . Why should it be added to add_files, which needs to be involved in compilation and generation of obj. . .h will not compile to obj