Skip to content

Commit 9f9a3e5

Browse files
committed
Allow compiler classes to supply include and library dirs at the class level.
1 parent 53eace5 commit 9f9a3e5

2 files changed

Lines changed: 29 additions & 30 deletions

File tree

distutils/_msvccompiler.py

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ def __init__(self, verbose=0, dry_run=0, force=0):
224224
self.plat_name = None
225225
self.initialized = False
226226

227+
@classmethod
228+
def _configure(cls, vc_env):
229+
"""
230+
Set class-level include/lib dirs.
231+
"""
232+
cls.include_dirs = cls._parse_path(vc_env.get('include', ''))
233+
cls.library_dirs = cls._parse_path(vc_env.get('lib', ''))
234+
235+
@staticmethod
236+
def _parse_path(val):
237+
return [dir.rstrip(os.sep) for dir in val.split(os.pathsep) if dir]
238+
227239
def initialize(self, plat_name=None):
228240
# multi-init means we would need to check platform same each time...
229241
assert not self.initialized, "don't init multiple times"
@@ -243,6 +255,7 @@ def initialize(self, plat_name=None):
243255
raise DistutilsPlatformError(
244256
"Unable to find a compatible " "Visual Studio installation."
245257
)
258+
self._configure(vc_env)
246259

247260
self._paths = vc_env.get('path', '')
248261
paths = self._paths.split(os.pathsep)
@@ -253,16 +266,6 @@ def initialize(self, plat_name=None):
253266
self.mc = _find_exe("mc.exe", paths) # message compiler
254267
self.mt = _find_exe("mt.exe", paths) # message compiler
255268

256-
self.__include_dirs = [
257-
dir.rstrip(os.sep)
258-
for dir in vc_env.get('include', '').split(os.pathsep)
259-
if dir
260-
]
261-
262-
self.__library_dirs = [
263-
dir.rstrip(os.sep) for dir in vc_env.get('lib', '').split(os.pathsep) if dir
264-
]
265-
266269
self.preprocess_options = None
267270
# bpo-38597: Always compile with dynamic linking
268271
# Future releases of Python 3.x will include all past
@@ -559,26 +562,6 @@ def _fallback_spawn(self, cmd, env):
559562
with mock.patch.dict('os.environ', env):
560563
bag.value = super().spawn(cmd)
561564

562-
def _fix_compile_args(self, output_dir, macros, include_dirs):
563-
"""Corrects arguments to the compile() method and add compiler-specific dirs"""
564-
fixed_args = super()._fix_compile_args(output_dir, macros, include_dirs)
565-
return (
566-
fixed_args[0], # output_dir
567-
fixed_args[1], # macros
568-
fixed_args[2] + self.__include_dirs,
569-
)
570-
571-
def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs):
572-
"""Corrects arguments to the link_*() methods and add linker-specific dirs"""
573-
fixed_args = super()._fix_lib_args(
574-
libraries, library_dirs, runtime_library_dirs
575-
)
576-
return (
577-
fixed_args[0], # libraries
578-
fixed_args[1] + self.__library_dirs,
579-
fixed_args[2], # runtime_library_dirs
580-
)
581-
582565
# -- Miscellaneous methods -----------------------------------------
583566
# These are all used by the 'gen_lib_options() function, in
584567
# ccompiler.py.

distutils/ccompiler.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ class CCompiler:
9191
}
9292
language_order = ["c++", "objc", "c"]
9393

94+
include_dirs = []
95+
"""
96+
include dirs specific to this compiler class
97+
"""
98+
99+
library_dirs = []
100+
"""
101+
library dirs specific to this compiler class
102+
"""
103+
94104
def __init__(self, verbose=0, dry_run=0, force=0):
95105
self.dry_run = dry_run
96106
self.force = force
@@ -383,6 +393,9 @@ def _fix_compile_args(self, output_dir, macros, include_dirs):
383393
else:
384394
raise TypeError("'include_dirs' (if supplied) must be a list of strings")
385395

396+
# add include dirs for class
397+
include_dirs += self.__class__.include_dirs
398+
386399
return output_dir, macros, include_dirs
387400

388401
def _prep_compile(self, sources, output_dir, depends=None):
@@ -439,6 +452,9 @@ def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs):
439452
else:
440453
raise TypeError("'library_dirs' (if supplied) must be a list of strings")
441454

455+
# add library dirs for class
456+
library_dirs += self.__class__.library_dirs
457+
442458
if runtime_library_dirs is None:
443459
runtime_library_dirs = self.runtime_library_dirs
444460
elif isinstance(runtime_library_dirs, (list, tuple)):

0 commit comments

Comments
 (0)