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

Foreign functions not working on Windows MSYS2 MinGW32 #614

Closed
seb-jones opened this issue Feb 16, 2020 · 9 comments
Closed

Foreign functions not working on Windows MSYS2 MinGW32 #614

seb-jones opened this issue Feb 16, 2020 · 9 comments

Comments

@seb-jones
Copy link

I have a simple test program for using sexp_define_foreign:

#include <stdio.h>
#include <chibi/eval.h>

sexp sexp_hello(sexp ctx, sexp self, sexp_sint_t n)
{
    printf("Hello, World\n");
    return SEXP_VOID;
}

void dostuff(sexp ctx) {
    sexp_gc_var1(res);
    sexp_gc_preserve1(ctx, res);

    res = sexp_eval_string(ctx, "(hello)", -1, NULL);

    if (sexp_exceptionp(res)) {
        sexp_debug(ctx, "Hello", res);
    }

    sexp_gc_release1(ctx);
}

int main(int argc, char *argv[])
{
    sexp ctx;

    ctx = sexp_make_eval_context(NULL, NULL, NULL, 0, 0);

    sexp_load_standard_env(ctx, NULL, SEXP_SEVEN);
    sexp_load_standard_ports(ctx, NULL, stdin, stdout, stderr, 1);

    sexp_define_foreign(ctx, sexp_context_env(ctx), "hello",
            0, sexp_hello);

    dostuff(ctx);

    sexp_destroy_context(ctx);

    return 0;
}

This works correctly on Linux, printing "Hello, World" to stdout. However when running on Windows in MSYS2, the sexp_eval_string appears to do nothing and the sexp_debug causes a Segmentation Fault.

@okuoku
Copy link
Collaborator

okuoku commented Feb 16, 2020

Hmm it works for me.

SnapCrab_NoName_2020-2-16_21-40-41_No-00

Few points, assuming you are on Win64;

  • Makefile on master is broken for default MinGW build (Some manual edits are required). To compile against MSYS, make PLATFORM=msys and make PLATFORM=msys install
  • It is not recommended linking MSYS chibi-scheme with MinGW program and vice versa. They will use different libc(MSYS uses variant of Cygwin, MinGW uses MSVCRT + some MinGW-w64 specific code)

@okuoku
Copy link
Collaborator

okuoku commented Feb 16, 2020

... and same for Win32.

On Win32, they will use different calling convention and it might describe what you see -- I (weakly) guess calling back from MSYS chibi-scheme to MinGW32 app causing crash.

@seb-jones
Copy link
Author

Compiling it against MSYS does seem to make it work. Is there definitely no safe way to build chibi scheme with MinGW? I was hoping to use it as an extension language for an SDL2 program.

@okuoku
Copy link
Collaborator

okuoku commented Feb 16, 2020

I'll try to fix Makefile for MinGW. Meanwhile, CMakeLists.txt is sort-of (written by me) -- it will generate static library by default.

@okuoku
Copy link
Collaborator

okuoku commented Feb 16, 2020

Ugh, that was my own MSYS installation issue.. With MSYS fresh install and fresh checkout chibi-scheme Git, I was able to build MinGW32/64 chibi-scheme with make flawlessly.

To wrap-up:

  1. Do not mix MSYS and MinGW code
  2. Do not install MinGW version of chibi-scheme bin under /usr/local/bin or other paths

Makefile tries to avoid to pick-up wrong chibi-scheme installation by overriding LD_LIBRARY_PATH but it is not effective under MSYS. (Perhaps we should use PATH instead here.)

@seb-jones
Copy link
Author

seb-jones commented Feb 16, 2020

Would you mind explaining the steps you took to get from a fresh install of MSYS to getting a functioning build?

okuoku added a commit to okuoku/issue614 that referenced this issue Feb 16, 2020
@okuoku
Copy link
Collaborator

okuoku commented Feb 16, 2020

... I'm still struggling with creating reliable reproduction (for both success/fail) on MinGW..

But, on MSVC, I can reliably recreate issue seemingly matching with yours. It raises "undefined variable" exception while evaluating (hello); I'm not sure why this happens yet (FWIW test/apply-loop.c https://github.com/ashinn/chibi-scheme/blob/3161edbe86d676329832829e4e6b8635ac09a21c/tests/foreign/apply-loop.c still works on Win32/Win64).

Created: https://github.com/okuoku/issue614

@okuoku
Copy link
Collaborator

okuoku commented Feb 16, 2020

Adding -DSEXP_USE_GREEN_THREADS=0 seems fixing issue, for me.. okuoku/issue614@7ef5740

@ashinn : Does it intentional that SEXP_USE_GREEN_THREADS affects ABI?

@seb-jones
Copy link
Author

Disabling green threads worked! Thanks :)

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

2 participants