Skip to content

Commit

Permalink
[feature] Added cross-building conf variable (#15616)
Browse files Browse the repository at this point in the history
* Added tools.build.cross_building:force to force or not the cross-building mechanism

* Fixed tests

* Renamed
  • Loading branch information
franramirez688 authored Feb 7, 2024
1 parent c043da6 commit 1c18f76
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
5 changes: 4 additions & 1 deletion conan/tools/build/cross_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ def cross_building(conanfile=None, skip_x64_x86=False):
x86_64 to x86, sparcv9 to sparc or ppc64 to ppc32
:return: ``True`` if we are cross building, ``False`` otherwise.
"""
cross_build = conanfile.conf.get("tools.build.cross_building:cross_build", check_type=bool)
if cross_build is not None:
return cross_build

build_os = conanfile.settings_build.get_safe('os')
build_arch = conanfile.settings_build.get_safe('arch')
Expand All @@ -32,7 +35,7 @@ def cross_building(conanfile=None, skip_x64_x86=False):
def can_run(conanfile):
"""
Validates whether is possible to run a non-native app on the same architecture.
It’s an useful feature for the case your architecture can run more than one target.
It’s a useful feature for the case your architecture can run more than one target.
For instance, Mac M1 machines can run both `armv8` and `x86_64`.
:param conanfile: The current recipe object. Always use ``self``.
Expand Down
1 change: 1 addition & 0 deletions conans/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"tools.apple:enable_visibility": "(boolean) Enable/Disable Visibility Apple Clang flags",
"tools.env.virtualenv:powershell": "If it is set to True it will generate powershell launchers if os=Windows",
# Compilers/Flags configurations
"tools.build.cross_building:cross_build": "Decides whether cross-building or not regardless of arch/OS settings. Used by 'conan.tools.build.cross_building' function.",
"tools.build:compiler_executables": "Defines a Python dict-like with the compilers path to be used. Allowed keys {'c', 'cpp', 'cuda', 'objc', 'objcxx', 'rc', 'fortran', 'asm', 'hip', 'ispc'}",
"tools.build:cxxflags": "List of extra CXX flags used by different toolchains like CMakeToolchain, AutotoolsToolchain and MesonToolchain",
"tools.build:cflags": "List of extra C flags used by different toolchains like CMakeToolchain, AutotoolsToolchain and MesonToolchain",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def test_apple_arch_flag():

# Only set when crossbuilding
conanfile = ConanFileMock()
conanfile.conf = {"tools.apple:sdk_path": "/path/to/sdk"}
conanfile.conf.define("tools.apple:sdk_path", "/path/to/sdk")
conanfile.settings = MockSettings(
{"build_type": "Debug",
"os": "Macos",
Expand Down Expand Up @@ -365,7 +365,7 @@ def test_apple_isysrootflag():

# Only set when crossbuilding
conanfile = ConanFileMock()
conanfile.conf = {"tools.apple:sdk_path": "/path/to/sdk"}
conanfile.conf.define("tools.apple:sdk_path", "/path/to/sdk")
conanfile.settings = MockSettings(
{"build_type": "Debug",
"os": "Macos",
Expand Down
16 changes: 16 additions & 0 deletions conans/test/unittests/tools/build/test_cross_building.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

from conan.tools.build import cross_building
from conans.test.utils.mocks import ConanFileMock


@pytest.mark.parametrize("cross_build", (True, False))
def test_using_cross_build_conf(cross_build):
"""
Tests cross_building function is using the conf variable to force or not.
Issue related: https://github.com/conan-io/conan/issues/15392
"""
conanfile = ConanFileMock()
conanfile.conf.define("tools.build.cross_building:cross_build", cross_build)
assert cross_building(conanfile) == cross_build

0 comments on commit 1c18f76

Please sign in to comment.