Skip to content

Compile native libraries

Compare
Choose a tag to compare
@razshare razshare released this 06 Jul 11:47
· 182 commits to master since this release

Changes

You can now compile native Go libraries directly using php catpaw.phar -g

Here's an example give the following go project structure

image

php catpaw.phar -g src/lib/Go/Resources/CaptureScreen/main.go

You should get an output like this

❯ php catpaw.phar -g src/lib/Go/Resources/CaptureScreen/main.go 
<14>1 2024-07-06T11:29:32.118346+00:00 raz catpaw.phar 130078 catpaw.phar - INFO: Go program compiled successfully into `/home/raz/GitHub/tncrazvan/catpaw/catpaw/src/lib/Go/Resources/CaptureScreen/main.so`.  

And the resulting shared object and static (without any macros) header file should now be present in the same directory as your go file.

image

Note

The full name of the flag is --gompile.

You can now safely proceed and load the library using GoInterface

<?php
use function CatPaw\Core\anyError;
use function CatPaw\Core\asFileName;
use CatPaw\Core\None;
use function CatPaw\Core\ok;
use CatPaw\Core\Unsafe;
use CatPaw\Go\Interfaces\GoInterface;

interface CaptureScreenInterface {
    public function CaptureScreen(string $fileName):void;
}

/**
 * @param  GoInterface  $go
 * @return Unsafe<None>
 */
function main(GoInterface $go):Unsafe {
    return anyError(function() use ($go) {
        $library = $go->load(CaptureScreenInterface::class, asFileName(__DIR__, './lib/Go/Resources/CaptureScreen/main.so'))->try();
        $library->CaptureScreen('desktop.png');
        return ok();
    });
}