Repository: conan-io/conan-center-index

Issue:
[package] 7zip/17.00: does not build when using mingw compiler
7zip for Windows does not build (needed for a 32-bit target) when needed for a mingw target (`build_policy` is set to `missing`).

The current 7zip recipe on Windows assumes that the compiler on Windows is Visual Studio.

See this error:
https://ci.appveyor.com/project/madebr/conan-cc65-installer/builds/27788555/job/mv109nwpcoc6s2kf#L1557
`gcc` is mixed with `link`.


### Package and Environment Details (include every applicable attribute)
  * Package Name/Version: **7zip/17.00**
  * Operating System+version: **Windows** (appveyor)
  * Compiler+version: **GCC 7**
  * Conan version: **conan 1.18.5**
  * Python version: **Python 3.7.x**


### Logs (Include/Attach if Applicable)
<details><summary>Click to expand log</summary>

```
7zip/19.00: Calling build()
Found Visual Studio 14
Microsoft (R) Program Maintenance Utility Version 14.00.24210.0
Copyright (C) Microsoft Corporation.  All rights reserved.
	cd UI
	"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe" -nologo
	cd Client7z
	"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe" -nologo 
	if not exist "x64" mkdir "x64"
	if not exist "x64/asm" mkdir "x64/asm"
	C:/Users/appveyor/.conan/data/mingw_installer/1.0/conan/stable/package/8c3c054ccca80f623bdfdc301f27c3a281e37b54/bin/gcc.exe  -DUNICODE -D_UNICODE -Gr -nologo -c -Fox64/ -W4 -WX -EHsc -Gy -GR- -GF -MT -GS- -Zc:forScope -Zc:wchar_t -MP2 -D_CONSOLE -O1 -Yc"StdAfx.h" -Fpx64/a.pch StdAfx.cpp
	rc  -fox64\resource.res resource.rc
Microsoft (R) Windows (R) Resource Compiler Version 10.0.10011.16384
Copyright (C) Microsoft Corporation.  All rights reserved.
	C:/Users/appveyor/.conan/data/mingw_installer/1.0/conan/stable/package/8c3c054ccca80f623bdfdc301f27c3a281e37b54/bin/gcc.exe  -DUNICODE -D_UNICODE -Gr -nologo -c -Fox64/ -W4 -WX -EHsc -Gy -GR- -GF -MT -GS- -Zc:forScope -Zc:wchar_t -MP2 -D_CONSOLE -O1 -Yu"StdAfx.h" -Fpx64/a.pch .\Client7z.cpp 
	C:/Users/appveyor/.conan/data/mingw_installer/1.0/conan/stable/package/8c3c054ccca80f623bdfdc301f27c3a281e37b54/bin/gcc.exe  -DUNICODE -D_UNICODE -Gr -nologo -c -Fox64/ -W4 -WX -EHsc -Gy -GR- -GF -MT -GS- -Zc:forScope -Zc:wchar_t -MP2 -D_CONSOLE -O1 -Yu"StdAfx.h" -Fpx64/a.pch ../../../Common\IntToString.cpp ../../../Common\NewHandler.cpp ../../../Common\MyString.cpp ../../../Common\StringConvert.cpp ../../../Common\StringToInt.cpp ../../../Common\MyVector.cpp ../../../Common\Wildcard.cpp 
	C:/Users/appveyor/.conan/data/mingw_installer/1.0/conan/stable/package/8c3c054ccca80f623bdfdc301f27c3a281e37b54/bin/gcc.exe  -DUNICODE -D_UNICODE -Gr -nologo -c -Fox64/ -W4 -WX -EHsc -Gy -GR- -GF -MT -GS- -Zc:forScope -Zc:wchar_t -MP2 -D_CONSOLE -O1 -Yu"StdAfx.h" -Fpx64/a.pch ../../../Windows\DLL.cpp ../../../Windows\FileDir.cpp ../../../Windows\FileFind.cpp ../../../Windows\FileIO.cpp ../../../Windows\FileName.cpp ../../../Windows\PropVariant.cpp ../../../Windows\PropVariantConv.cpp 
	C:/Users/appveyor/.conan/data/mingw_installer/1.0/conan/stable/package/8c3c054ccca80f623bdfdc301f27c3a281e37b54/bin/gcc.exe  -DUNICODE -D_UNICODE -Gr -nologo -c -Fox64/ -W4 -WX -EHsc -Gy -GR- -GF -MT -GS- -Zc:forScope -Zc:wchar_t -MP2 -D_CONSOLE -O1 -Yu"StdAfx.h" -Fpx64/a.pch ../../Common\FileStreams.cpp 
	link  -nologo -OPT:REF -OPT:ICF /LARGEADDRESSAWARE /FIXED:NO -out:x64\7zcl.exe x64\StdAfx.obj  x64\Client7z.obj  x64\IntToString.obj  x64\NewHandler.obj  x64\MyString.obj  x64\StringConvert.obj  x64\StringToInt.obj  x64\MyVector.obj  x64\Wildcard.obj  x64\DLL.obj  x64\FileDir.obj  x64\FileFind.obj  x64\FileIO.obj  x64\FileName.obj  x64\PropVariant.obj  x64\PropVariantConv.obj    x64\FileStreams.obj                                                x64\resource.res  oleaut32.lib ole32.lib user32.lib advapi32.lib shell32.lib
LINK : fatal error LNK1181: cannot open input file 'x64\StdAfx.obj'
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\link.EXE"' : return code '0x49d'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.
7zip/19.00: 
7zip/19.00: ERROR: Package 'b2fcbc60b3bee75023a4f7934cadb723eec550bd' build failed
7zip/19.00: WARN: Build folder C:\Users\appveyor\.conan\data\7zip\19.00\_\_\build\b2fcbc60b3bee75023a4f7934cadb723eec550bd
Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\conans\errors.py", line 34, in conanfile_exception_formatter
    yield
  File "C:\Python37\lib\site-packages\conans\client\installer.py", line 121, in _build
    conanfile.build()
  File "C:\Users\appveyor\.conan\data\7zip\19.00\_\_\export\conanfile.py", line 42, in build
    self.run("%s && nmake /f makefile" % vcvars)
  File "C:\Python37\lib\site-packages\conans\model\conan_file.py", line 273, in run
    raise ConanException("Error %d while executing %s" % (retcode, command))
conans.errors.ConanException: Error 2 while executing call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC/vcvarsall.bat" x86_amd64 && nmake /f makefile
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "build.py", line 25, in <module>
    builder.run()
  File "C:\Python37\lib\site-packages\cpt\packager.py", line 499, in run
    self.run_builds(base_profile_name=base_profile_name)
  File "C:\Python37\lib\site-packages\cpt\packager.py", line 582, in run_builds
    r.run()
  File "C:\Python37\lib\site-packages\cpt\runner.py", line 114, in run
    test_folder=self._test_folder)
  File "C:\Python37\lib\site-packages\conans\client\conan_api.py", line 78, in wrapper
    return f(*args, **kwargs)
  File "C:\Python37\lib\site-packages\conans\client\conan_api.py", line 364, in create
    test_build_folder, test_folder, conanfile_path)
  File "C:\Python37\lib\site-packages\conans\client\cmd\create.py", line 43, in create
    test_build_folder=test_build_folder)
  File "C:\Python37\lib\site-packages\conans\client\cmd\test.py", line 42, in install_build_and_test
    keep_build=keep_build)
  File "C:\Python37\lib\site-packages\conans\client\manager.py", line 81, in install
    installer.install(deps_graph, remotes, keep_build=keep_build, graph_info=graph_info)
  File "C:\Python37\lib\site-packages\conans\client\installer.py", line 310, in install
    self._build(nodes_by_level, keep_build, root_node, graph_info, remotes)
  File "C:\Python37\lib\site-packages\conans\client\installer.py", line 332, in _build
    self._handle_node_cache(node, keep_build, processed_package_refs, remotes)
  File "C:\Python37\lib\site-packages\conans\client\installer.py", line 396, in _handle_node_cache
    pref = self._build_package(node, output, keep_build, remotes)
  File "C:\Python37\lib\site-packages\conans\client\installer.py", line 438, in _build_package
    pref = builder.build_package(node, keep_build, self._recorder, remotes)
  File "C:\Python37\lib\site-packages\conans\client\installer.py", line 225, in build_package
    raise exc
  File "C:\Python37\lib\site-packages\conans\client\installer.py", line 211, in build_package
    self._build(conanfile, pref, build_folder)
  File "C:\Python37\lib\site-packages\conans\client\installer.py", line 132, in _build
    raise exc
  File "C:\Python37\lib\site-packages\conans\client\installer.py", line 121, in _build
    conanfile.build()
  File "C:\Python37\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Python37\lib\site-packages\conans\errors.py", line 40, in conanfile_exception_formatter
    raise ConanExceptionInUserConanfileMethod(msg)
conans.errors.ConanExceptionInUserConanfileMethod: 7zip/19.00: Error in build() method, line 42
	self.run("%s && nmake /f makefile" % vcvars)
	ConanException: Error 2 while executing call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC/vcvarsall.bat" x86_amd64 && nmake /f makefile
```

