|
| 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 <windows.h> |
| 18 | + |
| 19 | +#include "util.h" |
| 20 | + |
| 21 | +#include "getopt.h" |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +void Usage() { |
| 26 | + printf( |
| 27 | +"ninja-msvc-helper: adjust msvc command-line tools for use by ninja.\n" |
| 28 | +"\n" |
| 29 | +"usage: ninja-mvsc-helper [options] -- command args\n" |
| 30 | +"options:\n" |
| 31 | +" -e ENVFILE load environment block from ENVFILE as environment\n" |
| 32 | +" -r BASE normalize paths and make relative to BASE before output\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 | +} // anonymous namespace |
| 50 | + |
| 51 | +int main(int argc, char** argv) { |
| 52 | + const char* output_filename = NULL; |
| 53 | + const char* relative_to = NULL; |
| 54 | + const char* envfile = NULL; |
| 55 | + |
| 56 | + const option kLongOptions[] = { |
| 57 | + { "help", no_argument, NULL, 'h' }, |
| 58 | + { NULL, 0, NULL, 0 } |
| 59 | + }; |
| 60 | + int opt; |
| 61 | + while ((opt = getopt_long(argc, argv, "e:o:r:h", kLongOptions, NULL)) != -1) { |
| 62 | + switch (opt) { |
| 63 | + case 'e': |
| 64 | + envfile = optarg; |
| 65 | + break; |
| 66 | + case 'o': |
| 67 | + output_filename = optarg; |
| 68 | + break; |
| 69 | + case 'r': |
| 70 | + relative_to = optarg; |
| 71 | + break; |
| 72 | + case 'h': |
| 73 | + default: |
| 74 | + Usage(); |
| 75 | + return 0; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (!output_filename) |
| 80 | + Fatal("-o required"); |
| 81 | + |
| 82 | + string env; |
| 83 | + if (envfile) { |
| 84 | + string err; |
| 85 | + if (ReadFile(envfile, &env, &err) != 0) |
| 86 | + Fatal("couldn't open %s: %s", envfile, err.c_str()); |
| 87 | + PushPathIntoEnvironment(env); |
| 88 | + } |
| 89 | + |
| 90 | + char* command = GetCommandLine(); |
| 91 | + command = strstr(command, " -- "); |
| 92 | + if (!command) { |
| 93 | + Fatal("expected command line to end with \" -- command args\""); |
| 94 | + } |
| 95 | + command += 4; |
| 96 | + |
| 97 | + CLWrapper cl; |
| 98 | + if (!env.empty()) |
| 99 | + cl.SetEnvBlock((void*)env.data()); |
| 100 | + int exit_code = cl.Run(command); |
| 101 | + |
| 102 | + string depfile = string(output_filename) + ".d"; |
| 103 | + FILE* output = fopen(depfile.c_str(), "w"); |
| 104 | + if (!output) { |
| 105 | + Fatal("opening %s: %s", depfile.c_str(), GetLastErrorString().c_str()); |
| 106 | + } |
| 107 | + fprintf(output, "%s: ", output_filename); |
| 108 | + for (vector<string>::iterator i = cl.includes_.begin(); |
| 109 | + i != cl.includes_.end(); ++i) { |
| 110 | + fprintf(output, "%s\n", i->c_str()); |
| 111 | + } |
| 112 | + fclose(output); |
| 113 | + |
| 114 | + return exit_code; |
| 115 | +} |
0 commit comments