diff options
| author | ruki <[email protected]> | 2021-11-15 21:38:01 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-11-15 21:38:01 +0800 |
| commit | f31338f204618c41ebadede7f99b15a0d2bb61a5 (patch) | |
| tree | d92e3af8e5a069dcea52236a28ac97aacf85c58c /xmake/plugins | |
| parent | f8679754274d3b7d7897d81387d36d1df9921056 (diff) | |
add custom commands for vs
Diffstat (limited to 'xmake/plugins')
| -rw-r--r-- | xmake/plugins/project/vstudio/impl/vs201x.lua | 1 | ||||
| -rw-r--r-- | xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua | 124 |
2 files changed, 125 insertions, 0 deletions
diff --git a/xmake/plugins/project/vstudio/impl/vs201x.lua b/xmake/plugins/project/vstudio/impl/vs201x.lua index da0dd9317..7101c3331 100644 --- a/xmake/plugins/project/vstudio/impl/vs201x.lua +++ b/xmake/plugins/project/vstudio/impl/vs201x.lua @@ -330,6 +330,7 @@ function make(outputdir, vsinfo) _target.pcxxheader = target:pcheaderfile("cxx") -- header.[hpp|inl] -- init target info + _target.targetinst = target _target.name = targetname _target.kind = target:kind() _target.scriptdir = target:scriptdir() diff --git a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua index b1bd6f764..1a55eb551 100644 --- a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua +++ b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua @@ -20,7 +20,9 @@ -- imports import("core.project.config") +import("core.project.project") import("core.language.language") +import("private.utils.batchcmds") import("vsfile") -- get toolset version @@ -369,6 +371,125 @@ function _make_source_options(vcxprojfile, flags, condition) end end +-- get command string +function _get_command_string(cmd) + local kind = cmd.kind + local opt = cmd.opt + if cmd.program then + elseif kind == "cp" then + elseif kind == "rm" then + elseif kind == "mv" then + elseif kind == "cd" then + elseif kind == "mkdir" then + elseif kind == "show" then + return string.format("echo %s", cmd.showtext) + end +end + +-- add custom command +function _make_custom_command(vcxprojfile, target, command, suffix) + if suffix == "after" then + vcxprojfile:print("<PostBuildEvent>") + elseif suffix == "before" then + vcxprojfile:print("<PreBuildEvent>") + end + vcxprojfile:print("<Message></Message>") + vcxprojfile:print("<Command>setlocal") + vcxprojfile:print("%s", command) + vcxprojfile:write([[if %errorlevel% neq 0 goto :xmEnd +:xmEnd +endlocal & call :xmErrorLevel %errorlevel% & goto :xmDone +:xmErrorLevel +exit /b %1 +:xmDone +if %errorlevel% neq 0 goto :VCEnd</Command> +]]) + if suffix == "after" then + vcxprojfile:print("</PostBuildEvent>") + elseif suffix == "before" then + vcxprojfile:print("</PreBuildEvent>") + end +end + +-- add target custom commands for target +function _make_custom_commands_for_target(vcxprojfile, target, suffix) + for _, ruleinst in ipairs(target:orderules()) do + local scriptname = "buildcmd" .. (suffix and ("_" .. suffix) or "") + local script = ruleinst:script(scriptname) + if script then + local batchcmds_ = batchcmds.new({target = target}) + script(target, batchcmds_, {}) + if not batchcmds_:empty() then + for _, cmd in ipairs(batchcmds_:cmds()) do + local command = _get_command_string(cmd) + if command then + _make_custom_command(vcxprojfile, target, command, suffix) + end + end + end + end + end +end + +-- add target custom commands for object rules +function _make_custom_commands_for_objectrules(vcxprojfile, target, sourcebatch, suffix) + + -- get rule + local rulename = assert(sourcebatch.rulename, "unknown rule for sourcebatch!") + local ruleinst = assert(project.rule(rulename) or rule.rule(rulename), "unknown rule: %s", rulename) + + -- generate commands for xx_buildcmd_files + local scriptname = "buildcmd_files" .. (suffix and ("_" .. suffix) or "") + local script = ruleinst:script(scriptname) + if script then + local batchcmds_ = batchcmds.new({target = target}) + script(target, batchcmds_, sourcebatch, {}) + if not batchcmds_:empty() then + for _, cmd in ipairs(batchcmds_:cmds()) do + local command = _get_command_string(cmd) + if command then + _make_custom_command(vcxprojfile, target, command, suffix) + end + end + end + end + + -- generate commands for xx_buildcmd_file + if not script then + scriptname = "buildcmd_file" .. (suffix and ("_" .. suffix) or "") + script = ruleinst:script(scriptname) + if script then + local sourcekind = sourcebatch.sourcekind + for _, sourcefile in ipairs(sourcebatch.sourcefiles) do + local batchcmds_ = batchcmds.new({target = target}) + script(target, batchcmds_, sourcefile, {}) + if not batchcmds_:empty() then + for _, cmd in ipairs(batchcmds_:cmds()) do + local command = _get_command_string(cmd) + if command then + _make_custom_command(vcxprojfile, target, command, suffix) + end + end + end + end + end + end +end + +-- make custom commands +function _make_custom_commands(vcxprojfile, target) + _make_custom_commands_for_target(vcxprojfile, target, "before") + for _, sourcebatch in pairs(target:sourcebatches()) do + local sourcekind = sourcebatch.sourcekind + if sourcekind ~= "cc" and sourcekind ~= "cxx" and sourcekind ~= "as" then + _make_custom_commands_for_objectrules(vcxprojfile, target, sourcebatch, "before") + _make_custom_commands_for_objectrules(vcxprojfile, target, sourcebatch) + _make_custom_commands_for_objectrules(vcxprojfile, target, sourcebatch, "after") + end + end + _make_custom_commands_for_target(vcxprojfile, target, "after") +end + -- make common item function _make_common_item(vcxprojfile, vsinfo, target, targetinfo, vcxprojdir) @@ -456,6 +577,9 @@ function _make_common_item(vcxprojfile, vsinfo, target, targetinfo, vcxprojdir) vcxprojfile:leave("</ClCompile>") + -- make custom commands + _make_custom_commands(vcxprojfile, target.targetinst) + -- leave ItemDefinitionGroup vcxprojfile:leave("</ItemDefinitionGroup>") end |
