Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run FFI extension modules (.so files) made by Golang? #21

Open
KEINOS opened this issue Sep 27, 2020 · 1 comment
Open

How to run FFI extension modules (.so files) made by Golang? #21

KEINOS opened this issue Sep 27, 2020 · 1 comment

Comments

@KEINOS
Copy link
Owner

KEINOS commented Sep 27, 2020

I can't make the FFI (Foreign Function Interface) work.

$ docker run --rm -it test:local php /app/sample.php
Fatal error: Uncaught FFI\Exception: Failed loading '/usr/local/lib/php/extensions/no-debug-non-zts-20200804/libutil.so' in /app/sample.php:6
Stack trace:
#0 /app/sample.php(6): FFI::cdef('void print(char...', '/usr/local/lib/...')
#1 {main}
  thrown in /app/sample.php on line 6
<?php

$util = FFI::cdef(
    "void print(char* p0);
    int sum(int p0, int p1);",
    "/usr/local/lib/php/extensions/no-debug-non-zts-20200804/libutil.so"
);

$util->print(
    (string) $util->sum(2, 4)
);
  • Dockerfile (Multi-state-build)
FROM golang:alpine AS builder

RUN \
    apk --no-cache add \
        git \
        gcc \
        musl-dev \
    && git clone https://github.com/eislambey/php-ffi-go-example.git /go/src

RUN \
    cd /go/src && \
    GOOS=linux go build -o libutil.so -buildmode=c-shared util.go

FROM keinos/php8-jit:latest

USER root

COPY sample.php /app/sample.php
COPY --from=builder /go/src/libutil.h /usr/local/lib/php/extensions/no-debug-non-zts-20200804/libutil.h
COPY --from=builder /go/src/libutil.so /usr/local/lib/php/extensions/no-debug-non-zts-20200804/libutil.so

RUN \
    apk --no-cache add \
        libffi-dev \
        # needs for "stddef.h"
        gcc \
        # requirement for ld-musl-x86_64.so.1
        musl-dev \
    && chmod 0755 /usr/local/lib/php/extensions/no-debug-non-zts-20200804/libutil.*
  • Result of php -i
$ docker run --rm test:local php -i | grep ffi
Configure Command =>  './configure'  '--build=x86_64-linux-musl' '--with-config-file-path=/usr/local/etc/php' '--with-config-file-scan-dir=/usr/local/etc/php/conf.d' '--enable-option-checking=fatal' '--with-mhash' '--enable-ftp' '--enable-mbstring' '--enable-mysqlnd' '--with-password-argon2' '--with-sodium=shared' '--with-pdo-sqlite=/usr' '--with-sqlite3=/usr' '--with-openssl=/usr' '--with-system-ciphers' '--with-ffi' '--enable-gd' '--with-external-gd' '--with-webp' '--with-jpeg' '--with-xpm' '--with-freetype' '--with-curl' '--with-libedit' '--with-zlib' '--with-pear' '--enable-soap' '--enable-pcntl' '--enable-opcache' '--enable-sockets' 'build_alias=x86_64-linux-musl' 'CPPFLAGS='
ffi.enable => preload => preload
ffi.preload => no value => no value
  • PHP Extension dir
$  docker run --rm test:local php -i | grep extension_dir
extension_dir => /usr/local/lib/php/extensions/no-debug-non-zts-20200804 => /usr/local/lib/php/extensions/no-debug-non-zts-20200804
sqlite3.extension_dir => no value => no value
KEINOS added a commit that referenced this issue Oct 29, 2020
- Related issue #21 : not a golang made '.so' file but to prove FFI it self is working.
@KEINOS
Copy link
Owner Author

KEINOS commented Oct 29, 2020

So far, with libc.so.6, FFI seems to work. So the functionality of FFI might be OK.

We better check the samples in the official doc one-by-one rather than using the Golang ones first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant