RFR: 8309880: Add support for linking libffi on Windows and Mac

Erik Joelsson erikj at openjdk.org
Tue Jun 13 16:09:52 UTC 2023


On Tue, 13 Jun 2023 12:48:12 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

> Update the make/autoconf/lib-ffi.m4 script to support using libffi on Windows and Mac.
> 
> For Windows I had to tweak `fallbackLinker.c` to be able to build: there was an import of `stdint.h` missing, and since it was using `WSAGetLastError` it needed to link against `ws2_32.lib`.
> 
> This PR also contains a fix originally made by @shipilev as part  of: https://github.com/openjdk/jdk/pull/13827 (with a minor tweak), in order to be able to build the fallback linker on mac correctly. I also disabled 3 failing tests in the jdk_foreign suite on that platform.
> 
> ---
> 
> I've updated the `createLibffiBundle.sh` script for Mac, since I got it to work with a devkit, but I didn't manage to do the same for Windows. The steps I took to make my Windows libffi bundle were as follows:
> 1. run 'x64 Native Tools Command Prompt for VS 2022'. cl.exe and link.exe should be on path
> 2. run `ucrt64` (this is one of the shell environments that comes with MSYS2). This should carry over the environment set up by the VS dev prompt.
> 3. then, in the libffi repo root folder:
>   3.a run `autogen.sh`
>   3.b run:
> 
> bash configure \
>   CC="/path/to/libffi/msvcc.sh -m64" \
>   CXX="/path/to/libffi/msvcc.sh -m64" \
>   CPPFLAGS="-DFFI_BUILDING_DLL" \
>   --disable-docs \
>   --prefix=<install dest>
> 
> (`<install dest>` can be whatever you like. That's what you point `--with-libffi` to).
> 
> 4. run `make install`. This should create the `<intstall dest>` directory with the files: `include/ffi.h`, `include/ffitarget.h`, `lib/libffi.dll`. It also creates a `lib/libffi.lib` file, but it is of the wrong file type, `DLL` rather than `LIBRARY`.
> 5. Manually create a working `.lib` file:
>   5.a use `dumpbin /exports libffi.dll` to get a list of exported symbols
>   5.b put them in a `libffi.def` file: `EXPORTS` on the first line, then a symbol on each line following
>   5.c run `lib /def:libffi.def /machine:x64 /out:libffi.lib` to create the right `.lib` file (`lib` is a visual studio tool)
> 
> ---
> 
> Testing:
> - manual testing on Windows/x64 and Mac/AArch64, by running the `jdk_foreign` test suite with `-Djdk.internal.foreign.CABI=FALLBACK` (i.e. using the fallback linker).
> - Linux/x64 Zero test run of the `jdk_foreign` suite
> - Linux/AArch64 Zero build

Could you add the instructions for building libffi for Windows in a comment in createLibffiBundle.sh? I think it would be good to keep those around now that you have figured out how to do it.

make/modules/java.base/Lib.gmk line 231:

> 229:                  $(call SET_SHARED_LIBRARY_ORIGIN), \
> 230:       LIBS := $(LIBFFI_LIBS), \
> 231: 	  LIBS_windows := $(LIBFFI_LIBS) ws2_32.lib, \

Indentation looks off. Make sure you aren't mixing in tabs here.

-------------

PR Review: https://git.openjdk.org/jdk/pull/14446#pullrequestreview-1477569193
PR Review Comment: https://git.openjdk.org/jdk/pull/14446#discussion_r1228379034


More information about the core-libs-dev mailing list