Repository: conan-io/conan-center-index

Issue:
[package] pkgconf/1.7.3 and 1.7.4: MinGW build fails
<!-- 
  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: **pkgconf/1.7.4**
  * Operating System+version: **Windows 10**
  * Compiler+version: **GCC 10.3.0 + MinGW 8.0.0**
  * Conan version: **conan 1.38.0**
  * Python version: **Python 3.9.1**


### Conan profile (output of `conan profile show default` or `conan profile show <profile>` if custom profile is in use)
```
[settings]
os=Windows
os_build=Windows
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=10
compiler.libcxx=libstdc++11
compiler.threads=posix
compiler.exception=seh
build_type=Release
[options]
[build_requires]
[env]
```


### Steps to reproduce (Include if Applicable)

`conan install pkgconf/1.7.4@ -b pkgconf`

### Logs (Include/Attach if Applicable)
<details><summary>Click to expand log</summary>

```
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.exception=seh
compiler.libcxx=libstdc++11
compiler.threads=posix
compiler.version=10
os=Windows
os_build=Windows
[options]
[build_requires]
[env]
CC=C:/mingw64/bin/gcc.exe
CXX=C:/mingw64/bin/g++.exe
pkgconf/1.7.4: Forced build from source
Installing package: pkgconf/1.7.4
Requirements
    pkgconf/1.7.4 from 'conan-center' - Cache
Packages
    pkgconf/1.7.4:48882e2c31b13947b68f156127a94913f34be744 - Build
Build requirements
    meson/0.58.1 from 'conan-center' - Cache
    ninja/1.10.2 from 'conan-center' - Cache
Build requirements packages
    meson/0.58.1:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9 - Cache
    ninja/1.10.2:0a420ff5c47119e668867cdb51baff0eca1fdb68 - Cache

Installing (downloading, building) binaries...
ninja/1.10.2: Already installed!
[HOOK - conan-center.py] post_package_info(): [CMAKE FILE NOT IN BUILD FOLDERS (KB-H019)] OK
meson/0.58.1: Already installed!
meson/0.58.1: Appending PATH environment variable: C:\Users\spaceim\.conan\data\meson\0.58.1\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\bin
[HOOK - conan-center.py] post_package_info(): [CMAKE FILE NOT IN BUILD FOLDERS (KB-H019)] OK
pkgconf/1.7.4: Applying build-requirement: meson/0.58.1
pkgconf/1.7.4: Applying build-requirement: ninja/1.10.2
pkgconf/1.7.4: Copying sources to build folder
pkgconf/1.7.4: Building your package in C:\Users\spaceim\.conan\data\pkgconf\1.7.4\_\_\build\48882e2c31b13947b68f156127a94913f34be744
pkgconf/1.7.4: Generator txt created conanbuildinfo.txt
[HOOK - conan-center.py] pre_build(): [FPIC MANAGEMENT (KB-H007)] 'fPIC' option not found
[HOOK - conan-center.py] pre_build(): [FPIC MANAGEMENT (KB-H007)] OK
pkgconf/1.7.4: Calling build()
The Meson build system
Version: 0.58.1
Source dir: C:\Users\spaceim\.conan\data\pkgconf\1.7.4\_\_\build\48882e2c31b13947b68f156127a94913f34be744\source_subfolder
Build dir: C:\Users\spaceim\.conan\data\pkgconf\1.7.4\_\_\build\48882e2c31b13947b68f156127a94913f34be744\build_subfolder
Build type: native build
Project name: pkgconf
Project version: 1.7.4
C compiler for the host machine: C:/mingw64/bin/gcc.exe (gcc 10.3.0 "gcc.exe (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) 10.3.0")
C linker for the host machine: C:/mingw64/bin/gcc.exe ld.bfd 2.36.1
Host machine cpu family: x86_64
Host machine cpu: x86_64
Checking for function "cygwin_conv_path" : NO 
Checking for function "cygwin_conv_path" : NO 
Checking for function "strlcat" : NO 
Checking for function "strlcat" : NO 
Checking for function "strlcpy" : NO 
Checking for function "strlcpy" : NO 
Checking for function "strndup" : YES 
Configuring config.h using configuration
Build targets in project: 2

Found ninja-1.10.2 at C:\Users\spaceim\.conan\data\ninja\1.10.2\_\_\package\0a420ff5c47119e668867cdb51baff0eca1fdb68\bin\ninja.EXE
ninja: Entering directory `C:\Users\spaceim\.conan\data\pkgconf\1.7.4\_\_\build\48882e2c31b13947b68f156127a94913f34be744\build_subfolder'
[1/19] Compiling C object libpkgconf.a.p/libpkgconf_audit.c.obj
[2/19] Compiling C object libpkgconf.a.p/libpkgconf_bsdstubs.c.obj
../source_subfolder/libpkgconf/bsdstubs.c: In function 'pkgconf_strndup':
../source_subfolder/libpkgconf/bsdstubs.c:138:9: warning: implicit declaration of function 'strndup' [-Wimplicit-function-declaration]
  138 |  return strndup(src, len);
      |         ^~~~~~~
