Skip to content

Commit

Permalink
Feature/fix cl2 (#561)
Browse files Browse the repository at this point in the history
* Develop version

* more or less work with saving bat file

* fixing broken tests

* fixed ENV tests in linux

* adding defines to ENV and minor refactor

* added clang in test for OSX

* removed temp conan_env.bat file & fixed test creating it

* incremented times for DiamondTest failing with new ENV
  • Loading branch information
memsharded authored and lasote committed Oct 13, 2016
1 parent 5b2d99e commit 14dcc1f
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 118 deletions.
1 change: 1 addition & 0 deletions conans/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
import os

__version__ = '0.13.3'

163 changes: 122 additions & 41 deletions conans/client/configure_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import copy
from conans.client.generators.virtualenv import get_setenv_variables_commands
from conans.model.env_info import DepsEnvInfo
from conans.util.files import save
from conans import tools


class ConfigureEnvironment(object):
Expand Down Expand Up @@ -54,51 +56,130 @@ def command_line(self):
command = '%s && nmake /f Makefile.msvc"' % env.command_line
self.run(command)
"""
command = ""
if self.os == "Linux" or self.os == "Macos" or (self.os == "Windows" and
self.compiler == "gcc"):
libflags = " ".join(["-l%s" % lib for lib in self._deps_cpp_info.libs])
libs = 'LIBS="%s"' % libflags
archflag = "-m32" if self.arch == "x86" else ""
exe_linker_flags = " ".join(self._deps_cpp_info.exelinkflags)
shared_linker_flags = " ".join(self._deps_cpp_info.sharedlinkflags)
lib_paths = " ".join(["-L%s" % lib for lib in self._deps_cpp_info.lib_paths])
ldflags = 'LDFLAGS="%s %s %s %s $LDFLAGS"' % (lib_paths, archflag,
exe_linker_flags, shared_linker_flags)
debug = "-g" if self.build_type == "Debug" else "-s -DNDEBUG"
include_flags = " ".join(['-I%s' % i for i in self._deps_cpp_info.include_paths])
cflags = 'CFLAGS="$CFLAGS %s %s %s %s"' % (archflag,
" ".join(self._deps_cpp_info.cflags),
debug, include_flags)

# Append the definition for libcxx
all_cpp_flags = copy.copy(self._deps_cpp_info.cppflags)

if self.os == "Linux" or self.os == "Macos":
return self._nix_env()
if self.os == "Windows":
if self.compiler == "Visual Studio":
cl_args = " ".join(['/I"%s"' % lib for lib in self._deps_cpp_info.include_paths])
lib_paths = ";".join(['"%s"' % lib for lib in self._deps_cpp_info.lib_paths])
commands = []
commands.append('SET LIB={};%LIB%'.format(lib_paths))
commands.append('SET CL={}'.format(cl_args))
commands.extend(get_setenv_variables_commands(self._deps_env_info, "SET"))
command = " && ".join(commands)

return command

def _nix_env(self):
libflags = " ".join(["-l%s" % lib for lib in self._deps_cpp_info.libs])
libs = 'LIBS="%s"' % libflags
archflag = "-m32" if self.arch == "x86" else ""
exe_linker_flags = " ".join(self._deps_cpp_info.exelinkflags)
shared_linker_flags = " ".join(self._deps_cpp_info.sharedlinkflags)
lib_paths = " ".join(["-L%s" % lib for lib in self._deps_cpp_info.lib_paths])
ldflags = 'LDFLAGS="%s %s %s %s $LDFLAGS"' % (lib_paths, archflag,
exe_linker_flags, shared_linker_flags)
debug = "-g" if self.build_type == "Debug" else "-s -DNDEBUG"
include_flags = " ".join(['-I%s' % i for i in self._deps_cpp_info.include_paths])
defines = " ".join(['-D%s' % i for i in self._deps_cpp_info.defines])
cflags = 'CFLAGS="$CFLAGS %s %s %s %s %s"' % (archflag,
" ".join(self._deps_cpp_info.cflags),
debug, include_flags, defines)

# Append the definition for libcxx
all_cpp_flags = copy.copy(self._deps_cpp_info.cppflags)
if self.libcxx:
if str(self.libcxx) == "libstdc++":
all_cpp_flags.append("-D_GLIBCXX_USE_CXX11_ABI=0")
elif str(self.libcxx) == "libstdc++11":
all_cpp_flags.append("-D_GLIBCXX_USE_CXX11_ABI=1")

if "clang" in str(self.compiler):
if str(self.libcxx) == "libc++":
all_cpp_flags.append("-stdlib=libc++")
else:
all_cpp_flags.append("-stdlib=libstdc++")

cpp_flags = 'CPPFLAGS="$CPPFLAGS %s %s %s %s %s"' % (archflag, " ".join(all_cpp_flags),
debug, include_flags, defines)
include_paths = ":".join(['"%s"' % lib for lib in self._deps_cpp_info.include_paths])
headers_flags = ('C_INCLUDE_PATH=$C_INCLUDE_PATH:{0} '
'CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:{0}'.format(include_paths))
command = "env %s %s %s %s %s" % (libs, ldflags, cflags, cpp_flags, headers_flags)
# Add the rest of env variables from deps_env_info, WITHOUT SET, not needed with ENV
command += " ".join(get_setenv_variables_commands(self._deps_env_info, ""))
return command

@property
def command_line_env(self):
if self.os == "Linux" or self.os == "Macos":
return self._nix_env()

if self.compiler == "Visual Studio":
cl_args = " ".join(['/I"%s"' % lib for lib in self._deps_cpp_info.include_paths])
lib_paths = ";".join(['%s' % lib for lib in self._deps_cpp_info.lib_paths])
commands = []
commands.append("@echo off")
commands.append('if defined LIB (SET "LIB=%LIB%;{0}") else (SET "LIB={0}")'.
format(lib_paths))
commands.append('if defined CL (SET "CL=%CL% {0}") else (SET "CL={0}")'.
format(cl_args))
commands.extend(get_setenv_variables_commands(self._deps_env_info, "SET"))

save("_conan_env.bat", "\r\n".join(commands))
command = "call _conan_env.bat"
vcvars = tools.vcvars_command(self._settings)
if vcvars:
command = vcvars + " && " + command
elif self.os == "Windows" and self.compiler == "gcc":
include_paths = ";".join(['%s'
% lib for lib in self._deps_cpp_info.include_paths])
commands = []
commands.append("@echo off")
commands.append('if defined C_INCLUDE_PATH (SET "C_INCLUDE_PATH=%C_INCLUDE_PATH%;{0}")'
' else (SET "C_INCLUDE_PATH={0}")'.format(include_paths))
commands.append('if defined CPLUS_INCLUDE_PATH '
'(SET "CPLUS_INCLUDE_PATH=%CPLUS_INCLUDE_PATH%;{0}")'
' else (SET "CPLUS_INCLUDE_PATH={0}")'.format(include_paths))

lib_paths = ";".join([lib for lib in self._deps_cpp_info.lib_paths])
commands.append('if defined LIBRARY_PATH (SET "LIBRARY_PATH=%LIBRARY_PATH%;{0}")'
' else (SET "LIBRARY_PATH={0}")'.format(lib_paths))

commands.extend(get_setenv_variables_commands(self._deps_env_info, "SET"))
save("_conan_env.bat", "\r\n".join(commands))

This comment has been minimized.

Copy link
@annulen

annulen Nov 1, 2016

Contributor

Using _conan_env.bat breaks working in MSYS shell, which is required for building anything using autotools

This comment has been minimized.

Copy link
@annulen

annulen Nov 1, 2016

Contributor

Never mind. I got impression that self.run() is using parent shell to run commands, however it uses cmd always. Got it working finally with 0.14.1

command = "call _conan_env.bat"

return command

@property
def compile_flags(self):
if self.compiler == "gcc" or "clang" in str(self.compiler):
flags = []
flags.extend("-l%s" % lib for lib in self._deps_cpp_info.libs)
if self.arch == "x86":
flags.append("-m32")
flags.extend(self._deps_cpp_info.exelinkflags)
flags.extend(self._deps_cpp_info.sharedlinkflags)
flags.append("-g" if self.build_type == "Debug" else "-s -DNDEBUG")
flags.extend('-D%s' % i for i in self._deps_cpp_info.defines)
flags.extend('-I"%s"' % i for i in self._deps_cpp_info.include_paths)
flags.extend('-L"%s"' % i for i in self._deps_cpp_info.lib_paths)
flags.extend(self._deps_cpp_info.cppflags)
if self.libcxx:
if str(self.libcxx) == "libstdc++":
all_cpp_flags.append("-D_GLIBCXX_USE_CXX11_ABI=0")
flags.append("-D_GLIBCXX_USE_CXX11_ABI=0")
elif str(self.libcxx) == "libstdc++11":
all_cpp_flags.append("-D_GLIBCXX_USE_CXX11_ABI=1")
flags.append("-D_GLIBCXX_USE_CXX11_ABI=1")

if "clang" in str(self.compiler):
if str(self.libcxx) == "libc++":
all_cpp_flags.append("-stdlib=libc++")
flags.append("-stdlib=libc++")
else:
all_cpp_flags.append("-stdlib=libstdc++")

cpp_flags = 'CPPFLAGS="$CPPFLAGS %s %s %s %s"' % (archflag, " ".join(all_cpp_flags),
debug, include_flags)
include_paths = ":".join(['"%s"' % lib for lib in self._deps_cpp_info.include_paths])
headers_flags = ('C_INCLUDE_PATH=$C_INCLUDE_PATH:{0} '
'CPP_INCLUDE_PATH=$CPP_INCLUDE_PATH:{0}'.format(include_paths))
command = "env %s %s %s %s %s" % (libs, ldflags, cflags, cpp_flags, headers_flags)
elif self.os == "Windows" and self.compiler == "Visual Studio":
cl_args = " ".join(['/I"%s"' % lib for lib in self._deps_cpp_info.include_paths])
lib_paths = ";".join(['"%s"' % lib for lib in self._deps_cpp_info.lib_paths])
command = ("(if defined LIB (SET LIB=%LIB%;{lib_paths}) else (SET LIB={lib_paths}))"
" && (if defined CL (SET CL=%CL% {cl_args}) else (SET CL={cl_args}))".
format(lib_paths=lib_paths, cl_args=cl_args))

# Add the rest of env variables from deps_env_info
command += " ".join(get_setenv_variables_commands(self._deps_env_info,
"" if self.os != "Windows" else "SET"))
return command
flags.append("-stdlib=libstdc++")

return " ".join(flags)
if self.compiler == "Visual Studio":
libs = " ".join("%s.lib" % lib for lib in self._deps_cpp_info.libs)
return libs
52 changes: 28 additions & 24 deletions conans/test/compile_helpers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
from conans.client.runner import ConanRunner
from conans.test.tools import TestBufferConanOutput
from conans.test.utils.test_files import temp_folder


class MockWinSettings(Settings):
Expand Down Expand Up @@ -75,6 +76,10 @@ def sharedlinkflags(self):
def include_paths(self):
return ["path/to/includes/lib1", "path/to/includes/lib2"]

@property
def defines(self):
return ["MYDEF1", "MYDEF2"]

@property
def libs(self):
return ["lib1", "lib2"]
Expand All @@ -90,38 +95,36 @@ def cppflags(self):

class CompileHelpersTest(unittest.TestCase):

def setUp(self):
self.current = os.getcwd()
os.chdir(temp_folder())

def tearDown(self):
os.chdir(self.current)

def configure_environment_test(self):
env = ConfigureEnvironment(BuildInfoMock(), MockWinSettings())

expected = ('(if defined LIB (SET LIB=%LIB%;"path/to/lib1";"path/to/lib2")'
' else (SET LIB="path/to/lib1";"path/to/lib2")) && '
'(if defined CL (SET CL=%CL% /I"path/to/includes/lib1" /I"path/to/includes/lib2")'
' else (SET CL=/I"path/to/includes/lib1" /I"path/to/includes/lib2"))')
expected = ('SET LIB="path/to/lib1";"path/to/lib2";%LIB% && '
'SET CL=/I"path/to/includes/lib1" '
'/I"path/to/includes/lib2"')

self.assertEquals(env.command_line, expected)

env = ConfigureEnvironment(BuildInfoMock(), MockLinuxSettings())
self.assertEquals(env.command_line, 'env LIBS="-llib1 -llib2" LDFLAGS="-Lpath/to/lib1 '
'-Lpath/to/lib2 -m32 -framework thing -framework thing2 $LDFLAGS" '
'CFLAGS="$CFLAGS -m32 cflag1 -s -DNDEBUG '
'-Ipath/to/includes/lib1 -Ipath/to/includes/lib2" '
'-Ipath/to/includes/lib1 -Ipath/to/includes/lib2 -DMYDEF1 -DMYDEF2" '
'CPPFLAGS="$CPPFLAGS -m32 cppflag1 -s -DNDEBUG '
'-Ipath/to/includes/lib1 -Ipath/to/includes/lib2" '
'-Ipath/to/includes/lib1 -Ipath/to/includes/lib2 -DMYDEF1 -DMYDEF2" '
'C_INCLUDE_PATH=$C_INCLUDE_PATH:"path/to/includes/lib1":'
'"path/to/includes/lib2" '
'CPP_INCLUDE_PATH=$CPP_INCLUDE_PATH:"path/to/includes/lib1":'
'CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:"path/to/includes/lib1":'
'"path/to/includes/lib2"')
# Not supported yet
env = ConfigureEnvironment(BuildInfoMock(), MockWinGccSettings())
self.assertEquals(env.command_line, 'env LIBS="-llib1 -llib2" LDFLAGS="-Lpath/to/lib1 '
'-Lpath/to/lib2 -m32 -framework thing -framework thing2 $LDFLAGS" '
'CFLAGS="$CFLAGS -m32 cflag1 -s -DNDEBUG '
'-Ipath/to/includes/lib1 -Ipath/to/includes/lib2" '
'CPPFLAGS="$CPPFLAGS -m32 cppflag1 -s -DNDEBUG '
'-Ipath/to/includes/lib1 -Ipath/to/includes/lib2" '
'C_INCLUDE_PATH=$C_INCLUDE_PATH:"path/to/includes/lib1":'
'"path/to/includes/lib2" '
'CPP_INCLUDE_PATH=$CPP_INCLUDE_PATH:"path/to/includes/lib1":'
'"path/to/includes/lib2"')
self.assertEquals(env.command_line_env, 'call _conan_env.bat')

def gcc_test(self):
gcc = GCC(MockLinuxSettings())
Expand All @@ -138,31 +141,32 @@ def append_variables_test(self):
os.environ["CPPFLAGS"] = "-cppflag -othercppflag"
os.environ["CFLAGS"] = "-cflag"
os.environ["C_INCLUDE_PATH"] = "/path/to/c_include_path:/anotherpath"
os.environ["CPP_INCLUDE_PATH"] = "/path/to/cpp_include_path:/anotherpathpp"
os.environ["CPLUS_INCLUDE_PATH"] = "/path/to/cpp_include_path:/anotherpathpp"

env = ConfigureEnvironment(BuildInfoMock(), MockLinuxSettings())
runner(env.command_line, output=output)
self.assertIn("LDFLAGS=-Lpath/to/lib1 -Lpath/to/lib2 -m32 -framework thing -framework thing2 ldflag=23 otherldflag=33\n", output)
self.assertIn("CPPFLAGS=-cppflag -othercppflag -m32 cppflag1 -s -DNDEBUG -Ipath/to/includes/lib1 -Ipath/to/includes/lib2\n", output)
self.assertIn("CFLAGS=-cflag -m32 cflag1 -s -DNDEBUG -Ipath/to/includes/lib1 -Ipath/to/includes/lib2\n", output)
self.assertIn("CPPFLAGS=-cppflag -othercppflag -m32 cppflag1 -s -DNDEBUG -Ipath/to/includes/lib1 -Ipath/to/includes/lib2 -DMYDEF1 -DMYDEF2\n", output)
self.assertIn("CFLAGS=-cflag -m32 cflag1 -s -DNDEBUG -Ipath/to/includes/lib1 -Ipath/to/includes/lib2 -DMYDEF1 -DMYDEF2\n", output)
self.assertIn("C_INCLUDE_PATH=/path/to/c_include_path:/anotherpath:path/to/includes/lib1:path/to/includes/lib2\n", output)
self.assertIn("CPP_INCLUDE_PATH=/path/to/cpp_include_path:/anotherpathpp:path/to/includes/lib1:path/to/includes/lib2\n", output)
self.assertIn("CPLUS_INCLUDE_PATH=/path/to/cpp_include_path:/anotherpathpp:path/to/includes/lib1:path/to/includes/lib2\n", output)

# Reset env vars to not mess with other tests
os.environ["LDFLAGS"] = ""
os.environ["CPPFLAGS"] = ""
os.environ["CFLAGS"] = ""
os.environ["C_INCLUDE_PATH"] = ""
os.environ["CPP_INCLUDE_PATH"] = ""
os.environ["CPLUS_INCLUDE_PATH"] = ""
else:
os.environ["LIB"] = '"/path/to/lib.a"'
os.environ["CL"] = '/I"path/to/cl1" /I"path/to/cl2"'

env = ConfigureEnvironment(BuildInfoMock(), MockWinSettings())
command = "%s && SET" % env.command_line
runner(command, output=output)
self.assertIn('LIB="/path/to/lib.a";"path/to/lib1";"path/to/lib2"', output)
self.assertIn('CL=/I"path/to/cl1" /I"path/to/cl2" /I"path/to/includes/lib1" /I"path/to/includes/lib2"' , output)

self.assertIn('LIB="path/to/lib1";"path/to/lib2";"/path/to/lib.a"', output)
self.assertIn('CL=/I"path/to/includes/lib1" /I"path/to/includes/lib2"', output)

os.environ["LIB"] = ""
os.environ["CL"] = ""
1 change: 1 addition & 0 deletions conans/test/conanfile_helpers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import random
import string


class ConanfileHelpersTest(unittest.TestCase):

def test_replace_in_file(self):
Expand Down
Loading

0 comments on commit 14dcc1f

Please sign in to comment.