• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 gRPC authors.
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"""Patches the spawn() command for windows compilers.
15
16Windows has an 8191 character command line limit, but some compilers
17support an @command_file directive where command_file is a file
18containing the full command line.
19"""
20
21from distutils import ccompiler
22import os
23import os.path
24import shutil
25import sys
26import tempfile
27
28MAX_COMMAND_LENGTH = 8191
29
30_classic_spawn = ccompiler.CCompiler.spawn
31
32
33def _commandfile_spawn(self, command, **kwargs):
34    if os.name == "nt":
35        if any(arg.startswith("/Tc") for arg in command):
36            # Remove /std:c++17 option if this is a MSVC C complation
37            command = [arg for arg in command if arg != "/std:c++17"]
38        elif any(arg.startswith("/Tp") for arg in command):
39            # Remove /std:c11 option if this is a MSVC C++ complation
40            command = [arg for arg in command if arg != "/std:c11"]
41
42    command_length = sum([len(arg) for arg in command])
43    if os.name == "nt" and command_length > MAX_COMMAND_LENGTH:
44        # Even if this command doesn't support the @command_file, it will
45        # fail as is so we try blindly
46        print("Command line length exceeded, using command file")
47        print(" ".join(command))
48        temporary_directory = tempfile.mkdtemp()
49        command_filename = os.path.abspath(
50            os.path.join(temporary_directory, "command")
51        )
52        with open(command_filename, "w") as command_file:
53            escaped_args = [
54                '"' + arg.replace("\\", "\\\\") + '"' for arg in command[1:]
55            ]
56            # add each arg on a separate line to avoid hitting the
57            # "line in command file contains 131071 or more characters" error
58            # (can happen for extra long link commands)
59            command_file.write(" \n".join(escaped_args))
60        modified_command = command[:1] + ["@{}".format(command_filename)]
61        try:
62            _classic_spawn(self, modified_command, **kwargs)
63        finally:
64            shutil.rmtree(temporary_directory)
65    else:
66        _classic_spawn(self, command, **kwargs)
67
68
69def monkeypatch_spawn():
70    """Monkeypatching is dumb, but it's either that or we become maintainers of
71    something much, much bigger."""
72    ccompiler.CCompiler.spawn = _commandfile_spawn
73