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

Fix cross-build vars in CMakeToolchain #10856

Merged
merged 17 commits into from Mar 30, 2022

Conversation

czoido
Copy link
Contributor

@czoido czoido commented Mar 22, 2022

Changelog: Fix: Setting CMAKE_SYSTEM_PROCESSOR for Apple cross-compiling including M1.
Changelog: Fix: Do not overwrite values for CMAKE_SYSTEM_NAME, CMAKE_SYSTEM_VERSION and CMAKE_SYSTEM_PROCESSOR set from the [conf] or the user_toolchain.
Changelog: Fix: Fix architecture translation from Conan syntax to build system in CMakeToolchain.
Docs: conan-io/docs#2472

I tried to maintain all the logic like it was before for the “Generic” part and fix setting CMAKE_SYSTEM_NAME, CMAKE_SYSTEM_VERSION and CMAKE_SYSTEM_PROCESSOR for Apple.

A bit of background on these variables:

They are typically set when we cross-compile, just setting the CMAKE_SYSTEM_NAME variable manually will make that CMake sets CMAKE_CROSSCOMPILING=True. If you set CMAKE_SYSTEM_NAME, you should also set CMAKE_SYSTEM_VERSION and CMAKE_SYSTEM_PROCESSOR.

The Apple case:

From CMake docs:

Apple case is a bit particular. Apple platforms other than macOS are handled differently to other cross compiling
scenarios. Rather than relying on CMAKE_SYSTEM_NAME to select the target platform, Apple device builds use
CMAKE_OSX_SYSROOT to select the appropriate SDK, which indirectly determines the target platform.

That means for some cases we could not set CMAKE_SYSTEM_NAME and as we are setting CMAKE_OSX_SYSROOT everything should be ok. Seems that this holds true for the M1 case, in fact in that case we were not setting CMAKE_SYSTEM_NAME before.

Also from CMake docs:

Furthermore, when using the Xcode generator, developers can switch between device and simulator builds at build time rather than having a single choice at configure time, so the concept of whether the build is cross compiling or not is more complex. Therefore, the use of CMAKE_CROSSCOMPILING is not recommended for projects targeting Apple devices.

Maybe we should not set these variables for the Apple case but we may affect someone using CMAKE_CROSSCOMPILING (although it’s not recommended).

I have also checked the popular ios-cmake toolchain, and they seem to set those vars for the Apple case (with the same values that we do now)

#TAGS: svn, slow

@czoido czoido added this to the 1.47 milestone Mar 22, 2022
@czoido czoido requested a review from memsharded March 22, 2022 16:47
@czoido czoido marked this pull request as draft March 22, 2022 17:19
Copy link
Member

@memsharded memsharded left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the major advantages of the AppleBlock is that it is esier to maintain, reason about, or customize, if necessary, it is easy to replace the AppleBlock without touching the rest of cross building. More modular than a very large _get_cross_build method with all the cross-build logic for all platforms.

Just a balance, I am not saying this is worse than the current situation, I need to wrap my head around it first.

@czoido
Copy link
Contributor Author

czoido commented Mar 23, 2022

  • I have added a test (test_apple_vars_overwrite_user_conf) that fails in develop and shows that the [conf] values for CMAKE_SYSTEM_NAME/VERSION are overwritten by the Apple block. I think we should not set the same values in two blocks and also they are more related to cross-building that to Apple.

  • There's other thing I have noticed we were doing wrong, for Apple CMAKE_SYSTEM_VERSION should be set to the sdk version, not the os version

  • I'm also not sure we are doing things right for M1, I'll try to investigate further and are more tests

@czoido czoido changed the title Separate and centralize cross-building logic in CMakeToolchain Fix cross-build vars in CMakeToolchain Mar 23, 2022
Copy link
Member

@memsharded memsharded left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is overall looking better.

return arch_host

def _get_cross_build(self):
user_toolchain = self._conanfile.conf.get("tools.cmake.cmaketoolchain:user_toolchain")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe (not related to this PR, just a possible improvement) it is worth honoring the other confs, and letting users passing both the user_toolchain and the other system_xxxx```confs. This can be done by swapping place and doing a return system_name, system_version, system_processor``.

The thing is that it would be weird to define tools.cmake.cmaketoolchain:system_name and that being completely rejected if you have tools.cmake.cmaketoolchain:user_toolchain defined?


def _system_version(self):
os_host = self._conanfile.settings.get_safe("os")
if self._is_apple_cross_building():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if self._is_apple_cross_building(): is the possible factorization. If basically every method that does something starts with if self._is_apple_cross_building():, then that means that it could be separated earlier?


if system_name is not None and system_version is None:
system_version = _system_version
if system_name is not None and system_processor is None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the logic changed a bit here, before we were only setting system_processor in the case that if arch != arch_build: but I think we should set this variable anyway if we activated the CMake cross-compiling setting CMAKE_SYSTEM_NAME manually.

@czoido czoido marked this pull request as ready for review March 24, 2022 10:27
@lasote lasote merged commit 7383a8b into conan-io:develop Mar 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants