2
2
# packages cross-platform. If the folder containing this file is on
3
3
# your PYTHONPATH when you invoke pip, pip will behave as if it were
4
4
# running on {{os}}.
5
+ import distutils .ccompiler
6
+ import distutils .unixccompiler
5
7
import os
6
8
import platform
7
9
import sys
8
10
import sysconfig
11
+ import types
9
12
10
13
# Make platform.system() return "{{os}}"
11
14
def custom_system ():
@@ -19,6 +22,71 @@ def custom_get_platform():
19
22
20
23
sysconfig .get_platform = custom_get_platform
21
24
25
+ # Make distutils raise errors if you try to use it to build modules.
26
+ DISABLED_COMPILER_ERROR = "Cannot compile native modules"
27
+
28
+ distutils .ccompiler .get_default_compiler = lambda * args , ** kwargs : "disabled"
29
+ distutils .ccompiler .compiler_class ["disabled" ] = (
30
+ "disabledcompiler" ,
31
+ "DisabledCompiler" ,
32
+ "Compiler disabled ({})" .format (DISABLED_COMPILER_ERROR ),
33
+ )
34
+
35
+
36
+ def disabled_compiler (prefix ):
37
+ # No need to give any more advice here: that will come from the higher-level code in pip.
38
+ from distutils .errors import DistutilsPlatformError
39
+
40
+ raise DistutilsPlatformError ("{}: {}" .format (prefix , DISABLED_COMPILER_ERROR ))
41
+
42
+
43
+ class DisabledCompiler (distutils .ccompiler .CCompiler ):
44
+ compiler_type = "disabled"
45
+
46
+ def preprocess (* args , ** kwargs ):
47
+ disabled_compiler ("CCompiler.preprocess" )
48
+
49
+ def compile (* args , ** kwargs ):
50
+ disabled_compiler ("CCompiler.compile" )
51
+
52
+ def create_static_lib (* args , ** kwargs ):
53
+ disabled_compiler ("CCompiler.create_static_lib" )
54
+
55
+ def link (* args , ** kwargs ):
56
+ disabled_compiler ("CCompiler.link" )
57
+
58
+
59
+ # To maximize the chance of the build getting as far as actually calling compile(), make
60
+ # sure the class has all of the expected attributes.
61
+ for name in [
62
+ "src_extensions" ,
63
+ "obj_extension" ,
64
+ "static_lib_extension" ,
65
+ "shared_lib_extension" ,
66
+ "static_lib_format" ,
67
+ "shared_lib_format" ,
68
+ "exe_extension" ,
69
+ ]:
70
+ setattr (
71
+ DisabledCompiler , name , getattr (distutils .unixccompiler .UnixCCompiler , name )
72
+ )
73
+
74
+ DisabledCompiler .executables = {
75
+ name : [DISABLED_COMPILER_ERROR .replace (" " , "_" )]
76
+ for name in distutils .unixccompiler .UnixCCompiler .executables
77
+ }
78
+
79
+ disabled_mod = types .ModuleType ("distutils.disabledcompiler" )
80
+ disabled_mod .DisabledCompiler = DisabledCompiler
81
+ sys .modules ["distutils.disabledcompiler" ] = disabled_mod
82
+
83
+
84
+ # Try to disable native builds for packages which don't use the distutils native build
85
+ # system at all (e.g. uwsgi), or only use it to wrap an external build script (e.g. pynacl).
86
+ for tool in ["ar" , "as" , "cc" , "cxx" , "ld" ]:
87
+ os .environ [tool .upper ()] = DISABLED_COMPILER_ERROR .replace (" " , "_" )
88
+
89
+
22
90
# Call the next sitecustomize script if there is one
23
91
# (https://nedbatchelder.com/blog/201001/running_code_at_python_startup.html).
24
92
del sys .modules ["sitecustomize" ]
0 commit comments