Skip to content

Commit

Permalink
- fix vcvars test
Browse files Browse the repository at this point in the history
Signed-off-by: SSE4 <tomskside@gmail.com>
  • Loading branch information
SSE4 committed Mar 28, 2020
1 parent f57cceb commit dde7605
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 39 deletions.
63 changes: 48 additions & 15 deletions conans/client/tools/env.py
@@ -1,6 +1,8 @@
import json
import os
import platform
import sys
import uuid
from contextlib import contextmanager

from conans.client.run_environment import RunEnvironment
Expand Down Expand Up @@ -116,28 +118,59 @@ def remove_from_path(command):


def env_diff(cmd, only_diff):
known_path_lists = ("include", "lib", "libpath", "path")
if platform.system() == "Windows":
cmd += " && set"
else:
cmd += " && export"
# portable variant of shell/export command, as export may have different results, e.g.:
# USER=sse4 (zsh)
# declare -x USER="sse4" (bash)
# or may not be available at all! (csh)
# output as dictionary, as values may contain spaces, newlines, etc.
uuid_str = uuid.uuid4().hex # needed to reliably find the start of the script output
get_env = 'from __future__ import print_function; import os; import json; ' \
'print(\'%s\'); print(json.dumps(dict(os.environ)))' % uuid_str
known_path_lists = (
"INCLUDE",
"LIB",
"LIBPATH",
"PATH",
"CPATH",
"C_INCLUDE_PATH",
"CPLUS_INCLUDE_PATH",
"OBJC_INCLUDE_PATH",
"FPATH",
"MANPATH",
"PYTHONPATH",
"CLASSPATH",
"LD_LIBRARY_PATH",
"DYLD_LIBRARY_PATH",
"DYLD_FALLBACK_LIBRARY_PATH",
"DYLD_VERSIONED_LIBRARY_PATH",
"DYLD_FRAMEWORK_PATH",
"DYLD_FALLBACK_FRAMEWORK_PATH",
"DYLD_VERSIONED_FRAMEWORK_PATH",
"DYLD_ROOT_PATH",
"PERL5LIB",
"GCONV_PATH",
"LOCPATH",
"NIS_PATH,"
"NLSPATH",
"GOPATH"
)
cmd += " && \"%s\" -c \"%s\"" % (sys.executable, get_env)
ret = check_output_runner(cmd)
ret = ret[ret.find(uuid_str) + len(uuid_str):]
new_env = {}
for line in ret.splitlines():
line = line.strip()

if line == "\n" or not line:
continue
for name_var, value in json.loads(ret).items():
try:
name_var, value = line.split("=", 1)
name_var = str(name_var)
value = str(value)
new_value = value.split(os.pathsep) if name_var.lower() in known_path_lists else value
# environment variables are case-insensitive on Windows, case-sensitive otherwise
if platform.system() == "Windows":
normalized_name_var = name_var.upper()
else:
normalized_name_var = name_var
new_value = value.split(os.pathsep) if normalized_name_var in known_path_lists else value
# Return only new vars & changed ones, but only with the changed elements if the var is
# a list
if only_diff:
old_value = os.environ.get(name_var)
if name_var.lower() == "path":
if normalized_name_var == "PATH":
old_values_lower = [v.lower() for v in old_value.split(os.pathsep)]
# Clean all repeated entries, not append if the element was already there
new_env[name_var] = [v for v in new_value if v.lower() not in old_values_lower]
Expand Down
14 changes: 9 additions & 5 deletions conans/client/tools/intel.py
Expand Up @@ -35,10 +35,14 @@ def intel_installation_path(version, arch):
intel_arch = "EM64T"
else:
raise ConanException("don't know how to find Intel compiler on %s" % arch)

