How to port fbc to new platform?

General discussion for topics related to the FreeBASIC project or its community.
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

How to port fbc to new platform?

Post by Cretin Ho »

After a quick look at the Solaris support of fbc, I found some problems:

First, Solaris should be dimmed as SunOS, so it should be FB_COMPTARGET_SUNOS and __FB_SUNOS__, why we use DARWIN?

I found these on fb.bi and fbc.bas: __FB_DARWIN__ and FB_COMPTARGET_DARWIN

This is wrong. DARWIN should be Mac OS X. We currently wrongly use DARWIN for Solaris.

BTW, where is __FB_DARWIN__ and friends defined? I didn't find it on fb.bi. Please correct me if I'm wrong.

Second, we have skeleton support for all of the three major BSDs: Free, Open, Net. But why don't we complete it by adding Dragonfly, too? We pretty much could reuse FreeBSD bootstrap for Dragonfly. The problem is, I don't know what Dragonfly defined for themselves, maybe __DRAGONFLY__? I really don't know.

Third, Solaris unlike other platforms, they consider amd64 is just an extension over the existing x86 but not a separate platform. So even though their OS is 64 bit only, when we use uname -m, it will just show 'i86pc'.

So we need to modify our makefile.

BTW, the output of uname -a on OpenIndiana is:

SunOS openindiana 5.11 illumos-13904da86c i86pc i386 i86pc

I don't know it's good or not if we split illumos and solaris to separate platform. But they are diverting very quickly over their OpenSolaris root.

Fourth, because the reason above, OpenIndiana put their libraries a bit different than other platform:

/usr/lib is 32 bit, if you want to link with 64 bit, it's /usr/lib/amd64. There is another place /usr/lib/64 which is just a symlink to /usr/lib/amd64. So /usr/lib/amd64 should be preferred.

Fifth, when I tried to gmake bootstrap -j4 on OpenIndiana, it failed because of can't find ffi.h. I have to use gmake bootstrap-minimal -j4.

The location of ffi.h on OpenIndiana is /usr/lib/libffi-3.2.1/include/ffi.h for 32 bit and /usr/lib/amd64/libffi-3.2.1/include/ffi.h for 64 bit.

We need to modify our makefile.

Note: please use gmake, if you forgot the g prefix, it will use another incompatible version of make.

If you want to know how I could have bootstrap-minimal worked for me on OpenIndiana, please read:

viewtopic.php?f=17&t=29149

But I really need guidance now, I have no idea how to go further.

fbc experts please help me.
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: How to port fbc to new platform?

Post by Cretin Ho »

The Solaris support we currently have is too much guess work when dealing with the platform specific details. The best someone could do is actually running it on a VM and got real life experience. The forum topic I linked on #1 I have uploaded my OpenIndiana VM, you could use it.
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: How to port fbc to new platform?

Post by Cretin Ho »

Cretin Ho wrote:Second, we have skeleton support for all of the three major BSDs: Free, Open, Net. But why don't we complete it by adding Dragonfly, too? We pretty much could reuse FreeBSD bootstrap for Dragonfly. The problem is, I don't know what Dragonfly defined for themselves, maybe __DRAGONFLY__? I really don't know.
Finally. After dig up DragonflyBSD's source code on github I find it, they defined themselves as __DragonFly__

Don't know if we need to write it uppercase or not, but on the source it's simply __DragonFly__
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: How to port fbc to new platform?

Post by Cretin Ho »

Another weirdness of OpenIndiana. Due to the 3rd problem I listed on #1, the GCC compiler of this platform generate 32 bit binary by default even though the OS is 64 bit only. To have a 64 bit binary, we have to pass '-m64' to it, e.g:

gcc -m64 test.c -o test

CC='gcc -m64' ./configure

It's real annoying. But luckily with higher version of GCC, they finally changed to generate 64 bit binary by default.

The GCC I included with my OpenIndiana VM is GCC 10, it will generate 64 bit binary by default, without having to pass '-m64' like prior GCC versions.

Note: don't install the build-essential package. This package is only needed if you want to build the OS from source. After this package is installed, it will overwrite the GCC version to be 7.5.0 which will generate 32 bit binary by default.

I made this mistake but luckily I created a VirtualBox snapshot of it before trying so I could roll it back. VirtualBox snapshot is very helpful.
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: How to port fbc to new platform?

Post by Cretin Ho »

I was left in darkness without any guidance. This is all guess work and not tested. Keep in mind, there might be dragons inside this code.

Apply it against the source tree you get after extract FreeBASIC-1.07.2-source-bootstrap.tar.xz from github release.

https://ln2.sync.com/dl/f7329e820/tf7xy ... e-mzckrthz
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: How to port fbc to new platform?

Post by dkl »

Looks pretty good, I think you've got most of it figured out already.

One way to do/test it is probably something like this:
- apply patches to normal fbc sources
- build linux fbc which can now target solaris with the help of a linux-to-solaris gcc toolchain
- build FB runtime library (rtlib) with the linux-to-solaris gcc toolchain
- build a new solaris fbc using the linux fbc with -target solaris and link it against the solaris rtlib

If you're unsure about linker options or such - the general strategy is to look at whatever gcc is doing, and copy that.

If you have a somewhat working fbc on the new target, and rtlib is compiling too, things to do next:
- try to compile and run a "hello world" program
- compile fbc again using itself, see if it still works after that
- try compiling and running the fbc test suite
- check if the gfxlib2 can maybe be compiled and/or needs any adjustments
- take a look at headers in fbc/inc/crt and check whether they need adjustments to work under the new target or not

By the way, if you want, consider putting the patches into Git (such as a fork of https://github.com/freebasic/fbc) and making a pull request. I don't think any of the existing fbc developers have much experience with Solaris, so probably can't help with that part of it, but will probably still be able to help with certain steps of the integration (adding the new compilation target(s) in fbc/src/compiler, extending fbc/src/rtlib for the new target, build system fixes, etc.).
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: How to port fbc to new platform?

Post by Cretin Ho »

dkl wrote:Looks pretty good, I think you've got most of it figured out already.

One way to do/test it is probably something like this:
- apply patches to normal fbc sources
- build linux fbc which can now target solaris with the help of a linux-to-solaris gcc toolchain
- build FB runtime library (rtlib) with the linux-to-solaris gcc toolchain
- build a new solaris fbc using the linux fbc with -target solaris and link it against the solaris rtlib

If you're unsure about linker options or such - the general strategy is to look at whatever gcc is doing, and copy that.

If you have a somewhat working fbc on the new target, and rtlib is compiling too, things to do next:
- try to compile and run a "hello world" program
- compile fbc again using itself, see if it still works after that
- try compiling and running the fbc test suite
- check if the gfxlib2 can maybe be compiled and/or needs any adjustments
- take a look at headers in fbc/inc/crt and check whether they need adjustments to work under the new target or not

By the way, if you want, consider putting the patches into Git (such as a fork of https://github.com/freebasic/fbc) and making a pull request. I don't think any of the existing fbc developers have much experience with Solaris, so probably can't help with that part of it, but will probably still be able to help with certain steps of the integration (adding the new compilation target(s) in fbc/src/compiler, extending fbc/src/rtlib for the new target, build system fixes, etc.).
Hi. Could you let me know where __FB_DARWIN__, __FB_DOS__,... and friends defined? I can't find it.

My work is pretty much guess work and I have many things unsure so I'm not confident for a pull request.

Further test is needed.
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: How to port fbc to new platform?

Post by dkl »

Check out symb-define.bas, specifically: https://github.com/freebasic/fbc/blob/1 ... e.bas#L270
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: How to port fbc to new platform?

Post by Cretin Ho »

dkl wrote:Check out symb-define.bas, specifically: https://github.com/freebasic/fbc/blob/1 ... e.bas#L270
There is no __FB_DARWIN__, __FB_FREEBSD__,... on this file.

What is the mechanism of these macros?

I did a search on all of the source files under src/compiler and still not see where they are defined.

Let me guess:

For example, FreeBSD define themselves as __FreeBSD__, so __FB_FREEBSD__ is just an uppercase version of the former and the are practically the same?

This is a wild guess, as I didn't see the logic like this defined anywhere on the source files.
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: How to port fbc to new platform?

Post by Cretin Ho »

With the patch above I forgot to close the if statement with endif. Correct it to this will stop make from complaining about syntax error:

# For DragonflyBSD and Solaris, always use x86_64
ifeq ($(TARGET_OS),dragonfly)
TARGET_ARCH := x86_64
endif
ifeq ($(TARGET_OS),solaris)
TARGET_ARCH := x86_64
endif

When running TARGET=x86_64-pc-sun2.11 make bootstrap-dist, it failed with this error:

# Precompile fbc sources for various targets
rm -rf bootstrap
mkdir -p bootstrap/dos
mkdir -p bootstrap/freebsd-x86
mkdir -p bootstrap/freebsd-x86_64
mkdir -p bootstrap/dragonfly-x86_64
mkdir -p bootstrap/solaris-x86_64
mkdir -p bootstrap/linux-x86
mkdir -p bootstrap/linux-x86_64
mkdir -p bootstrap/win32
mkdir -p bootstrap/win64
mkdir -p bootstrap/linux-arm
mkdir -p bootstrap/linux-aarch64
./bin/fbc src/compiler/*.bas -m fbc -i inc -e -r -v -target dos && mv src/compiler/*.asm bootstrap/dos
/bin/sh: 1: ./bin/fbc: not found
make: *** [makefile:1108: bootstrap-dist] Error 127

I does have fbc installed on /usr/local already.

But I tried to make bootstrap anyway, fail:

viewtopic.php?f=17&t=29171

make bootstrap-minimal also fail:

viewtopic.php?f=17&t=29172

I have no knowledge about the bootstrapping process of fbc. It's all trial and error.
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: How to port fbc to new platform?

Post by Cretin Ho »

Cretin Ho wrote:When running TARGET=x86_64-pc-sun2.11 make bootstrap-dist, it failed with this error:

# Precompile fbc sources for various targets
rm -rf bootstrap
mkdir -p bootstrap/dos
mkdir -p bootstrap/freebsd-x86
mkdir -p bootstrap/freebsd-x86_64
mkdir -p bootstrap/dragonfly-x86_64
mkdir -p bootstrap/solaris-x86_64
mkdir -p bootstrap/linux-x86
mkdir -p bootstrap/linux-x86_64
mkdir -p bootstrap/win32
mkdir -p bootstrap/win64
mkdir -p bootstrap/linux-arm
mkdir -p bootstrap/linux-aarch64
./bin/fbc src/compiler/*.bas -m fbc -i inc -e -r -v -target dos && mv src/compiler/*.asm bootstrap/dos
/bin/sh: 1: ./bin/fbc: not found
make: *** [makefile:1108: bootstrap-dist] Error 127
Wait a minute. I specify the target as x86_64-pc-sun2.11, why it think it's bootstrapping for DOS?

With someone doesn't have any knowledge about make files and it's the first time I edited one, getting that far is a surprise for me.

But this porting needs a real fbc expert. I'm just not qualified. I think I created a mess in the source code, too, not just the make file.
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: How to port fbc to new platform?

Post by Cretin Ho »

Any fbc developers if interested could resume this work or start a completely new one if needed, my OpenIndiana VM is still there. Bye.
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: How to port fbc to new platform?

Post by dkl »

Cretin Ho wrote:
dkl wrote:Check out symb-define.bas, specifically: https://github.com/freebasic/fbc/blob/1 ... e.bas#L270
There is no __FB_DARWIN__, __FB_FREEBSD__,... on this file.
It automatically defines a macro named __FB_<target_name>__, where <target_name> is the upper-case version of the name specified in the targetinfo table in fb.bas. So -target linux becomes __FB_LINUX__, etc.
Cretin Ho wrote:When running TARGET=x86_64-pc-sun2.11 make bootstrap-dist, it failed with this error:
[...]
I specify the target as x86_64-pc-sun2.11, why it think it's bootstrapping for DOS?
"make bootstrap-dist" is a command which is meant to create pre-compiled versions of the fbc sources for multiple targets, such as dos, linux, win32 (more could be added to this makefile command). Doing this requires a working fbc. Also, it ignores the TARGET setting (on purpose - since it basically compiles for multiple targets in one command).
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: How to port fbc to new platform?

Post by Cretin Ho »

dkl wrote:
Cretin Ho wrote:
dkl wrote:Check out symb-define.bas, specifically: https://github.com/freebasic/fbc/blob/1 ... e.bas#L270
There is no __FB_DARWIN__, __FB_FREEBSD__,... on this file.
It automatically defines a macro named __FB_<target_name>__, where <target_name> is the upper-case version of the name specified in the targetinfo table in fb.bas. So -target linux becomes __FB_LINUX__, etc.
If I know this sooner. My work has to be done from scratch again.

I have made a pull request to add DragonflyBSD support. Please have a look at it.

I'm very new to github and it's the first time I do something like that.

Meanwhile, github doesn't like my browser Pale Moon, I struggled to be able to make this pull request. It doesn't allow me to make any comment hence the empty comment. I will have to switch to Firefox for github.
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: How to port fbc to new platform?

Post by Cretin Ho »

I have just made a pull request to add Solaris support to the compiler. Please have a look at it. Thanks.
Post Reply