Repository: conan-io/conan-center-index

Issue:
[package] swig/4.0.1: wrong bison used in windows with MinGW
<!-- 
  Please don't forget to update the issue title.
  Include all applicable information to help us reproduce your problem.
-->

### Package and Environment Details
  * Package Name/Version: **swig/4.0.1**
  * Operating System+version: **Windows 10*
  * Compiler+version: **GCC 8 (MinGW)**
  * Conan version: **conan 1.27.1**
  * Python version: **Python 3.7.4**


### Conan profile
```
[settings]
os=Windows
os_build=Windows
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=8
build_type=Release
compiler.exception=seh
compiler.libcxx=libstdc++11
compiler.threads=posix
[options]
[build_requires]
mingw_installer/1.0@conan/stable
[env]

```

### Steps to reproduce (Include if Applicable)

`conan install -pr=mingw-w64 --build=missing swig/4.0.1@`

Build fails because swig tries to use winflexbison/2.5.22.


### Logs from winflexbison
```
......
-- Conan: Using cmake global configuration
-- Conan: Adjusting default RPATHs Conan policies
-- Conan: Adjusting language standard
-- Conan setting CPP STANDARD: 14 WITH EXTENSIONS OFF
-- Conan: Compiler GCC>=5, checking major version 8.1
-- Conan: Checking correct version: 8.1
-- Conan: C++ stdlib: libstdc++11
CMake Error at source_subfolder/CMakeLists.txt:6 (message):
  Visual Studio Build supported only
```

See here:
https://github.com/conan-io/conan-center-index/issues/1967

### Root cause

In the conanfile.py of swig, the build requirements are wrong:

```python
    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")
        if tools.os_info.is_windows: // Bad!
            self.build_requires("winflexbison/2.5.22")
        else:
            self.build_requires("bison/3.5.3")
        self.build_requires("automake/1.16.2")
```

`winflexbison` only works with Visual Studio. So `bison` should be used instead.




Discussion:
After fixing this error, I get another one:

```
swig/4.0.1: Copying sources to build folder
swig/4.0.1: Building your package in C:\Users\...\.conan\data\swig\4.0.1\_\_\build\94cc29fcbe006625851f8cb3f61460b0a9711fb3
swig/4.0.1: Generator txt created conanbuildinfo.txt
swig/4.0.1: Calling build()
swig/4.0.1: run_in_windows_bash: C:\.conan\7c82f526\1\bin\usr\bin\bash.exe --login -c ^"cd \^"/c/users/.../.conan/data/swig/4.0.1/_/_/build/94cc29fcbe006625851f8cb3f61460b0a9711fb3/source_subfolder\^" ^&^& PATH=\^"/c/users/.../.conan/data/ninja_installer/1.9.0/bincra
fters/stable/package/456f15897172eef340fcbac8a70811f2beb26a93/bin:/c/users/.../.conan/data/cmake_installer/3.14.2/conan/stable/package/60b04a80ac4dd5c23f1acffacc679e9902e636e5/bin:/c/users/.../.conan/data/mingw_installer/1.0/conan/stable/package/5be7c40ee82c6e1540292
990cc561633993c5650/bin:/c/.conan/7c82f526/1/bin/usr/bin:/c/users/.../.conan/data/bison/3.5.3/_/_/package/74e99a0ab97aae69780f9bca4b455c5c1ff7c437/bin:/c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin:/c/users/.../
.conan/data/autoconf/2.69/_/_/package/456f15897172eef340fcbac8a70811f2beb26a93/bin:/c/users/.../.conan/data/m4/1.4.18/_/_/package/5e2231d46e594565cc5301666a572443f2ab35c2/bin:$PATH\^" ^&^& ./autogen.sh ^"
swig/4.0.1: + test -d Tools/config
swig/4.0.1: + /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/aclocal.exe -I Tools/config
swig/4.0.1: + /c/users/.../.conan/data/autoconf/2.69/_/_/package/456f15897172eef340fcbac8a70811f2beb26a93/bin/autoheader.exe
swig/4.0.1: + /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/automake.exe --add-missing --copy --force-missing
swig/4.0.1: configure.ac: error: no proper invocation of AM_INIT_AUTOMAKE was found.
swig/4.0.1: configure.ac: You should verify that configure.ac invokes AM_INIT_AUTOMAKE,
swig/4.0.1: configure.ac: that aclocal.m4 is present in the top-level directory,
swig/4.0.1: configure.ac: and that aclocal.m4 was recently regenerated (using aclocal)
swig/4.0.1: configure.ac:11: installing 'Tools/config/config.guess'
swig/4.0.1: configure.ac:11: installing 'Tools/config/config.sub'
swig/4.0.1: Source/Makefile.am: installing 'Tools/config/depcomp'
swig/4.0.1: /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/share/automake-1.16/am/depend2.am: error: am__fastdepCC does not appear in AM_CONDITIONAL
swig/4.0.1: /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/share/automake-1.16/am/depend2.am:   The usual way to define 'am__fastdepCC' is to add 'AC_PROG_CC'
swig/4.0.1: /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/share/automake-1.16/am/depend2.am:   to 'configure.ac' and run 'aclocal' and 'autoconf' again
swig/4.0.1: /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/share/automake-1.16/am/depend2.am: error: AMDEP does not appear in AM_CONDITIONAL
swig/4.0.1: /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/share/automake-1.16/am/depend2.am:   The usual way to define 'AMDEP' is to add one of the compiler tests
swig/4.0.1: /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/share/automake-1.16/am/depend2.am:     AC_PROG_CC, AC_PROG_CXX, AC_PROG_OBJC, AC_PROG_OBJCXX,
swig/4.0.1: /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/share/automake-1.16/am/depend2.am:     AM_PROG_AS, AM_PROG_GCJ, AM_PROG_UPC
swig/4.0.1: /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/share/automake-1.16/am/depend2.am:   to 'configure.ac' and run 'aclocal' and 'autoconf' again
swig/4.0.1: /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/share/automake-1.16/am/depend2.am: error: am__fastdepCXX does not appear in AM_CONDITIONAL
swig/4.0.1: /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/share/automake-1.16/am/depend2.am:   The usual way to define 'am__fastdepCXX' is to add 'AC_PROG_CXX'
swig/4.0.1: /c/users/.../.conan/data/automake/1.16.2/_/_/package/3e48e69237f7f2196164383ef9dedf0f93cbf249/bin/share/automake-1.16/am/depend2.am:   to 'configure.ac' and run 'aclocal' and 'autoconf' again
swig/4.0.1: configure.ac: installing 'Tools/config/ylwrap'
swig/4.0.1: 
swig/4.0.1: ERROR: Package '94cc29fcbe006625851f8cb3f61460b0a9711fb3' build failed
swig/4.0.1: WARN: Build folder C:\Users\...\.conan\data\swig\4.0.1\_\_\build\94cc29fcbe006625851f8cb3f61460b0a9711fb3

: swig/4.0.1: Error in build() method, line 94
        self.run("./autogen.sh", win_bash=tools.os_info.is_windows)
        ConanException: Error 1 while executing ./autogen.sh
```
Does replacing
```python
        if tools.os_info.is_windows: // Bad!
            self.build_requires("winflexbison/2.5.22")
        else:
            self.build_requires("bison/3.5.3")
````
with
```python
        if self.settings.compiler == "Visual Studio":
            self.build_requires("winflexbison/2.5.22")
        else:
            self.build_requires("bison/3.5.3")
```
fix your initial problem?

I think your second problem is the same as the one experienced in the wolfssl pr at https://github.com/conan-io/conan-center-index/pull/2294. Can you try removing the requirement on `m4` in the `autoconf` recipe?
Yes `if self.settings.compiler == "Visual Studio":` works great. No it is using bison!

I had to remove `m4` from autoconf **and** bison. Both required it. So what is the condition on the settings to require it or not? 

Should I make one PR for all 3 packages?


I.'ve been too early pleased - now swig fails to compile cscanner.c:

```
swig/4.0.1: In file included from C:/users/.../.conan/data/swig/4.0.1/_/_/build/94cc29fcbe006625851f8cb3f61460b0a9711fb3/source_subfolder/Source/CParse/cscanner.c:19:
swig/4.0.1: c:\users\...\.conan\data\mingw_installer\1.0\conan\stable\package\5be7c40ee82c6e1540292990cc561633993c5650\x86_64-w64-mingw32\include\parser.h:46:1: error: unknown type name 'class'
```
There "parser.h" file in mingw has a class, which cannot be parsed by a C-compiler. What the hell is going on?



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
@@ -27,10 +27,10 @@ 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")
-        if tools.os_info.is_windows:
+        if self.settings.compiler == "Visual Studio":
             self.build_requires("winflexbison/2.5.22")
         else:
-            self.build_requires("bison/3.5.3")
+            self.build_requires("bison/3.7.1")
         self.build_requires("automake/1.16.2")
 
     def requirements(self):
@@ -42,18 +42,22 @@ def source(self):
 
     @contextmanager
     def _build_context(self):
+        env = {}
+        if self.settings.compiler != "Visual Studio":
+            env["YACC"] = self.deps_user_info["bison"].YACC
         if self.settings.compiler == "Visual Studio":
             with tools.vcvars(self.settings):
-                env = {
+                env.update({
                     "CC": "{} cl -nologo".format(tools.unix_path(self.deps_user_info["automake"].compile)),
                     "CXX": "{} cl -nologo".format(tools.unix_path(self.deps_user_info["automake"].compile)),
                     "AR": "{} link".format(self.deps_user_info["automake"].ar_lib),
                     "LD": "link",
-                }
+                })
                 with tools.environment_append(env):
                     yield
         else:
-            yield
+            with tools.environment_append(env):
+                yield
 
     def _configure_autotools(self):
         if self._autotools:
@@ -73,15 +77,21 @@ def _configure_autotools(self):
             "--host={}".format(self.settings.arch),
             "--with-swiglibdir={}".format(self._swiglibdir),
         ]
+
+        host, build = None, None
+
         if self.settings.compiler == "Visual Studio":
             self.output.warn("Visual Studio compiler cannot create ccache-swig. Disabling ccache-swig.")
             args.append("--disable-ccache")
             self._autotools.flags.append("-FS")
+            # MSVC canonical names aren't understood
+            host, build = False, False
 
         self._autotools.libs = []
         self._autotools.library_paths = []
 
-        self._autotools.configure(args=args, configure_dir=self._source_subfolder)
+        self._autotools.configure(args=args, configure_dir=self._source_subfolder,
+                                  host=host, build=build)
         return self._autotools
 
     def _patch_sources(self):
@@ -105,7 +115,7 @@ def package(self):
 
         if self.settings.compiler != "Visual Studio":
             with tools.chdir(os.path.join(self.package_folder, "bin")):
-                strip = tools.get_env("STRIP") or tools.which("strip")
+                strip = (tools.get_env("STRIP") or tools.which("strip")).replace("\\", "/")
                 ext = ".exe" if tools.os_info.is_windows else ""
                 if strip:
                     self.run("{} swig{}".format(strip, ext), win_bash=tools.os_info.is_windows)


Test patch:
diff --git a/recipes/swig/all/test_package/CMakeLists.txt b/recipes/swig/all/test_package/CMakeLists.txt
--- a/recipes/swig/all/test_package/CMakeLists.txt
+++ b/recipes/swig/all/test_package/CMakeLists.txt
@@ -1,4 +1,4 @@
-cmake_minimum_required(VERSION 2.8.12)
+cmake_minimum_required(VERSION 3.1)
 project(PackageTest C)
 
 include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)