diff options
| author | ruki <[email protected]> | 2024-09-27 14:25:18 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-09-27 14:25:18 +0800 |
| commit | 9738030c15c8e4b6d9b4a9001964cdf159155d55 (patch) | |
| tree | dc6edd280cad6f386b956e5c211f528cb769b095 | |
| parent | 2a3ae0690dd4d76033340bf3b3ad00a2fd96d8e7 (diff) | |
| parent | 2bd33b1ea96413be391a57dadf79596467ce4791 (diff) | |
Merge pull request #5667 from star-hengxing/generator-object-kind
Improve object target for generator
| -rw-r--r-- | xmake/plugins/project/cmake/cmakelists.lua | 47 | ||||
| -rw-r--r-- | xmake/plugins/project/vsxmake/getinfo.lua | 2 |
2 files changed, 45 insertions, 4 deletions
diff --git a/xmake/plugins/project/cmake/cmakelists.lua b/xmake/plugins/project/cmake/cmakelists.lua index 48a637a78..3002079a0 100644 --- a/xmake/plugins/project/cmake/cmakelists.lua +++ b/xmake/plugins/project/cmake/cmakelists.lua @@ -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()) @@ -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) @@ -984,13 +998,38 @@ 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()) + for _, obj in ipairs(dep:objectfiles()) do + objectfiles_set:insert(obj) + end + 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 @@ -1196,6 +1235,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 diff --git a/xmake/plugins/project/vsxmake/getinfo.lua b/xmake/plugins/project/vsxmake/getinfo.lua index 4d6618ca9..8c1a477e7 100644 --- a/xmake/plugins/project/vsxmake/getinfo.lua +++ b/xmake/plugins/project/vsxmake/getinfo.lua @@ -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 |
