Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[toolchains]Tests for compilers path from conf in build context #15895

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import pytest

from conan.tools.cmake.presets import load_cmake_presets
from conans.model.recipe_ref import RecipeReference
from conan.tools.microsoft.visual import vcvars_command
from conans.model.recipe_ref import RecipeReference
from conans.test.assets.cmake import gen_cmakelists
from conans.test.assets.genconanfile import GenConanfile
from conans.test.utils.test_files import temp_folder
Expand Down
80 changes: 80 additions & 0 deletions conans/test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,3 +1334,83 @@ def test_toolchain_ends_newline():
client.run("install . -g CMakeToolchain")
toolchain = client.load("conan_toolchain.cmake")
assert toolchain[-1] == "\n"


def test_toolchain_and_compilers_build_context():
"""
Tests how CMakeToolchain manages the build context profile if the build profile is
specifying another compiler path (using conf)

Issue related: https://github.com/conan-io/conan/issues/15878
"""
host = textwrap.dedent("""
[settings]
arch=armv8
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=11
os=Linux

[conf]
tools.build:compiler_executables={"c": "gcc", "cpp": "g++"}
""")
build = textwrap.dedent("""
[settings]
os=Linux
arch=x86_64
compiler=clang
compiler.version=12
compiler.libcxx=libc++
compiler.cppstd=11

[conf]
tools.build:compiler_executables={"c": "clang", "cpp": "clang++"}
""")
tool = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import load

class toolRecipe(ConanFile):
name = "tool"
version = "1.0"
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeToolchain"

def build(self):
toolchain = os.path.join(self.generators_folder, "conan_toolchain.cmake")
content = load(self, toolchain)
assert 'set(CMAKE_C_COMPILER "clang")' in content
assert 'set(CMAKE_CXX_COMPILER "clang++")' in content
""")
consumer = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import load

class consumerRecipe(ConanFile):
name = "consumer"
version = "1.0"
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeToolchain"
tool_requires = "tool/1.0"

def build(self):
toolchain = os.path.join(self.generators_folder, "conan_toolchain.cmake")
content = load(self, toolchain)
assert 'set(CMAKE_C_COMPILER "gcc")' in content
assert 'set(CMAKE_CXX_COMPILER "g++")' in content
""")
client = TestClient()
client.save({
"host": host,
"build": build,
"tool/conanfile.py": tool,
"consumer/conanfile.py": consumer
})
client.run("export tool")
client.run("create consumer -pr:h host -pr:b build --build=missing")
80 changes: 80 additions & 0 deletions conans/test/integration/toolchains/gnu/test_autotoolstoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,83 @@ def test_unknown_compiler():
# this used to crash, because of build_type_flags in AutotoolsToolchain returning empty string
client.run("install . -s compiler=xlc")
assert "conanfile.py: Generator 'AutotoolsToolchain' calling 'generate()'" in client.out


def test_toolchain_and_compilers_build_context():
"""
Tests how AutotoolsToolchain manages the build context profile if the build profile is
specifying another compiler path (using conf)

Issue related: https://github.com/conan-io/conan/issues/15878
"""
host = textwrap.dedent("""
[settings]
arch=armv8
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=11
os=Linux

[conf]
tools.build:compiler_executables={"c": "gcc", "cpp": "g++"}
""")
build = textwrap.dedent("""
[settings]
os=Linux
arch=x86_64
compiler=clang
compiler.version=12
compiler.libcxx=libc++
compiler.cppstd=11

[conf]
tools.build:compiler_executables={"c": "clang", "cpp": "clang++"}
""")
tool = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import load

class toolRecipe(ConanFile):
name = "tool"
version = "1.0"
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
generators = "AutotoolsToolchain"

def build(self):
toolchain = os.path.join(self.generators_folder, "conanautotoolstoolchain.sh")
content = load(self, toolchain)
assert 'export CC="clang"' in content
assert 'export CXX="clang++"' in content
""")
consumer = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import load

class consumerRecipe(ConanFile):
name = "consumer"
version = "1.0"
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
generators = "AutotoolsToolchain"
tool_requires = "tool/1.0"

def build(self):
toolchain = os.path.join(self.generators_folder, "conanautotoolstoolchain.sh")
content = load(self, toolchain)
assert 'export CC="gcc"' in content
assert 'export CXX="g++"' in content
""")
client = TestClient()
client.save({
"host": host,
"build": build,
"tool/conanfile.py": tool,
"consumer/conanfile.py": consumer
})
client.run("export tool")
client.run("create consumer -pr:h host -pr:b build --build=missing")
146 changes: 146 additions & 0 deletions conans/test/integration/toolchains/meson/test_mesontoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,149 @@ def generate(self):
base_folder = t.current_folder
assert f"pkg_config_path = '{base_folder}'" in content
assert f"build.pkg_config_path = '{os.path.join(base_folder, 'build')}'" in content



def test_toolchain_and_compilers_build_context():
"""
Tests how MesonToolchain manages the build context profile if the build profile is
specifying another compiler path (using conf).

It should create both native and cross files.

Issue related: https://github.com/conan-io/conan/issues/15878
"""
host = textwrap.dedent("""
[settings]
arch=armv8
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=11
os=Linux
""")
build = textwrap.dedent("""
[settings]
os=Linux
arch=x86_64
compiler=clang
compiler.version=12
compiler.libcxx=libc++
compiler.cppstd=11

[conf]
tools.build:compiler_executables={"asm": "clang", "c": "clang", "cpp": "clang++"}
""")
conanfile = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import replace_in_file
class helloRecipe(ConanFile):
name = "hello"
version = "1.0.0"
package_type = "application"
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
generators = "MesonToolchain"

def build(self):
native_path = os.path.join(self.generators_folder, "conan_meson_native.ini")
cross_path = os.path.join(self.generators_folder, "conan_meson_cross.ini")
assert os.path.exists(cross_path) # sanity check
assert os.path.exists(native_path) # sanity check
# This should not raise anything!! Notice the strict=True
replace_in_file(self, cross_path, 'c = gcc', "#Hey", strict=True)
replace_in_file(self, cross_path, 'cpp = g++', "#Hey", strict=True)
replace_in_file(self, native_path, 'c = clang', "#Hey", strict=True)
replace_in_file(self, native_path, 'cpp = clang++', "#Hey", strict=True)
""")
client = TestClient()
client.save({
"host": host,
"build": build,
"conanfile.py": conanfile
})
client.run("build . -pr:h host -pr:b build")


def test_toolchain_and_compilers_build_context():
"""
Tests how MesonToolchain manages the build context profile if the build profile is
specifying another compiler path (using conf).

It should create both native and cross files.

Issue related: https://github.com/conan-io/conan/issues/15878
"""
host = textwrap.dedent("""
[settings]
arch=armv8
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=11
os=Linux

[conf]
tools.build:compiler_executables={"c": "gcc", "cpp": "g++"}
""")
build = textwrap.dedent("""
[settings]
os=Linux
arch=x86_64
compiler=clang
compiler.version=12
compiler.libcxx=libc++
compiler.cppstd=11

[conf]
tools.build:compiler_executables={"c": "clang", "cpp": "clang++"}
""")
tool = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import load

class toolRecipe(ConanFile):
name = "tool"
version = "1.0"
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
generators = "MesonToolchain"

def build(self):
toolchain = os.path.join(self.generators_folder, "conan_meson_native.ini")
content = load(self, toolchain)
assert "c = 'clang'" in content
assert "cpp = 'clang++'" in content
""")
consumer = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import load

class consumerRecipe(ConanFile):
name = "consumer"
version = "1.0"
# Binary configuration
settings = "os", "compiler", "build_type", "arch"
generators = "MesonToolchain"
tool_requires = "tool/1.0"

def build(self):
toolchain = os.path.join(self.generators_folder, "conan_meson_cross.ini")
content = load(self, toolchain)
assert "c = 'gcc'" in content
assert "cpp = 'g++'" in content
""")
client = TestClient()
client.save({
"host": host,
"build": build,
"tool/conanfile.py": tool,
"consumer/conanfile.py": consumer
})
client.run("export tool")
client.run("create consumer -pr:h host -pr:b build --build=missing")