From af1c1b3100531647c0ac692469579158e55ed05a Mon Sep 17 00:00:00 2001 From: Joris Dauphin Date: Fri, 8 Mar 2024 22:08:49 +0100 Subject: [PATCH] Handle `buildaction "Copy"` for gmake2. (#2188) * Handle `buildaction "Copy"` for gmake2. * Fix codelite priority of custombuild and `buildaction` for codelite. Custombuild has higher priority than `buildaction`. --- modules/codelite/codelite_project.lua | 32 +++++++++---------- modules/gmake2/gmake2.lua | 9 ++++++ modules/gmake2/gmake2_cpp.lua | 17 +++++++++- .../gmake2/tests/test_gmake2_file_rules.lua | 24 ++++++++++++++ modules/gmake2/tests/test_gmake2_objects.lua | 23 +++++++++++++ 5 files changed, 88 insertions(+), 17 deletions(-) diff --git a/modules/codelite/codelite_project.lua b/modules/codelite/codelite_project.lua index c5b2e4fe27..a6efbb30ea 100755 --- a/modules/codelite/codelite_project.lua +++ b/modules/codelite/codelite_project.lua @@ -405,7 +405,21 @@ local dependencies = {} local makefilerules = {} local function addrule(dependencies, makefilerules, config, filename) - if config.buildaction == "Copy" and filename ~= "" then + if #config.buildcommands > 0 and #config.buildoutputs > 0 then + local inputs = table.implode(project.getrelative(cfg.project, config.buildinputs), "", "", " ") + if filename ~= "" and inputs ~= "" then + filename = filename .. " " + end + local outputs = project.getrelative(cfg.project, config.buildoutputs[1]) + local buildmessage = "" + if config.buildmessage then + buildmessage = "\t@{ECHO} " .. p.quote(config.buildmessage) .. "\n" + end + local commands = table.implode(config.buildcommands,"\t","\n","") + table.insert(makefilerules, os.translateCommandsAndPaths(outputs .. ": " .. filename .. inputs .. "\n" .. buildmessage .. "\t@$(MakeDirCommand) $(@D)\n" .. commands, cfg.project.basedir, cfg.project.location)) + table.insertflat(dependencies, outputs) + return true + elseif config.buildaction == "Copy" and filename ~= "" then local output = project.getrelative(cfg.workspace, path.join(cfg.targetdir, config.name)) local create_directory_command = '\t@$(MakeDirCommand) $(@D)\n' local command = '\t' .. os.translateCommands('{COPYFILE} "' .. filename .. '" "' .. output ..'"') .. '\n' @@ -413,23 +427,9 @@ table.insert(makefilerules, output .. ": " .. filename .. '\n' .. create_directory_command .. command) table.insert(dependencies, output) return true - end - if #config.buildcommands == 0 or #config.buildoutputs == 0 then + else return false end - local inputs = table.implode(project.getrelative(cfg.project, config.buildinputs), "", "", " ") - if filename ~= "" and inputs ~= "" then - filename = filename .. " " - end - local outputs = project.getrelative(cfg.project, config.buildoutputs[1]) - local buildmessage = "" - if config.buildmessage then - buildmessage = "\t@{ECHO} " .. p.quote(config.buildmessage) .. "\n" - end - local commands = table.implode(config.buildcommands,"\t","\n","") - table.insert(makefilerules, os.translateCommandsAndPaths(outputs .. ": " .. filename .. inputs .. "\n" .. buildmessage .. "\t@$(MakeDirCommand) $(@D)\n" .. commands, cfg.project.basedir, cfg.project.location)) - table.insertflat(dependencies, outputs) - return true end local tr = project.getsourcetree(cfg.project) p.tree.traverse(tr, { diff --git a/modules/gmake2/gmake2.lua b/modules/gmake2/gmake2.lua index e735497836..ca141f67a5 100644 --- a/modules/gmake2/gmake2.lua +++ b/modules/gmake2/gmake2.lua @@ -129,6 +129,15 @@ _p('endif') end + function gmake2.copyfile_cmds(source, dest) + local cmd = '$(SILENT) {COPYFILE} ' .. source .. ' ' .. dest + return { 'ifeq (posix,$(SHELLTYPE))', + '\t' .. os.translateCommands(cmd, 'posix'), + 'else', + '\t' .. os.translateCommands(cmd, 'windows'), + 'endif' } + end + function gmake2.mkdirRules(dirname) _p('%s:', dirname) _p('\t@echo Creating %s', dirname) diff --git a/modules/gmake2/gmake2_cpp.lua b/modules/gmake2/gmake2_cpp.lua index a02893dfd2..63b3d331b5 100644 --- a/modules/gmake2/gmake2_cpp.lua +++ b/modules/gmake2/gmake2_cpp.lua @@ -192,6 +192,17 @@ cpp.addGeneratedFile(cfg, node, output) end end + elseif filecfg.buildaction == "Copy" then + local output = '$(TARGETDIR)/' .. node.name + local file = { + buildoutputs = { output }, + source = node.relpath, + buildmessage = '$(notdir $<)', + verbatimbuildcommands = gmake2.copyfile_cmds('"$<"', '"$@"'), + buildinputs = {'$(TARGETDIR)'} + } + table.insert(cfg._gmake.fileRules, file) + cpp.addGeneratedFile(cfg, node, output) else cpp.addRuleFile(cfg, node) end @@ -761,7 +772,11 @@ end end end - + if file.verbatimbuildcommands then + for _, cmd in ipairs(file.verbatimbuildcommands) do + _p('%s', cmd); + end + end -- TODO: this is a hack with some imperfect side-effects. -- better solution would be to emit a dummy file for the rule, and then outputs depend on it (must clean up dummy in 'clean') -- better yet, is to use pattern rules, but we need to detect that all outputs have the same stem diff --git a/modules/gmake2/tests/test_gmake2_file_rules.lua b/modules/gmake2/tests/test_gmake2_file_rules.lua index c420779edb..4e61ce975a 100644 --- a/modules/gmake2/tests/test_gmake2_file_rules.lua +++ b/modules/gmake2/tests/test_gmake2_file_rules.lua @@ -409,3 +409,27 @@ test2.obj: test2.rule $(SILENT) dorule S1 "test2.rule" ]] end + + function suite.fileRulesOnBuildactionCopy() + files { "hello.dll" } + filter { "Debug", "files:hello.dll" } + buildaction "Copy" + filter {} + prepare() + test.capture [[ +# File Rules +# ############################################# + +ifeq ($(config),debug) +$(TARGETDIR)/hello.dll: hello.dll $(TARGETDIR) + @echo "$(notdir $<)" +ifeq (posix,$(SHELLTYPE)) + $(SILENT) cp -f "$<" "$@" +else + $(SILENT) copy /B /Y "$<" "$@" +endif + +endif +]] + end + diff --git a/modules/gmake2/tests/test_gmake2_objects.lua b/modules/gmake2/tests/test_gmake2_objects.lua index 2c7881d461..2314fe47a5 100644 --- a/modules/gmake2/tests/test_gmake2_objects.lua +++ b/modules/gmake2/tests/test_gmake2_objects.lua @@ -494,3 +494,26 @@ endif ]] end + + function suite.objectsOnBuildactionCopy() + files { "hello.dll" } + filter { "Debug", "files:hello.dll" } + buildaction "Copy" + filter {} + prepare() + test.capture [[ +# File sets +# ############################################# + +CUSTOM := +GENERATED := + +ifeq ($(config),debug) +CUSTOM += $(TARGETDIR)/hello.dll +GENERATED += $(TARGETDIR)/hello.dll + +endif + + ]] + end +