tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

all: make TinyGo code usable with "big Go" CGo

Open aykevl opened this issue 1 year ago • 2 comments

I managed to get CGo sort-of working in VSCode (meaning that it will typecheck code in the IDE itself) using a few crude hacks, but it requires a few minor changes to the TinyGo standard library.

I intend to eventually add this support in the TinyGo extension for VSCode directly, but for now I've manually updated .vscode/settings.json to get IDE support. In any case, it would be nice to have this for when I hopefully add this to the TinyGo extension eventually.

For reference, I'm using TinyGo target "gopher-badge" in VSCode with the following custom settings:

{
    "cmake.configureOnOpen": false,
    "go.toolsEnvVars": {
        "CGO_ENABLED": "1",
        "CC": "clang",
        "GOOS": "linux",
        "GOARCH": "arm",{
        "CGO_CFLAGS": "-Werror -fshort-enums -fomit-frame-pointer -mfloat-abi=soft -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -ffunction-sections -fdata-sections -nostdlibinc -isystem /home/ayke/.cache/tinygo/picolibc-thumbv6m-unknown-unknown-eabi-cortex-m0plus/include -isystem /home/ayke/src/tinygo/tinygo/lib/picolibc/newlib/libc/include -isystem /home/ayke/src/tinygo/tinygo/lib/picolibc/newlib/libc/tinystdio -Oz --target=thumbv6m-unknown-unknown-eabi -mcpu=cortex-m0plus -mthumb -fno-PIC",
        "GOROOT": "/home/ayke/.cache/tinygo/goroot-8d8f8a7da5d85cd177e3196b59dcd519d76b00dc6a0aae2635059910f11c4e8c",
        "GOFLAGS": "-tags=cortexm,baremetal,linux,arm,rp2040,rp,gopher_badge,math_big_pure_go,gc.conservative,scheduler.tasks,serial.usb",
        "CCC_OVERRIDE_OPTIONS": "+-mthumb",
    },
}

Some notes:

  • I got the list of CFlags by patching the tinygo binary, I should make a separate PR for that too.
  • I had to remove the tinygo build tag to get things to work (the tinygo files often can't be parsed by upstream CGo).
  • The CCC_OVERRIDE_OPTIONS is the especially crude hack: Go adds -marm and there doesn't appear to be a way to override it, except using this special (undocumented) environment variable. It's really meant to be used for tests, not for production code.

aykevl avatar Jan 27 '24 18:01 aykevl

Size difference with the dev branch:

Binary size difference
drivers/sizes-pr.txt has more commands than drivers/sizes-dev.txt
    tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/slideshow
 flash                          ram
 before   after   diff          before   after   diff
  60736   60736      0   0.00%    6160    6160      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adt7410/main.go
   9612    9612      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/adxl345/main.go
  13232   13232      0   0.00%    6780    6780      0   0.00% tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/amg88xx
   8600    8600      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/main.go
  11584   11584      0   0.00%    6556    6556      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/apds9960/proximity/main.go
   9660    9660      0   0.00%    4752    4752      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/apa102/itsybitsy-m0/main.go
   8180    8180      0   0.00%    2304    2304      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/at24cx/main.go
   8220    8220      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bh1750/main.go
   7508    7508      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/blinkm/main.go
  70220   70220      0   0.00%    3676    3676      0   0.00% tinygo build -size short -o ./build/test.hex -target=pinetime     ./examples/bma42x/main.go
  63156   63156      0   0.00%    6160    6160      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmi160/main.go
  27644   27644      0   0.00%    4772    4772      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp180/main.go
  63264   63264      0   0.00%    6192    6192      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/bmp280/main.go
  12212   12212      0   0.00%    4804    4804      0   0.00% tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bmp388/main.go
   8128    8128      0   0.00%    3332    3332      0   0.00% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/sram/main.go
  22088   22088      0   0.00%    3528    3528      0   0.00% tinygo build -size short -o ./build/test.hex -target=bluepill ./examples/ds1307/time/main.go
  69044   69044      0   0.00%    6332    6332      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/ds3231/main.go
   4704    4704      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/easystepper/main.go
  24792   24792      0   0.00%    6292    6292      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/espat/espconsole/main.go
  24940   24940      0   0.00%    6292    6292      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/espat/esphub/main.go
  24792   24792      0   0.00%    6292    6292      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/espat/espstation/main.go
  68620   68620      0   0.00%    6940    6940      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/flash/console/spi
  64652   64652      0   0.00%    8980    8980      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/flash/console/qspi
   7040    7040      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/gc9a01/main.go
  67468   67468      0   0.00%    6336    6336      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/i2c/main.go
  67896   67896      0   0.00%    6472    6472      0   0.00% tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/gps/uart/main.go
   8260    8260      0   0.00%    4732    4732      0   0.00% tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/hcsr04/main.go
   5612    5612      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/customchar/main.go
   5656    5656      0   0.00%    2264    2264      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hd44780/text/main.go
  10440   10440      0   0.00%    4740    4740      0   0.00% tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/hd44780i2c/main.go
  14496   14496      0   0.00%    6556    6556      0   0.00% tinygo build -size short -o ./build/test.hex -target=nano-33-ble ./examples/hts221/main.go
  16868   16868      0   0.00%    2344    2344      0   0.00% tinygo build -size short -o ./build/test.hex -target=microbit ./examples/hub75/main.go
  10056   10056      0   0.00%    6900    6900      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/basic
  10696   10696      0   0.00%    4852    4852      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/basic
  28996   28996      0   0.00%   38060   38060      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/pyportal_boing
  10080   10080      0   0.00%    6908    6908      0   0.00% tinygo build -size short -o ./build/test.hex -target=pyportal ./examples/ili9341/scroll
  10784   10784      0   0.00%    4860    4860      0   0.00% tinygo build -size short -o ./build/test.hex -target=xiao ./examples/ili9341/scroll
 959936  959936      0   0.00%  220896  220896      0   0.00%

github-actions[bot] avatar Jan 27 '24 18:01 github-actions[bot]

Thank you for looking into this. I'm pretty annoyed with VSCode red marks all over when working on something that uses our bluetooth lib.

ysoldak avatar Feb 08 '24 08:02 ysoldak

Thanks for adding this @aykevl and to @ysoldak for review. Now merging.

deadprogram avatar Feb 19 '24 14:02 deadprogram

Looks like this might have had an unintended side effect @aykevl

Please see https://github.com/tinygo-org/drivers/actions/runs/7961592170/job/21733165140#step:8:509

deadprogram avatar Feb 19 '24 15:02 deadprogram

@deadprogram pretty sure that's unrelated. I've seen many weird flaky tests lately, I think we have another memory corruption error.

aykevl avatar Feb 19 '24 15:02 aykevl