Recent

Author Topic: Cross Compiling on Mac M1  (Read 5573 times)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Cross Compiling on Mac M1
« on: December 25, 2021, 11:36:51 pm »
My old MacBook Pro (circa 2012) was set up with VirtualBox so I could easily compile for Mac, Windows, and Linux.

I am now migrating to a new MaxBook Pro M1 (2021 with Monterey). It is a fantastic machine, but I am running into some stumbling blocks. I was able to install Lazarus 2.2.0 RC2 with FPC 3.2.2 without major issues (wow, many thanks to the dev team!).

First question, has anyone successfully cross compiled from M1 to Windows 64? I am trying to follow:

https://wiki.lazarus.freepascal.org/Cross_compiling#From_macOS_to_Win64

but get linking errors.

I also tried fpcupdeluxe, but it gives an access violation.

Second question, has anyone successfully installed Lazarus on Ubuntu in a UTM virtual machine? I have UTM running Ubuntu 20, but am not finding a way to install aarch64/arm64 Free Pascal/Lazarus.

Season's greetings!

EDIT: I suppose this question should have been posted under Operating Systems - macOS / Mac OS X, my apologies.
« Last Edit: December 26, 2021, 12:29:02 am by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Cross Compiling on Mac M1
« Reply #1 on: December 26, 2021, 02:13:19 am »
Sorry, you cannot cross-compile from one CPU architecture to another CPU architecture (ie aarch64 to x86_*).
« Last Edit: December 26, 2021, 02:16:57 am by trev »

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Cross Compiling on Mac M1
« Reply #2 on: December 26, 2021, 02:31:10 am »
Thanks trev,

I thought this:

https://wiki.lazarus.freepascal.org/Cross_compiling#Host_and_target_on_different_CPUs

indicated that it was possible to compile for another cpu.

I'm trying to understand.



“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Cross Compiling on Mac M1
« Reply #3 on: December 26, 2021, 02:42:17 am »
fpcupdeluxe seems to be able to do this also, although I have not yet used it. Am I incorrect?
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Cross Compiling on Mac M1
« Reply #4 on: December 26, 2021, 04:36:01 am »
It seems I was wrong... confused running VMs of different architectures on Mac M1.

fpc -Px86_64 test.pas does indeed successfully create an X86 binary on the M1. I guess for Windows you'd need to add -Twin64 but what the pre-requisites are for that I do not know (I create my Windows X86 applications on an Intel Mac mini).

I'm sure Jonas will know the answer...

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Cross Compiling on Mac M1
« Reply #5 on: December 26, 2021, 07:39:53 am »
Code: Text  [Select][+][-]
  1. make clean all OS_TARGET=win64 CPU_TARGET=x86_64 OPT="-XR/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"

succeeds, but:

Code: Text  [Select][+][-]
  1. sudo make crossinstall OS_TARGET=win64 CPU_TARGET=x86_64 INSTALL_PREFIX=/usr

fails with:

Code: Text  [Select][+][-]
  1. make: *** No rule to make target `crossinstall`.  Stop.

I wrote the following script to install the necessary bits created by the first incantation:

Code: Bash  [Select][+][-]
  1. #!/bin/sh
  2. cd ~/fpc-win/packages
  3.            
  4. if [ ! -d /usr/local/lib/fpc/3.3.1/units/x86_64-win64 ]
  5.   then
  6.     mkdir /usr/local/lib/fpc/3.3.1/units/x86_64-win64
  7. fi
  8.  
  9. ls | while read dir
  10.   do
  11.     if [ -d ${dir} ]
  12.       then
  13.         if [ -d ${dir}/units ]
  14.           then
  15.             cd ${dir}/units/x86_64-win64/
  16.             if [ ! -d /usr/local/lib/fpc/3.3.1/units/x86_64-win64/${dir} ]
  17.               then
  18.                 mkdir /usr/local/lib/fpc/3.3.1/units/x86_64-win64/${dir}
  19.             fi
  20.             rsync -avr * /usr/local/lib/fpc/3.3.1/units/x86_64-win64/${dir}/
  21.             cd ../../../
  22.         else
  23.           continue
  24.         fi
  25.     else
  26.       continue
  27.     fi
  28.   done
  29.  
  30. if [ ! -d /usr/local/lib/fpc/3.3.1/units/x86_64-win64/rtl ]
  31.   then
  32.     mkdir /usr/local/lib/fpc/3.3.1/units/x86_64-win64/rtl
  33. fi
  34.  
  35. cd ~/fpc-win/rtl/units/x86_64-win64/
  36. rsync -avr * /usr/local/lib/fpc/3.3.1/units/x86_64-win64/rtl/
  37.  
  38. cd ~/fpc-win/rtl/win64
  39. rsync -avr x86_64 /usr/local/lib/fpc/3.3.1/units/x86_64-win64/rtl/
  40. rsync -avr *.pp /usr/local/lib/fpc/3.3.1/units/x86_64-win64/rtl/

Notes:

0. The script needs excepting with sudo
1. fpc-win in my home directory contains the fpc source
2. /usr/local/lib/fpc/3.3.1/ contains my installed FPC executables, units etc

My Windows test program was simply:

Code: Pascal  [Select][+][-]
  1. Program test_win64;
  2.  
  3. begin
  4.  WriteLn('Hello World!');
  5. end.

which I compiled with "fpc -Twin64 -Px86_64 test_win64.pas" and which produced "test_win64.exe" which ran successfully on Windows 10 64 bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Cross Compiling on Mac M1
« Reply #6 on: December 26, 2021, 12:11:03 pm »
Please note that compiling for Win64 is the odd-one out when compiling from non-x86 to x86: as Win64 does not use the Extended type (which does not exist on non-x86 anyway) it is essentially the only platform that you currently can successfully cross compile to from non-x86.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Cross Compiling on Mac M1
« Reply #7 on: December 26, 2021, 06:26:39 pm »
Wow, thanks trev. I will see if I can make some headway.

Regarding virtual machines, yes I have to leave behind VirtualBox which I was quite fond of. UTM works pretty well for Ubuntu arm64, but I'm still getting used to it. The x64 version seems too slow to be useful.

Thanks PascalDragon. That is good to know, I guess I will be hanging on to the old Intel PowerBook for a while yet. :(
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

 

TinyPortal © 2005-2018