hxcpp icon indicating copy to clipboard operation
hxcpp copied to clipboard

hxcpp runtime not compiling on Apple Silicon

Open pztrn opened this issue 3 years ago • 10 comments

Using:

  • haxe 4.2.2 from brew
  • hxcpp 4.2.1
  • Apple MacBook Pro 2020 with M1 chip (arm64)

Trying to compile simple hello world:

## main.hx:
import lib.Logger;

class Main {
	static public function main():Void {
		var l = new Logger();
		l.log("Hello, world");
	}
}

## lib/Logger.hx:
package lib;

class Logger {
	public function new() {}

	public function log(message:String):Void {
		trace(message);
	}
}

With:

haxe --main Main --cpp ./.build/cpp

And getting assembly errors for runtime complilation:

Compiling group: runtime
g++ -D_CRT_SECURE_NO_DEPRECATE -DHX_UNDEFINE_H -c -fvisibility=hidden -stdlib=libc++ -Qunused-arguments -O2 -I/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/include -DHX_MACOS -m64 -Wno-parentheses -Wno-unused-value -Wno-format-extra-args -Wno-overflow -DHXCPP_M64 -DHXCPP_VISIT_ALLOCS(haxe) -DHX_SMART_STRINGS(haxe) -DHXCPP_API_LEVEL=400(haxe) ... tags=[haxe]
 - src/Enum.cpp
 - src/Array.cpp
 - src/hx/Anon.cpp
 - src/hx/Date.cpp
 - src/hx/Interface.cpp
 - src/hx/CFFI.cpp  [haxe,static]
 - src/hx/Object.cpp
 - src/hx/Boot.cpp
 - src/hx/Class.cpp
 - src/Dynamic.cpp
 - src/hx/gc/GcRegCapture.cpp  [haxe,gc]
 - src/Math.cpp
Error: /Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:69:12: error: unknown token in expression
      asm ("movq %%rbx, %0\n\t" : "=r" (regBx) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %rbx, x8
             ^
/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:69:12: error: invalid operand
      asm ("movq %%rbx, %0\n\t" : "=r" (regBx) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %rbx, x8
             ^
/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:70:12: error: unknown token in expression
      asm ("movq %%rbp, %0\n\t" : "=r" (regBp) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %rbp, x9
             ^
/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:70:12: error: invalid operand
      asm ("movq %%rbp, %0\n\t" : "=r" (regBp) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %rbp, x9
             ^
/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:72:12: error: unknown token in expression
      asm ("movq %%r12, %0\n\t" : "=r" (reg12) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %r12, x8
             ^
/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:72:12: error: invalid operand
      asm ("movq %%r12, %0\n\t" : "=r" (reg12) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %r12, x8
             ^
/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:73:12: error: unknown token in expression
      asm ("movq %%r13, %0\n\t" : "=r" (reg13) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %r13, x9
             ^
/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:73:12: error: invalid operand
      asm ("movq %%r13, %0\n\t" : "=r" (reg13) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %r13, x9
             ^
/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:74:12: error: unknown token in expression
      asm ("movq %%r14, %0\n\t" : "=r" (reg14) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %r14, x10
             ^
/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:74:12: error: invalid operand
      asm ("movq %%r14, %0\n\t" : "=r" (reg14) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %r14, x10
             ^
/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:75:12: error: unknown token in expression
      asm ("movq %%r15, %0\n\t" : "=r" (reg15) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %r15, x11
             ^
/Users/pztrn/.haxelib-repo/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:75:12: error: invalid operand
      asm ("movq %%r15, %0\n\t" : "=r" (reg15) );
           ^
<inline asm>:1:7: note: instantiated into assembly here
        movq %r15, x11
             ^
12 errors generated.
Error: Build failed

pztrn avatar Jun 14 '21 14:06 pztrn

For anyone exploring, I wonder if HXCPP_CAPTURE_SETJMP would help (I don't have an M1 to test with yet)

haxiomic avatar Sep 28 '21 14:09 haxiomic

This seems different to what I get. It looks like it is compiling for x64, and so the asm should be valid. There may be some internal define or compiler flag that is different.

To compile for arm on mac (cross compile from intel or native arm), add the "-D HXCPP_ARM64" compile flag

hughsando avatar Oct 16 '21 07:10 hughsando

Exact same error building the HaxeUI sample using the OpenFL backend to build for mac. (OpenFL to build html5 works).

  • haxe 4.2.4 (bottled) from Homebrew
  • hxcpp 4.2.1
  • Apple MacBook Pro M1 (2021) with Rosetta (ARM64 with x86_64 translation)

Error:

 - src/hx/Hash.cpp 
Error: /opt/homebrew/Cellar/haxe/lib/hxcpp/4,2,1/src/hx/gc/GcRegCapture.cpp:69:12: error: unknown token in expression
      asm ("movq %%rbx, %0\n\t" : "=r" (regBx) );
           ^

Interestingly, it's compiling the Mac64 version, not the MacArm64 version. Not sure how to specify that I want it to use ARM64. `

exastencil avatar Dec 09 '21 10:12 exastencil

I haven't been able to get it working. Has anyone else?

onehundredfeet avatar Apr 01 '22 22:04 onehundredfeet

While adding mac support for my hxgodot library I'm having also trouble with hxcpp not respecting any setting relating to the arm64 target architecture.

Static libraries are always output as x86_64 which makes it impossible to link them further down the road for godot 4 arm64 builds.

dazKind avatar Jan 09 '23 12:01 dazKind

Ok, after a closer look I can say, that you also need to define -D HXCPP_ARCH=arm64 in your build.hxml. This triggers a proper switch to the correct architecture. -D HXCPP_ARM64 seems to have no effect whatsoever.

After this we still run into the errors above about the registers. Seems like, again, HXCPP_ARM64 is not respected here: https://github.com/HaxeFoundation/hxcpp/blob/master/src/hx/gc/GcRegCapture.h#L21

If I comment all that logic and #define HXCPP_CAPTURE_ARM64 manually we also get the correct output and the binary compiles and runs.

Atm I struggle a bit understanding why HXCPP_ARM64 might get lost in the context of the file.

@Apprentice-Alchemist @hughsando Would love to hear your feedback

dazKind avatar Jan 09 '23 17:01 dazKind

Could be a problem with gcc vs xcode build. The "macos-toolchain" has the arm flags in there, but the "gcc-toolchain" file does not (I have only really tried macos toolchain). So it might be a matter of adding the flags from macos to gcc in the apple case. Do you have xcode installed?

If you setenv HXCPP_VERBOSE=1 you can get more details on what hxcpp thinks is going on.

hughsando avatar Jan 11 '23 13:01 hughsando

@hughsando Thanks for working on this.

poke1024 avatar Feb 08 '23 10:02 poke1024

It is now working with haxe 4.3.0 and hxcpp 4.3.2 🎉

romamik avatar Apr 20 '23 09:04 romamik

Great news!

onehundredfeet avatar Apr 20 '23 14:04 onehundredfeet