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

luajit: fix building for iOS and Android #26577

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

kambala-decapitator
Copy link
Contributor

@kambala-decapitator kambala-decapitator commented Feb 11, 2025

Summary

Changes to recipe: luajit/all

Motivation

  • Fixes building for iOS and Android
  • forbids building for 32-bit platform on macOS: Cross-compiling 32 bit LuaJIT on a 64 bit host LuaJIT/LuaJIT#664
  • adds passing CPPFLAGS as TARGET_CFLAGS since upstream doesn't read this standard variable
  • fixes producing .so symlinks when ldconfig is unavailable (e.g. building for Android on macOS) via upstream patch, but I had to adjust it a bit that it applies on 2.1.0-beta3
  • simplifies using Clang compiler

Details

Followed the documentation at https://luajit.org/install.html#cross, tested building 2.1.0-beta3 for iOS on macOS and for Android on macOS and Linux.

I will explain why env modification is done. Without it target flags (i.e. host in Conan terms) "taint" the host ones (build in Conan terms) which results in non-working executable that's meant to run on the build platform, it can be seen in the build log:

HOSTCC    host/minilua.o
clang  -O2 -fomit-frame-pointer -Wall   -O3 -isysroot /Applications/Xcode-15.4.0.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.5.sdk -arch arm64 -mios-version-min=12.0 -I. -DLUAJIT_OS=LUAJIT_OS_OSX -DLUAJIT_TARGET=LUAJIT_ARCH_arm64 -DLJ_ARCH_HASFPU=1 -DLJ_ABI_SOFTFP=0 -DLUAJIT_NO_UNWIND  -c -o host/minilua.o host/minilua.c

HOSTLINK  host/minilua
clang   -isysroot /Applications/Xcode-15.4.0.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.5.sdk -arch arm64 -mios-version-min=12.0   -o host/minilua host/minilua.o -lm 

Checking how the host executable is produced:

$(HOST_O): %.o: %.c
	$(E) "HOSTCC    $@"
	$(Q)$(HOST_CC) $(HOST_ACFLAGS) -c -o $@ $<


ASOPTIONS= $(CCOPT) $(CCWARN) $(XCFLAGS) $(CFLAGS)
CCOPTIONS= $(CCDEBUG) $(ASOPTIONS)
HOST_ACFLAGS= $(CCOPTIONS) $(HOST_XCFLAGS) $(TARGET_ARCH) $(HOST_CFLAGS)

first I came up with the following solution that overwrites those target flags with proper ones:

xcrun_host = XCRun(self, sdk='macosx')
arch_host = {
    'x86_64': 'x86_64',
    'armv8': 'arm64',
}.get(str(self.settings_build.get_safe("arch")))
target_flag_host = f"{arch_host}-apple-macos{self.settings_build.get_safe("os.version", default='')}"
flags_host = f"'-isysroot \"{xcrun_host.sdk_path}\" -target {target_flag_host}'"

# and pass additional variables as make args:
[
    f"HOST_CFLAGS={flags_host}",
    f"HOST_LDFLAGS={flags_host}",
]

which results in proper executable:

HOSTCC    host/minilua.o
clang  -O2 -fomit-frame-pointer -Wall   -O3 -isysroot /Applications/Xcode-15.4.0.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.5.sdk -arch arm64 -mios-version-min=12.0 -I. -DLUAJIT_OS=LUAJIT_OS_OSX -DLUAJIT_TARGET=LUAJIT_ARCH_arm64 -DLJ_ARCH_HASFPU=1 -DLJ_ABI_SOFTFP=0 -DLUAJIT_NO_UNWIND -isysroot "/Applications/Xcode-15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk" -target arm64-apple-macos -c -o host/minilua.o host/minilua.c
clang: warning: overriding '-mios-version-min=12.0' option with '-target arm64-apple-macos' [-Woverriding-t-option]


HOSTLINK  host/minilua
clang   -isysroot /Applications/Xcode-15.4.0.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.5.sdk -arch arm64 -mios-version-min=12.0  -isysroot "/Applications/Xcode-15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk" -target arm64-apple-macos -o host/minilua host/minilua.o -lm   
clang: warning: overriding '-mios-version-min=12.0' option with '-target arm64-apple-macos' [-Woverriding-t-option]

But later I figured out that it's much easier to simply drop CFLAGS / LDFLAGS from the environment, as LuaJIT uses its own variables for host/target flags. And Android has quite a similar issue.

Also when building a build binary in Android build on macOS, a workaround is required - passing -isysroot in HOST_CFLAGS and HOST_LDFLAGS. This looks like a common issue related to the NDK recipe, I've opened #26585 about that.


Comment on lines +98 to +99
if "clang" in str(self.settings.compiler):
args.append("DEFAULT_CC=clang")
Copy link
Contributor

Choose a reason for hiding this comment

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

This is incorrect and basically reproduces the existing sloppy handling of CC in the recipe. It hackily replaces the default CC=gcc with clang, but it ignores the exact compiler executable set in the Conan profile and won't work for any other compilers in general.

It should really be something like

cc = AutotoolsToolchain(self).vars()["CC"]
args.append(f"CC={cc}")

Similarly, HOST_CC needs to be set to the CC_FOR_BUILD value from the toolchain vars. Something like conan-io/conan#17602 would be very welcome here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's what iOS build docs suggest to do. So should I add this variable only for iOS then and revert the general compiler adjustment?

@valgur
Copy link
Contributor

valgur commented Feb 11, 2025

I have not opened a PR yet, but most of the make args except TARGET_FLAGS set here seem to be necessary for cross-compilation in general: https://github.com/valgur/conan-center-index/blob/2aab3d3379aad15e80e40721351a5dca0d99a629/recipes/luajit/all/conanfile.py#L53-L88

@kambala-decapitator
Copy link
Contributor Author

kambala-decapitator commented Feb 12, 2025

@valgur would it be ok to add another commit that fixes Android build (it builds on top of this change w.r.t. clearing CFLAGS / LDFLAGS) or wait for this PR to be merged and then open another one?

@valgur
Copy link
Contributor

valgur commented Feb 12, 2025

Would be ok by me, but I have no say in this as a regular contibutor.

@CLAassistant
Copy link

CLAassistant commented Feb 13, 2025

CLA assistant check
All committers have signed the CLA.

@kambala-decapitator kambala-decapitator changed the title luajit: fix building for iOS luajit: fix building for iOS and Android Feb 13, 2025
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.

3 participants