Repository: conan-io/conan-center-index

Issue:
[question][package] How to compile 7zip in GCC 9.2.0 on Windows?
I'm trying to compile 7zip with the new GCC 9.2.0 from STL.

I created a very simple conanfile.txt:
```
[requires]
7zip/19.00

[generators]
cmake
```

After executing:
`conan install .. -pr mingw32 -b=missing`

I get the following error:
```
C:\Users\User\Documents\Sources\testing\conan>conan install . -pr mingw32 -b=missing
Configuration:
[settings]
arch=x86
arch_build=x86
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=9
os=Windows
os_build=Windows
[options]
[build_requires]
openssl*: msys2/20161025
[env]
CC=C:\MinGW\bin\gcc.exe
CXX=C:\MinGW\bin\g++.exe
7zip/19.00: WARN: Package is corrupted, removing folder: C:\Users\User\.conan\data\7zip\19.00\_\_\package\b2fcbc60b3bee75023a4f7934cadb723eec550bd
conanfile.txt: Installing package
Requirements
    7zip/19.00 from 'conan-center' - Cache
Packages
    7zip/19.00:b2fcbc60b3bee75023a4f7934cadb723eec550bd - Build

7zip/19.00: WARN: Build folder is dirty, removing it: C:\Users\User\.conan\data\7zip\19.00\_\_\build\b2fcbc60b3bee75023a4f7934cadb723eec550bd
7zip/19.00: Copying sources to build folder
7zip/19.00: Building your package in C:\Users\User\.conan\data\7zip\19.00\_\_\build\b2fcbc60b3bee75023a4f7934cadb723eec550bd
7zip/19.00: Generator txt created conanbuildinfo.txt
7zip/19.00: Calling build()
g++ -O2 -c -D_7ZIP_ST ../../../Common/C_FileIO.cpp
g++ -O2 -c -D_7ZIP_ST LzmaAlone.cpp
g++ -O2 -c -D_7ZIP_ST ../../UI/Common/Bench.cpp
g++ -O2 -c -D_7ZIP_ST ../../UI/Console/BenchCon.cpp
g++ -O2 -c -D_7ZIP_ST ../../UI/Console/ConsoleClose.cpp
g++ -O2 -c -D_7ZIP_ST ../../Compress/LzmaDecoder.cpp
g++ -O2 -c -D_7ZIP_ST ../../Compress/LzmaEncoder.cpp
g++ -O2 -c -D_7ZIP_ST ../../Compress/LzmaRegister.cpp
g++ -O2 -c -D_7ZIP_ST ../../Common/CreateCoder.cpp
g++ -O2 -c -D_7ZIP_ST ../../Common/CWrappers.cpp
g++ -O2 -c -D_7ZIP_ST ../../Common/FileStreams.cpp
g++ -O2 -c -D_7ZIP_ST ../../Common/FilterCoder.cpp
g++ -O2 -c -D_7ZIP_ST ../../Common/MethodProps.cpp
g++ -O2 -c -D_7ZIP_ST ../../Common/StreamUtils.cpp
g++ -O2 -c -D_7ZIP_ST ../../../Common/CommandLineParser.cpp
g++ -O2 -c -D_7ZIP_ST ../../../Common/CRC.cpp
g++ -O2 -c -D_7ZIP_ST ../../../Common/CrcReg.cpp
g++ -O2 -c -D_7ZIP_ST ../../../Common/IntToString.cpp
g++ -O2 -c -D_7ZIP_ST ../../../Common/MyString.cpp
g++ -O2 -c -D_7ZIP_ST ../../../Common/MyVector.cpp
g++ -O2 -c -D_7ZIP_ST ../../../Common/MyWindows.cpp
g++ -O2 -c -D_7ZIP_ST ../../../Common/StringConvert.cpp
g++ -O2 -c -D_7ZIP_ST ../../../Common/StringToInt.cpp
g++ -O2 -c -D_7ZIP_ST ../../../Windows/PropVariant.cpp
g++ -O2 -c -D_7ZIP_ST ../../../Windows/System.cpp
gcc -O2 -Wall -Werror -c -D_7ZIP_ST ../../../../C/7zCrc.c
gcc -O2 -Wall -Werror -c -D_7ZIP_ST ../../../../C/7zCrcOpt.c
gcc -O2 -Wall -Werror -c -D_7ZIP_ST ../../../../C/Alloc.c
gcc -O2 -Wall -Werror -c -D_7ZIP_ST ../../../../C/Bra86.c
gcc -O2 -Wall -Werror -c -D_7ZIP_ST ../../../../C/CpuArch.c
gcc -O2 -Wall -Werror -c -D_7ZIP_ST ../../../../C/LzFind.c
gcc -O2 -Wall -Werror -c -D_7ZIP_ST ../../../../C/LzmaDec.c
gcc -O2 -Wall -Werror -c -D_7ZIP_ST ../../../../C/LzmaEnc.c
gcc -O2 -Wall -Werror -c -D_7ZIP_ST ../../../../C/Lzma86Dec.c
gcc -O2 -Wall -Werror -c -D_7ZIP_ST ../../../../C/Lzma86Enc.c
g++ -O2 -o lzma   C_FileIO.o LzmaAlone.o Bench.o BenchCon.o ConsoleClose.o LzmaDecoder.o LzmaEncoder.o LzmaRegister.o CreateCoder.o CWrappers.o FileStreams.o FilterCoder.o MethodProps.o StreamUtils.o CommandLineParser.o CRC.o CrcReg.o IntToString.o MyString.o MyVector.o MyWindows.o StringConvert.o StringToInt.o PropVariant.o System.o 7zCrc.o 7zCrcOpt.o Alloc.o Bra86.o CpuArch.o LzFind.o LzmaDec.o LzmaEnc.o Lzma86Dec.o Lzma86Enc.o
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: LzmaAlone.o:LzmaAlone.cpp:(.text+0x92a): undefined reference to `NWindows::NFile::NIO::CFileBase::GetLength(unsigned long long&) const'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: LzmaAlone.o:LzmaAlone.cpp:(.text+0xc8e): undefined reference to `NWindows::NFile::NIO::CInFile::Open(wchar_t const*)'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: LzmaAlone.o:LzmaAlone.cpp:(.text+0xd00): undefined reference to `NWindows::NFile::NIO::COutFile::Create(wchar_t const*, bool)'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: LzmaAlone.o:LzmaAlone.cpp:(.rdata$.refptr.IID_IUnknown[.refptr.IID_IUnknown]+0x0): undefined reference to `IID_IUnknown'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: Bench.o:Bench.cpp:(.text+0x44a9): undefined reference to `NWindows::NFile::NIO::CInFile::Open(wchar_t const*)'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: Bench.o:Bench.cpp:(.text+0x44c4): undefined reference to `NWindows::NFile::NIO::CFileBase::GetLength(unsigned long long&) const'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: Bench.o:Bench.cpp:(.text+0x453d): undefined reference to `NWindows::NFile::NIO::CInFile::Read(void*, unsigned int, unsigned int&)'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: Bench.o:Bench.cpp:(.text+0x458c): undefined reference to `NWindows::NFile::NIO::CFileBase::Close()'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: Bench.o:Bench.cpp:(.text+0x4cfa): undefined reference to `NWindows::NFile::NIO::CFileBase::Close()'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: Bench.o:Bench.cpp:(.text+0x6995): undefined reference to `NWindows::NFile::NIO::CFileBase::Close()'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text+0x220): undefined reference to `NWindows::NFile::NIO::CFileBase::Seek(long long, unsigned long, unsigned long long&) const'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text+0x22f): undefined reference to `NWindows::NFile::NIO::COutFile::SetLength(unsigned long long)'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text+0x245): undefined reference to `NWindows::NFile::NIO::CFileBase::Seek(unsigned long long, unsigned long long&) const'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text+0x286): undefined reference to `NWindows::NFile::NIO::CInFile::ReadPart(void*, unsigned int, unsigned int&)'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text+0x3c9): undefined reference to `NWindows::NFile::NIO::CFileBase::GetLength(unsigned long long&) const'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text+0x438): undefined reference to `NWindows::NFile::NIO::CFileBase::Seek(long long, unsigned long, unsigned long long&) const'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text+0x4a8): undefined reference to `NWindows::NFile::NIO::CFileBase::Seek(long long, unsigned long, unsigned long long&) const'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text+0x516): undefined reference to `NWindows::NFile::NIO::COutFile::Write(void const*, unsigned int, unsigned int&)'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text+0x669): undefined reference to `NWindows::NFile::NIO::CFileBase::Close()'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text+0x6b9): undefined reference to `NWindows::NFile::NIO::CFileBase::GetLength(unsigned long long&) const'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text+0x47): undefined reference to `NWindows::NFile::NIO::CFileBase::Close()'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text$_ZN14COutFileStreamD1Ev[_ZN14COutFileStreamD1Ev]+0xf): undefined reference to `NWindows::NFile::NIO::CFileBase::Close()'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text$_ZN14COutFileStreamD0Ev[_ZN14COutFileStreamD0Ev]+0x18): undefined reference to `NWindows::NFile::NIO::CFileBase::Close()'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: FileStreams.o:FileStreams.cpp:(.text$_ZN14COutFileStream7ReleaseEv[_ZN14COutFileStream7ReleaseEv]+0x3e): undefined reference to `NWindows::NFile::NIO::CFileBase::Close()'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: MyString.o:MyString.cpp:(.text+0x2026): undefined reference to `__imp_SysStringLen'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: PropVariant.o:PropVariant.cpp:(.text+0x1c): undefined reference to `__imp_SysAllocStringLen'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: PropVariant.o:PropVariant.cpp:(.text+0x7c): undefined reference to `__imp_SysAllocStringLen'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: PropVariant.o:PropVariant.cpp:(.text+0x162): undefined reference to `__imp_VariantClear'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: PropVariant.o:PropVariant.cpp:(.text+0x1d5): undefined reference to `__imp_SysAllocString'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: PropVariant.o:PropVariant.cpp:(.text+0x258): undefined reference to `__imp_SysAllocStringLen'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: PropVariant.o:PropVariant.cpp:(.text+0x2c8): undefined reference to `__imp_SysAllocStringLen'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: PropVariant.o:PropVariant.cpp:(.text+0x3e8): undefined reference to `__imp_SysAllocStringLen'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: PropVariant.o:PropVariant.cpp:(.text+0x600): undefined reference to `__imp_VariantClear'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: PropVariant.o:PropVariant.cpp:(.text+0x630): undefined reference to `__imp_VariantCopy'
collect2.exe: error: ld returned 1 exit status
make: *** [makefile.gcc:78: lzma] Error 1
7zip/19.00:
7zip/19.00: ERROR: Package 'b2fcbc60b3bee75023a4f7934cadb723eec550bd' build failed
7zip/19.00: WARN: Build folder C:\Users\User\.conan\data\7zip\19.00\_\_\build\b2fcbc60b3bee75023a4f7934cadb723eec550bd
ERROR: 7zip/19.00: Error in build() method, line 62
        self._build_autotools()
while calling '_build_autotools', line 56
        self.run("make -f makefile.gcc all")
        ConanException: Error 2 while executing make -f makefile.gcc all
```
It looks like the linker is not recognizing the object files properly and it also links to win32api available in MinGW.


Discussion:
I have tested downloading and compiling 7zip manually.
It works as shown below:
```
C:\Users\User\Desktop\7z1900-src\CPP\7zip\Bundles\LzmaCon>c:\MinGW\set_distro_paths.bat

C:\Users\User\Desktop\7z1900-src\CPP\7zip\Bundles\LzmaCon>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/9.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../src/configure --enable-languages=c,c++ --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-multilib --prefix=/c/temp/gcc/dest --with-sysroot=/c/temp/gcc/dest --disable-libstdcxx-pch --disable-libstdcxx-verbose --disable-nls --disable-shared --disable-win32-registry --with-tune=haswell --enable-threads=posix --enable-libgomp
Thread model: posix
gcc version 9.2.0 (GCC)

C:\Users\User\Desktop\7z1900-src\CPP\7zip\Bundles\LzmaCon>make -f makefile.gcc all
gcc -O2 -Wall -Werror -c ../../../../C/LzFindMt.c
gcc -O2 -Wall -Werror -c ../../../../C/Threads.c
g++ -O2 -c ../../../Windows/FileIO.cpp
g++ -O2 -c LzmaAlone.cpp
g++ -O2 -c ../../UI/Common/Bench.cpp
g++ -O2 -c ../../UI/Console/BenchCon.cpp
g++ -O2 -c ../../UI/Console/ConsoleClose.cpp
g++ -O2 -c ../../Compress/LzmaDecoder.cpp
g++ -O2 -c ../../Compress/LzmaEncoder.cpp
g++ -O2 -c ../../Compress/LzmaRegister.cpp
g++ -O2 -c ../../Common/CreateCoder.cpp
g++ -O2 -c ../../Common/CWrappers.cpp
g++ -O2 -c ../../Common/FileStreams.cpp
g++ -O2 -c ../../Common/FilterCoder.cpp
g++ -O2 -c ../../Common/MethodProps.cpp
g++ -O2 -c ../../Common/StreamUtils.cpp
g++ -O2 -c ../../../Common/CommandLineParser.cpp
g++ -O2 -c ../../../Common/CRC.cpp
g++ -O2 -c ../../../Common/CrcReg.cpp
g++ -O2 -c ../../../Common/IntToString.cpp
g++ -O2 -c ../../../Common/MyString.cpp
g++ -O2 -c ../../../Common/MyVector.cpp
g++ -O2 -c ../../../Common/MyWindows.cpp
g++ -O2 -c ../../../Common/StringConvert.cpp
g++ -O2 -c ../../../Common/StringToInt.cpp
g++ -O2 -c ../../../Windows/PropVariant.cpp
g++ -O2 -c ../../../Windows/System.cpp
gcc -O2 -Wall -Werror -c ../../../../C/7zCrc.c
gcc -O2 -Wall -Werror -c ../../../../C/7zCrcOpt.c
gcc -O2 -Wall -Werror -c ../../../../C/Alloc.c
gcc -O2 -Wall -Werror -c ../../../../C/Bra86.c
gcc -O2 -Wall -Werror -c ../../../../C/CpuArch.c
gcc -O2 -Wall -Werror -c ../../../../C/LzFind.c
gcc -O2 -Wall -Werror -c ../../../../C/LzmaDec.c
gcc -O2 -Wall -Werror -c ../../../../C/LzmaEnc.c
gcc -O2 -Wall -Werror -c ../../../../C/Lzma86Dec.c
gcc -O2 -Wall -Werror -c ../../../../C/Lzma86Enc.c
g++ -O2 -o lzma -s LzFindMt.o Threads.o  FileIO.o LzmaAlone.o Bench.o BenchCon.o ConsoleClose.o LzmaDecoder.o LzmaEncoder.o LzmaRegister.o CreateCoder.o CWrappers.o FileStreams.o FilterCoder.o MethodProps.o StreamUtils.o CommandLineParser.o CRC.o CrcReg.o IntToString.o MyString.o MyVector.o MyWindows.o StringConvert.o StringToInt.o PropVariant.o System.o 7zCrc.o 7zCrcOpt.o Alloc.o Bra86.o CpuArch.o LzFind.o LzmaDec.o LzmaEnc.o Lzma86Dec.o Lzma86Enc.o  -loleaut32 -luuid

C:\Users\User\Desktop\7z1900-src\CPP\7zip\Bundles\LzmaCon>
```
Hi @Facon,

I have moved this issue to this repository as you are using the packages from conan center and it is a specific issue of this recipe.

Unfortunately, I also faced the same issue you report when trying to build 7zip/19.00 from sources with MinGW version 7.

I don't know where the linker error is coming from at this moment 😕 
Those missing symbols are functions from a Windows library.
Looks like it needs to link to `oleaut32` (or `ole32`) and `uuid`. I think.
@madebr is more like conan is not taking the library path on its environment.
Those libraries are already referenced in standard MinGW 9.2.0 environment. This is shown on my second post, where compilation works.
this turns out to be a conan bug. the makefile has a code to detect MinGW:
```
ifdef SystemDrive
IS_MINGW = 1
endif
```
so, it basically checks for the `%SystemDrive%` environment variable. e.g. if I run **set** in plain Windows shell I get:
```
SystemDrive=C:
SystemRoot=C:\WINDOWS
```
while running **set** from the conan:
```
SYSTEMDRIVE=C:
SYSTEMROOT=C:\WINDOWS
```
(the code to check):
```
    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("set")
```
therefore, conan upper-cases environment variable. but they appears to be case-sensitive, at least on certain systems (e.g. Windows), therefore `%SystemDrive%` and `%SYSTEMDRIVE%` might be the different things.
similar earlier report: https://github.com/conan-io/conan/issues/1706
I fixed this locally by setting the environment variable `IS_MINGW=1`.
Is it ok if I open a pr fixing this?

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
@@ -1,5 +1,5 @@
 import os
-from conans import ConanFile, tools, AutoToolsBuildEnvironment, VisualStudioBuildEnvironment
+from conans import ConanFile, tools, AutoToolsBuildEnvironment
 from conans.errors import ConanInvalidConfiguration
 
 
@@ -27,35 +27,48 @@ def source(self):
         tools.download(url, filename)
         tools.check_sha256(filename, sha256)
         self._uncompress_7z(filename)
+        os.unlink(filename)
 
     def build_requirements(self):
         if not tools.which("7zr"):
             self.build_requires("lzma_sdk/9.20")
 
+        if tools.os_info.is_windows and "make" not in os.environ.get("CONAN_MAKE_PROGRAM", ""):
+            self.build_requires("make/4.2.1")
+
     def _uncompress_7z(self, filename):
         self.run("7zr x {}".format(filename))
 
     _msvc_platforms = {
-        'x86_64': 'x64',
-        'x86': 'x86',
+        "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)]))
+        with tools.vcvars(self.settings):
+            with tools.chdir(os.path.join("CPP", "7zip")):
+                self.run("nmake /f makefile PLATFORM=%s" % (
+                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")
+        autotools = AutoToolsBuildEnvironment(self)
+        extra_env = {}
+        if self.settings.os_build == "Windows" and self.settings.compiler == "gcc":
+            extra_env["IS_MINGW"] = "1"
+        with tools.environment_append(extra_env):
+            with tools.chdir(os.path.join("CPP", "7zip", "Bundles", "LzmaCon")):
+                autotools.make(args=["-f", "makefile.gcc"], target="all")
+
+    def _patch_sources(self):
+        if self.settings.compiler == "Visual Studio":
+            fn = os.path.join("CPP", "Build.mak")
+            os.chmod(fn, 0o644)
+            tools.replace_in_file(fn, "-MT", "-{}".format(str(self.settings.compiler.runtime)))
+            tools.replace_in_file(fn, "-MD", "-{}".format(str(self.settings.compiler.runtime)))
 
     def build(self):
+        self._patch_sources()
         if self.settings.compiler == "Visual Studio":
             self._build_msvc()
         else:


Test patch: