summaryrefslogtreecommitdiffstats
path: root/src/msvc_helper_main-win32.cc
diff options
context:
space:
mode:
authorEvan Martin <martine@danga.com>2012-08-15 03:59:21 (GMT)
committerEvan Martin <martine@danga.com>2012-08-15 04:01:22 (GMT)
commitac04abe2f9c87afe4e4d43ac63e5af2dd10376fb (patch)
tree4c76804459e399ad3d61571920622a3e4250e48b /src/msvc_helper_main-win32.cc
parent59e0d69ec2775f1aa46d87ad7d14e6985e5187b6 (diff)
downloadNinja-ac04abe2f9c87afe4e4d43ac63e5af2dd10376fb.zip
Ninja-ac04abe2f9c87afe4e4d43ac63e5af2dd10376fb.tar.gz
Ninja-ac04abe2f9c87afe4e4d43ac63e5af2dd10376fb.tar.bz2
add a helper binary for wrapping cl.exe
Modify bootstrap etc. to make use of this binary.
Diffstat (limited to 'src/msvc_helper_main-win32.cc')
-rw-r--r--src/msvc_helper_main-win32.cc115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/msvc_helper_main-win32.cc b/src/msvc_helper_main-win32.cc
new file mode 100644
index 0000000..f265010
--- /dev/null
+++ b/src/msvc_helper_main-win32.cc
@@ -0,0 +1,115 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "msvc_helper.h"
+
+#include <windows.h>
+
+#include "util.h"
+
+#include "getopt.h"
+
+namespace {
+
+void Usage() {
+ printf(
+"ninja-msvc-helper: adjust msvc command-line tools for use by ninja.\n"
+"\n"
+"usage: ninja-mvsc-helper [options] -- command args\n"
+"options:\n"
+" -e ENVFILE load environment block from ENVFILE as environment\n"
+" -r BASE normalize paths and make relative to BASE before output\n"
+" -o FILE write output dependency information to FILE.d\n"
+ );
+}
+
+void PushPathIntoEnvironment(const string& env_block) {
+ const char* as_str = env_block.c_str();
+ while (as_str[0]) {
+ if (_strnicmp(as_str, "path=", 5) == 0) {
+ _putenv(as_str);
+ return;
+ } else {
+ as_str = &as_str[strlen(as_str) + 1];
+ }
+ }
+}
+
+} // anonymous namespace
+
+int main(int argc, char** argv) {
+ const char* output_filename = NULL;
+ const char* relative_to = NULL;
+ const char* envfile = NULL;
+
+ const option kLongOptions[] = {
+ { "help", no_argument, NULL, 'h' },
+ { NULL, 0, NULL, 0 }
+ };
+ int opt;
+ while ((opt = getopt_long(argc, argv, "e:o:r:h", kLongOptions, NULL)) != -1) {
+ switch (opt) {
+ case 'e':
+ envfile = optarg;
+ break;
+ case 'o':
+ output_filename = optarg;
+ break;
+ case 'r':
+ relative_to = optarg;
+ break;
+ case 'h':
+ default:
+ Usage();
+ return 0;
+ }
+ }
+
+ if (!output_filename)
+ Fatal("-o required");
+
+ string env;
+ if (envfile) {
+ string err;
+ if (ReadFile(envfile, &env, &err) != 0)
+ Fatal("couldn't open %s: %s", envfile, err.c_str());
+ PushPathIntoEnvironment(env);
+ }
+
+ char* command = GetCommandLine();
+ command = strstr(command, " -- ");
+ if (!command) {
+ Fatal("expected command line to end with \" -- command args\"");
+ }
+ command += 4;
+
+ CLWrapper cl;
+ if (!env.empty())
+ cl.SetEnvBlock((void*)env.data());
+ int exit_code = cl.Run(command);
+
+ string depfile = string(output_filename) + ".d";
+ FILE* output = fopen(depfile.c_str(), "w");
+ if (!output) {
+ Fatal("opening %s: %s", depfile.c_str(), GetLastErrorString().c_str());
+ }
+ fprintf(output, "%s: ", output_filename);
+ for (vector<string>::iterator i = cl.includes_.begin();
+ i != cl.includes_.end(); ++i) {
+ fprintf(output, "%s\n", i->c_str());
+ }
+ fclose(output);
+
+ return exit_code;
+}