From 9ad1383808d20fbb5ffcfd69dfb507f87c28c3ac Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 17 Mar 2026 00:03:55 +0800 Subject: move generate csproj to prepare.lua --- xmake/rules/csharp/build.lua | 86 +++++++++++++++++++++++++ xmake/rules/csharp/build/target.lua | 97 ----------------------------- xmake/rules/csharp/modules/csharp_utils.lua | 54 ---------------- xmake/rules/csharp/prepare.lua | 50 +++++++++++++++ xmake/rules/csharp/xmake.lua | 3 +- 5 files changed, 138 insertions(+), 152 deletions(-) create mode 100644 xmake/rules/csharp/build.lua delete mode 100644 xmake/rules/csharp/build/target.lua delete mode 100644 xmake/rules/csharp/modules/csharp_utils.lua create mode 100644 xmake/rules/csharp/prepare.lua diff --git a/xmake/rules/csharp/build.lua b/xmake/rules/csharp/build.lua new file mode 100644 index 000000000..9bc6f2e76 --- /dev/null +++ b/xmake/rules/csharp/build.lua @@ -0,0 +1,86 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, Xmake Open Source Community. +-- +-- @author ruki +-- @file target.lua +-- + +-- imports +import("core.base.option") +import("core.tool.compiler") +import("core.project.depend") +import("utils.progress") + +-- build the source files +function build_sourcefiles(target, sourcebatch, opt) + + -- get the target file + local targetfile = target:targetfile() + + -- get source files and kind + local sourcefiles = sourcebatch.sourcefiles + local sourcekind = sourcebatch.sourcekind + local csprojfile = target:data("csharp.csproj") + + -- get depend file + local dependfile = target:dependfile(targetfile) + + -- load compiler + local compinst = compiler.load(sourcekind, {target = target}) + + -- get compile flags + local compflags = compinst:compflags({target = target}) + + -- load dependent info + local dependinfo = option.get("rebuild") and {} or (depend.load(dependfile) or {}) + + -- need build this target? + local depvalues = {compinst:program(), compflags} + if not depend.is_changed(dependinfo, {lastmtime = os.mtime(targetfile), values = depvalues}) then + return + end + + -- trace progress info + progress.show(opt.progress, "${color.build.target}linking.$(mode) %s", path.filename(targetfile)) + + -- trace verbose info + vprint(compinst:buildcmd(sourcefiles, targetfile, {target = target, compflags = compflags})) + + -- flush io buffer to update progress info + io.flush() + + -- build it + dependinfo.files = {} + assert(compinst:build(sourcefiles, targetfile, {target = target, dependinfo = dependinfo, compflags = compflags})) + + -- update files and values to the dependent file + dependinfo.values = depvalues + table.join2(dependinfo.files, sourcefiles, csprojfile) + depend.save(dependinfo, dependfile) +end + +-- build target +function main(target, opt) + + -- @note only support one source kind! + local sourcebatches = target:sourcebatches() + if sourcebatches then + local sourcebatch = sourcebatches["csharp.build"] + if sourcebatch then + build_sourcefiles(target, sourcebatch, opt) + end + end +end diff --git a/xmake/rules/csharp/build/target.lua b/xmake/rules/csharp/build/target.lua deleted file mode 100644 index eb61bdef9..000000000 --- a/xmake/rules/csharp/build/target.lua +++ /dev/null @@ -1,97 +0,0 @@ ---!A cross-platform build utility based on Lua --- --- Licensed under the Apache License, Version 2.0 (the "License"); --- you may not use this file except in compliance with the License. --- You may obtain a copy of the License at --- --- http://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, --- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --- See the License for the specific language governing permissions and --- limitations under the License. --- --- Copyright (C) 2015-present, Xmake Open Source Community. --- --- @author ruki --- @file target.lua --- - --- imports -import("core.base.option") -import("core.tool.compiler") -import("core.project.depend") -import("utils.progress") -import(".modules.csharp_utils") - --- generate .csproj file -function _get_csprojfile(target) - local csprojfile = target:data("csharp.csproj") - if not csprojfile then - csprojfile = csharp_utils.generate_csproj_file(target) - target:data_set("csharp.csproj", csprojfile) - end - return csprojfile -end - --- build the source files -function build_sourcefiles(target, sourcebatch, opt) - - -- get the target file - local targetfile = target:targetfile() - - -- get source files and kind - local sourcefiles = sourcebatch.sourcefiles - local sourcekind = sourcebatch.sourcekind - local csprojfile = _get_csprojfile(target) - - -- get depend file - local dependfile = target:dependfile(targetfile) - - -- load compiler - local compinst = compiler.load(sourcekind, {target = target}) - - -- get compile flags - local compflags = compinst:compflags({target = target}) - - -- load dependent info - local dependinfo = option.get("rebuild") and {} or (depend.load(dependfile) or {}) - - -- need build this target? - local depvalues = {compinst:program(), compflags} - if not depend.is_changed(dependinfo, {lastmtime = os.mtime(targetfile), values = depvalues}) then - return - end - - -- trace progress info - progress.show(opt.progress, "${color.build.target}linking.$(mode) %s", path.filename(targetfile)) - - -- trace verbose info - vprint(compinst:buildcmd(sourcefiles, targetfile, {target = target, compflags = compflags})) - - -- flush io buffer to update progress info - io.flush() - - -- build it - dependinfo.files = {} - assert(compinst:build(sourcefiles, targetfile, {target = target, dependinfo = dependinfo, compflags = compflags})) - - -- update files and values to the dependent file - dependinfo.values = depvalues - table.join2(dependinfo.files, sourcefiles, csprojfile) - depend.save(dependinfo, dependfile) -end - --- build target -function main(target, opt) - - -- @note only support one source kind! - local sourcebatches = target:sourcebatches() - if sourcebatches then - local sourcebatch = sourcebatches["csharp.build"] - if sourcebatch then - build_sourcefiles(target, sourcebatch, opt) - end - end -end diff --git a/xmake/rules/csharp/modules/csharp_utils.lua b/xmake/rules/csharp/modules/csharp_utils.lua deleted file mode 100644 index 78837cff9..000000000 --- a/xmake/rules/csharp/modules/csharp_utils.lua +++ /dev/null @@ -1,54 +0,0 @@ ---!A cross-platform build utility based on Lua --- --- Licensed under the Apache License, Version 2.0 (the "License"); --- you may not use this file except in compliance with the License. --- You may obtain a copy of the License at --- --- http://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, --- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --- See the License for the specific language governing permissions and --- limitations under the License. --- --- Copyright (C) 2015-present, Xmake Open Source Community. --- --- @author JassJam --- @file csharp_utils.lua --- - --- imports -import("core.project.config") -import("csproj_generator", {alias = "generate_csproj"}) - -function _is_csharp_target(target) - if target:rule("csharp") then - return true - end - for _, sourcefile in ipairs(target:sourcefiles()) do - local ext = path.extension(sourcefile):lower() - if ext == ".cs" or ext == ".csproj" then - return true - end - end - return false -end - -function _generated_csproj_path(target) - local targetkey = target:fullname():replace("::", path.sep()) - local csprojdir = path.join(config.directory(), "rules", "csharp", targetkey, target:plat(), target:arch()) - local csprojname = target:name() .. ".csproj" - return path.join(csprojdir, csprojname) -end - -function generate_csproj_file(target) - if not _is_csharp_target(target) then - return nil - end - local csprojfile = _generated_csproj_path(target) - generate_csproj(target, csprojfile) - target:data_set("csharp.csproj", csprojfile) - return csprojfile -end - diff --git a/xmake/rules/csharp/prepare.lua b/xmake/rules/csharp/prepare.lua new file mode 100644 index 000000000..611ad5890 --- /dev/null +++ b/xmake/rules/csharp/prepare.lua @@ -0,0 +1,50 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, Xmake Open Source Community. +-- +-- @author ruki +-- @file prepare.lua +-- + +-- imports +import("core.project.config") +import("core.project.depend") +import("modules.csproj_generator", {rootdir = os.scriptdir()}) + +function main(target) + + -- compute csproj path + local targetkey = target:fullname():replace("::", path.sep()) + local csprojdir = path.join(config.directory(), "rules", "csharp", targetkey, target:plat(), target:arch()) + local csprojfile = path.join(csprojdir, target:name() .. ".csproj") + local dependfile = target:dependfile(csprojfile) + + -- collect source files and dep csproj paths as depend values + local sourcefiles = target:sourcefiles() + local depcsproj = {} + for _, dep in ipairs(target:orderdeps()) do + local depcsproj_path = dep:data("csharp.csproj") + if depcsproj_path then + table.insert(depcsproj, depcsproj_path) + end + end + + -- generate csproj incrementally + depend.on_changed(function () + csproj_generator(target, csprojfile) + end, {dependfile = dependfile, files = sourcefiles, values = depcsproj}) + + target:data_set("csharp.csproj", csprojfile) +end diff --git a/xmake/rules/csharp/xmake.lua b/xmake/rules/csharp/xmake.lua index 30611d68a..1d5818337 100644 --- a/xmake/rules/csharp/xmake.lua +++ b/xmake/rules/csharp/xmake.lua @@ -20,7 +20,8 @@ rule("csharp.build") set_sourcekinds("cs") - on_build("build.target") + on_prepare("prepare") + on_build("build") rule("csharp") add_deps("csharp.build") -- cgit v1.3.1