xmake-io / xmake

🔥 A cross-platform build utility based on Lua
https://xmake.io
Apache License 2.0
10.13k stars 782 forks source link

ninja: error: build.ninja:68: unknown build rule 'cu' #5646

Open dddddjent opened 1 month ago

dddddjent commented 1 month ago

Xmake 版本

2.9.4

操作系统版本和架构

Archlinux

描述问题

xmake project -k ninja 生成的build.ninja没有cuda编译相关的rule,不能正常编译 但是直接xmake build可以

期待的结果

ninja能够成功编译

工程配置

xmake create [project_name]
mv src/main.cpp src/main.cu
add_rules("mode.debug", "mode.release")

target("xmake_cuda_ninja_test")
    set_kind("binary")
    add_files("src/*.cu")

附加信息和错误日志

create ok!

Issues-translate-bot commented 1 month ago

Bot detected the issue body's language is not English, translate it automatically.


Xmake version

2.9.4

Operating system version and architecture

Archlinux

Describe the problem

The build.ninja generated by xmake project -k ninja does not have rules related to cuda compilation and cannot be compiled normally. But you can directly xmake build

Expected results

ninja can compile successfully

Project configuration

xmake create [project_name]
mv src/main.cpp src/main.cu
add_rules("mode.debug", "mode.release")

target("xmake_cuda_ninja_test")
    set_kind("binary")
    add_files("src/*.cu")

Additional information and error logs

Create OK!

waruqi commented 1 month ago

我暂时没有 cuda 环境,你可以自己在这个位置加上 cuda.build 的 rule 试试,然后提个 pr 过来。。

https://github.com/xmake-io/xmake/blob/6295d86eeb3be2761a61ffa1672c0b2ca4492643/xmake/plugins/project/ninja/build_ninja.lua#L38

Issues-translate-bot commented 1 month ago

Bot detected the issue body's language is not English, translate it automatically.


I don’t have a cuda environment for the time being. You can try adding the cuda.build rule to this location and submit a PR. .

https://github.com/xmake-io/xmake/blob/6295d86eeb3be2761a61ffa1672c0b2ca4492643/xmake/plugins/project/ninja/build_ninja.lua#L38

star-hengxing commented 1 month ago

rule name 很早就加了,所以这里加一行就能编译正常 cuda 代码。不过 devlink 不是基于 batchcmd,目前不太好处理。

diff --git a/xmake/plugins/project/ninja/build_ninja.lua b/xmake/plugins/project/ninja/build_ninja.lua
index 2e55faf05..4d913a3ea 100644
--- a/xmake/plugins/project/ninja/build_ninja.lua
+++ b/xmake/plugins/project/ninja/build_ninja.lua
@@ -182,6 +182,14 @@ function _add_rules_for_compiler_windres(ninjafile, sourcekind, program)
     ninjafile:print("")
 end

+-- add rules for complier (cuda/nvcc)
+function _add_rules_for_compiler_cuda(ninjafile, sourcekind, program)
+    ninjafile:print("rule %s", sourcekind)
+    ninjafile:print(" command = %s -c $ARGS $in -o $out", program)
+    ninjafile:print(" description = compiling.%s $in", config.mode())
+    ninjafile:print("")
+end
+
 -- add rules for complier
 function _add_rules_for_compiler(ninjafile)
     ninjafile:print("# rules for compiler")
@@ -203,7 +211,8 @@ function _add_rules_for_compiler(ninjafile)
         ml      = _add_rules_for_compiler_msvc_ml,
         ml64    = _add_rules_for_compiler_msvc_ml,
         rc      = _add_rules_for_compiler_msvc_rc,
-        windres = _add_rules_for_compiler_windres
+        windres = _add_rules_for_compiler_windres,
+        nvcc    = _add_rules_for_compiler_cuda,
     }
     for sourcekind, _ in pairs(language.sourcekinds()) do
         local program, toolname = platform.tool(sourcekind)
Issues-translate-bot commented 1 month ago

Bot detected the issue body's language is not English, translate it automatically.


The rule name was added very early, so adding one line here can compile normal cuda code. However, devlink is not based on batchcmd and is currently not easy to handle.

diff --git a/xmake/plugins/project/ninja/build_ninja.lua b/xmake/plugins/project/ninja/build_ninja.lua
index 2e55faf05..4d913a3ea 100644
--- a/xmake/plugins/project/ninja/build_ninja.lua
+++ b/xmake/plugins/project/ninja/build_ninja.lua
@@ -182,6 +182,14 @@ function _add_rules_for_compiler_windres(ninjafile, sourcekind, program)
     ninjafile:print("")
 end

+-- add rules for complier (cuda/nvcc)
+function _add_rules_for_compiler_cuda(ninjafile, sourcekind, program)
+ ninjafile:print("rule %s", sourcekind)
+ ninjafile:print(" command = %s -c $ARGS $in -o $out", program)
+ ninjafile:print(" description = compiling.%s $in", config.mode())
+ ninjafile:print("")
+end
+
 -- add rules for complier
 function _add_rules_for_compiler(ninjafile)
     ninjafile:print("# rules for compiler")
@@ -203,7 +211,8 @@ function _add_rules_for_compiler(ninjafile)
         ml = _add_rules_for_compiler_msvc_ml,
         ml64 = _add_rules_for_compiler_msvc_ml,
         rc = _add_rules_for_compiler_msvc_rc,
- windres = _add_rules_for_compiler_windres
+ windres = _add_rules_for_compiler_windres,
+ nvcc = _add_rules_for_compiler_cuda,
     }
     for sourcekind, _ in pairs(language.sourcekinds()) do
         local program, toolname = platform.tool(sourcekind)
waruqi commented 3 weeks ago
+-- add rules for complier (cuda/nvcc)
+function _add_rules_for_compiler_cuda(ninjafile, sourcekind, program)
+    ninjafile:print("rule %s", sourcekind)
+    ninjafile:print(" command = %s -c $ARGS $in -o $out", program)
+    ninjafile:print(" description = compiling.%s $in", config.mode())
+    ninjafile:print("")
+end

这个 patch 我加上了,可以先试下

@dddddjent

Issues-translate-bot commented 3 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


+-- add rules for complier (cuda/nvcc)
+function _add_rules_for_compiler_cuda(ninjafile, sourcekind, program)
+ ninjafile:print("rule %s", sourcekind)
+ ninjafile:print(" command = %s -c $ARGS $in -o $out", program)
+ ninjafile:print(" description = compiling.%s $in", config.mode())
+ ninjafile:print("")
+end

I added this patch, you can try it first

@dddddjent