Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XMake的add_includedirs功能在Keil/C51的环境中未生效 #3215

Closed
lanjackg2003 opened this issue Dec 27, 2022 · 20 comments
Closed

XMake的add_includedirs功能在Keil/C51的环境中未生效 #3215

lanjackg2003 opened this issue Dec 27, 2022 · 20 comments
Labels
Milestone

Comments

@lanjackg2003
Copy link
Contributor

Xmake 版本

v2.7.4+HEAD.8340827ed

操作系统版本和架构

Windows 11 专业版 22H2

描述问题

  • 工程目录文件组织如下
    │─xmake.lua

    ├─inlcude\main.h

    └─src\main.c

  • 相关文件内容

  1. xmake.lua
add_rules("mode.debug", "mode.release")
set_defaultarchs("mcs51")
target("test")
    add_rules("c51.binary")
    set_toolchains("c51")
    set_kind("binary")
    add_includedirs("inlcude")
    add_files("src/*.c")
  1. main.c
#include "main.h"
#pragma NOAREGS
void main()
{
	int i = HEADER_TEST;
	while (1)
	{
		/* code */
	}
}
  1. main.h
#ifndef __MAIN_H_H__
#define __MAIN_H_H__
#define HEADER_TEST                                     0
#endif  
  • 执行编译命令xmake -rv ,得到错误如下
hecking for c51 ... no
checking for C:\Keil_v5\C51\bin\c51 ... ok
checking for bl51 ... C:\Keil_v5\C51\bin\bl51
checking for the linker (ld) ... bl51
checking for c51 ... C:\Keil_v5\C51\bin\c51
checking for the c compiler (cc) ... c51
[ 25%]: cache compiling.release src\main.c
C:\Keil_v5\C51\bin\c51 src\main.c -DNDEBUG
error: 
C51 COMPILER V9.60.0.0 - SN: K1DZC-HAZGGC
COPYRIGHT Copyright (C) 2012 - 2019 ARM Ltd and ARM Germany GmbH. All rights reserved.
*** WARNING C318 IN LINE 1 OF build\.objs\test\windows\mcs51\release\src\main.c.obj: can't open file 'main.h'
*** ERROR C202 IN LINE 7 OF build\.objs\test\windows\mcs51\release\src\main.c.obj: 'HEADER_TEST': undefined identifier

C51 COMPILATION COMPLETE.  1 WARNING(S),  1 ERROR(S)

xmake.lua中的add_includedirs("inlcude")去掉并将main.c代码修改为

#include "inlcude/main.h"
#pragma NOAREGS
void main()
{
	int i = HEADER_TEST;
	while (1)
	{
		/* code */
	}
}

重新执行编译命令xmake -rv ,此次编译通过

[ 25%]: cache compiling.release src\main.c
C:\Keil_v5\C51\bin\c51 src\main.c -DNDEBUG
C51 COMPILER V9.60.0.0 - SN: K1DZC-HAZGGC
COPYRIGHT Copyright (C) 2012 - 2019 ARM Ltd and ARM Germany GmbH. All rights reserved.
C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
[ 50%]: linking.release test
C:\Keil_v5\C51\bin\bl51 build\.objs\test\windows\mcs51\release\src\main.c.obj TO build\windows\mcs51\release\test
checking for bl51.exe ... no
checking for C:\Keil_v5\C51\bin\bl51.exe ... ok
checking for oh51.exe ... no
checking for C:\Keil_v5\C51\bin\oh51.exe ... ok
[ 50%]: generating.release build\windows\mcs51\release\test.hex
[100%]: build ok!

因此,我理解在xmake.lua中通过add_includedirs添加头文件搜索路径并未生效。

期待的结果

使用add_includedirs添加头文件搜索路径,应该可以生效。
无需在代码中添加文件头文件相对路径

工程配置

