Skip to content

support linker specifying "unmodified library names" #3951

Closed

Description

@mwichmann

Issue created after discussion on libera.chat irc channel #scons.

The GNU linker now has syntax which allows specifying a library filename which will not be modified before searching for it. This is a lightly edited copy of the text from a recent manpage:

-l namespec, --library=namespec
Add the archive or object file specified by namespec to the list of files to link. This option may be used any number of times. If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a. ... on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a. (By convention, a .so extension indicates a shared library.) Note that this behavior does not apply to :filename, which always specifies a file called filename.

Seems like SCons should understand this :foo syntax in handling LIBS. The alternative of trying to specify a specific library name/path which is taken without modification is to add it to LINKFLAGS instead of LIBS, but that loses the library ordering of the command line, so usually isn't a desirable approach. The other approach of using File('libname') to specify the library is okay for a specific project-local library, but I believe will not respect the defined library search path as the colon syntax does.

A little research shows this was added in binutils 2.18 (which was 2007-ish):

Changes in 2.18:
...
* -l:foo now searches the library path for a filename called foo, 
  without converting it to libfoo.a or libfoo.so.

Activity

  1. mwichmann commented on Jun 8, 2021

    @mwichmann
    CollaboratorAuthor

    I suppose the more general purpose way to handle this is a new construction var that describes a no-modify prefix - the code could just look for a libname that starts with : and not transform it, but that wires in gcc syntax...

  2. bdbaddog commented on Jun 8, 2021

    @bdbaddog
    Contributor

    Related (slightly) to #1161

  3. mwichmann commented on Jun 10, 2021

    @mwichmann
    CollaboratorAuthor

    These issues are related: #2743 #2925 #2912

  4. mwichmann commented on Aug 15, 2023

    @mwichmann
    CollaboratorAuthor

    Seems like this can be implemented in _stripixes - simply telling it not to strip if the magic prefix is found. I'm presuming we'd want to parameterize that (construction variable). The following works for very simple tests - it's not a complete patch as we'd need to set up initialization of the "literal" prefix and documentation.

    diff --git a/SCons/Defaults.py b/SCons/Defaults.py
    index cabadcc67..534c57575 100644
    --- a/SCons/Defaults.py
    +++ b/SCons/Defaults.py
    @@ -477,6 +477,7 @@ def _stripixes(prefix, itms, suffix, stripprefixes, stripsuffixes, env, c=None):
             else:
                 c = _concat_ixes
     
    +    libliteral = env.get('LIBLITERAL')
         stripprefixes = list(map(env.subst, flatten(stripprefixes)))
         stripsuffixes = list(map(env.subst, flatten(stripsuffixes)))
     
    @@ -489,6 +490,11 @@ def _stripixes(prefix, itms, suffix, stripprefixes, stripsuffixes, env, c=None):
             if not is_String(l):
                 l = str(l)
     
    +        # if special prefix is found, do not strip the name
    +        if libliteral and l.startswith(libliteral):
    +            stripped.append(l)
    +            continue
    +
             for stripprefix in stripprefixes:
                 lsp = len(stripprefix)
                 if l[:lsp] == stripprefix:

    Verified only with a trivial unittest (pending a decision of whether this is something we even want to do):

    diff --git a/SCons/EnvironmentTests.py b/SCons/EnvironmentTests.py
    index e7cb99f34..2fa917d2a 100644
    --- a/SCons/EnvironmentTests.py
    +++ b/SCons/EnvironmentTests.py
    @@ -1522,6 +1522,21 @@ def exists(env):
             x = e.subst('$( ${_concat(PRE, L1, SUF, __env__)} $)')
             assert x == 'preasuf prebsuf precsuf predsuf precsuf predsuf', x
     
    +
    +    def test__stripixes(self) -> None:
    +        """Test _stripixes()"""
    +        e = self.TestEnvironment(
    +            PRE='PRE',
    +            SUF='SUF',
    +            LIST=['xxx-a', 'b.yyy', 'zzxxx-c.yyy'],
    +            LIBPREFIXES=['xxx-'],
    +            LIBSUFFIXES=['.yyy'],
    +            LIBLITERAL='zz',
    +        )
    +        x = e.subst('$( ${_stripixes(PRE, LIST, SUF, LIBPREFIXES, LIBSUFFIXES, __env__)} $)')
    +        self.assertEqual(x, 'PREaSUF PREbSUF PREzzxxx-c.yyySUF')
    +
    +
         def test_gvars(self) -> None:
             """Test the Environment gvars() method"""
             env = self.TestEnvironment(XXX = 'x', YYY = 'y', ZZZ = 'z')
  5. added
    has patch
    on Aug 15, 2023
  6. mwichmann commented on Aug 24, 2023

    @mwichmann
    CollaboratorAuthor

    Sadly, while the change works fine at the _stripixes level, but it doesn't actually work at the E2E level, so there's something else going on.

  7. mwichmann commented on Oct 9, 2023

    @mwichmann
    CollaboratorAuthor

    The "something else" is a call to PathList, which, for string-valued members, does a split on os.pathsep. Which just happens to be the same magic character as the literal-lib introducer.

  8. added a commit that references this issue on Oct 11, 2023
    2a52282
  9. added a commit that references this issue on Oct 28, 2023
    d7904ef
  10. added this to the 4.6 milestone on Oct 30, 2023
  11. augfab commented on Oct 31, 2023

    @augfab

    Thanks a lot for the fix. I'm looking forward to version 4.6 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions