Re: Building universal libraries (x86_64 and arm64) for osx

  • From: Chris Apple <chrisa@xxxxxxxxxxxxxx>
  • To: "luajit@xxxxxxxxxxxxx" <luajit@xxxxxxxxxxxxx>
  • Date: Mon, 20 Mar 2023 15:02:10 +0000

Answering my own question, as I had a breakthrough!

Changing the CFLAGS and LDFLAGS to be “TARGET_” fixed it 😊

make amalg 'MACOSX_DEPLOYMENT_TARGET=11.0' 'TARGET_CFLAGS=-arch arm64 
-DLUAJIT_ENABLE_GC64 -DLUAJIT_ENABLE_LUA52COMPAT' 'TARGET_LDFLAGS=-arch arm64' 
'-j' '10' 'HOST_CC=clang' 'CROSS='

Hope this helps someone else in the future.


From: Chris Apple <chrisa@xxxxxxxxxxxxxx>
Date: Sunday, March 19, 2023 at 4:47 PM
To: luajit@xxxxxxxxxxxxx <luajit@xxxxxxxxxxxxx>
Subject: Re: Building universal libraries (x86_64 and arm64) for osx
How about in the reverse direction? Building arm64 from an intel machine?


The issue I’m seeing here is very similar, but in this case the intel machine 
CANNOT run the arm code, while the arm code technically can run the x86 code 
via rosetta



% make amalg 'MACOSX_DEPLOYMENT_TARGET=11.0' 'CFLAGS=-arch arm64 
-DLUAJIT_ENABLE_GC64 -DLUAJIT_ENABLE_LUA52COMPAT' 'LDFLAGS=-arch arm64' '-j' 
'10' 'HOST_CC=clang' 'CROSS='
Building LuaJIT 2.1.0-beta3
/Library/Developer/CommandLineTools/usr/bin/make -C src amalg
+--------------------------------------------------------------------------+    
                                                                                
                                                      | WARNING: Compiling the 
amalgamation needs a lot of virtual memory        |
| (around 300 MB with GCC 4.x)! If you don't have enough physical memory   |
| your machine will start swapping to disk and the compile will not finish |
| within a reasonable amount of time.                                      |
| So either compile on a bigger machine or use the non-amalgamated build.  |
+--------------------------------------------------------------------------+
/Library/Developer/CommandLineTools/usr/bin/make all "LJCORE_O=ljamalg.o"
DYNASM    host/buildvm_arch.h
make[2]: host/minilua: Bad CPU type in executable
make[2]: *** [host/buildvm_arch.h] Error 1
make[1]: *** [amalg] Error 2
make: *** [amalg] Error 2




From: Chris Apple <chrisa@xxxxxxxxxxxxxx>
Date: Sunday, March 19, 2023 at 1:32 PM
To: luajit@xxxxxxxxxxxxx <luajit@xxxxxxxxxxxxx>
Subject: Re: Building universal libraries (x86_64 and arm64) for osx
Works perfectly! Thank you so much for your help.

From: luajit-bounce@xxxxxxxxxxxxx <luajit-bounce@xxxxxxxxxxxxx> on behalf of 
madigest i <madigest@xxxxxxxxxxxxx>
Date: Sunday, March 19, 2023 at 12:05 PM
To: luajit@xxxxxxxxxxxxx <luajit@xxxxxxxxxxxxx>
Subject: Re: Building universal libraries (x86_64 and arm64) for osx

Hello.

I'm using:

export MACOSX_DEPLOYMENT_TARGET=10.14
make clean
LDFLAGS="-arch x86_64" CFLAGS="-arch x86_64 -DLUAJIT_ENABLE_LUA52COMPAT" make 
amalg

- stuta




On 19. Mar 2023, at 18.03, Chris Apple <chrisa@xxxxxxxxxxxxxx> wrote:

Hi,

I’m attempting to build a universal or “fat” library for luajit for osx. This 
process is described in detail here [1]. The sparknotes is “build a binary with 
arch x86_64, then build a binary with arch arm64, then glue them together using 
the lipo tool”.

In attempting to do so with luajit, I try to build on my arm64 machine with the 
command:

make CFLAGS="-DLUAJIT_ENABLE_GC64 -DLUAJIT_ENABLE_LUA52COMPAT" 
TARGET_CFLAGS="-arch x86_64"
==== Building LuaJIT 2.1.0-beta3 ====
/Applications/Xcode.app/Contents/Developer/usr/bin/make -C src
LINK      luajit
ld: warning: Linking with PIE, -image_base will be ignored
ld: warning: ignoring file luajit.o, building for macOS-arm64 but attempting to 
link with file built for unknown-x86_64
ld: warning: ignoring file libluajit.a, building for macOS-arm64 but attempting 
to link with file built for macOS-x86_64
Undefined symbols for architecture arm64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [luajit] Error 1
make: *** [default] Error 2

Essentially, everything passes until the very end where it attempts to link the 
binary. No arm64 main is found (as expected) so it dies.

I’m wondering a few things:

  *   Is there something I’m missing to compile for x86 on my arm64 machine for 
OSX?
  *   If not, is there a way to run make, such that I only build the libraries, 
and not the binary? I have confirmed that the library is built as expected in 
the correct arch, so linking the binary is unnecessary for me.


Really appreciate the help. I have been trying different combos of the cross 
build opts [2] with no luck.

Thanks,
Chris

[1] 
https://developer.apple.com/documentation/apple-silicon/building-a-universal-macos-binary
[2] https://luajit.org/install.html

Other related posts: