Skip to content

Commit

Permalink
Allow compiler classes to supply include and library dirs at the clas…
Browse files Browse the repository at this point in the history
…s level.
  • Loading branch information
jaraco committed Aug 10, 2022
1 parent 53eace5 commit 9f9a3e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
43 changes: 13 additions & 30 deletions distutils/_msvccompiler.py
Expand Up @@ -224,6 +224,18 @@ def __init__(self, verbose=0, dry_run=0, force=0):
self.plat_name = None
self.initialized = False

@classmethod
def _configure(cls, vc_env):
"""
Set class-level include/lib dirs.
"""
cls.include_dirs = cls._parse_path(vc_env.get('include', ''))
cls.library_dirs = cls._parse_path(vc_env.get('lib', ''))

@staticmethod
def _parse_path(val):
return [dir.rstrip(os.sep) for dir in val.split(os.pathsep) if dir]

def initialize(self, plat_name=None):
# multi-init means we would need to check platform same each time...
assert not self.initialized, "don't init multiple times"
Expand All @@ -243,6 +255,7 @@ def initialize(self, plat_name=None):
raise DistutilsPlatformError(
"Unable to find a compatible " "Visual Studio installation."
)
self._configure(vc_env)

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

self.__include_dirs = [
dir.rstrip(os.sep)
for dir in vc_env.get('include', '').split(os.pathsep)
if dir
]

self.__library_dirs = [
dir.rstrip(os.sep) for dir in vc_env.get('lib', '').split(os.pathsep) if dir
]

self.preprocess_options = None
# bpo-38597: Always compile with dynamic linking
# Future releases of Python 3.x will include all past
Expand Down Expand Up @@ -559,26 +562,6 @@ def _fallback_spawn(self, cmd, env):
with mock.patch.dict('os.environ', env):
bag.value = super().spawn(cmd)

def _fix_compile_args(self, output_dir, macros, include_dirs):
"""Corrects arguments to the compile() method and add compiler-specific dirs"""
fixed_args = super()._fix_compile_args(output_dir, macros, include_dirs)
return (
fixed_args[0], # output_dir
fixed_args[1], # macros
fixed_args[2] + self.__include_dirs,
)

def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs):
"""Corrects arguments to the link_*() methods and add linker-specific dirs"""
fixed_args = super()._fix_lib_args(
libraries, library_dirs, runtime_library_dirs
)
return (
fixed_args[0], # libraries
fixed_args[1] + self.__library_dirs,
fixed_args[2], # runtime_library_dirs
)

# -- Miscellaneous methods -----------------------------------------
# These are all used by the 'gen_lib_options() function, in
# ccompiler.py.
Expand Down
16 changes: 16 additions & 0 deletions distutils/ccompiler.py
Expand Up @@ -91,6 +91,16 @@ class CCompiler:
}
language_order = ["c++", "objc", "c"]

include_dirs = []
"""
include dirs specific to this compiler class
"""

library_dirs = []
"""
library dirs specific to this compiler class
"""

def __init__(self, verbose=0, dry_run=0, force=0):
self.dry_run = dry_run
self.force = force
Expand Down Expand Up @@ -383,6 +393,9 @@ def _fix_compile_args(self, output_dir, macros, include_dirs):
else:
raise TypeError("'include_dirs' (if supplied) must be a list of strings")

# add include dirs for class
include_dirs += self.__class__.include_dirs

return output_dir, macros, include_dirs

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

# add library dirs for class
library_dirs += self.__class__.library_dirs

if runtime_library_dirs is None:
runtime_library_dirs = self.runtime_library_dirs
elif isinstance(runtime_library_dirs, (list, tuple)):
Expand Down

0 comments on commit 9f9a3e5

Please sign in to comment.