Repository: conan-io/conan-center-index

Issue:
[package] swig/4.0.1, swig/4.0.2: invalid swiglib path makes installed swig unusable
<!-- 
  Please don't forget to update the issue title.
  Include all applicable information to help us reproduce your problem.
-->

### Package and Environment Details (include every applicable attribute)
  * Package Name/Version: **swig/4.0.1**, **swig/4.02**
  * Operating System+version: **Linux Ubuntu 20.04**
  * Compiler+version: **GCC 9**
  * Conan version: **conan 1.34.1**
  * Python version: **Python 3.8.2**


### Conan profile (output of `conan profile show default` or `conan profile show <profile>` if custom profile is in use)
```
Configuration for profile default:

[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=9
compiler.libcxx=libstdc++
build_type=Release
[options]
[build_requires]
[env]
```


### Steps to reproduce (Include if Applicable)

conanfile.txt contents:
```
[build_requires]
swig/4.0.1 # 4.0.2 has same behaviour

[generators]
virtualenv
```

Run the following commands:
```
mkdir ~/build
cd ~/build
conan install <folder-containing-above-file>
. activate.sh
which swig
swig -swiglib
```
On my machine, I see:
```
#### which swig
/home/vagrant/.conan/data/swig/4.0.2/_/_/package/4a3775e698f76c30fab71ea08d47f61f009d1176/bin/swig

#### swig -swiglib
/home/conan/w/BuildSingleReference/.conan/data/swig/4.0.2/_/_/package/4a3775e698f76c30fab71ea08d47f61f009d1176/bin/swiglib
```
The initial segment of swiglib directory path is wrong (and non-existent); it appears to be from the package build machine.

The invalid swiglib path results in the following messages like the following when attempting to process a .i file:

```
    :1. Unable to find 'swig.swg'
    :3. Unable to find 'python.swg'
```

### In comparison to swig_installer/4.0.1@bincrafters/stable

