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

Improve object target for generator #5667

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions xmake/plugins/project/cmake/cmakelists.lua
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,14 @@ function _add_target_phony(cmakelists, target)
cmakelists:print("")
end

-- add target: object
function _add_target_object(cmakelists, target, outputdir)
-- Can't change the output directory of object/intermediate files in CMake
-- https://stackoverflow.com/questions/46330056/set-output-directory-for-cmake-object-libraries
cmakelists:print("add_library(%s OBJECT \"\")", target:name())
cmakelists:print("set_target_properties(%s PROPERTIES LIBRARY_OUTPUT_DIRECTORY \"%s\")", target:name(), _get_relative_unix_path_to_cmake(target:targetdir(), outputdir))
end

-- add target: binary
function _add_target_binary(cmakelists, target, outputdir)
cmakelists:print("add_executable(%s \"\")", target:name())
Expand Down Expand Up @@ -430,8 +438,14 @@ end

-- add target dependencies
function _add_target_dependencies(cmakelists, target)
local deps = target:get("deps")
if deps then
local deps = {}
for _, dep in ipairs(target:orderdeps()) do
if not dep:is_object() then
deps:insert(dep:name())
end
end

if #deps ~= 0 then
cmakelists:printf("add_dependencies(%s", target:name())
for _, dep in ipairs(deps) do
cmakelists:write(" " .. dep)
Expand Down Expand Up @@ -984,13 +998,36 @@ function _add_target_link_libraries(cmakelists, target, outputdir)
end
end
end
if #target:objectfiles() > objectfiles_set:size() then

local object_deps = {}
for _, dep in ipairs(target:orderdeps()) do
if dep:is_object() then
table.insert(object_deps, dep:name())
objectfiles_set:insert(table.unpack(dep:objectfiles()))
star-hengxing marked this conversation as resolved.
Show resolved Hide resolved
end
end

local has_links = #target:objectfiles() > objectfiles_set:size()
if has_links then
cmakelists:print("target_link_libraries(%s PRIVATE", target:name())
for _, objectfile in ipairs(target:objectfiles()) do
if not objectfiles_set:has(objectfile) then
cmakelists:print(" " .. _get_relative_unix_path_to_cmake(objectfile, outputdir))
end
end
end

if #object_deps ~= 0 then
if not has_links then
cmakelists:print("target_link_libraries(%s PRIVATE", target:name())
has_links = true
end
for _, dep in ipairs(object_deps) do
cmakelists:print(" " .. dep)
end
end

if has_links then
cmakelists:print(")")
end
end
Expand Down Expand Up @@ -1196,6 +1233,8 @@ function _add_target(cmakelists, target, outputdir)
-- is phony target?
if target:is_phony() then
return _add_target_phony(cmakelists, target)
elseif target:is_object() then
_add_target_object(cmakelists, target, outputdir)
elseif target:is_binary() then
_add_target_binary(cmakelists, target, outputdir)
elseif target:is_static() then
Expand Down
2 changes: 1 addition & 1 deletion xmake/plugins/project/vsxmake/getinfo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function _make_targetinfo(mode, arch, target)
-- fix c++17 to cxx17 for Xmake.props
targetinfo.languages = targetinfo.languages:replace("c++", "cxx", {plain = true})
end
if target:is_phony() or target:is_headeronly() or target:is_moduleonly() then
if target:is_phony() or target:is_headeronly() or target:is_moduleonly() or target:is_object() then
return targetinfo
end

Expand Down
Loading