Add tool to print code page information on Windows

Since commit 00459e2b (Use UTF-8 on Windows 10 Version 1903, fix #1195,
2021-02-17), `ninja` does not always expect `build.ninja` to be encoded
in the system's ANSI code page.  The expected encoding now depends on
how `ninja` is built and the version of Windows on which it is running.

Add a `-t wincodepage` tool that generators can use to ask `ninja`
what encoding it expects.

Issue: #1195
This commit is contained in:
Brad King 2021-02-26 09:21:03 -05:00
parent 212cd97679
commit 706e16bee5
2 changed files with 29 additions and 0 deletions

View File

@ -308,6 +308,18 @@ file. _Available since Ninja 1.10._
if they have one). It can be used to know which rule name to pass to if they have one). It can be used to know which rule name to pass to
+ninja -t targets rule _name_+ or +ninja -t compdb+. +ninja -t targets rule _name_+ or +ninja -t compdb+.
`wincodepage`:: available on Windows hosts. Prints the ANSI code page
used by `ninja`, whose encoding is expected in `build.ninja`. Also prints
the Console code page for reference. The output has the form:
+
----
ANSI code page: %u
Console code page: %u
----
+
where each `%u` is an integer code page identifier, expressed in decimal.
_Available since Ninja 1.11._
Writing your own Ninja files Writing your own Ninja files
---------------------------- ----------------------------

View File

@ -133,6 +133,7 @@ struct NinjaMain : public BuildLogUser {
int ToolRestat(const Options* options, int argc, char* argv[]); int ToolRestat(const Options* options, int argc, char* argv[]);
int ToolUrtle(const Options* options, int argc, char** argv); int ToolUrtle(const Options* options, int argc, char** argv);
int ToolRules(const Options* options, int argc, char* argv[]); int ToolRules(const Options* options, int argc, char* argv[]);
int ToolWinCodePage(const Options* options, int argc, char* argv[]);
/// Open the build log. /// Open the build log.
/// @return LOAD_ERROR on error. /// @return LOAD_ERROR on error.
@ -641,6 +642,18 @@ int NinjaMain::ToolRules(const Options* options, int argc, char* argv[]) {
return 0; return 0;
} }
#ifdef _WIN32
int NinjaMain::ToolWinCodePage(const Options* options, int argc, char* argv[]) {
if (argc != 0) {
printf("usage: ninja -t wincodepage\n");
return 1;
}
printf("ANSI code page: %u\n", GetACP());
printf("Console code page: %u\n", GetConsoleOutputCP());
return 0;
}
#endif
enum PrintCommandMode { PCM_Single, PCM_All }; enum PrintCommandMode { PCM_Single, PCM_All };
void PrintCommands(Edge* edge, EdgeSet* seen, PrintCommandMode mode) { void PrintCommands(Edge* edge, EdgeSet* seen, PrintCommandMode mode) {
if (!edge) if (!edge)
@ -1009,6 +1022,10 @@ const Tool* ChooseTool(const string& tool_name) {
Tool::RUN_AFTER_LOGS, &NinjaMain::ToolCleanDead }, Tool::RUN_AFTER_LOGS, &NinjaMain::ToolCleanDead },
{ "urtle", NULL, { "urtle", NULL,
Tool::RUN_AFTER_FLAGS, &NinjaMain::ToolUrtle }, Tool::RUN_AFTER_FLAGS, &NinjaMain::ToolUrtle },
#ifdef _WIN32
{ "wincodepage", "print the Windows ANSI code page identifier",
Tool::RUN_AFTER_FLAGS, &NinjaMain::ToolWinCodePage },
#endif
{ NULL, NULL, Tool::RUN_AFTER_FLAGS, NULL } { NULL, NULL, Tool::RUN_AFTER_FLAGS, NULL }
}; };