xmake-io / xmake

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

windows 环境下,生成 CMakeLists.txt 文件,使用 cmake + Ninja + tcc 编译时报错 #5448

Closed ZCB1208 closed 3 months ago

ZCB1208 commented 3 months ago

Is your feature request related to a problem? Please describe.

  1. 生成 cmakelist 文件 xmake project -k cmakelists 可以正常生成 CMakeList.txt 文件

  2. 然后创建 cmake_build 文件夹,并进入 cmake_build 文件夹: cd cmake_build

  3. 使用命令调用 cmake 编译 cmakelist 文件 cmake .. -G Ninja

  4. 此时报错信息如下: ` The C compiler identification is unknown CMake Error at CMakeLists.txt:11 (project): No CMAKE_C_COMPILER could be found.

The C compiler identification is unknown CMake Error at CMakeLists.txt:11 (project): No CMAKE_C_COMPILER could be found.

Configuring incomplete, errors occurred!`

无法进行正常编译生成 ninja 文件

Describe the solution you'd like

set(CMAKE_C_COMPILER "tcc") 这一行,移动至 CMakeLists.txt 最上面,即可解决问题

临时解决,让cmake 通过编译可以采用这种方式

Describe alternatives you've considered

此种解决方案,在下一次生成 cmakelists 文件后又会无法解决,故而需要对 生成 cmakelist 文件的脚本进行修改

在文件 xmake\plugins\project\cmake\cmakelists.lua 文件中增加如下函数

function _add_CMAKE_C_COMPILER_first(cmakelists, target)
    cmakelists:print("# tmp fix tcc use xmake create cmakelists bug ")
    if config.get("toolchain") or target:get("toolchains") then
        local cc = target:tool("cc")
        if cc then
            cc = path.unix(cc)
            cmakelists:print("set(CMAKE_C_COMPILER \"%s\")", cc)
        end
    end
    cmakelists:print("")
end

再在函数_generate_cmakelists 中增加调用

-- _add CMAKE_C_COMPILER first 
    for _, target in table.orderpairs(project.targets()) do
        _add_CMAKE_C_COMPILER_first(cmakelists, target)
    end

原则上需要对下面生成的set(CMAKE_C_COMPILER "tcc") 内容注释掉,但是实际测试,不注释也没有问题, 暂时用这样不修改原有生成脚本的基础上可以解决问题

Additional context

image

image

image

Issues-translate-bot commented 3 months ago

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


Title: windows environment, the CMakeLists.txt file is generated, and an error is reported when using cmake + Ninja + tcc to compile.

waruqi commented 3 months ago

xmake 可以直接编译干嘛不用? xmake 可以直接生成 build ninja 干嘛不用?要这么饶两层?

另外,为啥移动至 CMakeLists.txt 最上面就可以,这不应该是 cmake 的 issues 么,为啥要改 xmake。。

Issues-translate-bot commented 3 months ago

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


Why not use xmake if you can compile it directly? xmake can directly generate build ninja, why not use it? Do you want to spare two layers like this?

In addition, why can it be moved to the top of CMakeLists.txt? Shouldn't this be an issue of cmake? Why should xmake be changed? .

ZCB1208 commented 3 months ago

是的,确实是绕了两层,非常无奈公司要求就是必须要用使用 cmake + ninja ,原因在于国外的团队开发组用了这个组合,所以要求我们也要切换至这样。

虽然极力推荐 使用 xmake ,并阐述了个人的几个看法,无奈人微言轻,只能被迫这样的兼容设计,自己在开发与测试的时候,都是直接使用 xmake 进行的,方便很多。 目前在本地开发以及 cicd 测试的时候,我尽量都是使用 xmake 二 cmake 只是作为一个补充,只是为了符合公司要求而已。

虽然这确实是 cmake 的问题,但是目前我只能这样选择去做兼容,最终选择做出这样的提交,希望能为与我一样处境的开发者提供一些便利,确实找这个问题也是花费了一些时间,并不许想后续如我类似的开发者在遇到这样的问题就放弃l 这么好的工具,希望未来 xmake 能成为开发者的首选。看看我们国人做的多么优秀的工具,对洋人,洋工具能祛魅。