version_with_dot = version
if "." not in version_with_dot:
version_with_dot += ".0"
if is_win64():
base = r"SOFTWARE\WOW6432Node\Intel\Suites\{version}".format(version=version)
base = r"SOFTWARE\WOW6432Node\Intel\Suites\{version}".format(version=version_with_dot)
else:
base = r"SOFTWARE\Intel\Suites\{version}".format(version=version)
base = r"SOFTWARE\Intel\Suites\{version}".format(version=version_with_dot)
from six.moves import winreg # @UnresolvedImport
path = base + r"\Defaults\C++\{arch}".format(arch=intel_arch)
subkey = _system_registry_key(winreg.HKEY_LOCAL_MACHINE, path, "SubKey")
Expand All @@ -63,7 +67,7 @@ def compilervars_command(settings, arch=None, compiler_version=None, force=False
compiler_version = compiler_version or settings.get_safe("compiler.version")
arch = arch or settings.get_safe("arch")
system = platform.system()
cvars = "compilervars.bat" if system == "Window" else "compilervars.sh"
cvars = "compilervars.bat" if system == "Windows" else "compilervars.sh"
command = os.path.join(intel_installation_path(version=compiler_version, arch=arch), "bin", cvars)
if system != "Windows":
command = ". " + command # dot is more portable than source
Expand All @@ -84,14 +88,14 @@ def compilervars_command(settings, arch=None, compiler_version=None, force=False
command += " -platform linux"
command = "COMPILERVARS_PLATFORM=linux " + command
elif system == "Windows":
command += " -platform windows"
pass # there is no "-platform windows"
else:
raise ConanException("don't know how to call %s for %s" % (cvars, system))
compiler_base = settings.get_safe("compiler.base")
if compiler_base == "Visual Studio":
base_version = settings.get_safe("compiler.base.version")
if base_version:
command += "vs%s" % MSVS_YEAR.get(base_version)
command += " vs%s" % MSVS_YEAR.get(base_version)
return command


Expand Down
35 changes: 16 additions & 19 deletions conans/test/unittests/client/tools/win/vcvars_test.py
@@ -1,6 +1,7 @@
import os
import platform
import unittest
import uuid

import mock
import six
Expand Down Expand Up @@ -224,23 +225,17 @@ def vcvars_dict_diff_test(self):

def vcvars_dict_test(self):
# https://github.com/conan-io/conan/issues/2904
output_with_newline_and_spaces = """
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=9e09
set nl=^
env_var=
without_equals_sign
ProgramFiles(x86)=C:\Program Files (x86)
output_with_newline_and_spaces = """6ba7b8109dad11d180b400c04fd430c8
{
"PROCESSOR_ARCHITECTURE": "AMD64",
"PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 158 Stepping 9, GenuineIntel",
"PROCESSOR_LEVEL": "6",
"PROCESSOR_REVISION": "9e09",
"set nl": "^",
"env_var": "",
"without_equals_sign": "",
"ProgramFiles(x86)": "C:\\\\Program Files (x86)"
}
"""

def vcvars_command_mock(settings, arch, compiler_version, force, vcvars_ver, winsdk_version,
Expand All @@ -251,9 +246,11 @@ def subprocess_check_output_mock(cmd):
self.assertIn("unused command", cmd)
return output_with_newline_and_spaces

test_uuid = uuid.UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
with mock.patch('conans.client.tools.win.vcvars_command', new=vcvars_command_mock):
with patch('conans.client.tools.win.check_output_runner',
new=subprocess_check_output_mock):
with patch('conans.client.tools.env.check_output_runner',
new=subprocess_check_output_mock),\
patch('uuid.uuid4', mock.MagicMock(return_value=test_uuid)):
vcvars = tools.vcvars_dict(None, only_diff=False, output=self.output)
self.assertEqual(vcvars["PROCESSOR_ARCHITECTURE"], "AMD64")
self.assertEqual(vcvars["PROCESSOR_IDENTIFIER"],
Expand Down

0 comments on commit dde7605

Please sign in to comment.