Skip to content

Commit

Permalink
Add tool to print code page information on Windows
Browse files Browse the repository at this point in the history
Since commit 00459e2 (Use UTF-8 on Windows 10 Version 1903, fix ninja-build#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: ninja-build#1195
  • Loading branch information
bradking committed Feb 26, 2021
1 parent 100efba commit a1db7de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/manual.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ 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. _Available since Ninja 1.11._
Writing your own Ninja files
----------------------------
Expand Down
17 changes: 17 additions & 0 deletions src/ninja.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 }
};

Expand Down

0 comments on commit a1db7de

Please sign in to comment.