Issues-translate-bot commented 3 months ago

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


Yes, it is indeed two layers of detours. It is very frustrating that the company requires that cmake + ninja must be used. The reason is that the foreign team development team uses this combination, so we are required to switch to this.

Although I strongly recommend the use of xmake and elaborated on several personal opinions, I have no choice but to speak lightly and can only be forced to adopt such a compatible design. When developing and testing, I directly use xmake, which is much more convenient. At present, when developing locally and testing cicd, I try to use xmake and cmake as a supplement, just to meet the company's requirements.

Although this is indeed a problem with cmake, at present I can only choose to make it compatible. I finally chose to make this submission, hoping to provide some convenience to developers in the same situation as me. It did take some time to find this problem. , and I don’t think that developers like me will give up such a good tool when encountering such problems. I hope that xmake will become the first choice of developers in the future. Look at the excellent tools our Chinese make. For foreigners, foreign tools can dispel their charm.

waruqi commented 3 months ago

这可能是由于 project( C) 里面有 language 设置,cmake 解析到这直接开始探测编译器了。所以 set compilers 得放到 project 之前才行。

可以试下这个 patch https://github.com/xmake-io/xmake/pull/5449

不过你这种搞法,之后会碰到很多问题的,有些特性问题 就没那么好规避了,毕竟多绕一层,就会损失一些 xmake 原有的特性,导致差异,还饶了两层。

尤其是一些 rules 目前还无法很好适配 project generator ,不光只是 cmakelists generator.

waruqi commented 3 months ago

既然你这公司硬性规避不让用,个人又想要用 xmake,其实个人建议,你通过 xmake 生成初始 cmakelists.txt 后,自己改改,然后同时维护,cmakelists.txt 和 xmake.lua 。。自己平常用 xmake.lua ,对外提供 cmakelists.txt 互不冲突,也不用每次去生成,后期也就是加加 files flags啥的 。

我自己也是这么搞得。

Issues-translate-bot commented 3 months ago

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


This may be because there is a language setting in project(C), and cmake parses this and starts detecting the compiler directly. So set compilers must be placed before project.

You can try this patch https://github.com/xmake-io/xmake/pull/5449

However, you will encounter many problems in the future with this approach, and some feature issues are not so easy to avoid. After all, if you go around one more layer, you will lose some of xmake's original features, resulting in differences, and you will save two layers.

In particular, some rules are currently not well adapted to the project generator, not just the cmakelists generator.

Issues-translate-bot commented 3 months ago

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


Since your company strictly avoids using it, and I personally want to use xmake, I personally suggest that after you generate the initial cmakelists.txt through xmake, you can modify it yourself, and then maintain cmakelists.txt and xmake.lua at the same time. . I usually use xmake.lua, and the external cmakelists.txt provided does not conflict with each other, and there is no need to generate it every time. In the later stage, I just add files flags and so on.

That's what I did myself.

waruqi commented 3 months ago

我 merge 了,xmake update -s dev

Issues-translate-bot commented 3 months ago

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


I merged, xmake update -s dev

ZCB1208 commented 3 months ago

既然你这公司硬性规避不让用,个人又想要用 xmake,其实个人建议,你通过 xmake 生成初始 cmakelists.txt 后,自己改改,然后同时维护,cmakelists.txt 和 xmake.lua 。。自己平常用 xmake.lua ,对外提供 cmakelists.txt 互不冲突,也不用每次去生成,后期也就是加加 files flags啥的 。

我自己也是这么搞得。

嗯呢,很好的建议,感谢,我也还是维护两份好了

Issues-translate-bot commented 3 months ago

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


Since your company strictly avoids using it, and I personally want to use xmake, I personally suggest that after generating the initial cmakelists.txt through xmake, you can modify it yourself, and then maintain cmakelists.txt and xmake.lua at the same time. . I usually use xmake.lua, and the cmakelists.txt provided to the outside world does not conflict with each other, and there is no need to generate it every time. In the later stage, I just add files flags and so on.

I did the same thing myself.

Well, that’s a very good suggestion. Thank you. I’ll just maintain two copies.