Skip to content

Commit

Permalink
improving toolchain checkers (#8343)
Browse files Browse the repository at this point in the history
* improving toolchain checkers

* fixing tests
  • Loading branch information
memsharded committed Feb 3, 2021
1 parent a24c406 commit c5abf1a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 48 deletions.
22 changes: 14 additions & 8 deletions conans/test/assets/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
std::cout << "{{ msg or name }}: Debug!\n";
#endif
// ARCHITECTURES
#ifdef _M_X64
std::cout << " {{ msg or name }} _M_X64 defined\n";
#endif
Expand All @@ -26,6 +27,15 @@
std::cout << " {{ msg or name }} _M_IX86 defined\n";
#endif
#if __i386__
std::cout << " {{ msg or name }} __i386__ defined\n";
#endif
#if __x86_64__
std::cout << " {{ msg or name }} __x86_64__ defined\n";
#endif
// COMPILER VERSIONS
#if _MSC_VER
std::cout << " {{ msg or name }} _MSC_VER" << _MSC_VER<< "\n";
#endif
Expand All @@ -34,6 +44,10 @@
std::cout << " {{ msg or name }} _MSVC_LANG" << _MSVC_LANG<< "\n";
#endif
#if __cplusplus
std::cout << " {{ msg or name }} __cplusplus" << __cplusplus<< "\n";
#endif
#if __INTEL_COMPILER
std::cout << " {{ msg or name }} __INTEL_COMPILER" << __INTEL_COMPILER<< "\n";
#endif
Expand All @@ -58,14 +72,6 @@
std::cout << " {{ msg or name }} __apple_build_version__" << __apple_build_version__<< "\n";
#endif
#if __i386__
std::cout << " {{ msg or name }} __i386__ defined\n";
#endif
#if __x86_64__
std::cout << " {{ msg or name }} __x86_64__ defined\n";
#endif
{% for it in preprocessor -%}
std::cout << " {{msg}} {{it}}: " << {{it}} << "\n";
{%- endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from conans.model.ref import ConanFileReference, PackageReference
from conans.test.assets.sources import gen_function_cpp, gen_function_h
from conans.test.functional.utils import check_vs_runtime, check_msc_ver
from conans.test.functional.utils import check_vs_runtime, check_exe_run
from conans.test.utils.tools import TestClient


Expand Down Expand Up @@ -165,7 +165,7 @@ class WinTest(Base):
@parameterized.expand([("Visual Studio", "Debug", "MTd", "15", "14", "x86", "v140", True),
("Visual Studio", "Release", "MD", "15", "17", "x86_64", "", False),
("msvc", "Debug", "static", "19.1", "14", "x86", None, True),
("msvc", "Release", "dynamic", "19.11", "17", "x86_64", None, False)]
("msvc", "Release", "dynamic", "19.1", "17", "x86_64", None, False)]
)
def test_toolchain_win(self, compiler, build_type, runtime, version, cppstd, arch, toolset,
shared):
Expand All @@ -190,7 +190,7 @@ def test_toolchain_win(self, compiler, build_type, runtime, version, cppstd, arc
self.assertIn("Microsoft Visual Studio/2017", self.client.out)

generator_platform = "x64" if arch == "x86_64" else "Win32"
arch = "x64" if arch == "x86_64" else "X86"
arch_flag = "x64" if arch == "x86_64" else "X86"
shared_str = "ON" if shared else "OFF"
vals = {"CMAKE_GENERATOR_PLATFORM": generator_platform,
"CMAKE_BUILD_TYPE": "",
Expand All @@ -200,8 +200,8 @@ def test_toolchain_win(self, compiler, build_type, runtime, version, cppstd, arc
"CMAKE_C_FLAGS": "/MP1 /DWIN32 /D_WINDOWS",
"CMAKE_C_FLAGS_DEBUG": "/Zi /Ob0 /Od /RTC1",
"CMAKE_C_FLAGS_RELEASE": "/O2 /Ob2 /DNDEBUG",
"CMAKE_SHARED_LINKER_FLAGS": "/machine:%s" % arch,
"CMAKE_EXE_LINKER_FLAGS": "/machine:%s" % arch,
"CMAKE_SHARED_LINKER_FLAGS": "/machine:%s" % arch_flag,
"CMAKE_EXE_LINKER_FLAGS": "/machine:%s" % arch_flag,
"CMAKE_CXX_STANDARD": cppstd,
"CMAKE_CXX_EXTENSIONS": "OFF",
"BUILD_SHARED_LIBS": shared_str}
Expand All @@ -227,11 +227,24 @@ def _verify_out(marker=">>"):
self._run_build(settings, options)

self._run_app("Release", bin_folder=True)
check_msc_ver(toolset or "v141", self.client.out)
self.assertIn("main _MSVC_LANG20{}".format(cppstd), self.client.out)
if compiler == "msvc":
visual_version = version
else:
visual_version = "19.0" if toolset == "v140" else "19.1"
check_exe_run(self.client.out, "main", "msvc", visual_version, "Release", arch, cppstd,
{"MYVAR": "MYVAR_VALUE",
"MYVAR_CONFIG": "MYVAR_RELEASE",
"MYDEFINE": "MYDEF_VALUE",
"MYDEFINE_CONFIG": "MYDEF_RELEASE"
})
self._run_app("Debug", bin_folder=True)
check_msc_ver(toolset or "v141", self.client.out)
self.assertIn("main _MSVC_LANG20{}".format(cppstd), self.client.out)
check_exe_run(self.client.out, "main", "msvc", visual_version, "Debug", arch, cppstd,
{"MYVAR": "MYVAR_VALUE",
"MYVAR_CONFIG": "MYVAR_DEBUG",
"MYDEFINE": "MYDEF_VALUE",
"MYDEFINE_CONFIG": "MYDEF_DEBUG"
})

static = (runtime == "static" or "MT" in runtime)
check_vs_runtime("build/Release/app.exe", self.client, "15", build_type="Release",
static=static)
Expand All @@ -249,6 +262,7 @@ def _verify_out(marker=">>"):
@parameterized.expand([("Debug", "libstdc++", "4.9", "98", "x86_64", True),
("Release", "libstdc++", "4.9", "11", "x86_64", False)])
def test_toolchain_mingw_win(self, build_type, libcxx, version, cppstd, arch, shared):
# FIXME: The version and cppstd are wrong, toolchain doesn't enforce it
settings = {"compiler": "gcc",
"compiler.version": version,
"compiler.libcxx": libcxx,
Expand Down Expand Up @@ -288,6 +302,12 @@ def _verify_out(marker=">>"):

_verify_out()
self._run_app(build_type)
check_exe_run(self.client.out, "main", "gcc", None, build_type, arch, None,
{"MYVAR": "MYVAR_VALUE",
"MYVAR_CONFIG": "MYVAR_{}".format(build_type.upper()),
"MYDEFINE": "MYDEF_VALUE",
"MYDEFINE_CONFIG": "MYDEF_{}".format(build_type.upper())
})

self._modify_code()
time.sleep(2)
Expand Down
35 changes: 15 additions & 20 deletions conans/test/functional/toolchains/test_msbuild.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import platform
import shutil
import re
import textwrap
import unittest

Expand All @@ -14,7 +13,7 @@
from conan.tools.microsoft.visual import vcvars_command
from conans.client.tools import vs_installation_path
from conans.test.assets.sources import gen_function_cpp
from conans.test.functional.utils import check_vs_runtime, check_msc_ver
from conans.test.functional.utils import check_vs_runtime, check_exe_run
from conans.test.utils.tools import TestClient


Expand Down Expand Up @@ -375,7 +374,8 @@ def build(self):
app = gen_function_cpp(name="main", includes=["hello"], calls=["hello"],
preprocessor=["DEFINITIONS_BOTH", "DEFINITIONS_CONFIG"])

def _run_app(self, client, arch, build_type, shared=None):
@staticmethod
def _run_app(client, arch, build_type, shared=None):
if build_type == "Release" and shared:
configuration = "ReleaseShared"
else:
Expand All @@ -391,14 +391,6 @@ def _run_app(self, client, arch, build_type, shared=None):
mkdir(os.path.dirname(new_cmd))
shutil.copy(command_str, new_cmd)
client.run_command(new_cmd)
if arch == "x86":
self.assertIn("main _M_IX86 defined", client.out)
else:
self.assertIn("main _M_X64 defined", client.out)
self.assertIn("Hello World %s" % build_type, client.out)
self.assertIn("main: %s!" % build_type, client.out)
self.assertIn("DEFINITIONS_BOTH: True", client.out)
self.assertIn("DEFINITIONS_CONFIG: %s" % build_type, client.out)

@pytest.mark.tool_cmake
@parameterized.expand([("Visual Studio", "15", "MT"),
Expand Down Expand Up @@ -431,12 +423,14 @@ def test_toolchain_win(self, compiler, version, runtime):
self.assertIn("conanfile.py: MSBuildToolchain created conantoolchain_release_win32.props",
client.out)
client.run("build . -if=conan")

self.assertIn("Visual Studio 2017", client.out)
self.assertIn("[vcvarsall.bat] Environment initialized for: 'x86'", client.out)

self._run_app(client, "x86", "Release")
check_msc_ver("v141", client.out)
self.assertIn("main _MSVC_LANG2017", client.out)
self.assertIn("Hello World Release", client.out)
check_exe_run(client.out, "main", "msvc", "19.1", "Release", "x86", "17",
{"DEFINITIONS_BOTH": "True",
"DEFINITIONS_CONFIG": "Release"})
check_vs_runtime("Release/MyApp.exe", client, "15", static=True, build_type="Release")

@pytest.mark.tool_cmake
Expand Down Expand Up @@ -470,8 +464,10 @@ def test_toolchain_win_debug(self):
self.assertIn("Visual Studio 2017", client.out)
self.assertIn("[vcvarsall.bat] Environment initialized for: 'x64'", client.out)
self._run_app(client, "x64", "Debug")
check_msc_ver("v140", client.out)
self.assertIn("main _MSVC_LANG2014", client.out)
self.assertIn("Hello World Debug", client.out)
check_exe_run(client.out, "main", "msvc", "19.0", "Debug", "x86_64", "14",
{"DEFINITIONS_BOTH": "True",
"DEFINITIONS_CONFIG": "Debug"})
check_vs_runtime("x64/Debug/MyApp.exe", client, "15", static=False, build_type="Debug")

@pytest.mark.tool_cmake
Expand Down Expand Up @@ -522,10 +518,9 @@ def test_toolchain_win_multi(self):
self.assertIn("[vcvarsall.bat] Environment initialized for: 'x64'", client.out)

self._run_app(client, arch, build_type, shared)
version = re.search("main _MSC_VER19([0-9]*)", str(client.out)).group(1)
version = int(version)
self.assertTrue(10 <= version < 20)
self.assertIn("main _MSVC_LANG2017", client.out)
check_exe_run(client.out, "main", "msvc", "19.1", build_type, arch, "17",
{"DEFINITIONS_BOTH": "True",
"DEFINITIONS_CONFIG": build_type})

new_cmd = "conan\\%s\\%s\\MyApp.exe" % (arch, configuration)
vcvars = vcvars_command(version="15", architecture="amd64")
Expand Down
50 changes: 39 additions & 11 deletions conans/test/functional/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import re

from conan.tools.microsoft.visual import vcvars_command


Expand All @@ -25,12 +23,42 @@ def check_vs_runtime(exe, client, vs_version, build_type, static, architecture="
raise NotImplementedError()


def check_msc_ver(toolset, output):
if toolset == "v140":
assert "main _MSC_VER1900" in output, "Error:{}".format(output)
elif toolset == "v141":
version = re.search("main _MSC_VER19([0-9]*)", str(output)).group(1)
version = int(version)
assert 10 <= version < 20, "Error:{}".format(output)
else:
raise NotImplementedError()
def check_exe_run(output, names, compiler, version, build_type, arch, cppstd, definitions=None):
output = str(output)
names = names if isinstance(names, list) else [names]

for name in names:
assert "{}: {}".format(name, build_type) in output
if compiler == "msvc":
if arch == "x86":
assert "{} _M_IX86 defined".format(name) in output
elif arch == "x86_64":
assert "{} _M_X64 defined".format(name) in output
else:
assert arch is None, "checked don't know how to validate this architecture"

assert "{} _MSC_VER{}".format(name, version.replace(".", "")) in output
assert "{} _MSVC_LANG20{}".format(name, cppstd) in output

elif compiler == "gcc":
if arch == "x86":
assert "{} __i386__ defined".format(name) in output
elif arch == "x86_64":
assert "{} __x86_64__ defined".format(name) in output
else:
assert arch is None, "checked don't know how to validate this architecture"

if version: # FIXME: At the moment, the GCC version is not controlled, will change
major, minor = version.split(".")[0:2]
assert "{} __GNUC__{}".format(name, major) in output
assert "{} __GNUC_MINOR__{}".format(name, minor) in output
if cppstd:
cppstd_value = {"98": "199711",
"11": "201103",
"14": "201402",
"17": "201703"}[cppstd]
assert "{} __cplusplus{}".format(name, cppstd_value) in output

if definitions:
for k, v in definitions.items():
assert "{}: {}".format(k, v) in output

0 comments on commit c5abf1a

Please sign in to comment.