freebasic / fbc

FreeBASIC is a completely free, open-source, multi-platform BASIC compiler, with syntax similar to MS-QuickBASIC, that adds new features such as pointers, object orientation, unsigned data types, inline assembly, and many others.
https://www.freebasic.net
877 stars 137 forks source link

build FreeBasic on macOS 13.1 with clang #409

Open Dieken opened 1 year ago

Dieken commented 1 year ago

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

@@ -3745,7 +3741,6 @@ private sub hAddDefaultLibs( ) end if

    case FB_COMPTARGET_DARWIN
  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
  2. 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.

rversteegen commented 1 year ago

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.