Repository: conan-io/conan-center-index

Issue:
[package] swig/4.0.2: SWIG_DIR (swiglib) is wrong on mac and points to the build machine
### Package and Environment Details (include every applicable attribute)
  * Package Name/Version: **swig/4.0.2**
  * Operating System+version: **macOS**
  * Compiler+version: **apple-clang 11 12 13**
  * Conan version: **conan 1.44.0**
  * Python version: **Python 3.8**

### Steps to reproduce (Include if Applicable)

Using swig/4.0.2 revision 187cec21277aafbeae11dcc3b936ce74,

```
find_package(SWIG REQUIRED)
message(STATUS "FOUND SWIG_EXECUTABLE=${SWIG_EXECUTABLE}, SWIG_DIR=${SWIG_DIR}")
```

On Ubuntu and Windows this is just fine. it does output the correct SWIG_DIR

```
 -- Found SWIG: /home/conan/.conan/data/swig/4.0.2/_/_/package/ecb2d71ae4bad3b51dade00ec89140273402b5d7/bin/swig (found version "4.0.2")  
-- FOUND SWIG_EXECUTABLE=/home/conan/.conan/data/swig/4.0.2/_/_/package/ecb2d71ae4bad3b51dade00ec89140273402b5d7/bin/swig, SWIG_DIR=/home/conan/.conan/data/swig/4.0.2/_/_/package/ecb2d71ae4bad3b51dade00ec89140273402b5d7/bin/swiglib
```

https://github.com/NREL/conan-openstudio-ruby/runs/4740291834?check_suite_focus=true#step:5:3005

--- 

**on macOS, it outputs a wrong path that's on the build machine**

```
swig/4.0.2: Downloaded recipe revision 187cec21277aafbeae11dcc3b936ce74
[....]

-- Found SWIG: /Users/runner/.conan/data/swig/4.0.2/_/_/package/099d7b9cd06e9bd11e92b9a2ddf3b29cd986fdcb/bin/swig (found version "4.0.2")  
-- FOUND SWIG_EXECUTABLE=/Users/runner/.conan/data/swig/4.0.2/_/_/package/099d7b9cd06e9bd11e92b9a2ddf3b29cd986fdcb/bin/swig, SWIG_DIR=/Users/jenkins/w/BuildSingleReference@3/.conan/data/swig/4.0.2/_/_/package/099d7b9cd06e9bd11e92b9a2ddf3b29cd986fdcb/bin/swiglib
```

cf: https://github.com/NREL/conan-openstudio-ruby/runs/4740291938?check_suite_focus=true#step:5:3127

That cmake variable could be fixed by just doing `SWIG_EXECUTABLE + "/swiglib"` but the main problem is that the swig exe points to the wrong swig lib so any call to the exe would need to set the env variable SWIG_LIB (via `${CMAKE_COMMAND} -E env SWIG_LIB="${SWIG_EXECUTABLE}/swiglib"` for eg)

### Logs (Include/Attach if Applicable)

Cf the github actions run I linked to above

</details>



Discussion:
Locally on mac I can confirm the issue. If I relocate the conan package I built myself, the swiglib is wrong.

The Sources/Modules/main.cxx file after patch https://github.com/conan-io/conan-center-index/blob/master/recipes/swig/all/patches/0001-swig-linux-library-path.patch is applied looks like this:

```c++
  // Check for SWIG_LIB environment variable
  if ((c = getenv("SWIG_LIB")) == (char *) 0) {
#if defined(_WIN32)
    char buf[MAX_PATH];
    char *p;
    if (!(GetModuleFileName(0, buf, MAX_PATH) == 0 || (p = strrchr(buf, '\\')) == 0)) {
      *(p + 1) = '\0';
      SwigLib = NewStringf("%sswiglib", buf); // Native windows installation path
    } else {
      SwigLib = NewStringf("");	// Unexpected error
    }
    if (Len(SWIG_LIB_WIN_UNIX) > 0)
      SwigLibWinUnix = NewString(SWIG_LIB_WIN_UNIX); // Unix installation path using a drive letter (for msys/mingw)
#elif defined(HAVE_UNISTD_H) && !defined(_WIN32)
    SwigLib = get_exe_path();
#else
    SwigLib = NewString(SWIG_LIB);
#endif
  } else {
    SwigLib = NewString(c);
  }
```

`HAVE_UNISTD_H` is 1 in the swigconfig.h. That means the get_exe_path is at fault probably:

```c++
#if defined(HAVE_UNISTD_H) && !defined(_WIN32)
#include <libgen.h>
#include <unistd.h>

static String *get_exe_path(void) {
    char buffer[PATH_MAX];
    ssize_t nb = readlink("/proc/self/exe", buffer, PATH_MAX);
    if (nb != -1) {
        buffer[nb] = '\0';
        dirname(buffer);
        strcat(buffer, "/swiglib");
        return NewStringWithSize(buffer, strlen(buffer));
    }
    return NewString(SWIG_LIB);
}
#endif
```
Yup, /proc/self/exe won't work on mac. See https://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe

Resolving patch:
diff --git a/recipes/swig/all/conanfile.py b/recipes/swig/all/conanfile.py
--- a/recipes/swig/all/conanfile.py
+++ b/recipes/swig/all/conanfile.py
@@ -86,6 +86,8 @@ def _configure_autotools(self):
             "--host={}".format(self.settings.arch),
             "--with-swiglibdir={}".format(self._swiglibdir),
         ]
+        if self.settings.compiler == 'gcc':
+            args.append("LIBS=-ldl")
 
         host, build = None, None
 
@@ -108,7 +110,7 @@ def _configure_autotools(self):
             autotools.libs.extend(["mingwex", "ssp"])
 
         autotools.configure(args=args, configure_dir=self._source_subfolder,
-                                  host=host, build=build)
+                            host=host, build=build)
         return autotools
 
     def _patch_sources(self):


Test patch: