-
-
Notifications
You must be signed in to change notification settings - Fork 784
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
Support for dynamic creation and injection of rules and targets in script scope #2879
Comments
Clone and modify target dynamicallywe must call target:clone() in after_load(). target("test")
set_kind("binary")
add_files("src/*.cpp")
add_defines("TEST")
after_load(function (target)
import("core.project.project")
local t = target:clone()
t:name_set("test2")
t:add("deps", "test")
t:add("defines", "TEST2")
t:set("link_before", function (target)
print("link1", target:name())
assert(target:dep("test"):data("linked"))
end)
project.target_add(t)
end)
before_link(function (target)
print("link2", target:name())
target:data_set("linked", true)
end) Clone and modify rule dynamicallyrule("cppfront")
set_extensions(".cpp2")
on_load(function (target)
local rule = target:rule("c++.build"):clone()
rule:add("deps", "cppfront", {order = true})
target:rule_add(rule)
end)
on_build_file(function (target, sourcefile, opt)
print("build cppfront file")
local objectfile = target:objectfile(sourcefile:gsub("cpp2", "cpp"))
assert(not os.isfile(objectfile), "invalid rule order!")
end)
target("test")
set_kind("binary")
add_rules("cppfront")
add_files("src/*.cpp")
add_files("src/*.cpp2")
before_build_file(function (target, sourcefile, opt)
local objectfile = target:objectfile(sourcefile)
os.tryrm(objectfile)
end) |
This looks fantastic. I presume that targets can (should?) be created only in the
What (if any) is the ordering guarantee between
i.e. B must not rely on |
you just call clone, add and access other targets in after_load. |
Right, but if I clone a target that has its own |
You can only call target:clone() in after_load, and the cloned target will not call any xx_load |
I have merged this patch into dev. xmake update -s dev |
I've tried to use this, but I get an error. As the first two lines in my target's rule("firmware")
after_load(function (target)
import("core.project.project")
local component = project.target("cherimcu.allocator"):clone()```
... I then get:
It appears that you must call The target that I'm cloning does not implement |
If I comment out that check, then I later hit an error that I am adding my cloned target to the target whose target:add("deps", cloned_target_name) And then it does not show up when I call |
please try it again. |
it works for me. target("hello")
set_kind("binary")
add_files("src/*.cpp")
target("test")
set_kind("binary")
add_files("src/*.cpp")
add_defines("TEST")
after_load(function (target)
import("core.project.project")
local t = project.target("hello"):clone()
t:name_set("hello_cloned")
t:add("defines", "TEST2")
target:add("deps", "hello_cloned")
project.target_add(t)
print(table.keys(target:deps()))
end) {
"hello_cloned"
}
1
[ 25%]: cache compiling.release src/main.cpp
[ 25%]: cache compiling.release src/main.cpp
[ 25%]: cache compiling.release src/main.cpp
[ 50%]: linking.release hello_cloned
[ 50%]: linking.release hello
[ 83%]: linking.release test
[100%]: build ok! |
Thanks. This was the key line: t:name_set("hello_cloned") I was using |
It appears that Without this, it's not possible to use stateful rules with cloned targets. |
The new target being cloned inherits exactly the state of the target, which has previously called on_load and should not be called again. Only when a new empty target is created should on_load be called |
Sorry, it looks as if it's the Again, this is why I wanted a clearly defined (and documented) order of all of the operations with respect to cloning. |
Can you provide an example/xmake.lua? |
Is your feature request related to a problem? Please describe.
#2874
#2866
Describe the solution you'd like
none
Describe alternatives you've considered
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: