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  );
35 }
36 
37 void PushPathIntoEnvironment(const string& env_block) {
38  const char* as_str = env_block.c_str();
39  while (as_str[0]) {
40  if (_strnicmp(as_str, "path=", 5) == 0) {
41  _putenv(as_str);
42  return;
43  } else {
44  as_str = &as_str[strlen(as_str) + 1];
45  }
46  }
47 }
48 
49 void WriteDepFileOrDie(const char* object_path, const CLParser& parse) {
50  string depfile_path = string(object_path) + ".d";
51  FILE* depfile = fopen(depfile_path.c_str(), "w");
52  if (!depfile) {
53  unlink(object_path);
54  Fatal("opening %s: %s", depfile_path.c_str(),
55  GetLastErrorString().c_str());
56  }
57  if (fprintf(depfile, "%s: ", object_path) < 0) {
58  unlink(object_path);
59  fclose(depfile);
60  unlink(depfile_path.c_str());
61  Fatal("writing %s", depfile_path.c_str());
62  }
63  const set<string>& headers = parse.includes_;
64  for (set<string>::const_iterator i = headers.begin();
65  i != headers.end(); ++i) {
66  if (fprintf(depfile, "%s\n", EscapeForDepfile(*i).c_str()) < 0) {
67  unlink(object_path);
68  fclose(depfile);
69  unlink(depfile_path.c_str());
70  Fatal("writing %s", depfile_path.c_str());
71  }
72  }
73  fclose(depfile);
74 }
75 
76 } // anonymous namespace
77 
78 int MSVCHelperMain(int argc, char** argv) {
79  const char* output_filename = NULL;
80  const char* envfile = NULL;
81 
82  const option kLongOptions[] = {
83  { "help", no_argument, NULL, 'h' },
84  { NULL, 0, NULL, 0 }
85  };
86  int opt;
87  while ((opt = getopt_long(argc, argv, "e:o:h", kLongOptions, NULL)) != -1) {
88  switch (opt) {
89  case 'e':
90  envfile = optarg;
91  break;
92  case 'o':
93  output_filename = optarg;
94  break;
95  case 'h':
96  default:
97  Usage();
98  return 0;
99  }
100  }
101 
102  string env;
103  if (envfile) {
104  string err;
105  if (ReadFile(envfile, &env, &err) != 0)
106  Fatal("couldn't open %s: %s", envfile, err.c_str());
107  PushPathIntoEnvironment(env);
108  }
109 
110  char* command = GetCommandLine();
111  command = strstr(command, " -- ");
112  if (!command) {
113  Fatal("expected command line to end with \" -- command args\"");
114  }
115  command += 4;
116 
117  CLWrapper cl;
118  if (!env.empty())
119  cl.SetEnvBlock((void*)env.data());
120  string output;
121  int exit_code = cl.Run(command, &output);
122 
123  if (output_filename) {
124  CLParser parser;
125  output = parser.Parse(output);
126  WriteDepFileOrDie(output_filename, parser);
127  }
128 
129  // CLWrapper's output already as \r\n line endings, make sure the C runtime
130  // doesn't expand this to \r\r\n.
131  _setmode(_fileno(stdout), _O_BINARY);
132  printf("%s", output.c_str());
133 
134  return exit_code;
135 }
#define no_argument
Definition: getopt.h:7
int MSVCHelperMain(int argc, char **argv)
string Parse(const string &output)
Parse the full output of cl, returning the output (if any) that should printed.
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:178
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:51
Visual Studio'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:46
Wraps a synchronous execution of a CL subprocess.
Definition: msvc_helper.h:50
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:55