How to link shared cxx_library to cxx_binary with mingw compiler in Buck?

352 Views Asked by At

I'm trying to cross compile Windows shared library and binary linked to this shared library with Buck. I set toolchain paths in .buckconfig:

[mingw]
  prefix = /usr/local/bin/i686-w64-mingw32

[cxx]
  cc = $(config mingw.prefix)-gcc
  cpp = $(config mingw.prefix)-cpp
  ld = $(config mingw.prefix)-ld
  ar = $(config mingw.prefix)-ar

Here is BUCK file:

cxx_library(
    name = 'bar',
    srcs = ['bar.c'],
    headers = ['bar.h'],
    exported_headers = ['bar.h']
)

cxx_binary(
    name = 'foo',
    srcs = ['foo.c'],
    link_style = 'SHARED'
)

foo.c:

#include <stdio.h>
#include "bar.h"

int main(int argc, char *argv[]) {
    bar_init();
    return 0;
}

bar.c:

#include <stdio.h>
#include "bar.h"

void bar_init() {
    printf("bar_init successful\n");
}

bar.h:

void bar_init();

When I do buck build //:foo, I get /usr/local/bin/i686-w64-mingw32-ld: unrecognised emulation mode: ap error (due to -map option passed to linker, Mac Os linker has it, but GNU linker hasn't, it has -Map).

When I add

archiver_platform = WINDOWS
linker_platform = WINDOWS

to .buckconfig, I get java.lang.UnsupportedOperationException at com.facebook.buck.cxx.WindowsLinker.origin(WindowsLinker.java:88). Either it's not supported, or WINDOWS means msvc tooling.

Is it possible to build Windows binaries and .dlls with Buck? How to do it? (I don't want to run Buck on Windows, cross-compilation with mingw or mingw-w64 is ok).

Update: I was able to make it work by:

  1. Setting linker_platform = GNU in .buckconfig (there are no such option mentioned in docs, only in issue)
  2. Setting ld to …-gcc instead of …-ld in .buckconfig. When invoking ld directly, it doesn't support some parameters which are available when invoking via gcc frontend.

However, it is not right solution: it creates PE file without extension dynamically linked to file with .dylib extension. This executable is runnable with Wine and it loads dynamic lib, but these settings are still not correct.

0

There are 0 best solutions below