From 706e16bee5f6b5fccd284476a9157ecc8c39f12c Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 26 Feb 2021 09:21:03 -0500 Subject: 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 --- doc/manual.asciidoc | 12 ++++++++++++ src/ninja.cc | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/doc/manual.asciidoc b/doc/manual.asciidoc index 0ac0ebb..204cc6d 100644 --- a/doc/manual.asciidoc +++ b/doc/manual.asciidoc @@ -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 +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 ---------------------------- diff --git a/src/ninja.cc b/src/ninja.cc index 96eac1d..3172ee5 100644 --- a/src/ninja.cc +++ b/src/ninja.cc @@ -133,6 +133,7 @@ struct NinjaMain : public BuildLogUser { int ToolRestat(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 ToolWinCodePage(const Options* options, int argc, char* argv[]); /// Open the build log. /// @return LOAD_ERROR on error. @@ -641,6 +642,18 @@ int NinjaMain::ToolRules(const Options* options, int argc, char* argv[]) { 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 }; void PrintCommands(Edge* edge, EdgeSet* seen, PrintCommandMode mode) { if (!edge) @@ -1009,6 +1022,10 @@ const Tool* ChooseTool(const string& tool_name) { Tool::RUN_AFTER_LOGS, &NinjaMain::ToolCleanDead }, { "urtle", NULL, 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 } }; -- cgit v0.12