Skip to content

Commit

Permalink
templates cmake v2 and cross_building (#10706)
Browse files Browse the repository at this point in the history
* templates cmake v2 and cross_building

* Fix test
  • Loading branch information
lasote authored Mar 3, 2022
1 parent ccc9ff7 commit 669157e
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 59 deletions.
18 changes: 14 additions & 4 deletions conan/tools/build/cross_building.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

def cross_building(conanfile=None, skip_x64_x86=False):
host_os = conanfile.settings.get_safe("os")
host_arch = conanfile.settings.get_safe("arch")
build_os = conanfile.settings_build.get_safe('os')
build_arch = conanfile.settings_build.get_safe('arch')

build_os, build_arch, host_os, host_arch = get_cross_building_settings(conanfile)

if skip_x64_x86 and host_os is not None and (build_os == host_os) and \
host_arch is not None and ((build_arch == "x86_64") and (host_arch == "x86") or
Expand All @@ -18,3 +16,15 @@ def cross_building(conanfile=None, skip_x64_x86=False):

return False


def get_cross_building_settings(conanfile):
# FIXME: Develop2 this shouldn't go in develop2 where the build settings always exists
# Keep the current develop2 implementation for the whole module while merging
os_host = conanfile.settings.get_safe("os")
arch_host = conanfile.settings.get_safe("arch")

if hasattr(conanfile, 'settings_build'):
return (conanfile.settings_build.get_safe('os'), conanfile.settings_build.get_safe('arch'),
os_host, arch_host)
else:
return os_host, arch_host, os_host, arch_host
2 changes: 1 addition & 1 deletion conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from conan.tools.build import build_jobs
from conan.tools.cmake.toolchain import CONAN_TOOLCHAIN_FILENAME
from conan.tools.cmake.utils import is_multi_configuration
from conan.tools.cross_building import cross_building
from conan.tools.build.cross_building import cross_building
from conan.tools.intel import IntelCC
from conan.tools.microsoft.visual import is_msvc
from conans.errors import ConanException
Expand Down
28 changes: 0 additions & 28 deletions conan/tools/cross_building.py

This file was deleted.

2 changes: 1 addition & 1 deletion conan/tools/gnu/autotoolstoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
build_type_link_flags
from conan.tools.apple.apple import apple_min_version_flag, to_apple_arch, \
apple_sdk_path
from conan.tools.cross_building import cross_building, get_cross_building_settings
from conan.tools.build.cross_building import cross_building, get_cross_building_settings
from conan.tools.env import Environment
from conan.tools.files.files import save_toolchain_args
from conan.tools.gnu.get_gnu_triplet import _get_gnu_triplet
Expand Down
2 changes: 1 addition & 1 deletion conan/tools/meson/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from conan.tools._check_build_profile import check_using_build_profile
from conan.tools.apple.apple import to_apple_arch, is_apple_os, apple_min_version_flag
from conan.tools.cross_building import cross_building, get_cross_building_settings
from conan.tools.build.cross_building import cross_building, get_cross_building_settings
from conan.tools.env import VirtualBuildEnv
from conan.tools.meson.helpers import *
from conan.tools.microsoft import VCVars, msvc_runtime_flag
Expand Down
32 changes: 21 additions & 11 deletions conans/assets/templates/new_v2_cmake.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
conanfile_sources_v2 = """from conans import ConanFile
conanfile_sources_v2 = """from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
Expand Down Expand Up @@ -48,8 +48,9 @@ def package_info(self):

test_conanfile_v2 = """import os
from conans import ConanFile, tools
from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout
from conan.tools.build import cross_building
class {package_name}TestConan(ConanFile):
Expand All @@ -58,6 +59,10 @@ class {package_name}TestConan(ConanFile):
# (it will be defined in Conan 2.0)
generators = "CMakeDeps", "CMakeToolchain", "VirtualBuildEnv", "VirtualRunEnv"
apply_env = False
test_type = "explicit"
def requirements(self):
self.requires(self.tested_reference_str)
def build(self):
cmake = CMake(self)
Expand All @@ -68,7 +73,7 @@ def layout(self):
cmake_layout(self)
def test(self):
if not tools.cross_building(self):
if not cross_building(self):
cmd = os.path.join(self.cpp.build.bindirs[0], "example")
self.run(cmd, env="conanrun")
"""
Expand Down Expand Up @@ -219,7 +224,7 @@ def get_cmake_lib_files(name, version, package_name="Pkg"):
return files


conanfile_exe = """from conans import ConanFile
conanfile_exe = """from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
Expand Down Expand Up @@ -263,15 +268,13 @@ def package(self):
add_executable({name} src/{name}.cpp src/main.cpp)
target_include_directories({name} PUBLIC include)
install(TARGETS {name} DESTINATION "."
RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
)
install(TARGETS {name})
"""

test_conanfile_exe_v2 = """import os
from conans import ConanFile, tools
from conan import ConanFile
from conan.tools.build import cross_building
from conan.tools.layout import basic_layout
class {package_name}TestConan(ConanFile):
Expand All @@ -280,9 +283,16 @@ class {package_name}TestConan(ConanFile):
# (it will be defined in Conan 2.0)
generators = "VirtualRunEnv"
apply_env = False
test_type = "explicit"
def requirements(self):
self.requires(self.tested_reference_str)
def layout(self):
basic_layout(self)
def test(self):
if not tools.cross_building(self):
if not cross_building(self):
self.run("{name}", env="conanrun")
"""

Expand Down
13 changes: 2 additions & 11 deletions conans/test/assets/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,10 @@ def gen_cmakelists(language="CXX", verify=True, project="project", libname="myli
{% if install %}
{% if appsources %}
install(TARGETS {{appname}} DESTINATION ".")
install(TARGETS {{appname}})
{% endif %}
{% if libsources %}
install(TARGETS {{libname}} DESTINATION "."
{% if public_header %}
PUBLIC_HEADER DESTINATION include
{% endif %}
RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
FRAMEWORK DESTINATION Frameworks
BUNDLE DESTINATION bin
)
install(TARGETS {{libname}})
{% endif %}
{% endif %}
""")
Expand Down
4 changes: 2 additions & 2 deletions conans/test/functional/toolchains/cmake/test_ninja.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_locally_build_linux(build_type, shared, client):
assert "main: {}!".format(build_type) in client.out
client.run("install hello/1.0@ -g=deploy -if=mydeploy {}".format(settings))
ldpath = os.path.join(client.current_folder, "mydeploy", "hello", "lib")
client.run_command("LD_LIBRARY_PATH='{}' ./mydeploy/hello/myapp".format(ldpath))
client.run_command("LD_LIBRARY_PATH='{}' ./mydeploy/hello/bin/myapp".format(ldpath))
check_exe_run(client.out, ["main", "hello"], "gcc", None, build_type, "x86_64", cppstd=None)


Expand Down Expand Up @@ -111,7 +111,7 @@ def test_locally_build_msvc(build_type, shared, client):
assert 'cmake -G "Ninja"' in client.out
assert "main: {}!".format(build_type) in client.out
client.run("install hello/1.0@ -g=deploy -if=mydeploy {}".format(settings))
client.run_command(r"mydeploy\hello\myapp.exe")
client.run_command(r"mydeploy\hello\bin\myapp.exe")
check_exe_run(client.out, ["main", "hello"], "msvc", "19", build_type, "x86_64", cppstd="14")


Expand Down

0 comments on commit 669157e

Please sign in to comment.