summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-10-28 23:31:06 +0800
committerruki <[email protected]>2021-10-28 23:31:06 +0800
commit6f1d565decf6623cad7c56ff3d1badcc2f40e6d4 (patch)
tree9e3f1313682b0e9e9c92e85bd87ec76775138573
parentee15c74a2d73fc8df4a11efdc2dd4848807ffe27 (diff)
improve pre-build for cmake custom command
-rw-r--r--CHANGELOG.md2
-rw-r--r--xmake/plugins/project/cmake/cmakelists.lua32
2 files changed, 26 insertions, 8 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")
+ 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
- cmakelists:print(" COMMAND %s", os.args(command))
- cmakelists:print(" VERBATIM")
- cmakelists:print(")")
end
-- add target custom commands for target