</details>



Discussion:
This building service does not support MinGW so it will be difficult to test a fix for this here. However, any fix to support MinGW will be always welcomed 😄 

Resolving patch:
diff --git a/recipes/7zip/19.00/conanfile.py b/recipes/7zip/19.00/conanfile.py
--- a/recipes/7zip/19.00/conanfile.py
+++ b/recipes/7zip/19.00/conanfile.py
@@ -13,11 +13,13 @@ class Package7Zip(ConanFile):
     author = "Conan Community"
     homepage = "https://www.7-zip.org"
     topics = ("conan", "7zip", "zip", "compression", "decompression")
-    settings = "os_build", "arch_build"
+    settings = "os_build", "arch_build", "compiler"
 
     def configure(self):
         if self.settings.os_build != "Windows":
             raise ConanInvalidConfiguration("Only Windows supported")
+        if self.settings.arch_build not in ("x86", "x86_64"):
+            raise ConanInvalidConfiguration("Unsupported architecture")
 
     def source(self):
         tools.download(**self.conan_data["sources"][self.version])
@@ -33,19 +35,31 @@ def _uncompress_7z(self, filename):
         tools.get(**self.conan_data["externals"]["lzma"])
         self.run("lzma920\\7zr.exe x {}".format(filename))
 
+    _msvc_platforms = {
+        'x86_64': 'x64',
+        'x86': 'x86',
+    }
+
+    def _build_msvc(self):
+        env_build = VisualStudioBuildEnvironment(self)
+        with tools.environment_append(env_build.vars):
+            vcvars = tools.vcvars_command(self.settings)
+            with tools.chdir("CPP/7zip"):
+                self.run("%s && nmake /f makefile PLATFORM=%s" % (
+                vcvars, self._msvc_platforms[str(self.settings.arch_build)]))
+
+    def _build_autotools(self):
+        # TODO: Enable non-Windows methods in configure
+        env_build = AutoToolsBuildEnvironment(self)
+        with tools.environment_append(env_build.vars):
+            with tools.chdir("CPP/7zip/Bundles/LzmaCon"):
+                self.run("make -f makefile.gcc all")
+
     def build(self):
-        if self.settings.os_build == "Windows":
-            env_build = VisualStudioBuildEnvironment(self)
-            with tools.environment_append(env_build.vars):
-                vcvars = tools.vcvars_command(self.settings)
-                with tools.chdir("CPP/7zip"):
-                    self.run("%s && nmake /f makefile" % vcvars)
+        if self.settings.compiler == "Visual Studio":
+            self._build_msvc()
         else:
-            # TODO: Enable non-Windows methods in configure
-            env_build = AutoToolsBuildEnvironment(self)
-            with tools.environment_append(env_build.vars):
-                with tools.chdir("CPP/7zip/Bundles/LzmaCon"):
-                    self.run("make -f makefile.gcc all")
+            self._build_autotools()
 
     def package(self):
         self.copy("DOC/License.txt", src="", dst="licenses")
@@ -56,6 +70,9 @@ def package(self):
 
         # TODO: Package the libraries: binaries and headers (add the rest of settings)
 
+    def package_id(self):
+        del self.info.settings.compiler
+
     def package_info(self):
         bin_path = os.path.join(self.package_folder, "bin")
         self.output.info('Appending PATH environment variable: %s' % bin_path)


Test patch: