From 6f1d565decf6623cad7c56ff3d1badcc2f40e6d4 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 28 Oct 2021 23:31:06 +0800 Subject: improve pre-build for cmake custom command --- CHANGELOG.md | 2 ++ xmake/plugins/project/cmake/cmakelists.lua | 34 ++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f0821a32..42a4452ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * [#1767](https://github.com/xmake-io/xmake/issues/1767): Support Circle compiler * [#1753](https://github.com/xmake-io/xmake/issues/1753): Support armcc/armclang toolchains for Keil/MDK * [#1774](https://github.com/xmake-io/xmake/issues/1774): Add table.contains api +* [#1735](https://github.com/xmake-io/xmake/issues/1735): Add custom command in cmake generator ### Changes @@ -1125,6 +1126,7 @@ * [#1767](https://github.com/xmake-io/xmake/issues/1767): 支持 Circle 编译器 * [#1753](https://github.com/xmake-io/xmake/issues/1753): 支持 Keil/MDK 的 armcc/armclang 工具链 * [#1774](https://github.com/xmake-io/xmake/issues/1774): 添加 table.contains api +* [#1735](https://github.com/xmake-io/xmake/issues/1735): 添加自定义命令到 cmake 生成器 ### 改进 diff --git a/xmake/plugins/project/cmake/cmakelists.lua b/xmake/plugins/project/cmake/cmakelists.lua index c43f7584f..b4d9fe952 100644 --- a/xmake/plugins/project/cmake/cmakelists.lua +++ b/xmake/plugins/project/cmake/cmakelists.lua @@ -481,15 +481,31 @@ end -- add custom command function _add_target_custom_command(cmakelists, target, command, suffix) - cmakelists:print("add_custom_command(TARGET %s", target:name()) - if suffix == "prefix" then - cmakelists:print(" PRE_BUILD") - elseif suffix == "suffix" then - cmakelists:print(" POST_BUILD") - end - cmakelists:print(" COMMAND %s", os.args(command)) - cmakelists:print(" VERBATIM") - cmakelists:print(")") + local command_str = os.args(command) + if suffix == "before" then + -- ADD_CUSTOM_COMMAND and PRE_BUILD did not work as I expected, + -- so we need use add_dependencies and fake target to support it. + -- + -- @see https://gitlab.kitware.com/cmake/cmake/-/issues/17802 + -- + local key = hash.uuid(command_str):split("-", {plain = true})[1] + cmakelists:print("add_custom_command(OUTPUT output_%s", key) + cmakelists:print(" COMMAND %s", command_str) + cmakelists:print(" VERBATIM") + cmakelists:print(")") + cmakelists:print("add_custom_target(target_%s", key) + cmakelists:print(" DEPENDS output_%s", key) + cmakelists:print(")") + cmakelists:print("add_dependencies(%s target_%s)", target:name(), key) + else + cmakelists:print("add_custom_command(TARGET %s", target:name()) + if suffix == "after" then + cmakelists:print(" POST_BUILD") + end + cmakelists:print(" COMMAND %s", command_str) + cmakelists:print(" VERBATIM") + cmakelists:print(")") + end end -- add target custom commands for target -- cgit v1.3.1