NMake: Use UTF-8 with BOM if supported by nmake

Fixes: #21792
This commit is contained in:
Amine Najahi 2021-04-22 20:26:30 +01:00 committed by Brad King
parent 186c9bff53
commit f3f57cc4ed
6 changed files with 15 additions and 2 deletions

View File

@ -0,0 +1,5 @@
nmake-utf8
----------
* The :generator:`NMake Makefiles` generator now encodes the generated
makefiles as UTF-8 with a BOM when using ``nmake`` from VS 9 or above.

View File

@ -42,6 +42,11 @@ cmGeneratedFileStream::cmGeneratedFileStream(std::string const& name,
#else
static_cast<void>(encoding);
#endif
if (encoding == codecvt::UTF8_WITH_BOM) {
// Write the BOM encoding header into the file
char magic[] = { char(0xEF), char(0xBB), char(0xBF) };
this->write(magic, 3);
}
}
cmGeneratedFileStream::~cmGeneratedFileStream()

View File

@ -31,7 +31,7 @@ public:
/** Get encoding used by generator for makefile files */
codecvt::Encoding GetMakefileEncoding() const override
{
return codecvt::ANSI;
return this->NMakeSupportsUTF8 ? codecvt::UTF8_WITH_BOM : codecvt::ANSI;
}
/** Get the documentation entry for this generator. */

View File

@ -2058,7 +2058,8 @@ std::string cmMakefileTargetGenerator::CreateResponseFile(
// Create the response file.
std::string responseFileNameFull =
cmStrCat(this->TargetBuildDirectoryFull, '/', name);
cmGeneratedFileStream responseStream(responseFileNameFull);
cmGeneratedFileStream responseStream(
responseFileNameFull, false, this->GlobalGenerator->GetMakefileEncoding());
responseStream.SetCopyIfDifferent(true);
responseStream << options << "\n";

View File

@ -31,6 +31,7 @@ codecvt::codecvt(Encoding e)
// We don't know which ANSI encoding to use for other platforms than
// Windows so we don't do any conversion there
case codecvt::UTF8:
case codecvt::UTF8_WITH_BOM:
// Assume internal encoding is UTF-8
case codecvt::None:
// No encoding

View File

@ -14,6 +14,7 @@ public:
{
None,
UTF8,
UTF8_WITH_BOM,
ANSI
};