From f416451b312c06a3b9d48ea389450dfa9b11d376 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 1 Apr 2026 23:27:51 +0800 Subject: add utils.replace rules --- tests/apis/add_files_replace/src/main.c | 10 ++++ tests/apis/add_files_replace/test.lua | 4 ++ tests/apis/add_files_replace/xmake.lua | 6 +++ xmake/rules/utils/replace/xmake.lua | 92 +++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 tests/apis/add_files_replace/src/main.c create mode 100644 tests/apis/add_files_replace/test.lua create mode 100644 tests/apis/add_files_replace/xmake.lua create mode 100644 xmake/rules/utils/replace/xmake.lua diff --git a/tests/apis/add_files_replace/src/main.c b/tests/apis/add_files_replace/src/main.c new file mode 100644 index 000000000..8b6ee15b8 --- /dev/null +++ b/tests/apis/add_files_replace/src/main.c @@ -0,0 +1,10 @@ +#include + +#ifndef REPLACED_VALUE +#define REPLACED_VALUE "original" +#endif + +int main(int argc, char** argv) { + printf("hello %s!\n", REPLACED_VALUE); + return 0; +} diff --git a/tests/apis/add_files_replace/test.lua b/tests/apis/add_files_replace/test.lua new file mode 100644 index 000000000..512be45b1 --- /dev/null +++ b/tests/apis/add_files_replace/test.lua @@ -0,0 +1,4 @@ +function main(t) + t:build() + t:run() +end diff --git a/tests/apis/add_files_replace/xmake.lua b/tests/apis/add_files_replace/xmake.lua new file mode 100644 index 000000000..24b98e7cd --- /dev/null +++ b/tests/apis/add_files_replace/xmake.lua @@ -0,0 +1,6 @@ +add_rules("mode.debug", "mode.release") +target("test") + set_kind("binary") + add_files("src/main.c", {rules = "utils.replace", replaces = { + {'"original"', '"replaced"'}, + }}) diff --git a/xmake/rules/utils/replace/xmake.lua b/xmake/rules/utils/replace/xmake.lua new file mode 100644 index 000000000..47340cb1a --- /dev/null +++ b/xmake/rules/utils/replace/xmake.lua @@ -0,0 +1,92 @@ +--!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 xmake.lua +-- + +-- @usage +-- +-- e.g. replace with lua pattern (default) +-- +-- add_files("src/foo.c", {rules = "utils.replace", replaces = {{"RDKCONST%);", "VDKREG);"}}}) +-- +-- e.g. replace with plain text +-- +-- add_files("src/foo.c", {rules = "utils.replace", replaces = {{"RDKCONST);", "VDKREG);"}}, replace_plain = true}) +-- +rule("utils.replace") + on_prepare_file(function (target, sourcefile, opt) + import("utils.progress") + import("core.base.option") + import("core.project.depend") + + -- get replace config from fileconfig + local fileconfig = target:fileconfig(sourcefile) + if not fileconfig or not fileconfig.replaces then + return + end + local replaces = fileconfig.replaces + local use_plain = fileconfig.replace_plain + + -- get the replaced file path + local replacefile = target:autogenfile(sourcefile) + + -- replace the sourcefile in the build sourcebatch, + -- so that the compiler uses the replaced file instead of the original + for _, sourcebatch in pairs(target:sourcebatches()) do + if sourcebatch.rulename ~= "utils.replace" then + for i, sf in ipairs(sourcebatch.sourcefiles) do + if sf == sourcefile then + sourcebatch.sourcefiles[i] = replacefile + end + end + end + end + + -- build values list from replace config for change detection + local depvalues = {} + for _, item in ipairs(replaces) do + table.insert(depvalues, item[1]) + table.insert(depvalues, item[2]) + end + if use_plain then + table.insert(depvalues, "plain") + end + + -- do replace if source file or replace config is changed + depend.on_changed(function () + progress.show(opt.progress, "${color.build.object}replacing.$(mode) %s", sourcefile) + vprint("replace %s to %s", sourcefile, replacefile) + if option.get("diagnosis") then + for _, item in ipairs(replaces) do + cprint("${dim} > \"%s\" -> \"%s\"%s", item[1], item[2], use_plain and " (plain)" or "") + end + end + local content = io.readfile(sourcefile) + for _, item in ipairs(replaces) do + if use_plain then + content = content:replace(item[1], item[2], {plain = true}) + else + content = content:gsub(item[1], item[2]) + end + end + io.writefile(replacefile, content) + end, {dependfile = target:dependfile(replacefile), + files = {sourcefile}, + values = depvalues, + changed = target:is_rebuilt()}) + end) -- cgit v1.3.1 From eedaa8c079ee0eb4cf9175db5c51f11c2748dcfd Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 1 Apr 2026 23:29:57 +0800 Subject: improve utils.replace --- tests/apis/add_files_replace/src/config.h | 2 ++ tests/apis/add_files_replace/src/main.c | 7 +++---- xmake/rules/utils/replace/xmake.lua | 5 +++++ 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 tests/apis/add_files_replace/src/config.h diff --git a/tests/apis/add_files_replace/src/config.h b/tests/apis/add_files_replace/src/config.h new file mode 100644 index 000000000..96d83f651 --- /dev/null +++ b/tests/apis/add_files_replace/src/config.h @@ -0,0 +1,2 @@ +#pragma once +#define VERSION "1.0" diff --git a/tests/apis/add_files_replace/src/main.c b/tests/apis/add_files_replace/src/main.c index 8b6ee15b8..d2ac8f8ae 100644 --- a/tests/apis/add_files_replace/src/main.c +++ b/tests/apis/add_files_replace/src/main.c @@ -1,10 +1,9 @@ #include +#include "config.h" -#ifndef REPLACED_VALUE -#define REPLACED_VALUE "original" -#endif +#define HELLO "original" int main(int argc, char** argv) { - printf("hello %s!\n", REPLACED_VALUE); + printf("hello %s, %s!\n", HELLO, VERSION); return 0; } diff --git a/xmake/rules/utils/replace/xmake.lua b/xmake/rules/utils/replace/xmake.lua index 47340cb1a..da04a0720 100644 --- a/xmake/rules/utils/replace/xmake.lua +++ b/xmake/rules/utils/replace/xmake.lua @@ -45,6 +45,11 @@ rule("utils.replace") -- get the replaced file path local replacefile = target:autogenfile(sourcefile) + -- add the original source directory as includedir for the replaced file, + -- so that relative #include paths still work + local sourcedir = path.directory(path.absolute(sourcefile)) + target:fileconfig_add(replacefile, {includedirs = sourcedir}) + -- replace the sourcefile in the build sourcebatch, -- so that the compiler uses the replaced file instead of the original for _, sourcebatch in pairs(target:sourcebatches()) do -- cgit v1.3.1 From a58f4d0c2e60cbe87ba3569a62fceeb1587c88c4 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 1 Apr 2026 23:32:54 +0800 Subject: fix includedir for project generator --- xmake/rules/utils/replace/xmake.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xmake/rules/utils/replace/xmake.lua b/xmake/rules/utils/replace/xmake.lua index da04a0720..a3608cc34 100644 --- a/xmake/rules/utils/replace/xmake.lua +++ b/xmake/rules/utils/replace/xmake.lua @@ -45,10 +45,10 @@ rule("utils.replace") -- get the replaced file path local replacefile = target:autogenfile(sourcefile) - -- add the original source directory as includedir for the replaced file, - -- so that relative #include paths still work + -- add the original source directory as includedir, + -- so that relative #include paths still work after replacing local sourcedir = path.directory(path.absolute(sourcefile)) - target:fileconfig_add(replacefile, {includedirs = sourcedir}) + target:add("includedirs", sourcedir) -- replace the sourcefile in the build sourcebatch, -- so that the compiler uses the replaced file instead of the original -- cgit v1.3.1 From 55b9013e699dbfc94aa9d77d3a9546c0740fc56e Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 1 Apr 2026 23:33:59 +0800 Subject: fix test --- tests/apis/add_files_replace/test.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/apis/add_files_replace/test.lua b/tests/apis/add_files_replace/test.lua index 512be45b1..b57362078 100644 --- a/tests/apis/add_files_replace/test.lua +++ b/tests/apis/add_files_replace/test.lua @@ -1,4 +1,3 @@ function main(t) t:build() - t:run() end -- cgit v1.3.1 From 57ef7e0332355a091abe1ee9eefe2a359aeb6f1a Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 1 Apr 2026 23:49:51 +0800 Subject: improve utils.replace to support function --- tests/apis/add_files_replace/xmake.lua | 10 ++++++++ xmake/rules/utils/replace/xmake.lua | 46 ++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/tests/apis/add_files_replace/xmake.lua b/tests/apis/add_files_replace/xmake.lua index 24b98e7cd..ee890c9f8 100644 --- a/tests/apis/add_files_replace/xmake.lua +++ b/tests/apis/add_files_replace/xmake.lua @@ -1,6 +1,16 @@ add_rules("mode.debug", "mode.release") + +-- test table replaces target("test") set_kind("binary") add_files("src/main.c", {rules = "utils.replace", replaces = { {'"original"', '"replaced"'}, }}) + +-- test function replaces +target("test2") + set_kind("binary") + add_files("src/main.c", {rules = "utils.replace", replaces = function (content) + content = content:gsub('"original"', '"func_replaced"') + return content + end}) diff --git a/xmake/rules/utils/replace/xmake.lua b/xmake/rules/utils/replace/xmake.lua index a3608cc34..8b865b750 100644 --- a/xmake/rules/utils/replace/xmake.lua +++ b/xmake/rules/utils/replace/xmake.lua @@ -28,6 +28,13 @@ -- -- add_files("src/foo.c", {rules = "utils.replace", replaces = {{"RDKCONST);", "VDKREG);"}}, replace_plain = true}) -- +-- e.g. replace with custom function +-- +-- add_files("src/foo.c", {rules = "utils.replace", replaces = function (content) +-- content = content:gsub("old", "new") +-- return content +-- end}) +-- rule("utils.replace") on_prepare_file(function (target, sourcefile, opt) import("utils.progress") @@ -64,29 +71,36 @@ rule("utils.replace") -- build values list from replace config for change detection local depvalues = {} - for _, item in ipairs(replaces) do - table.insert(depvalues, item[1]) - table.insert(depvalues, item[2]) - end - if use_plain then - table.insert(depvalues, "plain") + local is_function = type(replaces) == "function" + if not is_function then + for _, item in ipairs(replaces) do + table.insert(depvalues, item[1]) + table.insert(depvalues, item[2]) + end + if use_plain then + table.insert(depvalues, "plain") + end end -- do replace if source file or replace config is changed depend.on_changed(function () progress.show(opt.progress, "${color.build.object}replacing.$(mode) %s", sourcefile) vprint("replace %s to %s", sourcefile, replacefile) - if option.get("diagnosis") then - for _, item in ipairs(replaces) do - cprint("${dim} > \"%s\" -> \"%s\"%s", item[1], item[2], use_plain and " (plain)" or "") - end - end local content = io.readfile(sourcefile) - for _, item in ipairs(replaces) do - if use_plain then - content = content:replace(item[1], item[2], {plain = true}) - else - content = content:gsub(item[1], item[2]) + if is_function then + content = replaces(content) + else + if option.get("diagnosis") then + for _, item in ipairs(replaces) do + cprint("${dim} > \"%s\" -> \"%s\"%s", item[1], item[2], use_plain and " (plain)" or "") + end + end + for _, item in ipairs(replaces) do + if use_plain then + content = content:replace(item[1], item[2], {plain = true}) + else + content = content:gsub(item[1], item[2]) + end end end io.writefile(replacefile, content) -- cgit v1.3.1