add_rules("mode.debug", "mode.release")
set_defaultarchs("mcs51")
target("test")
    add_rules("c51.binary")
    set_toolchains("c51")
    set_kind("binary")
    add_includedirs("inlcude")
    add_files("src/*.c")

附加信息和错误日志

D:\xmake_test>xmake -r -vD
checking for platform ... windows
checking for architecture ... mcs51
checking for Microsoft Visual Studio (mcs51) version ... no
checkinfo: cannot runv(clang.exe --version), No such file or directory
checking for clang ... no
checkinfo: cannot runv(dmd.exe --version), No such file or directory
checking for dmd ... no
checkinfo: cannot runv(ldc2.exe --version), No such file or directory
checking for ldc2 ... no
checkinfo: cannot runv(gdc.exe --version), No such file or directory
checking for gdc ... no
checkinfo: cannot runv(zig.exe version), No such file or directory
checking for zig ... no
checkinfo: cannot runv(zig.exe version), No such file or directory
checking for zig ... no
checking for c51 directory ... C:\Keil_v5\C51
checkinfo: @programdir\core\sandbox\modules\os.lua:267: cannot runv(c51.exe test.c), No such file or directory
stack traceback:
    [C]: in function 'error'
    [@programdir\core\base\os.lua:897]:
    [@programdir\core\sandbox\modules\os.lua:267]: in function 'runv'
    [@programdir\modules\detect\tools\find_c51.lua:34]:
    [C]: in function 'xpcall'
    [@programdir\core\base\utils.lua:280]:
    [...\core\sandbox\modules\import\lib\detect\find_program.lua:65]: in function '_do_check'
    [...\core\sandbox\modules\import\lib\detect\find_program.lua:88]: in function '_check'
    [...\core\sandbox\modules\import\lib\detect\find_program.lua:237]: in function '_find'
    [...\core\sandbox\modules\import\lib\detect\find_program.lua:304]:
    [@programdir\modules\detect\tools\find_c51.lua:56]:
    [@programdir\modules\lib\detect\find_tool.lua:33]: in function '_find_from_modules'
    [@programdir\modules\lib\detect\find_tool.lua:48]: in function '_find_tool'
    [@programdir\modules\lib\detect\find_tool.lua:100]:
    [@programdir\toolchains\c51\xmake.lua:37]:
    [C]: in function 'xpcall'
    [@programdir\core\base\utils.lua:280]:
    [@programdir\core\tool\toolchain.lua:259]: in function 'check'
    [@programdir\actions\config\main.lua:140]: in function '_check_target_toolchains'
    [@programdir\actions\config\main.lua:486]:
    [C]: in function 'xpcall'
    [@programdir\core\base\utils.lua:280]:
    [@programdir\core\base\task.lua:501]: in function 'run'
    [@programdir\core\sandbox\modules\import\core\base\task.lua:63]: in function 'run'
    [@programdir\actions\build\main.lua:139]:
    [C]: in function 'xpcall'
    [@programdir\core\base\utils.lua:280]:
    [@programdir\core\base\task.lua:501]: in function 'run'
    [@programdir\core\main.lua:278]: in function 'cotask'
    [@programdir\core\base\scheduler.lua:404]:

checking for c51 ... no
checking for C:\Keil_v5\C51\bin\c51 ... ok
checking for bl51 ... C:\Keil_v5\C51\bin\bl51
checking for the linker (ld) ... bl51
checking for c51 ... C:\Keil_v5\C51\bin\c51
checking for the c compiler (cc) ... c51
[ 25%]: cache compiling.release src\main.c
C:\Keil_v5\C51\bin\c51 src\main.c -DNDEBUG
error: 
C51 COMPILER V9.60.0.0 - SN: K1DZC-HAZGGC
COPYRIGHT Copyright (C) 2012 - 2019 ARM Ltd and ARM Germany GmbH. All rights reserved.
*** WARNING C318 IN LINE 1 OF build\.objs\test\windows\mcs51\release\src\main.c.obj: can't open file 'main.h'
*** ERROR C202 IN LINE 7 OF build\.objs\test\windows\mcs51\release\src\main.c.obj: 'HEADER_TEST': undefined identifier

C51 COMPILATION COMPLETE.  1 WARNING(S),  1 ERROR(S)

stack traceback:
        [C]: in function 'error'
        @programdir\core\base\os.lua:901: in function 'base/os.raiselevel'
        (...tail calls...)
        @programdir\modules\private\async\runjobs.lua:256: in field 'catch'
        @programdir\core\sandbox\modules\try.lua:123: in global 'try'
        @programdir\modules\private\async\runjobs.lua:218: in upvalue 'cotask'
        @programdir\core\base\scheduler.lua:404: in function <@programdir\core\base\scheduler.lua:397>
@waruqi
Copy link
Member

waruqi commented Dec 27, 2022

https://github.com/xmake-io/xmake/blob/master/xmake/modules/core/tools/c51.lua

参考 gcc 里面的

function nf_includedir(self, dir)
提个 pr 过来

@lanjackg2003
Copy link
Contributor Author

https://github.com/xmake-io/xmake/blob/master/xmake/modules/core/tools/c51.lua

参考 gcc 里面的

function nf_includedir(self, dir)

提个 pr 过来

刚刚尝试修改了我自己的c51.lua文件

-- make the includedir flag
function nf_includedir(self, dir)
	local paths = '(' .. dir .. ')'
	cprint("${color.warning}%s", paths)
    return {"INCDIR" .. path.translate(paths)}
end

但是发现在编译的时候,参数被强制转换为字符串参数C:\Keil_v5\C51\bin\c51 src\main.c "INCDIR(inlcude)" -DNDEBUG,
更多输出如下

D:\xmake_test>xmake -rvD 
(inlcude)
[ 25%]: cache compiling.release src\main.c
C:\Keil_v5\C51\bin\c51 src\main.c "INCDIR(inlcude)" -DNDEBUG
error: 
C51 COMPILER V9.60.0.0 - SN: K1DZC-HAZGGC
COPYRIGHT Copyright (C) 2012 - 2019 ARM Ltd and ARM Germany GmbH. All rights reserved.
*** WARNING C318 IN LINE 1 OF build\.objs\test\windows\mcs51\release\src\main.c.obj: can't open file 'main.h'
*** ERROR C202 IN LINE 7 OF build\.objs\test\windows\mcs51\release\src\main.c.obj: 'HEADER_TEST': undefined identifier

C51 COMPILATION COMPLETE.  1 WARNING(S),  1 ERROR(S)

stack traceback:
        [C]: in function 'error'
        @programdir\core\base\os.lua:901: in function 'os.raiselevel'
        (...tail calls...)
        @programdir\modules\private\async\runjobs.lua:256: in field 'catch'
        @programdir\core\sandbox\modules\try.lua:123: in global 'try'
        @programdir\modules\private\async\runjobs.lua:218: in upvalue 'cotask'
        @programdir\core\base\scheduler.lua:404: in function <@programdir\core\base\scheduler.lua:397>

如果把c51.lua中的
local paths = '(' .. dir .. ')'
修改为
local paths = '[' .. dir .. ']'
相关输出参数变为C:\Keil_v5\C51\bin\c51 src\main.c INCDIR[inlcude] -DNDEBUG

D:\xmake_test>xmake -rvD 
[inlcude]
[ 25%]: cache compiling.release src\main.c
C:\Keil_v5\C51\bin\c51 src\main.c INCDIR[inlcude] -DNDEBUG
error: 
C51 COMPILER V9.60.0.0 - SN: K1DZC-HAZGGC
COPYRIGHT Copyright (C) 2012 - 2019 ARM Ltd and ARM Germany GmbH. All rights reserved.
*** WARNING C318 IN LINE 1 OF build\.objs\test\windows\mcs51\release\src\main.c.obj: can't open file 'main.h'
*** ERROR C202 IN LINE 7 OF build\.objs\test\windows\mcs51\release\src\main.c.obj: 'HEADER_TEST': undefined identifier

C51 COMPILATION COMPLETE.  1 WARNING(S),  1 ERROR(S)

stack traceback:
        [C]: in function 'error'
        @programdir\core\base\os.lua:901: in function 'os.raiselevel'
        (...tail calls...)
        @programdir\modules\private\async\runjobs.lua:256: in field 'catch'
        @programdir\core\sandbox\modules\try.lua:123: in global 'try'
        @programdir\modules\private\async\runjobs.lua:218: in upvalue 'cotask'
        @programdir\core\base\scheduler.lua:404: in function <@programdir\core\base\scheduler.lua:397>

似乎在其他的地方的代码会对小括号特别处理.


另外还有一个问题是nf_includedir使用接口无法实现c51_incdir要求的INCDIR(C:\MYINC;C:\CHIP_DIR)格式

因为只要add_includedirs只要增加一个项,nf_includedir就会被执行一次,那么它就会出现多个INCDIR的标记
如以下测试

  • xmake.lua
add_rules("mode.debug", "mode.release")
set_defaultarchs("mcs51")
target("test")
    add_rules("c51.binary")
    set_toolchains("c51")
    set_kind("binary")
    add_includedirs("inlcude","inlcude1")
    add_files("src/*.c")
  • 输出日志
[inlcude]
[inlcude1]
[ 25%]: cache compiling.release src\main.c
C:\Keil_v5\C51\bin\c51 src\main.c INCDIR[inlcude] INCDIR[inlcude1] -DNDEBUG
error: 
C51 COMPILER V9.60.0.0 - SN: K1DZC-HAZGGC
COPYRIGHT Copyright (C) 2012 - 2019 ARM Ltd and ARM Germany GmbH. All rights reserved.
*** WARNING C318 IN LINE 1 OF build\.objs\test\windows\mcs51\release\src\main.c.obj: can't open file 'main.h'
*** ERROR C202 IN LINE 7 OF build\.objs\test\windows\mcs51\release\src\main.c.obj: 'HEADER_TEST': undefined identifier

C51 COMPILATION COMPLETE.  1 WARNING(S),  1 ERROR(S)

@waruqi
Copy link
Member

waruqi commented Dec 27, 2022

你改成 nf_includedirs 就能取到 dirs ,一个列表

@lanjackg2003
Copy link
Contributor Author

你改成 nf_includedirs 就能取到 dirs ,一个列表

使用这个已经解决了列表问题

但是参数在小括号情况下被强制增加引号的问题,有什么方法解决

C:\Keil_v5\C51\bin\c51 src\main.c "INCDIR(inlcude;inlcude1)" -DNDEBUG

@waruqi
Copy link
Member

waruqi commented Dec 27, 2022

你加个 {} wrap 下再返回,return {"xxxxxx"}

@lanjackg2003
Copy link
Contributor Author

是的,我有参考GCC使用花括符进行包括
但是没有作用,以下是我的代码

-- make the nf_includedirs flag
function nf_includedirs(self, dirs)
	if #dirs == 0 then
		return ""
	end
	local paths = "("
	for key, value in pairs(dirs) do
		if key > 1 then
			paths = paths .. ";"
		end
		paths = paths .. path.translate(value)
	end
	paths = paths .. ")"
	return {"INCDIR" .. paths}
end

得出的结果就是如上所示。

@lanjackg2003
Copy link
Contributor Author

如果我将脚本代码稍微修改为如下

-- make the nf_includedirs flag
function nf_includedirs(self, dirs)
	if #dirs == 0 then
		return ""
	end
	local paths = "["
	for key, value in pairs(dirs) do
		if key > 1 then
			paths = paths .. ";"
		end
		paths = paths .. path.translate(value)
	end
	paths = paths .. "]"
	return {"INCDIR" .. paths}
end

重新编译代码,则输出是正常的
C:\Keil_v5\C51\bin\c51 src\main.c INCDIR[inlcude;sdaf] -DNDEBUG

@waruqi
Copy link
Member

waruqi commented Dec 27, 2022

嗯,目前内部对 () 会自动加 "" 转义,这个涉及的东西就多了,不太好搞,要花时间细调下的。。

@lanjackg2003
Copy link
Contributor Author

嗯,目前内部对 () 会自动加 "" 转义,这个涉及的东西就多了,不太好搞,要花时间细调下的。。

OK
那等你那边调试完毕后,可以给我一个分支。
我可以先验证一下

@waruqi
Copy link
Member

waruqi commented Dec 27, 2022

嗯 等后两天我看下

@waruqi
Copy link
Member

waruqi commented Dec 28, 2022

我测了下,加个 "" 并不会影响什么,一样可以 work,你这不生效?

我这边测试 includedir/defines 带 () 都没什么问题

image

@waruqi
Copy link
Member

waruqi commented Dec 28, 2022

dev 版本,我改了下,你也可以实时,不过我觉得即使啥也不改,就算是带了 "",也应该没啥问题才对,没什么影响

@lanjackg2003
Copy link
Contributor Author

dev 版本,我改了下,你也可以实时,不过我觉得即使啥也不改,就算是带了 "",也应该没啥问题才对,没什么影响

ok,我试试

waruqi added a commit that referenced this issue Dec 29, 2022
@waruqi
Copy link
Member

waruqi commented Dec 29, 2022

我已经加上了,试下

xmake update dev
xmake update -s dev

@waruqi waruqi added this to the v2.7.5 milestone Dec 29, 2022
@lanjackg2003
Copy link
Contributor Author

我测了下,加个 "" 并不会影响什么,一样可以 work,你这不生效?

我这边测试 includedir/defines 带 () 都没什么问题

image

刚刚验证了,确实是有没有引号没有影响

@lanjackg2003
Copy link
Contributor Author

我已经加上了,试下

xmake update dev
xmake update -s dev

刚刚测试,发现问题仍然存在。
然后我研究了这个问题
看一下c51.lua的写法,在编译的时代码将*.c文件拷贝成*.c.obj,然后再进行编译
编译的时候虽然日志输出编译的源文件,但是从输出的"*.lst"文件看,编译的文件却是*.c.obj,并且也未将INCDIR参数传递到里面
如下 截图所示
图片

这就是add_includedirs加了INCDIR`参数也加了,都不行的原因

@lanjackg2003
Copy link
Contributor Author

lanjackg2003 commented Dec 29, 2022

另外,C51可以通过OBJECT修改编译中间文件的路径及后缀名字
如以下命令,编译输出mian.o中间文件

C:\Keil_v5\C51\bin\c51 src\main.c INCDIR(inlcude) OBJECT(build\.objs\test\windows\mcs51\release\src\main.o) 

图片

@waruqi
Copy link
Member

waruqi commented Dec 29, 2022

return self:program(), table.join(sourcefile, flags)

这里把 objectfile 放进 OBJECT()

然后这里的逻辑去掉

local realsourcefile = objectfile:gsub("\\.obj$", "")

@lanjackg2003
Copy link
Contributor Author

return self:program(), table.join(sourcefile, flags)

这里把 objectfile 放进 OBJECT()

然后这里的逻辑去掉

local realsourcefile = objectfile:gsub("\\.obj$", "")

是的,我已经修改了一个版本
我再验证有没有其他问题。
再创建一个PR给你

lanjackg2003 added a commit to lanjackg2003/xmake that referenced this issue Dec 29, 2022
lanjackg2003 added a commit to lanjackg2003/xmake that referenced this issue Dec 29, 2022
@lanjackg2003
Copy link
Contributor Author

lanjackg2003 commented Dec 29, 2022

图片

@waruqi waruqi closed this as completed Dec 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants