Repository: conan-io/conan-center-index

Issue:
[package] m4/1.4.19: Runtime assertion windows pops up on MSVC in Debug mode

### Package and Environment Details (include every applicable attribute)
  * Package Name/Version: **m4/1.4.19**
  * Operating System+version: **Windows 10**
  * Compiler+version: **MSVC 2017, 2019 in DEBUG**
  * Conan version: **conan 1.44.0**
  * Python version: **Python 3.9.5**

I have a recipe that depends on bison. 3.7.6 gives me parse errors, so I keep it at 3.7.1. I had pinned the exact bison/3.7.1 recipe and all worked well. I am trying to update my dependencies, so I tried the newest bison/3.7.1 revision, and I am now getting an assertion popup in m4.

I have tried various combinations to see if I could pinpoint what changed. My conclusion is that it's the move from m4/1.4.18 to m4/1.4.19 (I have tried the newest recipe revision of m4/1.4.18 and it worked, and the only m4/1.4.19 available fails).

```python
        # Latest bison/3.7.1 with m4/1.4.18
        #self.build_requires("bison/3.7.1#dcffa3dd9204cb79ac7ca09a7f19bb8b") #  Works

        # First bison/3.7.1 with m4/1.4.19
        self.build_requires("bison/3.7.1#47f49e709ddb9f8e055471c4e3c4e67d"): # Fails

        # Latest bison/3.7.1
        #self.build_requires("bison/3.7.1#ad29e804e82c8b6d58765096676b5a5e") # Fails
```

<img width="886" alt="Screen Shot 2022-01-14 at 13 35 01" src="https://user-images.githubusercontent.com/5479063/149528919-eff87be5-e7cc-4924-bb52-e67246e3cf1f.png">





### Conan profile (output of `conan profile show default` or `conan profile show <profile>` if custom profile is in use)

```
[settings]
arch=x86_64
arch_build=x86_64
build_type=Debug
compiler=Visual Studio
compiler.runtime=MDd
compiler.version=16
os=Windows
os_build=Windows
[options]
[build_requires]
[env]
```


### Steps to reproduce (Include if Applicable)

```
git clone git@github.com:NREL/conan-openstudio-ruby.git
git checkout update_ruby_installer_and_remotes

'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat' x64

conan create . openstudio_ruby/2.7.2@nrel/testing -b missing -b openstudio_ruby -s build_type=Debug
```

### Logs (Include/Attach if Applicable)

Example of a failed run: https://github.com/NREL/conan-openstudio-ruby/runs/4787415523?check_suite_focus=true



Discussion:
@madebr is this `"reb"` on purpose? https://github.com/conan-io/conan-center-index/blob/cd61e7b56ab85f9f49ace2b4df6182ebefc20aec/recipes/m4/all/patches/1.4.19-0001-open-files-in-binary-mode.patch#L12

The 1.4.18 version has `"rb"` (which is familar to me, unlike reb...): https://github.com/conan-io/conan-center-index/blob/cd61e7b56ab85f9f49ace2b4df6182ebefc20aec/recipes/m4/all/patches/1.4.18-0008-open-files-in-binary-mode.patch#L12
The patch is adding `'b'` (it is already `"re"`).
It looks like it is a GNU extension:
```
       e (since glibc 2.7)
              Open  the  file with the O_CLOEXEC flag.  See open(2) for more information.  This flag is ignored for
              fdopen().
```

The problem you're seeing with debug 2019 is probably the same we saw when adding m4/1.4.19.
See https://github.com/conan-io/conan-center-index/pull/7369 for our analysis at that time.

We have [a short reproducer](https://github.com/conan-io/conan-center-index/pull/7369#issuecomment-924508173).

I don't remember why we didn't patch out the `_close` of `fclose`.
I just got the same issue building bison/3.7.6 in debug FYI

![image](https://user-images.githubusercontent.com/5479063/150038004-a6132d3c-82d4-47f2-81c6-804e9ebd1d08.png)

The problem is with m4. The bison version is kinda irrelevant.
m4 is only used as a build requirement since it provides no library.
I think you can work around the problem by adding `-s:b m4:build_type=Release`. 
I'd like to do that, but unfortunately all my attemps of forcing the build_requires to be in Release mode failed when using CPT and docker (locally it works plently fine if I issue the conan create command manually)

https://github.com/conan-io/conan-package-tools/issues/595#issuecomment-1015389623
@jmarrec have you already tried asking in m4 mail list, as it was discussed in #7369? it seems like there is no configure option to bypass the check, and proper fix may require to patch m4 sources.

Resolving patch:
diff --git a/recipes/m4/all/conanfile.py b/recipes/m4/all/conanfile.py
--- a/recipes/m4/all/conanfile.py
+++ b/recipes/m4/all/conanfile.py
@@ -27,7 +27,7 @@ def _settings_build(self):
 
     @property
     def _is_msvc(self):
-        return self.settings.compiler == "Visual Studio"
+        return self.settings.compiler == "Visual Studio" or self.settings.compiler == "msvc"
 
     def build_requirements(self):
         if self._settings_build.os == "Windows" and not tools.get_env("CONAN_BASH_PATH"):
@@ -46,7 +46,7 @@ def _configure_autotools(self):
         autotools = AutoToolsBuildEnvironment(self, win_bash=self._settings_build.os == "Windows")
         build_canonical_name = None
         host_canonical_name = None
-        if self.settings.compiler == "Visual Studio":
+        if self._is_msvc:
             # The somewhat older configure script of m4 does not understand the canonical names of Visual Studio
             build_canonical_name = False
             host_canonical_name = False
@@ -62,14 +62,18 @@ def _configure_autotools(self):
         elif self.settings.compiler == "clang":
             if tools.Version(self.version) < "1.4.19":
                 autotools.flags.extend(["-rtlib=compiler-rt", "-Wno-unused-command-line-argument"])
+        if self.settings.os == 'Windows':
+            conf_args.extend(["ac_cv_func__set_invalid_parameter_handler=yes"])
+
         autotools.configure(args=conf_args, configure_dir=self._source_subfolder, build=build_canonical_name, host=host_canonical_name)
         return autotools
 
     @contextmanager
     def _build_context(self):
-        if self.settings.compiler == "Visual Studio":
+        env = {"PATH": [os.path.abspath(self._source_subfolder)]}
+        if self._is_msvc:
             with tools.vcvars(self.settings):
-                env = {
+                env.update({
                     "AR": "{}/build-aux/ar-lib lib".format(tools.unix_path(self._source_subfolder)),
                     "CC": "cl -nologo",
                     "CXX": "cl -nologo",
@@ -78,17 +82,22 @@ def _build_context(self):
                     "OBJDUMP": ":",
                     "RANLIB": ":",
                     "STRIP": ":",
-                }
+                })
                 with tools.environment_append(env):
                     yield
         else:
-            yield
+            with tools.environment_append(env):
+                yield
 
     def _patch_sources(self):
         for patch in self.conan_data.get("patches", {}).get(self.version, []):
             tools.patch(**patch)
 
     def build(self):
+        with tools.chdir(self._source_subfolder):
+            tools.save("help2man", '#!/usr/bin/env bash\n:')
+            if os.name == 'posix':
+                os.chmod("help2man", os.stat("help2man").st_mode | 0o111)
         self._patch_sources()
         with self._build_context():
             autotools = self._configure_autotools()


Test patch: