理论上写中文注释是没问题的,因为 VS 是支持中文的。但是,很多用于 Build 的工具并没有考虑这种情况,因此会导致稀奇古怪的问题。最近我遇到了一个编译错误
C:\BuildBs\201903>build -a X64
Build environment: Windows-10-10.0.16299-SP0
Build start time: 09:15:25, Jun.02 2019
WORKSPACE = c:\buildbs\201903
EDK_TOOLS_PATH = c:\buildbs\201903\basetools
EDK_TOOLS_BIN = c:\buildbs\201903\basetools\bin\win32
CONF_PATH = c:\buildbs\201903\conf
PYTHON_COMMAND = py -3
Architecture(s) = X64
Build target = DEBUG
Toolchain = VS2015x86
Active Platform = c:\buildbs\201903\Nt32Pkg\Nt32Pkg.dsc
Flash Image Definition = c:\buildbs\201903\Nt32Pkg\Nt32Pkg.fdf
Processing meta-data .....
build.py...
: error C0DE: Unknown fatal error when processing [c:\buildbs\201903\MdeModulePkg\Application\UiApp\UiApp.inf]
(Please send email to edk2-devel@lists.01.org for help, attaching following call stack trace!)
(Python 3.6.6 on win32) Traceback (most recent call last):
File "C:\BuildBs\201903\BaseTools\Source\Python\build\build.py", line 2387, in Main
MyBuild.Launch()
File "C:\BuildBs\201903\BaseTools\Source\Python\build\build.py", line 2141, in Launch
self._MultiThreadBuildPlatform()
File "C:\BuildBs\201903\BaseTools\Source\Python\build\build.py", line 1967, in _MultiThreadBuildPlatform
Ma.CreateCodeFile(True)
File "C:\BuildBs\201903\BaseTools\Source\Python\AutoGen\AutoGen.py", line 4042, in CreateCodeFile
for File in self.AutoGenFileList:
File "C:\BuildBs\201903\BaseTools\Source\Python\Common\caching.py", line 34, in __get__
Value = obj.__dict__[self._function.__name__] = self._function(obj)
File "C:\BuildBs\201903\BaseTools\Source\Python\AutoGen\AutoGen.py", line 3295, in AutoGenFileList
GenC.CreateCode(self, AutoGenC, AutoGenH, StringH, AutoGenUniIdf, UniStringBinBuffer, StringIdf, AutoGenUniIdf, IdfGenBinBuffer)
File "C:\BuildBs\201903\BaseTools\Source\Python\AutoGen\GenC.py", line 2048, in CreateCode
CreateUnicodeStringCode(Info, AutoGenC, StringH, UniGenCFlag, UniGenBinBuffer)
File "C:\BuildBs\201903\BaseTools\Source\Python\AutoGen\GenC.py", line 1711, in CreateUnicodeStringCode
Header, Code = GetStringFiles(Info.UnicodeFileList, SrcList, IncList, Info.IncludePathList, ['.uni', '.inf'], Info.Name, CompatibleMode, ShellMode, UniGenCFlag, UniGenBinBuffer, FilterInfo)
File "C:\BuildBs\201903\BaseTools\Source\Python\AutoGen\StrGather.py", line 565, in GetStringFiles
Uni = SearchString(Uni, sorted (FileList), IsCompatibleMode)
File "C:\BuildBs\201903\BaseTools\Source\Python\AutoGen\StrGather.py", line 537, in SearchString
for Line in Lines:
File "C:\Python36\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 134: character maps to <undefined>
从错误上来看,发生在 c:\buildbs\201903\MdeModulePkg\Application\UiApp\UiApp.inf 文件中,反复检查该文件无法确定问题点。
最终比对相同文件的不同版本,确定错误是由于我在 \MdeModulePkg\Application\UiApp\FrontPage.c 函数 InitializeUserInterface 添加的一行注释导致的
EFI_STATUS
EFIAPI
InitializeUserInterface (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_HII_HANDLE HiiHandle;
EFI_STATUS Status;
EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *SimpleTextOut;
UINTN BootTextColumn;
UINTN BootTextRow;
//生成一个随机的字符串
if (!mModeInitialized) {
猜测可能的原因是 build 工具(不是VS 编译工具),在预处理 FrontPage.c 时无法正确处理注释中的中文导致的。因此,在编写代码的时候,尽量不要使用中文作为注释,以免遇到这样的问题。
是编码的问题。你看 Python 的调用栈就知道了。最后一个是尝试用 cp1252 编码解码你的文件,然后说无法解码。
Python 编码参见:
https://stackoverflow.com/a/9233174/1190191
看上去这个工具链是开源的,直接去报 bug 就行,要求加入更多编码。
Google 一下能找到源代码。
https://github.com/tianocore/buildtools-BaseTools/blob/master/Source/Python/AutoGen/StrGather.py
572 行那个 open 没有指定编码。问题应该是来源于此。
另外跨平台的涉及非 ASCII 字符,最好用 utf-8 编码。如果统一用 utf-8,那实现的时候只需要考虑这一种编码(Linux 和 Mac OS 大都用 utf-8)。
https://stackoverflow.com/q/41335199/1190191
谢谢哈,有空我研究一下