Ninja
msvc_helper_main-win32.cc
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "msvc_helper.h"
16 
17 #include <fcntl.h>
18 #include <io.h>
19 #include <stdio.h>
20 #include <windows.h>
21 
22 #include "util.h"
23 
24 #include "getopt.h"
25 
26 namespace {
27 
28 void Usage() {
29  printf(
30 "usage: ninja -t msvc [options] -- cl.exe /showIncludes /otherArgs\n"
31 "options:\n"
32 " -e ENVFILE load environment block from ENVFILE as environment\n"
33 " -o FILE write output dependency information to FILE.d\n"
34 " -p STRING localized prefix of msvc's /showIncludes output\n"
35  );
36 }
37 
38 void PushPathIntoEnvironment(const string& env_block) {
39  const char* as_str = env_block.c_str();
40  while (as_str[0]) {
41  if (_strnicmp(as_str, "path=", 5) == 0) {
42  _putenv(as_str);
43  return;
44  } else {
45  as_str = &as_str[strlen(as_str) + 1];
46  }
47  }
48 }
49 
50 void WriteDepFileOrDie(const char* object_path, const CLParser& parse) {
51  string depfile_path = string(object_path) + ".d";
52  FILE* depfile = fopen(depfile_path.c_str(), "w");
53  if (!depfile) {
54  unlink(object_path);
55  Fatal("opening %s: %s", depfile_path.c_str(),
56  GetLastErrorString().c_str());
57  }
58  if (fprintf(depfile, "%s: ", object_path) < 0) {
59  unlink(object_path);
60  fclose(depfile);
61  unlink(depfile_path.c_str());
62  Fatal("writing %s", depfile_path.c_str());
63  }
64  const set<string>& headers = parse.includes_;
65  for (set<string>::const_iterator i = headers.begin();
66  i != headers.end(); ++i) {
67  if (fprintf(depfile, "%s\n", EscapeForDepfile(*i).c_str()) < 0) {
68  unlink(object_path);
69  fclose(depfile);
70  unlink(depfile_path.c_str());
71  Fatal("writing %s", depfile_path.c_str());
72  }
73  }
74  fclose(depfile);
75 }
76 
77 } // anonymous namespace
78 
79 int MSVCHelperMain(int argc, char** argv) {
80  const char* output_filename = NULL;
81  const char* envfile = NULL;
82 
83  const option kLongOptions[] = {
84  { "help", no_argument, NULL, 'h' },
85  { NULL, 0, NULL, 0 }
86  };
87  int opt;
88  string deps_prefix;
89  while ((opt = getopt_long(argc, argv, "e:o:p:h", kLongOptions, NULL)) != -1) {
90  switch (opt) {
91  case 'e':
92  envfile = optarg;
93  break;
94  case 'o':
95  output_filename = optarg;
96  break;
97  case 'p':
98  deps_prefix = optarg;
99  break;
100  case 'h':
101  default:
102  Usage();
103  return 0;
104  }
105  }
106 
107  string env;
108  if (envfile) {
109  string err;
110  if (ReadFile(envfile, &env, &err) != 0)
111  Fatal("couldn't open %s: %s", envfile, err.c_str());
112  PushPathIntoEnvironment(env);
113  }
114 
115  char* command = GetCommandLine();
116  command = strstr(command, " -- ");
117  if (!command) {
118  Fatal("expected command line to end with \" -- command args\"");
119  }
120  command += 4;
121 
122  CLWrapper cl;
123  if (!env.empty())
124  cl.SetEnvBlock((void*)env.data());
125  string output;
126  int exit_code = cl.Run(command, &output);
127 
128  if (output_filename) {
129  CLParser parser;
130  output = parser.Parse(output, deps_prefix);
131  WriteDepFileOrDie(output_filename, parser);
132  }
133 
134  if (output.empty())
135  return exit_code;
136 
137  // CLWrapper's output already as \r\n line endings, make sure the C runtime
138  // doesn't expand this to \r\r\n.
139  _setmode(_fileno(stdout), _O_BINARY);
140  // Avoid printf and C strings, since the actual output might contain null
141  // bytes like UTF-16 does (yuck).
142  fwrite(&output[0], 1, output.size(), stdout);
143 
144  return exit_code;
145 }
#define no_argument
Definition: getopt.h:7
int MSVCHelperMain(int argc, char **argv)
int getopt_long(int argc, char **argv, const char *shortopts, const GETOPT_LONG_OPTION_T *longopts, int *longind)
char * optarg
int ReadFile(const string &path, string *contents, string *err)
Read a file to a string (in text mode: with CRLF conversion on Windows).
Definition: util.cc:282
int Run(const string &command, string *output)
Start a process and gather its raw output.
void Fatal(const char *msg,...)
Log a fatal message and exit.
Definition: util.cc:52
Visual Studio&#39;s cl.exe requires some massaging to work with Ninja; for example, it emits include info...
Definition: msvc_helper.h:26
set< string > includes_
Definition: msvc_helper.h:47
string Parse(const string &output, const string &deps_prefix)
Parse the full output of cl, returning the output (if any) that should printed.
Wraps a synchronous execution of a CL subprocess.
Definition: msvc_helper.h:51
string EscapeForDepfile(const string &path)
void SetEnvBlock(void *env_block)
Set the environment block (as suitable for CreateProcess) to be used by Run().
Definition: msvc_helper.h:56