Skip to content

Commit

Permalink
Add 'consolidate_linker_args' wrapper to protect the old behavior for…
Browse files Browse the repository at this point in the history
… now.

Closes pypa/distutils#246.
  • Loading branch information
jaraco committed Apr 13, 2024
1 parent a04913a commit d2581bf
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
15 changes: 15 additions & 0 deletions distutils/compat/__init__.py
@@ -0,0 +1,15 @@
from __future__ import annotations

from .py38 import removeprefix


def consolidate_linker_args(args: list[str]) -> str:
"""
Ensure the return value is a string for backward compatibility.
Retain until at least 2024-10-31.
"""

if not all(arg.startswith('-Wl,') for arg in args):
return args
return '-Wl,' + ','.join(removeprefix(arg, '-Wl,') for arg in args)
23 changes: 23 additions & 0 deletions distutils/compat/py38.py
@@ -0,0 +1,23 @@
import sys

if sys.version_info < (3, 9):

def removesuffix(self, suffix):
# suffix='' should not call self[:-0].
if suffix and self.endswith(suffix):
return self[: -len(suffix)]
else:
return self[:]

def removeprefix(self, prefix):
if self.startswith(prefix):
return self[len(prefix) :]
else:
return self[:]
else:

def removesuffix(self, suffix):
return self.removesuffix(suffix)

def removeprefix(self, prefix):
return self.removeprefix(prefix)
17 changes: 9 additions & 8 deletions distutils/tests/test_unixccompiler.py
Expand Up @@ -4,6 +4,7 @@
import sys
import unittest.mock as mock
from distutils import sysconfig
from distutils.compat import consolidate_linker_args
from distutils.errors import DistutilsPlatformError
from distutils.unixccompiler import UnixCCompiler
from distutils.util import _clear_cached_macosx_ver
Expand Down Expand Up @@ -149,10 +150,10 @@ def gcv(v):
return 'yes'

sysconfig.get_config_var = gcv
assert self.cc.rpath_foo() == [
assert self.cc.rpath_foo() == consolidate_linker_args([
'-Wl,--enable-new-dtags',
'-Wl,-rpath,/foo',
]
])

def gcv(v):
if v == 'CC':
Expand All @@ -161,10 +162,10 @@ def gcv(v):
return 'yes'

sysconfig.get_config_var = gcv
assert self.cc.rpath_foo() == [
assert self.cc.rpath_foo() == consolidate_linker_args([
'-Wl,--enable-new-dtags',
'-Wl,-rpath,/foo',
]
])

# GCC non-GNULD
sys.platform = 'bar'
Expand All @@ -189,10 +190,10 @@ def gcv(v):
return 'yes'

sysconfig.get_config_var = gcv
assert self.cc.rpath_foo() == [
assert self.cc.rpath_foo() == consolidate_linker_args([
'-Wl,--enable-new-dtags',
'-Wl,-rpath,/foo',
]
])

# non-GCC GNULD
sys.platform = 'bar'
Expand All @@ -204,10 +205,10 @@ def gcv(v):
return 'yes'

sysconfig.get_config_var = gcv
assert self.cc.rpath_foo() == [
assert self.cc.rpath_foo() == consolidate_linker_args([
'-Wl,--enable-new-dtags',
'-Wl,-rpath,/foo',
]
])

# non-GCC non-GNULD
sys.platform = 'bar'
Expand Down
5 changes: 3 additions & 2 deletions distutils/unixccompiler.py
Expand Up @@ -22,6 +22,7 @@
import sys

from . import sysconfig
from .compat import consolidate_linker_args
from ._log import log
from ._macos_compat import compiler_fixup
from ._modified import newer
Expand Down Expand Up @@ -315,11 +316,11 @@ def runtime_library_dir_option(self, dir: str) -> str | list[str]:
# For all compilers, `-Wl` is the presumed way to pass a
# compiler option to the linker
if sysconfig.get_config_var("GNULD") == "yes":
return [
return consolidate_linker_args([
# Force RUNPATH instead of RPATH
"-Wl,--enable-new-dtags",
"-Wl,-rpath," + dir,
]
])
else:
return "-Wl,-R" + dir

Expand Down

0 comments on commit d2581bf

Please sign in to comment.