Compiling gfortran in Macbook (M4 Pro)

Dear Fortran community,

I recently bought a Macbook pro with M4 Pro chips and trying to compile my project with fpm and gfortran. I used to compile on Linux system.

My rsp file for fpm is:

options run
options --compiler gfortran
options --flag “-O3 -march=native -mtune=native -pipe -fopenmp -flto -Wall -pedantic -g -llapack -lblas -lm”

With these flags, gfortran will generate the following error:

f951: Error: unknown value 'apple-m1' for '-march'
f951: note: valid arguments are: armv8-a armv8.1-a armv8.2-a armv8.3-a armv8.4-a armv8.5-a armv8.6-a armv8.7-a armv8.8-a armv8.9-a armv8-r armv9-a arm
v9.1-a armv9.2-a armv9.3-a armv9.4-a armv9.5-a native
f951: note: did you mean '-mcpu=apple-m1'?
clang: error: unsupported argument 'apple-m1' to option '-march='

I follow the error and change my rsp file to the following:

options run
options --compiler gfortran
options --flag "-O3 -mcpu=apple-m1 -mtune=native -pipe -fopenmp -flto -Wall -pedantic -g -llapack -lblas -lm"

And what’s happening is that all the files are compiled except for the topmost file. So I abort by pressing Ctrl-c, and the following error will occur:

warning: DWARF unit at offset 0x00000000 contains invalid abbreviation 105 at offset 0x000002fd, valid abbreviations are 1-70

What’s even weirder (or this is totally normal?) is that if I just run the fpm again (fpm run @fast as the rsp file is fast.rsp), the code will be compiled and run.

The same thing happened when I chose the flag as -mcpu=apple-m3.

Just asked chatgpt about the DWARF warning, and seems to get solved.

It is because I had the debug flag -g on. If I delete that, then the compile passes and run the code.

FYI, many people with Macs and arm64 hardware use the homebrew package manager to install the gfortran and, more recently, flang fortran compilers.

Yes, I am using homebrew to install (almost) everything on my macos. May I ask what the flang for Fortran compilers is? How’s that compared to gfortran?

Here is a recent discussion thread about the flang compiler. First official release of Flang! - #11 by wyphan I have only recently begun to use it, so I don’t know much about its capabilities. I have noticed that it does not support 80-bit or 128-bit floating point arithmetic, but it does support one of the 16-bit floating point formats.

It may depend on the target, but I’m seeing that it supports 80-bit (kind = 10) and both 16-bit (kind = 2 & 3) formats.

I’ve understood (maybe wongly) that conda is somehow the must-have package manager as of today for everything related to development. On Mac I prefer MacPorts to Homebrew (Homebrew attributing the ownership of /usr/local to a regular user is enough to keep me away…), but Macports has less packages, and they are not updated on a frequent basis. So I am considering to have conda besides MacPorts.

I don’t know why, but on intel hardware they do that. On arm64 hardware, it installs in /opt.

That’s better… But the real pain is the ownership given to the regular user: is it the same when it installs in /opt ?

I would not know. The Homebrew instructions say that you can install under a user account, but I’ve never done that. I always install software from an admin account, and then run it under a user account. That is the same way I have always installed Apple Xcode software. BTW, speaking of Xcode, you only need the command line tools, not the whole package, for both gfortran and flang. Homebrew will install those automatically if necessary.

When Apple switched from Intel to its own silicon, Homebrew moved from that location to the /opt directory —it seems, because of a conflict with the Rosetta compatibility layer.

Because of that, I gave MacPorts a try, but it was sort of a nightmare compared to Homebrew… YMMV.

It might have been just a coincidence, but that was about the same time that Apple locked down its / directory for security reasons. For example, the fink package manager switched then from using /sw to /opt/sw. I think intel had already been using /opt, I don’t remember any changes then for them. NAG also uses /opt, but I’m a recent nagfor user, so I don’t know if/when they switched directories.

What I meant was that, at the time (i.e., around Apple M1), you had the option to install Homebrew through Rosetta (for x86-64 binaries), and that one used /usr/local. And also native/ARM Homebrew, which used /opt.

I remember I tried that route for docker (which was almost unusable under ARM), but it was too slow due to the ISA emulation.