Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit ac04abe

Browse files
committed
add a helper binary for wrapping cl.exe
Modify bootstrap etc. to make use of this binary.
1 parent 59e0d69 commit ac04abe

File tree

3 files changed

+167
-8
lines changed

3 files changed

+167
-8
lines changed

bootstrap.py

+36-6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def run(*args, **kwargs):
6868
else:
6969
if src.endswith('-win32.cc'):
7070
continue
71+
if '_main' in src:
72+
continue
7173

7274
sources.append(src)
7375

@@ -110,13 +112,41 @@ def run(*args, **kwargs):
110112
if options.verbose:
111113
verbose = ['-v']
112114

113-
print 'Building ninja using itself...'
114-
run([sys.executable, 'configure.py'] + conf_args)
115-
run(['./' + binary] + verbose)
116-
os.unlink(binary)
117-
118115
if sys.platform.startswith('win32'):
116+
# Build ninja-msvc-helper using ninja without an msvc-helper.
117+
print 'Building ninja-msvc-helper...'
118+
run([sys.executable, 'configure.py', '--with-msvc-helper='] + conf_args)
119+
run(['./' + binary] + verbose + ['ninja-msvc-helper'])
120+
121+
# Rename the helper to the same name + .bootstrap.
122+
helper_binary = 'ninja-msvc-helper.bootstrap.exe'
123+
try:
124+
os.unlink(helper_binary)
125+
except:
126+
pass
127+
os.rename('ninja-msvc-helper.exe', helper_binary)
128+
129+
# Build ninja using the newly-built msvc-helper.
130+
print 'Building ninja using itself...'
131+
run([sys.executable, 'configure.py',
132+
'--with-msvc-helper=%s' % helper_binary] + conf_args)
133+
run(['./' + binary] + verbose)
134+
135+
# Clean up.
119136
for obj in glob.glob('*.obj'):
120137
os.unlink(obj)
121138

122-
print 'Done!'
139+
print """
140+
Done!
141+
142+
Note: to work around Windows file locking, where you can't rebuild an
143+
in-use binary, to run ninja after making any changes to build ninja itself
144+
you should run ninja.bootstrap instead. Your build is also configured to
145+
use ninja-msvc-helper.bootstrap.exe instead of the ninja-msvc-helper.exe
146+
that it builds; see the --help output of configure.py."""
147+
else:
148+
print 'Building ninja using itself...'
149+
run([sys.executable, 'configure.py'] + conf_args)
150+
run(['./' + binary] + verbose)
151+
os.unlink(binary)
152+
print 'Done!'

configure.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
parser.add_option('--with-python', metavar='EXE',
4646
help='use EXE as the Python interpreter',
4747
default=os.path.basename(sys.executable))
48+
parser.add_option('--with-msvc-helper', metavar='NAME',
49+
help="name for ninja-msvc-helper binary (MSVC only)")
4850
(options, args) = parser.parse_args()
4951
if args:
5052
print 'ERROR: extra unparsed command-line arguments:', args
@@ -177,8 +179,11 @@ def shell_escape(str):
177179
n.newline()
178180

179181
if platform == 'windows':
182+
compiler = '$cxx'
183+
if options.with_msvc_helper:
184+
compiler = '%s -o $out -- $cxx /showIncludes' % options.with_msvc_helper
180185
n.rule('cxx',
181-
command='$cxx $cflags -c $in /Fo$out',
186+
command='%s $cflags -c $in /Fo$out' % compiler,
182187
depfile='$out.d',
183188
description='CXX $out')
184189
else:
@@ -282,6 +287,16 @@ def shell_escape(str):
282287
n.newline()
283288
all_targets += ninja
284289

290+
if platform == 'windows':
291+
n.comment('Helper for working with MSVC.')
292+
msvc_helper = n.build(binary('ninja-msvc-helper'), 'link',
293+
cxx('msvc_helper_main-win32'),
294+
implicit=ninja_lib,
295+
variables=[('libs', libs)])
296+
n.default(msvc_helper)
297+
n.newline()
298+
all_targets += msvc_helper
299+
285300
n.comment('Tests all build into ninja_test executable.')
286301

287302
variables = []
@@ -397,7 +412,6 @@ def shell_escape(str):
397412
implicit=['configure.py', os.path.normpath('misc/ninja_syntax.py')])
398413
n.newline()
399414

400-
n.comment('Build only the main binary by default.')
401415
n.default(ninja)
402416
n.newline()
403417

src/msvc_helper_main-win32.cc

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)