fbc icon indicating copy to clipboard operation
fbc copied to clipboard

build FreeBasic on macOS 13.1 with clang

Open Dieken opened this issue 2 years ago • 1 comments

I almost successfully build FreeBasic on macOS 13.1, record the steps for your reference, hope FreeBasic can support macOS soon.

  1. patch FreeBASIC-1.09.0-source:
diff --git a/src/compiler/fbc.bas b/src/compiler/fbc.bas
index 975b60a..ecb11d3 100644
--- a/src/compiler/fbc.bas
+++ b/src/compiler/fbc.bas
@@ -1114,10 +1114,6 @@ private function hLinkFiles( ) as integer
 
        end select
 
-       if( fbGetOption( FB_COMPOPT_TARGET ) = FB_COMPTARGET_DARWIN ) then
-               ldcline += " -macosx_version_min 10.4"
-       end if
-
        '' extra options
        ldcline += " " + fbc.extopt.ld
 
@@ -3745,7 +3741,6 @@ private sub hAddDefaultLibs( )
                end if
 
        case FB_COMPTARGET_DARWIN
-               fbcAddDefLib( "gcc" )
                fbcAddDefLib( "System" )
                fbcAddDefLib( "pthread" )
                fbcAddDefLib( "ncurses" )
  1. generate bootstrap/darwin-x86_64on Linux and copy this directory to macOS
# on Linux
fbc src/compiler/*.bas -m fbc -i inc -e -r -v -target darwin-x86_64
mkdir -p bootstrap/darwin-x86_64
mv src/compiler/*.c   bootstrap/darwin-x86_64
  1. fix non-local goto, notice non-local goto is undefined behaviour even in GCC.
# this is a very rude hack, turn "goto *p" to "p()". there is no following "return"
# because the caller functions don't always return void.
perl -i -pe 's/goto \*(vr\S+);/((void (*)())(\1))();/' bootstrap/darwin-x86_64/*.c
  1. bootstrap on macOS
gmake V=1 bootstrap
  1. try the bootstrap fbc on macOS
bin/fbc -v -Wl -L`xcrun --show-sdk-path`/usr/lib examples/hello.bas

To fully build the final fbc, subroutines rtlErrorCheck and rtlErrorThrow in src/compiler/rtl-error.bas must be changed to not use non-local goto, I'm not familiar with FreeBasic code, don't have enough skill to patch that.

Dieken avatar Mar 05 '23 03:03 Dieken

Thanks for your notes.

The computed goto removal is actually needed for clang support rather than specifically Mac. fbc will not generate the computed gotos if you compile without -e (error checking). Or if you compile with -ex then it will take the address of some labels for RESUME support and clang won't throw an error either. The problem is just when using -e. (A workaround for the clang error is described here: adding crap: void *pcrap = &&crap; at the top of every generated function). Converting the goto to a function call seems to be the correct solution in the case of compiling with -e, but shouldn't be done with -ex, because I think it would break RESUME. (Yeah, noone should ever use RESUME and it's technically undefined behaviour with -gen gcc. But I think it does still work if it's within the same function.)

The hardcoded obsolete -macosx_version_min is definitely bad and should be replaced with a new fbc commandline arg. I'm not sure how to decide whether to link libgcc or not.

rversteegen avatar Mar 05 '23 08:03 rversteegen