Repository: SCons/scons

Issue:
Unguarded print statement in SharedLibrary builder
**Describe the bug**
Every call to _SharedLibrary_ prints `target.dir:<some path` to stdout twice.
Need to have SHLIBVERSION defined, though.

**Required information**
* Link to SCons Users thread discussing your issue.
   https://discord.com/channels/571796279483564041/571796280146133047/834003728734093312

* Version of SCons
  4.1.0

* Version of Python
  Python-3.7

* Which python distribution if applicable (python.org, cygwin, anaconda, macports, brew,etc)
  Python.org on Windows, Ubuntu, ...
  
* How you installed SCons
   _pip install scons_ in a virtualenv
   
* What Platform are you on? (Linux/Windows and which version)
  Linux Ubuntu-18.04, Ubuntu-20.04
  
* How to reproduce your issue?  Please include a small self contained reproducer. Likely a SConstruct should do for most issues.

~~~~python
# SConstruct:
# hello.c not needed for reproducing the issue
env = DefaultEnvironment(SHLIBVERSION="1")
SharedLibrary("this.is.the.output.directory/hello", "hello.c")
~~~~

* How you invoke scons (The command line you're using "scons --flags some_arguments")
~~~~shell
$ scons -c
scons: Reading SConscript files ...
target.dir:this.is.the.output.directory
target.dir:this.is.the.output.directory
scons: done reading SConscript files.
scons: Cleaning targets ...
scons: done cleaning targets.
~~~~


Discussion:

Resolving patch:
diff --git a/SCons/Tool/applelink.py b/SCons/Tool/applelink.py
--- a/SCons/Tool/applelink.py
+++ b/SCons/Tool/applelink.py
@@ -36,7 +36,7 @@
 # Even though the Mac is based on the GNU toolchain, it doesn't understand
 # the -rpath option, so we use the "link" tool instead of "gnulink".
 from SCons.Util import CLVar
-
+from SCons.Errors import UserError
 from . import link
 
 # User programmatically describes how SHLIBVERSION maps to values for compat/current.
@@ -141,6 +141,24 @@ def _applelib_compatVersionFromSoVersion(source, target, env, for_signature):
 
     return "-Wl,-compatibility_version,%s" % version_string
 
+def _applelib_soname(target, source, env, for_signature):
+    """
+    Override default _soname() function from SCons.Tools.linkCommon.SharedLibrary.
+    Apple's file naming for versioned shared libraries puts the version string before
+    the shared library suffix (.dylib), instead of after.
+    """
+    if "SONAME" in env:
+        # Now verify that SOVERSION is not also set as that is not allowed
+        if "SOVERSION" in env:
+            raise UserError(
+                "Ambiguous library .so naming, both SONAME: %s and SOVERSION: %s are defined. "
+                "Only one can be defined for a target library."
+                % (env["SONAME"], env["SOVERSION"])
+            )
+        return "$SONAME"
+    else:
+        return "$SHLIBPREFIX$_get_shlib_stem$_SHLIBSOVERSION${SHLIBSUFFIX}"
+
 
 def generate(env):
     """Add Builders and construction variables for applelink to an
@@ -178,6 +196,8 @@ def generate(env):
     env['__LDMODULEVERSIONFLAGS'] = '${__lib_either_version_flag(__env__,' \
                                     '"LDMODULEVERSION","_APPLELINK_CURRENT_VERSION", "_LDMODULEVERSIONFLAGS")}'
 
+    env["_SHLIBSONAME"] = _applelib_soname
+
 
 def exists(env):
     return env['PLATFORM'] == 'darwin'
diff --git a/SCons/Tool/linkCommon/SharedLibrary.py b/SCons/Tool/linkCommon/SharedLibrary.py
--- a/SCons/Tool/linkCommon/SharedLibrary.py
+++ b/SCons/Tool/linkCommon/SharedLibrary.py
@@ -166,8 +166,12 @@ def _get_shlib_dir(target, source, env, for_signature: bool) -> str:
     Returns:
         the directory the library will be in (empty string if '.')
     """
+    verbose = False
+
     if target.dir and str(target.dir) != ".":
-        print("target.dir:%s" % target.dir)
+        if verbose:
+            print("_get_shlib_dir: target.dir:%s" % target.dir)
+
         return "%s/" % str(target.dir)
     else:
         return ""


Test patch:
diff --git a/test/LINK/SHLIBVERSIONFLAGS.py b/test/LINK/SHLIBVERSIONFLAGS.py
--- a/test/LINK/SHLIBVERSIONFLAGS.py
+++ b/test/LINK/SHLIBVERSIONFLAGS.py
@@ -47,9 +47,9 @@
     soname = 'libfoo.so.4'
     sonameVersionFlags = r".+ -h %s( .+)+" % soname
 elif 'applelink' in tool_list:
-    versionflags = r"    'libfoo.1.dylib'->'libfoo.1.2.3.dylib'"
+    versionflags = r".+ -Wl,-current_version,1.2.3( .+)+"
     soname = 'libfoo.4.dylib'
-    sonameVersionFlags = r"    '%s'->'libfoo.1.2.3.dylib'(.+)+" % soname
+    sonameVersionFlags = r".+ -Wl,-compatibility_version,1.2.0(.+)+"
 else:
     test.skip_test('No testable linkers found, skipping the test\n')