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

Add tools.android:cmake_legacy_toolchain config #13459

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ class AndroidSystemBlock(Block):
set(ANDROID_STL {{ android_stl }})
{% endif %}
set(ANDROID_ABI {{ android_abi }})
{% if android_define_legacy_toolchain_flag %}
set(ANDROID_USE_LEGACY_TOOLCHAIN_FILE {{ android_use_legacy_toolchain_file }})
{% endif %}
include({{ android_ndk_path }}/build/cmake/android.toolchain.cmake)
""")

Expand All @@ -278,11 +281,16 @@ def context(self):
raise ConanException('CMakeToolchain needs tools.android:ndk_path configuration defined')
android_ndk_path = android_ndk_path.replace("\\", "/")

use_cmake_legacy_toolchain = self._conanfile.conf.get("tools.android:cmake_legacy_toolchain")
memsharded marked this conversation as resolved.
Show resolved Hide resolved
legacy_toolchain_flag_defined = use_cmake_legacy_toolchain is not None

ctxt_toolchain = {
'android_platform': 'android-' + str(self._conanfile.settings.os.api_level),
'android_abi': android_abi(self._conanfile),
'android_stl': libcxx_str,
'android_ndk_path': android_ndk_path,
'android_define_legacy_toolchain_flag': legacy_toolchain_flag_defined,
memsharded marked this conversation as resolved.
Show resolved Hide resolved
'android_use_legacy_toolchain_file': "ON" if use_cmake_legacy_toolchain else "OFF",
}
return ctxt_toolchain

Expand Down Expand Up @@ -564,6 +572,14 @@ def context(self):
sharedlinkflags = self._conanfile.conf.get("tools.build:sharedlinkflags", default=[], check_type=list)
exelinkflags = self._conanfile.conf.get("tools.build:exelinkflags", default=[], check_type=list)
defines = self._conanfile.conf.get("tools.build:defines", default=[], check_type=list)

# See https://github.com/conan-io/conan/issues/13374
android_ndk_path = self._conanfile.conf.get("tools.android:ndk_path")
android_legacy_toolchain = self._conanfile.conf.get("tools.android:cmake_legacy_toolchain")
memsharded marked this conversation as resolved.
Show resolved Hide resolved
if android_ndk_path and (cxxflags or cflags) and android_legacy_toolchain is not False:
self._conanfile.output.warning("tools.build:cxxflags or cflags are defined, but Android NDK toolchain may be overriding "
"the values. Consider setting tools.android:cmake_legacy_toolchain to False.")

return {
"cxxflags": cxxflags,
"cflags": cflags,
Expand Down
1 change: 1 addition & 0 deletions conans/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"core.gzip:compresslevel": "The Gzip compresion level for Conan artifacts (default=9)",
# Tools
"tools.android:ndk_path": "Argument for the CMAKE_ANDROID_NDK",
"tools.android:cmake_legacy_toolchain": "Define to explicitly pass ANDROID_USE_LEGACY_TOOLCHAIN_FILE in CMake toolchain",
"tools.build:skip_test": "Do not execute CMake.test() and Meson.test() when enabled",
"tools.build:download_source": "Force download of sources for every package",
"tools.build:jobs": "Default compile jobs number -jX Ninja, Make, /MP VS (default: max CPUs)",
Expand Down
45 changes: 45 additions & 0 deletions conans/test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,51 @@ def configure(self):
assert "include(/foo/build/cmake/android.toolchain.cmake)" in conan_toolchain
client.run("create . --name=foo --version=1.0 " + settings)

@pytest.mark.parametrize("cmake_legacy_toolchain", [True, False, None])
def test_android_legacy_toolchain_flag(cmake_legacy_toolchain):
client = TestClient()
conanfile = GenConanfile().with_settings("os", "arch")\
.with_generator("CMakeToolchain")
client.save({"conanfile.py": conanfile})
settings = "-s arch=x86_64 -s os=Android -s os.api_level=23 -c tools.android:ndk_path=/foo"
if cmake_legacy_toolchain is not None:
settings += f" -c tools.android:cmake_legacy_toolchain={cmake_legacy_toolchain}"
expected = "ON" if cmake_legacy_toolchain else "OFF"
client.run("install . " + settings)
conan_toolchain = client.load(os.path.join(client.current_folder, "conan_toolchain.cmake"))
if cmake_legacy_toolchain is not None:
assert f"set(ANDROID_USE_LEGACY_TOOLCHAIN_FILE {expected})" in conan_toolchain
else:
assert "ANDROID_USE_LEGACY_TOOLCHAIN_FILE" not in conan_toolchain


@pytest.mark.parametrize("cmake_legacy_toolchain", [True, False, None])
def test_android_legacy_toolchain_with_compileflags(cmake_legacy_toolchain):
# https://github.com/conan-io/conan/issues/13374
client = TestClient()
conanfile = GenConanfile().with_settings("os", "arch")\
.with_generator("CMakeToolchain")
profile = textwrap.dedent("""
[settings]
os=Android
os.api_level=23
arch=armv8

[conf]
tools.android:ndk_path=/foo
tools.build:cflags=["-foobar"]
tools.build:cxxflags=["-barfoo"]
""")
if cmake_legacy_toolchain is not None:
profile += f"\ntools.android:cmake_legacy_toolchain={cmake_legacy_toolchain}"

client.save({"conanfile.py": conanfile, "profile_host": profile})
client.run("install . -pr profile_host")
warning_text = "Consider setting tools.android:cmake_legacy_toolchain to False"
if cmake_legacy_toolchain is not False:
assert warning_text in client.out
else:
assert warning_text not in client.out

@pytest.mark.skipif(platform.system() != "Windows", reason="Only Windows")
def test_presets_paths_normalization():
Expand Down