../source_subfolder/libpkgconf/bsdstubs.c:138:9: warning: incompatible implicit declaration of built-in function 'strndup'
[3/19] Compiling C object libpkgconf.a.p/libpkgconf_cache.c.obj
[4/19] Compiling C object libpkgconf.a.p/libpkgconf_fileio.c.obj
[5/19] Compiling C object libpkgconf.a.p/libpkgconf_dependency.c.obj
[6/19] Compiling C object libpkgconf.a.p/libpkgconf_parser.c.obj
[7/19] Compiling C object libpkgconf.a.p/libpkgconf_argvsplit.c.obj
[8/19] Compiling C object libpkgconf.a.p/libpkgconf_client.c.obj
../source_subfolder/libpkgconf/client.c: In function 'pkgconf_trace':
../source_subfolder/libpkgconf/client.c:379:40: warning: format '%u' expects argument of type 'unsigned int', but argument 5 has type 'size_t' {aka 'long long unsigned int'} [-Wformat=]
  379 |  len = snprintf(errbuf, sizeof errbuf, "%s:" SIZE_FMT_SPECIFIER " [%s]: ", filename, lineno, funcname);
      |                                        ^~~~~                                         ~~~~~~
      |                                                                                      |
      |                                                                                      size_t {aka long long unsigned int}
In file included from ../source_subfolder/libpkgconf/client.c:17:
..\source_subfolder/libpkgconf/stdinc.h:35:35: note: format string is defined here
   35 | #  define SIZE_FMT_SPECIFIER "%I64u"
      |                               ~~~~^
      |                                   |
      |                                   unsigned int
      |                               %I64llu
[9/19] Compiling C object libpkgconf.a.p/libpkgconf_fragment.c.obj
[10/19] Compiling C object libpkgconf.a.p/libpkgconf_queue.c.obj
[11/19] Compiling C object libpkgconf.a.p/libpkgconf_path.c.obj
[12/19] Compiling C object pkgconf.exe.p/cli_renderer-msvc.c.obj
[13/19] Compiling C object pkgconf.exe.p/cli_getopt_long.c.obj
[14/19] Compiling C object libpkgconf.a.p/libpkgconf_personality.c.obj
[15/19] Compiling C object libpkgconf.a.p/libpkgconf_tuple.c.obj
[16/19] Compiling C object libpkgconf.a.p/libpkgconf_pkg.c.obj
[17/19] Compiling C object pkgconf.exe.p/cli_main.c.obj
[18/19] Linking static target libpkgconf.a
[19/19] Linking target pkgconf.exe
FAILED: pkgconf.exe 
"C:/mingw64/bin/gcc.exe"  -o pkgconf.exe pkgconf.exe.p/cli_main.c.obj pkgconf.exe.p/cli_getopt_long.c.obj pkgconf.exe.p/cli_renderer-msvc.c.obj "-Wl,--allow-shlib-undefined" "-Wl,-O1" "-m64" "-m64" "-O3" "-s" "-DNDEBUG" "-Wl,--start-group" "libpkgconf.a" "-Wl,--subsystem,console" "-lkernel32" "-luser32" "-lgdi32" "-lwinspool" "-lshell32" "-lole32" "-loleaut32" "-luuid" "-lcomdlg32" "-ladvapi32" "-Wl,--end-group"
c:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: libpkgconf.a(libpkgconf_bsdstubs.c.obj):bsdstubs.c:(.text+0x121): undefined reference to `strndup'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
pkgconf/1.7.4: 
```

</details>

If I revert modifications in https://github.com/conan-io/conan-center-index/pull/5758, it works.


Discussion:
@madebr any idea about this issue?
AFAIK `strndup` is not a standard function (so it may or may not exist in the given compiler)
yes, but how to fix the patch in pkgconf to make everybody happy? The patch does not refer specifically to `strndup`, if fixes a "meson function" called at configure time to check existence of several functions.
The fix for fedora/clang (https://github.com/conan-io/conan-center-index/pull/5758) broke MinGW@Windows.
https://linux.die.net/man/3/strndup
```
strndup():
Since glibc 2.10:
POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
Before glibc 2.10:
_GNU_SOURCE
```
https://en.cppreference.com/w/c/experimental/dynamic/strndup
```
As all functions from Dynamic Memory TR, strndup is only guaranteed to be available if __STDC_ALLOC_LIB__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT2__ to the integer constant 1 before including string.h.
```

Resolving patch:
diff --git a/recipes/pkgconf/all/conanfile.py b/recipes/pkgconf/all/conanfile.py
--- a/recipes/pkgconf/all/conanfile.py
+++ b/recipes/pkgconf/all/conanfile.py
@@ -42,12 +42,12 @@ def configure(self):
         del self.settings.compiler.libcxx
         del self.settings.compiler.cppstd
 
+    def build_requirements(self):
+        self.build_requires("meson/0.59.0")
+
     def source(self):
         tools.get(**self.conan_data["sources"][self.version], destination=self._source_subfolder, strip_root=True)
 
-    def build_requirements(self):
-        self.build_requires("meson/0.58.1")
-
     @property
     def _sharedstatedir(self):
         return os.path.join(self.package_folder, "bin", "share")
@@ -62,7 +62,7 @@ def _configure_meson(self):
         return self._meson
 
     def _patch_sources(self):
-        for patch in self.conan_data["patches"][self.version]:
+        for patch in self.conan_data.get("patches", {}).get(self.version, []):
             tools.patch(**patch)
         if not self.options.shared:
             tools.replace_in_file(os.path.join(self._source_subfolder, "meson.build"),


Test patch:
diff --git a/recipes/pkgconf/all/test_package/conanfile.py b/recipes/pkgconf/all/test_package/conanfile.py
--- a/recipes/pkgconf/all/test_package/conanfile.py
+++ b/recipes/pkgconf/all/test_package/conanfile.py
@@ -8,17 +8,20 @@ class TestPackageConan(ConanFile):
     settings = "os", "arch", "compiler", "build_type"
     generators = "cmake"
 
+    @property
+    def _settings_build(self):
+        return getattr(self, "settings_build", self.settings)
+
     def build_requirements(self):
         self.build_requires("automake/1.16.3")
-        if tools.os_info.is_windows and not tools.get_env("CONAN_BASH_PATH") \
-                and tools.os_info.detect_windows_subsystem() != "msys2":
+        if self._settings_build.os == "Windows" and not tools.get_env("CONAN_BASH_PATH"):
             self.build_requires("msys2/cci.latest")
 
     def build(self):
         # Test pkg.m4 integration into automake
         shutil.copy(os.path.join(self.source_folder, "configure.ac"),
                     os.path.join(self.build_folder, "configure.ac"))
-        self.run("autoreconf -fiv", run_environment=True, win_bash=tools.os_info.is_windows)
+        self.run("{} -fiv".format(tools.get_env("AUTORECONF")), run_environment=True, win_bash=tools.os_info.is_windows)
         autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows)
         with tools.environment_append(RunEnvironment(self).vars):
             autotools.configure()
@@ -28,7 +31,7 @@ def build(self):
         cmake.build()
 
     def test(self):
-        if not tools.cross_building(self.settings):
+        if not tools.cross_building(self):
             self.run(os.path.join("bin", "test_package"), run_environment=True)
 
             pkg_config = tools.get_env("PKG_CONFIG")