I have successfully used swig_installer/4.0.1@bincrafters/stable (https://github.com/bincrafters/conan-swig_installer/blob/testing/4.0.1/conanfile.py) across Windows 10, various Ubuntu (16.04, 18.04, 20.04), Debian (9, 10) and MacOS. Some significant differences:

* the bincrafters folders are organized differently, with bin and shared as peer directories
```
├── bin
├── licenses
└── share
    └── swig
        └── 4.0.1
```
* With this recipe, the layout is:
```
├── bin
│   └── swiglib
├── lib
│   └── cmake
└── licenses
```
* whether it is the current directory arrangement or the inclusion of --with-swiglibdir in your configuration, the net result is an invalid swiglib path
* the bincrafter's recipe, even without running the activate.sh script correctly reports the swiglib location. The current recipe does not
* Although it appears to be completely unnecessary, the bincrafters recipe also sets the SWIG_LIB environment variable
* Setting SWIG_LIB in your recipe would be a viable work around to the problem, although I am not sure that it would be the best

### Tested work arounds
* set SWIG_LIB environment variable locally prior to running swig
* use --build to force a local build: ```conan install --build=swig <path-to-conanfile-dir>```


Discussion:
I just ran the exact same commands on my machine and it is working properly:
```
[maarten@fedora build]$ cat conanfile.txt
[build_requires]
swig/4.0.2

[generators]
virtualenv
[maarten@fedora build]$ conan install .
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=11
os=Linux
os_build=Linux
[options]
[build_requires]
[env]

conanfile.txt: Installing package
Requirements
Packages
Build requirements
    bzip2/1.0.8 from 'conan-center' - Cache
    pcre/8.41 from 'conan-center' - Cache
    swig/4.0.2 from 'conan-center' - Cache
    zlib/1.2.11 from 'conan-center' - Cache
Build requirements packages
    bzip2/1.0.8:c32092bf4d4bb47cf962af898e02823f499b017e - Cache
    pcre/8.41:ef745e89a5945f365ff9a2757129501a5e9bebf1 - Cache
    swig/4.0.2:e8ea3abd03ff3e94d78caf3c18bf3c0cbd700c67 - Cache
    zlib/1.2.11:dfbe50feef7f3c6223a476cd5aeadb687084a646 - Cache

Installing (downloading, building) binaries...
bzip2/1.0.8: Already installed!
bzip2/1.0.8: Appending PATH environment variable: /home/maarten/.conan/data/bzip2/1.0.8/_/_/package/c32092bf4d4bb47cf962af898e02823f499b017e/bin
[HOOK - conan-center.py] post_package_info(): [CMAKE FILE NOT IN BUILD FOLDERS (KB-H019)] OK
zlib/1.2.11: Already installed!
[HOOK - conan-center.py] post_package_info(): [CMAKE FILE NOT IN BUILD FOLDERS (KB-H019)] OK
pcre/8.41: Already installed!
pcre/8.41: Appending PATH environment variable: /home/maarten/.conan/data/pcre/8.41/_/_/package/ef745e89a5945f365ff9a2757129501a5e9bebf1/bin
[HOOK - conan-center.py] post_package_info(): [CMAKE FILE NOT IN BUILD FOLDERS (KB-H019)] OK
swig/4.0.2: Already installed!
swig/4.0.2: Appending PATH environment variable: /home/maarten/.conan/data/swig/4.0.2/_/_/package/e8ea3abd03ff3e94d78caf3c18bf3c0cbd700c67/bin
[HOOK - conan-center.py] post_package_info(): [CMAKE FILE NOT IN BUILD FOLDERS (KB-H019)] OK
conanfile.txt: Applying build-requirement: swig/4.0.2
conanfile.txt: Applying build-requirement: pcre/8.41
conanfile.txt: Applying build-requirement: bzip2/1.0.8
conanfile.txt: Applying build-requirement: zlib/1.2.11
conanfile.txt: Generator txt created conanbuildinfo.txt
conanfile.txt: Generator virtualenv created activate.ps1
conanfile.txt: Generator virtualenv created deactivate.ps1
conanfile.txt: Generator virtualenv created environment.ps1.env
conanfile.txt: Generator virtualenv created activate.sh
conanfile.txt: Generator virtualenv created deactivate.sh
conanfile.txt: Generator virtualenv created environment.sh.env
conanfile.txt: Generated conaninfo.txt
conanfile.txt: Generated graphinfo
[maarten@fedora build]$ . activate.sh 
(conanenv) [maarten@fedora build]$ which swig
~/.conan/data/swig/4.0.2/_/_/package/e8ea3abd03ff3e94d78caf3c18bf3c0cbd700c67/bin/swig
(conanenv) [maarten@fedora build]$ swig -swiglib
/home/maarten/.conan/data/swig/4.0.2/_/_/package/e8ea3abd03ff3e94d78caf3c18bf3c0cbd700c67/bin/swiglib
```

Try removing all conan swig packages, before running your commands again:
```
conan remove swig
```
@madebr : Just retried on two environments, reproducing in each case: Ubuntu 18.04 and MacOS

As you suggested, I initially ran:

`conan remove swig`

MacOS currently has conan 1.34.1, while the Ubuntu 18.04 environment has 1.35.0.

The only difference I can see (apart from you using Fedora) is that I am using pyenv to manage a separate Python environment for Conan.

I'm willing to test more. Give me some ideas.

The complete log of my Ubuntu 18.04 session still illustrates the problem:

`vagrant@vagrant:~/test_swig$ PYENV_VERSION=vconan conan install .`

```
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=7
os=Linux
os_build=Linux
[options]
[build_requires]
[env]

swig/4.0.1: Not found in local cache, looking in remotes...
swig/4.0.1: Trying with 'conan-center'...
Downloading conanmanifest.txt completed [0.48k]
Downloading conanfile.py completed [5.76k]
Downloading conan_export.tgz completed [0.33k]
Decompressing conan_export.tgz completed [0.00k]
swig/4.0.1: Downloaded recipe revision 0
pcre/8.41: Not found in local cache, looking in remotes...
pcre/8.41: Trying with 'conan-center'...
Downloading conanmanifest.txt completed [0.17k]
Downloading conanfile.py completed [8.28k]
Downloading conan_export.tgz completed [0.24k]
Decompressing conan_export.tgz completed [0.00k]
pcre/8.41: Downloaded recipe revision 0
conanfile.txt: Installing package
Requirements
Packages
Build requirements
    bzip2/1.0.8 from 'conan-center' - Cache
    pcre/8.41 from 'conan-center' - Downloaded
    swig/4.0.1 from 'conan-center' - Downloaded
    zlib/1.2.11 from 'conan-center' - Cache
Build requirements packages
    bzip2/1.0.8:b27e2b40bcb70be2e7d6f0e0c6e43e35c530f8d3 - Cache
    pcre/8.41:dd026c9d5b970a971cbfec4dc98c1d39266672ce - Download
    swig/4.0.1:c6f1e0ee89a1fd45a631e7217c7e93d38188a64d - Download
    zlib/1.2.11:d50a0d523d98c15bb147b18fa7d203887c38be8b - Cache

Installing (downloading, building) binaries...
pcre/8.41: Retrieving package dd026c9d5b970a971cbfec4dc98c1d39266672ce from remote 'conan-center'
Downloading conanmanifest.txt completed [0.45k]
Downloading conaninfo.txt completed [1.17k]
Downloading conan_package.tgz completed [397.58k]
Decompressing conan_package.tgz completed [0.00k]
pcre/8.41: Package installed dd026c9d5b970a971cbfec4dc98c1d39266672ce
pcre/8.41: Downloaded package revision 0
swig/4.0.1: Retrieving package c6f1e0ee89a1fd45a631e7217c7e93d38188a64d from remote 'conan-center'
Downloading conanmanifest.txt completed [45.95k]
Downloading conaninfo.txt completed [0.99k]
Downloading conan_package.tgz completed [1422.51k]
Decompressing conan_package.tgz completed [0.00k]
swig/4.0.1: Package installed c6f1e0ee89a1fd45a631e7217c7e93d38188a64d
swig/4.0.1: Downloaded package revision 0
bzip2/1.0.8: Already installed!
zlib/1.2.11: Already installed!
pcre/8.41: Appending PATH environment variable: /home/vagrant/.conan/data/pcre/8.41/_/_/package/dd026c9d5b970a971cbfec4dc98c1d39266672ce/bin
swig/4.0.1: Appending PATH environment variable: /home/vagrant/.conan/data/swig/4.0.1/_/_/package/c6f1e0ee89a1fd45a631e7217c7e93d38188a64d/bin
conanfile.txt: Applying build-requirement: swig/4.0.1
conanfile.txt: Applying build-requirement: pcre/8.41
conanfile.txt: Applying build-requirement: bzip2/1.0.8
conanfile.txt: Applying build-requirement: zlib/1.2.11
conanfile.txt: Generator virtualenv created activate.ps1
conanfile.txt: Generator virtualenv created deactivate.ps1
conanfile.txt: Generator virtualenv created environment.ps1.env
conanfile.txt: Generator virtualenv created activate.sh
conanfile.txt: Generator virtualenv created deactivate.sh
conanfile.txt: Generator virtualenv created environment.sh.env
conanfile.txt: Generator txt created conanbuildinfo.txt
conanfile.txt: Generated conaninfo.txt
conanfile.txt: Generated graphinfo
```

Then my post-install commands:

```vagrant@vagrant:~/test_swig$ . activate.sh
(conanenv) vagrant@vagrant:~/test_swig$ which swig```

`/home/vagrant/.conan/data/swig/4.0.1/_/_/package/c6f1e0ee89a1fd45a631e7217c7e93d38188a64d/bin/swig`

`(conanenv) vagrant@vagrant:~/test_swig$ swig -swiglib`

`/home/conan/w/BuildSingleReference/.conan/data/swig/4.0.1/_/_/package/c6f1e0ee89a1fd45a631e7217c7e93d38188a64d/bin/swiglib`

Again, this latter directory is not on my machine.

The MacOS path is:

`/Users/jenkins/w/BuildSingleReference@2/.conan/data/swig/4.0.2/_/_/package/17df7fe8d57e8b6a03892bf29967b64f27a1dc67/bin/swiglib`


I feel a little dumb.
I forgot for a moment I had to rebuild swig because my state-of-the-art distro (:roll_eyes:) has gcc 11 which is not yet available on c3i.
Sorry for that...
When downloading swig for gcc-10, I can reproduce your problem.

It's weird. I've added a patch that uses `/proc/self/exe` to find out the location of the executable.
 
https://github.com/conan-io/conan-center-index/blob/05e86d37270fe1efffb5e69eb3db60b2cadfeade/recipes/swig/all/patches/0001-swig-linux-library-path.patch#L11-L21

When running 
```sh
strings $PATH_TO_SWIG_EXECUTABLE |  grep "proc\/self
```
on the swig executable downloaded from conan-center, there are no results.

When running it on a locally built one, I have a result.

I've looked at the build logs at https://github.com/conan-io/conan-center-index/pull/4938#issuecomment-804544538
and for gcc-10+Linux, the build logs contains:
```
checking for unistd.h... yes
```

The only thing else I can do is try to build swig in a docker image and see whether the resulting executable contains the `/proc/self/exe` string.
What! I'm not not only one to make silly errors? That's a relief.

I've been looking at your recipe vs the old bincrafters one. It appears that even the 'old' executable has a hardcoded swig lib that doesn't exist. The difference is that the old recipe also sets the SWIG_LIB environment variable, which overrides the default value that is compiled in. That appears to happen at [this point](https://github.com/bincrafters/conan-swig_installer/blob/a0ad9e307c9581836d47f7fffd396db25f49dab5/conanfile.py#L124) in their recipe.

Perhaps that is all that's needed?


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
@@ -23,18 +23,21 @@ def configure(self):
         del self.settings.compiler.libcxx
         del self.settings.compiler.cppstd
 
+    def package_id(self):
+        del self.info.settings.compiler
+
     def build_requirements(self):
         if tools.os_info.is_windows and not tools.get_env("CONAN_BASH_PATH") \
                 and tools.os_info.detect_windows_subsystem() != "msys2":
-            self.build_requires("msys2/20190524")
+            self.build_requires("msys2/cci.latest")
         if self.settings.compiler == "Visual Studio":
-            self.build_requires("winflexbison/2.5.22")
+            self.build_requires("winflexbison/2.5.24")
         else:
             self.build_requires("bison/3.7.1")
-        self.build_requires("automake/1.16.2")
+        self.build_requires("automake/1.16.3")
 
     def requirements(self):
-        self.requires("pcre/8.41")
+        self.requires("pcre/8.44")
 
     def source(self):
         tools.get(**self.conan_data["sources"][self.version])


Test patch: