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

Building on i686-pc-windows-gnu #9

Closed
CGMossa opened this issue Nov 19, 2020 · 51 comments
Closed

Building on i686-pc-windows-gnu #9

CGMossa opened this issue Nov 19, 2020 · 51 comments

Comments

@CGMossa
Copy link
Member

CGMossa commented Nov 19, 2020

It is important that we can build on 32-bit targets, I've tried to follow the instructions in the README.md,
but substituting with the 32-bit variants.

This doesn't work for some reason. And here's the latest error-message/hurdle:

[redacted] MINGW32 /c/Users/tpb398/Documents/GitHub/libR-sys
$ cargo build --target=i686-pc-windows-gnu
   Compiling libR-sys v0.1.9 (C:\Users\tpb398\Documents\GitHub\libR-sys)
error: failed to run custom build command for `libR-sys v0.1.9 (C:\Users\tpb398\Documents\GitHub\libR-sys)`

Caused by:
  process didn't exit successfully: `C:/cargo_target_dir\debug\build\libR-sys-46304186790a078b\build-script-build` (exit code: 101)
  --- stdout
  cargo:rustc-env=R_HOME=C:/R/R-4.0.2
  cargo:rustc-env=R_HOME=C:/R/R-4.0.2/bin/i386
  cargo:r_home=C:/R/R-4.0.2
  cargo:rustc-link-search=C:/R/R-4.0.2/bin/i386
  cargo:rustc-link-lib=dylib=R
  cargo:rerun-if-changed=build.rs
  cargo:rerun-if-changed=wrapper.h

  --- stderr
  thread 'main' panicked at 'Unable to find libclang: "couldn\'t find any valid shared libraries matching: [\'clang.dll\', \'libclang.dll\'], set the `LIBCLANG_PATH` environment variable to a path where one of these files can be found (invalid: [(C:\\Users\\tpb398\\scoop\\apps\\msys2\\current\\mingw32\\bin\\libclang.dll: invalid DLL (32-bit)), (C:\\Users\\tpb398\\scoop\\apps\\msys2\\current\\mingw32\\bin\\libclang.dll: invalid DLL (32-bit))])"', C:\Users\tpb398\.cargo\registry\src\github.jparrowsec.cn-1ecc6299db9ec823\bindgen-0.53.3\src/lib.rs:1956:31
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I've searched for clang.dll in the mingw folders, and there is nothing named that.

It is the libclang.dll that is problem.

Here's a few more diagnostic things that I see mentioned when trying to troubleshoot this:

$ clang -v
clang version 11.0.0 (https://github.com/msys2/MINGW-packages 20f70d38fae51f89ce3c06d054a77139606df472)
Target: i686-w64-windows-gnu
Thread model: posix
InstalledDir: C:\Users\tpb398\scoop\apps\msys2\current\mingw32\bin
@Ilia-Kosenkov
Copy link
Member

Hi @CGMossa,
Did you have any success building against i686?
There is one issue with the building process - the library relies heavily on the R executable (present in PATH) to determine R-specific indlude directories. On 64-bit system PATH usually contains x64 folder of R, meaning libR-sys is linking against 64-bit version of R.dll, and ultimately fails.

Another thing I have no explanation for - my path contains both mingw32 and mingw64, and by default a 64-bit clang is invoked. If I leave only mingw32, I get the exactly same error as you have posted.

However, with mingw64 and i386 R on the path, the library can be built and tests can be run, but two of them fail (looks like some native integer/pointer size mismatch). I have no experience in bindgen and cannot comment on that, but I attach a detailed stacktrace.
I do not understand why the pipeline is able to build a 32-bit app and link agains 32-bit version of R.dll while using 64-bit clang and libraries, this is beyond me.

Below is the result of
$(echo $env:RUST_BACKTRACE ; cargo test --release --target=i686-pc-windows-gnu ; clang -v ; Rscript.exe -e '.Platform$r_arch') *>&1 > dump.txt

dump.txt
full
   Compiling winapi v0.3.9
   Compiling memchr v2.3.4
   Compiling version_check v0.9.2
   Compiling glob v0.3.0
   Compiling cc v1.0.65
   Compiling libc v0.2.80
   Compiling lazy_static v1.4.0
   Compiling proc-macro2 v1.0.24
   Compiling bitflags v1.2.1
   Compiling log v0.4.11
   Compiling cfg-if v0.1.10
   Compiling unicode-xid v0.2.1
   Compiling regex-syntax v0.6.21
   Compiling quick-error v1.2.3
   Compiling unicode-width v0.1.8
   Compiling bindgen v0.53.3
   Compiling vec_map v0.8.2
   Compiling strsim v0.8.0
   Compiling lazycell v1.3.0
   Compiling shlex v0.1.1
   Compiling peeking_take_while v0.1.2
   Compiling rustc-hash v1.1.0
   Compiling nom v5.1.2
   Compiling clang-sys v0.29.3
   Compiling thread_local v1.0.1
   Compiling humantime v1.3.0
   Compiling textwrap v0.11.0
   Compiling libloading v0.5.2
   Compiling aho-corasick v0.7.15
   Compiling which v3.1.1
   Compiling quote v1.0.7
   Compiling regex v1.4.2
   Compiling winapi-util v0.1.5
   Compiling atty v0.2.14
   Compiling termcolor v1.1.2
   Compiling clap v2.33.3
   Compiling cexpr v0.4.0
   Compiling env_logger v0.7.1
   Compiling libR-sys v0.1.10 (C:\Users\[redacted]\OneDrive\Development\Playground\libR-sys)
    Finished release [optimized] target(s) in 1m 35s
     Running target\i686-pc-windows-gnu\release\deps\libR_sys-6c682b6e624d8212.exe

running 19 tests
test bindgen_test_layout_R_CMethodDef ... ok
test bindgen_test_layout_R_CallMethodDef ... ok
test bindgen_test_layout_R_inpstream_st ... ok
test bindgen_test_layout_R_outpstream_st ... ok
test bindgen_test_layout_Rcomplex ... ok
test bindgen_test_layout___mingw_dbl_type_t ... ok
test bindgen_test_layout___mingw_dbl_type_t__bindgen_ty_1 ... ok
test bindgen_test_layout___mingw_flt_type_t ... ok
test bindgen_test_layout___mingw_ldbl_type_t__bindgen_ty_1 ... ok
test bindgen_test_layout__complex ... ok
test bindgen_test_layout__exception ... ok
test bindgen_test_layout__iobuf ... ok
test bindgen_test_layout_localeinfo_struct ... ok
test bindgen_test_layout_tagLC_ID ... ok
test bindgen_test_layout_threadlocaleinfostruct ... ok
test bindgen_test_layout_threadlocaleinfostruct__bindgen_ty_1 ... ok
test bindgen_test_layout___mingw_ldbl_type_t ... FAILED
test bindgen_test_layout_max_align_t ... FAILED
test tests::test_eval ... ok

failures:

---- bindgen_test_layout___mingw_ldbl_type_t stdout ----
thread 'bindgen_test_layout___mingw_ldbl_type_t' panicked at 'assertion failed: `(left == right)`
  left: `8`,
 right: `4`: Alignment of __mingw_ldbl_type_t', C:\Users\[redacted]\OneDrive\Development\Playground\libR-sys\target\i686-pc-windows-gnu\release\build\libR-sys-3b2a1ee2f7fff495\out/bindings.rs:2941:5
stack backtrace:
   0:   0x4d1d1b - core::fmt::write::h3a9c091b8ca4c7a7
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\core\src\fmt/mod.rs:1080
   1:   0x402bb4 - std::io::Write::write_fmt::h55320c7185531d90
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src\io/mod.rs:1516
   2:   0x4603fb - std::io::impls::<impl std::io::Write for alloc::boxed::Box<W>>::write_fmt::h6f6daa32b1c2a916
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\io/impls.rs:179
   3:   0x474e7a - std::sys_common::backtrace::_print::hc2f2ae8a2de1951b
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common/backtrace.rs:61
   4:   0x474e7a - std::sys_common::backtrace::print::h3766cc9baca05bc8
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common/backtrace.rs:48
   5:   0x474e7a - std::panicking::default_hook::{{closure}}::h46a919c0bf907afe
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:208
   6:   0x474b0e - std::panicking::default_hook::hc08848c5073d9694
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:224
   7:   0x47553b - std::panicking::rust_panic_with_hook::h0cea3fe379539d48
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:577
   8:   0x4751b2 - std::panicking::begin_panic_handler::{{closure}}::h4309871eae5bd138
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:484
   9:   0x471bed - std::sys_common::backtrace::__rust_end_short_backtrace::h81ee9a5814303916
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common/backtrace.rs:153
  10:   0x475173 - rust_begin_unwind
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:483
  11:   0x475122 - std::panicking::begin_panic_fmt::h09febdf1830100f4
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:437
  12:   0x40193e - libR_sys::bindgen_test_layout___mingw_ldbl_type_t::h29799f4b51cd0da4
  13:   0x4018a8 - libR_sys::bindgen_test_layout___mingw_ldbl_type_t::{{closure}}::hbfb7aff559cfe3e0
  14:   0x4017d8 - core::ops::function::FnOnce::call_once::h614c99b5966abcde
  15:   0x4290d6 - core::ops::function::FnOnce::call_once::h83e7561c518a8a48
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\core\src\ops/function.rs:227
  16:   0x4290d6 - test::__rust_begin_short_backtrace::h90b11f61009b2e83
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\test\src/lib.rs:516
  17:   0x427948 - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::hf8c86ac279728e24
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\alloc\src/boxed.rs:1042
  18:   0x427948 - <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h2c82c02427f67527
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panic.rs:308
  19:   0x427948 - std::panicking::try::do_call::h6c28e5eff59b27d8
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panicking.rs:381
  20:   0x427948 - std::panicking::try::ha356e80f88bfb021
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panicking.rs:345
  21:   0x427948 - std::panic::catch_unwind::h6382aaa1f9c058d4
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panic.rs:382
  22:   0x427948 - test::run_test_in_process::h4409fa3820453ce1
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\test\src/lib.rs:543
  23:   0x427948 - test::run_test::run_test_inner::{{closure}}::hab3d81435f5f1c26
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\test\src/lib.rs:449
  24:   0x402085 - std::sys_common::backtrace::__rust_begin_short_backtrace::hbf6e7f2154633f10
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src\sys_common/backtrace.rs:137
  25:   0x407f8a - std::thread::Builder::spawn_unchecked::{{closure}}::{{closure}}::h8ce8ddd14af0ac44
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src\thread/mod.rs:464
  26:   0x407f8a - <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::hc91973fe6d3d7749
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panic.rs:308
  27:   0x407f8a - std::panicking::try::do_call::hc9a2a154e6245402
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panicking.rs:381
  28:   0x407f8a - std::panicking::try::hccfa1cfc786c6746
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panicking.rs:345
  29:   0x407f8a - std::panic::catch_unwind::h15f4b4dfb90d6aa7
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panic.rs:382
  30:   0x407f8a - std::thread::Builder::spawn_unchecked::{{closure}}::hd39261c1641a1b8a
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src\thread/mod.rs:463
  31:   0x407f8a - core::ops::function::FnOnce::call_once{{vtable.shim}}::h0d2c8144d1ade409
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\core\src\ops/function.rs:227
  32:   0x481e5b - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h47540c09aa657433
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\alloc\src/boxed.rs:1042
  33:   0x481e5b - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h23f63b1f1783f38a
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\alloc\src/boxed.rs:1042
  34:   0x481e5b - std::sys::windows::thread::Thread::new::thread_start::h86e8623cdc9f64d6
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys\windows/thread.rs:56
  35: 0x7736fa29 - <unknown>
  36: 0x775d75f4 - <unknown>
  37: 0x775d75c4 - <unknown>

---- bindgen_test_layout_max_align_t stdout ----
thread 'bindgen_test_layout_max_align_t' panicked at 'assertion failed: `(left == right)`
  left: `16`,
 right: `24`: Size of: max_align_t', C:\Users\[redacted]\OneDrive\Development\Playground\libR-sys\target\i686-pc-windows-gnu\release\build\libR-sys-3b2a1ee2f7fff495\out/bindings.rs:2651:5
stack backtrace:
   0:   0x4d1d1b - core::fmt::write::h3a9c091b8ca4c7a7
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\core\src\fmt/mod.rs:1080
   1:   0x402bb4 - std::io::Write::write_fmt::h55320c7185531d90
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src\io/mod.rs:1516
   2:   0x4603fb - std::io::impls::<impl std::io::Write for alloc::boxed::Box<W>>::write_fmt::h6f6daa32b1c2a916
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\io/impls.rs:179
   3:   0x474e7a - std::sys_common::backtrace::_print::hc2f2ae8a2de1951b
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common/backtrace.rs:61
   4:   0x474e7a - std::sys_common::backtrace::print::h3766cc9baca05bc8
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common/backtrace.rs:48
   5:   0x474e7a - std::panicking::default_hook::{{closure}}::h46a919c0bf907afe
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:208
   6:   0x474b0e - std::panicking::default_hook::hc08848c5073d9694
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:224
   7:   0x47553b - std::panicking::rust_panic_with_hook::h0cea3fe379539d48
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:577
   8:   0x4751b2 - std::panicking::begin_panic_handler::{{closure}}::h4309871eae5bd138
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:484
   9:   0x471bed - std::sys_common::backtrace::__rust_end_short_backtrace::h81ee9a5814303916
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common/backtrace.rs:153
  10:   0x475173 - rust_begin_unwind
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:483
  11:   0x475122 - std::panicking::begin_panic_fmt::h09febdf1830100f4
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/panicking.rs:437
  12:   0x40188e - libR_sys::bindgen_test_layout_max_align_t::h374144ebd117569d
  13:   0x4017f8 - libR_sys::bindgen_test_layout_max_align_t::{{closure}}::h8be0742b37d54879
  14:   0x4017c8 - core::ops::function::FnOnce::call_once::h0a57d00c7b916612
  15:   0x4290d6 - core::ops::function::FnOnce::call_once::h83e7561c518a8a48
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\core\src\ops/function.rs:227
  16:   0x4290d6 - test::__rust_begin_short_backtrace::h90b11f61009b2e83
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\test\src/lib.rs:516
  17:   0x427948 - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::hf8c86ac279728e24
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\alloc\src/boxed.rs:1042
  18:   0x427948 - <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h2c82c02427f67527
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panic.rs:308
  19:   0x427948 - std::panicking::try::do_call::h6c28e5eff59b27d8
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panicking.rs:381
  20:   0x427948 - std::panicking::try::ha356e80f88bfb021
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panicking.rs:345
  21:   0x427948 - std::panic::catch_unwind::h6382aaa1f9c058d4
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panic.rs:382
  22:   0x427948 - test::run_test_in_process::h4409fa3820453ce1
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\test\src/lib.rs:543
  23:   0x427948 - test::run_test::run_test_inner::{{closure}}::hab3d81435f5f1c26
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\test\src/lib.rs:449
  24:   0x402085 - std::sys_common::backtrace::__rust_begin_short_backtrace::hbf6e7f2154633f10
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src\sys_common/backtrace.rs:137
  25:   0x407f8a - std::thread::Builder::spawn_unchecked::{{closure}}::{{closure}}::h8ce8ddd14af0ac44
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src\thread/mod.rs:464
  26:   0x407f8a - <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::hc91973fe6d3d7749
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panic.rs:308
  27:   0x407f8a - std::panicking::try::do_call::hc9a2a154e6245402
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panicking.rs:381
  28:   0x407f8a - std::panicking::try::hccfa1cfc786c6746
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panicking.rs:345
  29:   0x407f8a - std::panic::catch_unwind::h15f4b4dfb90d6aa7
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src/panic.rs:382
  30:   0x407f8a - std::thread::Builder::spawn_unchecked::{{closure}}::hd39261c1641a1b8a
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\std\src\thread/mod.rs:463
  31:   0x407f8a - core::ops::function::FnOnce::call_once{{vtable.shim}}::h0d2c8144d1ade409
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\core\src\ops/function.rs:227
  32:   0x481e5b - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h47540c09aa657433
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\alloc\src/boxed.rs:1042
  33:   0x481e5b - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h23f63b1f1783f38a
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\alloc\src/boxed.rs:1042
  34:   0x481e5b - std::sys::windows::thread::Thread::new::thread_start::h86e8623cdc9f64d6
                       at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys\windows/thread.rs:56
  35: 0x7736fa29 - <unknown>
  36: 0x775d75f4 - <unknown>
  37: 0x775d75c4 - <unknown>


failures:
    bindgen_test_layout___mingw_ldbl_type_t
    bindgen_test_layout_max_align_t

test result: FAILED. 17 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out

[1] 1
error: test failed, to rerun pass '--lib'
clang version 11.0.0 (https://github.com/msys2/MINGW-packages c0083b9edc111315c868f9c53dd60aaa78d1669b)
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:\tools\msys64\mingw64\bin
[1] "i386"

@clauswilke
Copy link
Member

Using the R executable to find R_HOME is almost certainly the wrong approach and I already have a PR pending to fix this: #12

It's possible that we need the build script to detect whether it's trying to build a 32bit or a 64bit target and modify the paths accordingly. Could you post the typical path layout on Windows when both 32bit and 64bit versions are installed? Do they use two different R_HOME values or are they nested under one single R_HOME? Where are the include files (e.g. Rinternals.h)? Where are the library files (something like libR.dll)?

@Ilia-Kosenkov
Copy link
Member

Ilia-Kosenkov commented Nov 28, 2020

@clauswilke,
So the layout is typically like this:
There is a default Program Files\R folder, to where all versions go (unless the user modified it during installation). In my case, it can look like this:

    Directory: C:\Program Files\R                                                                                                                                                                                                               Mode                 LastWriteTime         Length Name                                                                 
 ----                 -------------         ------ ----                                                                  
d----          2020-04-06    15:10                R-3.6.3                                                               
d----          2020-07-09    18:25                R-4.0.2                                                               
d----          2020-11-05    23:13                R-4.0.3 

Within R-4.0.3

    Directory: C:\Program Files\R\R-4.0.3                                                                                                                                                                                                       Mode                 LastWriteTime         Length Name                                                                  
----                 -------------         ------ ----                                                                  
d----          2020-11-05    23:12                bin                                                                   
d----          2020-11-05    23:12                doc                                                                   
d----          2020-11-05    23:12                etc                                                                   
d----          2020-11-05    23:12                include                                                               
d----          2020-11-05    23:13                library                                                              
d----          2020-11-05    23:13                modules                                                              
d----          2020-11-05    23:13                share                                                                
d----          2020-11-05    23:13                src                                                                   
d----          2020-11-05    23:13                Tcl                                                                   
d----          2020-11-05    23:13                tests                                                                 
-a---          2020-10-10    09:24          27421 CHANGES                                                               
-a---          2018-09-25    09:47          18011 COPYING                                                               
-a---          2020-10-10    09:33         536651 MD5                                                                   
-a---          2018-09-25    09:47           4132 README                                                                
-a---          2020-10-10    09:33           8676 README.R-4.0.3                                                        
-a---          2020-11-05    23:13        1303256 unins000.dat                                                          
-a---          2020-11-05    23:12        2594145 unins000.exe     
there are shared \include

    Directory: C:\Program Files\R\R-4.0.3\include                                                                                                                                                                                               Mode                 LastWriteTime         Length Name                                                                 
 ----                 -------------         ------ ----                                                                  
d----          2020-11-05    23:12                R_ext                                                                 
-a---          2018-09-25    09:46           8172 ga.h                                                                  
-a---          2020-03-27    00:02          36322 graphapp.h                                                            
-a---          2020-10-10    09:14           2027 iconv.h                                                               
-a---          2020-10-10    09:14          14384 libintl.h                                                             
-a---          2018-09-25    09:46           3413 R.h                                                                  
-a---          2020-10-10    09:14            865 Rconfig.h                                                             
-a---          2018-09-25    09:46           6758 Rdefines.h                                                            
-a---          2018-09-25    09:46           2075 Rembedded.h                                                         
-a---          2020-05-27    23:15          62996 Rinternals.h                                                          
-a---          2020-10-10    09:14          17446 Rmath.h                                                               
-a---          2020-10-10    09:14            510 Rversion.h                                                            
-a---          2018-09-25    09:46           2593 S.h         

and platform-specific \bin

    Directory: C:\Program Files\R\R-4.0.3\bin                                                                                                                                                                                                   Mode                 LastWriteTime         Length Name                                                                  
----                 -------------         ------ ----                                                                  
d----          2020-11-05    23:12                i386                                                                  
d----          2020-11-05    23:12                x64                                                                   
-a---          2020-10-10    09:14          11957 config.sh                                                            
-a---          2020-10-10    09:19         514640 R.exe                                                                
-a---          2020-10-10    09:19         514640 Rscript.exe     

Here, R.exe is a 64-bit app and I guess this bin folder is added to the PATH.
Now, both x64 and i386 contain a full set of .exe apps and .dll, which are platform-specific, e.g.

    Directory: C:\Program Files\R\R-4.0.3\bin\x64                                                                                                                                                                                               Mode                 LastWriteTime         Length Name                                                                  
----                 -------------         ------ ----                                                                  
-a---          2020-10-10    09:19         434550 open.exe                                                              
-a---          2020-10-10    09:18       51840736 R.dll                                                                 
-a---          2020-10-10    09:19         625050 R.exe                                                                 
-a---          2020-10-10    09:18         508902 Rblas.dll                                                             
-a---          2020-10-10    09:19         621942 Rcmd.exe                                                              
-a---          2020-10-10    09:19         514640 Rfe.exe                                                               
-a---          2020-10-10    09:18        2064862 Rgraphapp.dll                                                         
-a---          2020-10-10    09:19         543177 Rgui.exe                                                              
-a---          2020-10-10    09:18         437454 Riconv.dll                                                            
-a---          2020-10-10    09:20        2535936 Rlapack.dll                                                           
-a---          2020-10-10    09:19         532869 Rscript.exe                                                           
-a---          2020-10-10    09:19         530053 RSetReg.exe                                                           
-a---          2020-10-10    09:19         562358 Rterm.exe   

So, to build a 32- or 64- bit project, a correct R.dll should be linked. The current approach invokes 64-bit R and gets an ....\bin\x64\R.dll as include, while the correct one for i686 is ....\bin\i386\R.dll.

There is yet another way. Default R installation can write values to Registry. Depending on whether the installer was executed as admin (elevated) or not, values can be written to different places, but they can (probably) be tested during the build process. Examples:
ls HKLM:\SOFTWARE\R-core\ gives

    Hive: HKEY_LOCAL_MACHINE\SOFTWARE\R-core                                                                                                                                                                                                    Name                           Property                                                                                 
----                           --------                                                                                 
R                              InstallPath     : C:\Program Files\R\R-4.0.3                                                                            Current Version : 4.0.3                                                                  
R64                            InstallPath     : C:\Program Files\R\R-4.0.3                                                                            Current Version : 4.0.3                                                                  
Rtools                         InstallPath     : C:\rtools40                                                                                           Current Version : 4.0        

and ls HKLM:\SOFTWARE\R-core\R produces all currently active installation of R (32-bit).

    Hive: HKEY_LOCAL_MACHINE\SOFTWARE\R-core\R

Name                           Property
----                           --------
3.6.3                          InstallPath : C:\Program Files\R\R-3.6.3
4.0.2                          InstallPath : C:\Program Files\R\R-4.0.2
4.0.3                          InstallPath : C:\Program Files\R\R-4.0.3

So, for instance (using powershell for the sake of example), a query of the form
"$(Get-ItemProperty HKLM:\SOFTWARE\R-core\R64 | select -expand "InstallPath")\bin\x64"
results in
C:\Program Files\R\R-4.0.3\bin\x64

and "$(Get-ItemProperty HKLM:\SOFTWARE\R-core\R | select -expand "InstallPath")\bin\i386"
results in
C:\Program Files\R\R-4.0.3\bin\i386 ,
which should give correct 64- and 32-bit paths no matter how R is installed.

@clauswilke
Copy link
Member

So we have the same Rinternals.h but different R.dll. I think when compiling on Windows we need to set the correct library path depending on the target we're compiling for. That should be possible.

@clauswilke
Copy link
Member

I have amended my PR #12 to set different library paths depending on the target architecture. But I can't test it since I don't have windows. Could you guys test it? Note that you'll have to set the R_HOME environment variable for this code branch to be executed.

@clauswilke
Copy link
Member

I have merged #12. Does this close this issue?

Note also the problem described in #16. @CGMossa, do you have this problem?

@clauswilke clauswilke added this to the 0.1.11 milestone Dec 10, 2020
@yutannihilation
Copy link
Contributor

yutannihilation commented Dec 12, 2020

I have merged #12. Does this close this issue?

I still see the same error on my MSYS2. The error @Ilia-Kosenkov described in #9 (comment) is solved, but the original one stays the same.

This error

thread 'main' panicked at 'Unable to find libclang: "couldn\'t find any valid shared libraries matching:
[\'clang.dll\', \'libclang.dll\'], set the `LIBCLANG_PATH` environment variable to a path where one of these files can be found 
(invalid: [(C:\\...\\msys2\\current\\mingw32\\bin\\libclang.dll: invalid DLL (32-bit)), 
(C:\\...\\msys2\\current\\mingw32\\bin\\libclang.dll: invalid DLL (32-bit))])"',

comes from clang-sys crate's validation:

        if cfg!(target_pointer_width = "64") && magic != 523 {
            return Err("invalid DLL (32-bit)".into());
        }

I really don't understand why target_pointer_width is 64 when we specify the target of 32bit...

@CGMossa
Copy link
Member Author

CGMossa commented Dec 12, 2020 via email

@yutannihilation
Copy link
Contributor

Try to print out the target

Here's printed-out bindgen_builder.command_line_flags(). Is this the same on your environment?

["wrapper.h", "--rust-target", "1.40", "--blacklist-item", "FP_NAN", "--blacklist-item", "FP_INFINITE", "--blacklist-item", "FP_ZERO", "--blacklist-item", "FP_SUBNORMAL", "--blacklist-item", "FP_NORMAL", "--no-derive-default", "--generate", "functions,types,vars,methods,constructors,destructors", "--", "-IC:\\Program Files\\R\\R-4.0.2\\include", "--target=i686-pc-windows-gnu"]

@CGMossa
Copy link
Member Author

CGMossa commented Dec 12, 2020 via email

@yutannihilation
Copy link
Contributor

Yeah, I think we are stuck in front of the same problem...

This seems because cargo looks for the default toolchain even when we specify target. If I switch the default toolchain to 32-bit,

rustup default stable-i686-pc-windows-gnu

I can pass the validation, but I still get this error (the message says "The specified procedure could not be found." Sorry I don't know how to show English messages...):

thread 'main' panicked at 'Unable to find libclang: "the libclang shared library at C:\\msys64\\mingw32\\bin\\libclang.dll could not be opened: 指定されたプロシージャが見つかりません。 (os error 127)"', C:\Users\hiroaki-yutani\.cargo\registry\src\github.jparrowsec.cn-1285ae84e5963aae\bindgen-0.53.3\src/lib.rs:1956:31

@clauswilke
Copy link
Member

I think what is happening is that cargo uses the default toolchain to compile build.rs, and then runs build.rs with the specific compile target.

If you switch the default toolchain, you also need a 32-bit clang. Did you install that? The github action does not.

@yutannihilation
Copy link
Contributor

Did you install that?

Yes, I just followed https://github.com/extendr/libR-sys#windows-specific-instructions

Btw,

The github action does not.

it seems GitHub Actions runner already has it installed. I tried to install mingw32/mingw-w64-i686-clang on GitHub Actions CI runner, but it says it's up to date.

   C:\windows\system32\cmd.exe /D /S /C D:\a\_temp\msys\msys2.cmd -c "'pacman' '--noconfirm' '-S' '--needed' '--overwrite' '*' 'mingw32/mingw-w64-i686-clang' 'mingw-w64-i686-toolchain'"
  warning: mingw-w64-i686-clang-11.0.0-4 is up to date -- skipping
  warning: mingw-w64-i686-binutils-2.35.1-3 is up to date -- skipping
  warning: mingw-w64-i686-crt-git-9.0.0.6029.ecb4ff54-1 is up to date -- skipping
  warning: mingw-w64-i686-gcc-10.2.0-5 is up to date -- skipping
  warning: mingw-w64-i686-gcc-ada-10.2.0-5 is up to date -- skipping
  warning: mingw-w64-i686-gcc-fortran-10.2.0-5 is up to date -- skipping
  warning: mingw-w64-i686-gcc-libgfortran-10.2.0-5 is up to date -- skipping
  warning: mingw-w64-i686-gcc-libs-10.2.0-5 is up to date -- skipping
  warning: mingw-w64-i686-gcc-objc-10.2.0-5 is up to date -- skipping
  warning: mingw-w64-i686-gdb-10.1-1 is up to date -- skipping
  warning: mingw-w64-i686-gdb-multiarch-10.1-1 is up to date -- skipping
  warning: mingw-w64-i686-headers-git-9.0.0.6029.ecb4ff54-1 is up to date -- skipping
  warning: mingw-w64-i686-libmangle-git-9.0.0.6029.ecb4ff54-1 is up to date -- skipping
  warning: mingw-w64-i686-libwinpthread-git-9.0.0.6029.ecb4ff54-1 is up to date -- skipping
  warning: mingw-w64-i686-make-4.3-1 is up to date -- skipping
  warning: mingw-w64-i686-pkgconf-1.7.3-5 is up to date -- skipping
  warning: mingw-w64-i686-tools-git-9.0.0.6029.ecb4ff54-1 is up to date -- skipping
  warning: mingw-w64-i686-winpthreads-git-9.0.0.6029.ecb4ff54-1 is up to date -- skipping
  warning: mingw-w64-i686-winstorecompat-git-9.0.0.6029.ecb4ff54-1 is up to date -- skipping

(from my test branch: https://github.com/yutannihilation/libR-sys/runs/1542066028?check_suite_focus=true#step:3:33)

@clauswilke
Copy link
Member

Side note: I find it so confusing to figure out what is and isn't already installed in the GitHub Actions runner. And if you once install something and then remove the install line it still remains in the cache. I wish there was an easy way to just clear the cache.

@Ilia-Kosenkov
Copy link
Member

@yutannihilation ,
I am having a similar issue.
If I use a non-i686 toolchaing, I get BadImageLoad - whatever message, saying that I run a 64-bit building process, but include library libclang.dll is 32 bit (as t should be for i686 target).
If I set default toolchain to nightly-i686-pc-windows-gnu, then I get what you are showing: The specified procedure could not be found (os error 127), which is honestly frustrating as I have no idea in what direction to dig.

@clauswilke
Copy link
Member

I think the next step is to figure out where exactly the 127 error is generated (Windows or mingw) and what it means. It may also be helpful to file an issue with the bindgen repo. I can reproduce this error in the GH actions, see my latest PR (#25).

@lebensterben
Copy link

have you done
rustup set default-host i686-pc-windows-gnu

@Ilia-Kosenkov
Copy link
Member

@clauswilke,
I also tried to look through the repos of clang-sys and bindgen.
There was a somewhat similar issue in clang-sys , but I think the answer is "not that crate's problem".
From backtrace I can say that the error originates from rust-bindgen/lib.rs:1938, which in turn is caused by clang-sys/link.rs:198 (you can probably spot familiar error messages).
The Windows 127 error is likely related to the fact that a process attempts to load a symbol which is not exported from the library. According to docs, the error is:

ERROR_PROC_NOT_FOUND

    127 (0x7F)

    The specified procedure could not be found.

For some reason, the build script does not retrieve error description from the error code (which is a standard practice), so we are left with code 127. If you try googling enum value ERROR_PROC_NOT_FOUND, you will find more details, e.g. this SO question.
If we could somehow identify what is being loaded from libclang.dll, we can probably then find the root problem.

@Ilia-Kosenkov
Copy link
Member

@lebensterben,
Tried this, no luck, same Os 127 error.

@clauswilke
Copy link
Member

Some thoughts:

1. You should be able to inspect the build script that cargo has built. In the GHA runs, it's in D:\a\libR-sys\libR-sys\target\debug\build\libR-sys-0f39f6f1fafbb160\build-script-build. In any case, cargo will tell you where it is:
https://github.com/extendr/libR-sys/pull/25/checks?check_run_id=1543618219#step:6:1274

If you don't get this type of output, run cargo build -vv and it should tell you everything.

2. I think the most likely reason why the linking doesn't work is that there's another clang version that rustc somehow finds as it builds the build script. This could be due to llvm-config. If llvm-config is called at some point and returns some invalid info (e.g., points to the 64-bit include files), you could end up with problems. You could check what llvm-config --includedir and llvm-config --libdir return and whether that output makes sense.

3. Related to the previous point, instead of setting LIBCLANG_PATH, maybe it's better to set LLVM_CONFIG_PATH. Note that LLVM_CONFIG_PATH needs to point to the llvm-config binary, not just the directory where it is located. So something like LLVM_CONFIG_PATH=C:/msys64/mingw32/bin/llvm-config.exe.

@clauswilke
Copy link
Member

I just tried point 3 from my previous comment and it didn't help.

One other thing somebody could try: Set up a minimal bindgen project, without R, just for a trivial C hello-world library, and see if that compiles on Windows. If it does we know there's some interference with R, and if it doesn't we may have a sufficiently minimal example that it would make sense to file an issue with clang-sys.

@yutannihilation
Copy link
Contributor

Set up a minimal bindgen project, without R, just for a trivial C hello-world library

Agreed, I was thinking I need to get familiar with bindgen itself a bit deeper...

@clauswilke
Copy link
Member

@Ilia-Kosenkov There's a lot of good advice with tips for debugging this kind of problem here:
https://stackoverflow.com/questions/1053180/loadlibrary-error-code-127

@Ilia-Kosenkov
Copy link
Member

@clauswilke,
Ok, this SO topic was somewhat helpful. I am no WinDbg ninja, but I tried walking the compilation process using it, and this is what I found

ModLoad: 00000000`78ed0000 00000000`7a912000   C:\tools\msys64\mingw32\bin\libclang.dll
ModLoad: 00000000`72870000 00000000`72878000   C:\Windows\SysWOW64\VERSION.dll
ModLoad: 00000000`6eb40000 00000000`6eb62000   C:\Users\[redacted]\.rustup\toolchains\nightly-i686-pc-windows-gnu\bin\libgcc_s_dw2-1.dll
ModLoad: 00000000`64b40000 00000000`64b53000   C:\Users\[redacted]\.rustup\toolchains\nightly-i686-pc-windows-gnu\bin\libwinpthread-1.dll
ModLoad: 00000000`038a0000 00000000`08b47000   C:\tools\msys64\mingw32\bin\libLLVM.dll
ModLoad: 00000000`50480000 00000000`5065a000   C:\tools\msys64\mingw32\bin\libstdc++-6.dll
ModLoad: 00000000`038a0000 00000000`08b47000   C:\tools\msys64\mingw32\bin\libLLVM.dll
ModLoad: 00000000`752c0000 00000000`753b7000   C:\Windows\SysWOW64\ole32.dll
ModLoad: 00000000`76890000 00000000`76b05000   C:\Windows\SysWOW64\combase.dll
ModLoad: 00000000`755e0000 00000000`75601000   C:\Windows\SysWOW64\GDI32.dll
ModLoad: 00000000`74ec0000 00000000`74ed7000   C:\Windows\SysWOW64\win32u.dll
ModLoad: 00000000`757e0000 00000000`7593c000   C:\Windows\SysWOW64\gdi32full.dll
ModLoad: 00000000`754e0000 00000000`7555c000   C:\Windows\SysWOW64\msvcp_win.dll
ModLoad: 00000000`76b10000 00000000`76ca8000   C:\Windows\SysWOW64\USER32.dll
ModLoad: 00000000`75a80000 00000000`75ffb000   C:\Windows\SysWOW64\SHELL32.dll
ModLoad: 00000000`75760000 00000000`7579b000   C:\Windows\SysWOW64\cfgmgr32.dll
ModLoad: 00000000`74dd0000 00000000`74e54000   C:\Windows\SysWOW64\shcore.dll
ModLoad: 00000000`762c0000 00000000`76881000   C:\Windows\SysWOW64\windows.storage.dll
ModLoad: 00000000`74fc0000 00000000`75003000   C:\Windows\SysWOW64\powrprof.dll
ModLoad: 00000000`75940000 00000000`7594d000   C:\Windows\SysWOW64\UMPDC.dll
ModLoad: 00000000`74cc0000 00000000`74d04000   C:\Windows\SysWOW64\shlwapi.dll
ModLoad: 00000000`76250000 00000000`7625f000   C:\Windows\SysWOW64\kernel.appcore.dll
ModLoad: 00000000`757a0000 00000000`757b3000   C:\Windows\SysWOW64\cryptsp.dll
ModLoad: 00000000`71240000 00000000`71250000   C:\tools\msys64\mingw32\bin\libffi-7.dll
ModLoad: 00000000`63080000 00000000`6309e000   C:\tools\msys64\mingw32\bin\zlib1.dll
(4424.c44): Unknown exception - code c0000139 (first chance)
ModLoad: 00000000`73d80000 00000000`73f0f000   C:\Windows\SysWOW64\dbghelp.dll
wow64cpu!CpupSyscallStub+0xc:
00000000`77071cbc c3              ret

This is what happens between two breakpoints. A bunch of modules are loaded, starting with libclang, ending with zlib1. After that, the exception is thrown c0000139 (Entry point not found). It looks like this may be our problem.

So, what goes wrong is libclang pulls in other libraries, and one of it (presumably zlib1) cannot be loaded. I dont-know why do we get "Entry point not found". There is an (optional) DllMain function that is called when library is loaded, but I guess it should work without it. Unfortunately I was unable to get more precise information like what function is loaded from zlib1 before it crashes.

@clauswilke
Copy link
Member

It's starting to look like msys2 is shipping a broken 32-bit version of clang. Maybe it's so rarely used that nobody has noticed. Do you want to file a bug report? I assume it's either here: https://github.com/msys2/MSYS2-packages or here: https://github.com/msys2/MINGW-packages

@Ilia-Kosenkov
Copy link
Member

@clauswilke,
Ok, I will try to do it tonight.
Let's see if we can get any help.

@clauswilke
Copy link
Member

@Ilia-Kosenkov Thanks for filing the issue. I just noticed on the msys2 website for the clang package that there's a link to bug reports, and it goes to the MINGW-packages repo: https://github.com/msys2/MINGW-packages/issues?q=is%3Aissue+is%3Aopen+clang

Maybe would be good to report there also. And the website also lists a specific user who signed the package and who could be tagged in the bug report: https://github.com/elieux

@Ilia-Kosenkov
Copy link
Member

Hi @clauswilke,
Could you make another issue there (maybe ping that person as well), as you seem to be better at this than I am?

@clauswilke
Copy link
Member

Done that: msys2/MINGW-packages#7442
I think you're doing great, though.

Btw., I liked your installation instructions in the bug report you filed. Would you be interested in creating a PR to update the Windows installation instructions in the README, here: https://github.com/extendr/libR-sys/blob/master/README.md#windows-specific-instructions

I would make two changes to what you posted in the issue:

  • Write it for 64 bit, not 32 bit
  • Suggest people use the stable-gnu toolchain. There's no need for nightly.

@clauswilke
Copy link
Member

If anybody with access to a Windows machine wants to try something else, maybe it's possible to use the binaries provided by llvm: https://releases.llvm.org/download.html

They're not built with mingw, so this may or may not work.

With the code in PR #27, it would be easy to distribute precomputed bindings with libR-sys, so we'd have to get this to run only once (for each R version). So even if the install/build process is super complex and tricky to pull off, that's fine because our endusers wouldn't have to jump through those hoops.

@Ilia-Kosenkov
Copy link
Member

If I am not mistaken, binaries lack llvm-config. One needs to build llvm from source, which I think I can attempt to do.

@CGMossa
Copy link
Member Author

CGMossa commented Dec 15, 2020

I've posted this on discord, but this is the place that this should reside in:

Given some.c that is this:

#include <stdio.h>

#include <clang-c/Index.h>  // This is libclang.

int main() {
  printf("Hello world\n");
  printf("With clang header..\n");
}

And using mingw32, and installing clang for i686-pc-windows-gnu,
this compiles fine with

clang -target i686-pc-windows-gnu some.c -o some.exe

As in:

C:\Users\[redacted]/Documents/GitHub/libR-sys/(alternative_win_build)> mingw32

MINGW32 /c/Users/[redacted]/Documents/GitHub/libR-sys
$ clang -target i686-pc-windows-gnu some.c -o some.exe

MINGW32 /c/Users/[redacted]/Documents/GitHub/libR-sys
$ ./some.exe
Hello world
With clang header..

MINGW32 /c/Users/[redacted]/Documents/GitHub/libR-sys
$

@clauswilke
Copy link
Member

You can use the WinDbg process discussed above to see if libclang is actually loaded dynamically and whether it then loads the same downstream libraries.

@Ilia-Kosenkov
Copy link
Member

Ilia-Kosenkov commented Dec 15, 2020

If anyone has access to Windows machine, please take this code sample that I prepared and try building both versions for Windows.
I need to confirm that it fails for i686 not only on my machine.

UPD: This is the result of GitHub Actions, it seems to confirm the hypothesis.

@yutannihilation
Copy link
Contributor

Thanks for the code sample, I confirmed it also fails on my Windows.

   Compiling clang_link_test v0.1.0 (C:\Users\hiroaki-yutani\Documents\repo\R\clang_link_test)
error: failed to run custom build command for `clang_link_test v0.1.0 (C:\Users\hiroaki-yutani\Documents\repo\R\clang_link_test)`

Caused by:
  process didn't exit successfully: `C:\Users\hiroaki-yutani\Documents\repo\R\clang_link_test\target\debug\build\clang_link_test-fb2b9da043710cb2\build-script-build` (exit code: 101)
  --- stderr
  thread 'main' panicked at 'Cannot load library! [the `libclang` shared library at C:\msys64\mingw32\bin\libclang.dll could not be opened: LoadLibraryExW failed]', build.rs:6:26
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

@clauswilke
Copy link
Member

So we're potentially looking at a problem with Rust itself, or how it builds build scripts. It's interesting that the exact same code runs fine inside the Rust library itself. Could you set up a repo that demonstrates that also, so we can point to the two side-by-side?

Btw., extern crate is not needed anymore. If the crate is listed in Cargo.toml it's available.

@Ilia-Kosenkov
Copy link
Member

Ilia-Kosenkov commented Dec 16, 2020

Long story short, I messed something up last night and arrived to an incorrect conclusion -- it is not rust issue with build script, but something else, as now both build script and simple program reliably fail for i686.
build repo contains sample proj with build script, run contains the same code but executed when app is launched.
run repo also has feature flags, runtime for at runtime dynamic loading, and static for, I guess, static.
On my machine, all i686 cases fail with the same issue. GitHub Actions seem to confirm this.
Any test on local hardware of your own are welcomed.

UPD: Here is GH Actions report to summarise the issue

@clauswilke
Copy link
Member

Honestly, I find that more plausible. Something is wrong with libclang on i686 and so you can't link it, ever. That makes sense.

If you google clang mingw 32-bit you encounter old problems such as this one where people also ran into trouble:
http://clang-developers.42468.n3.nabble.com/MinGW-32-Bit-Support-td4055984.html

There are two obvious next steps to try:

  1. Is it different if you build your example program with --release? Sometimes on Windows you get linking issues if you mix debug and release builds.
  2. Do you get the same problem if you build clang from source? There may also be differences between debug and release builds.

@Ilia-Kosenkov
Copy link
Member

@clauswilke,
--release has no effect - still fails both ways.
However, I put an example code using c++, in particular, msvc.
Strangely, it works fine for both x64 and x86, meaning I dynamically load dll, find two functions, invoke them, free resources.
The only difference so far is that I use simple LoadLibraryW, while rust fails with error at LoadLibraryExW, which has additional flags. I will try playing with LoadLibraryExW to see if it fails under certain conditions.
I can share a repo which can be reviewed and tested -- if needed.

Building from source is another step, I think there still maybe some rust-related issue there.

@Ilia-Kosenkov
Copy link
Member

This is getting weirder and weirder with every step.
In short, cargo run fails on a sample project, however if cargo build and then just execute the *.exe -- everything works fine.

If anyone is testing my rust_clang_run, could you please invoke the output .exe manually?

@clauswilke
Copy link
Member

Interesting! Can you reproduce this in the GH action? Run cargo build, run the *.exe, and then run it again with cargo run?

@Ilia-Kosenkov
Copy link
Member

Did not try running it with cargo run afterwards, just created separate jobs.
Here is the last report https://github.com/Ilia-Kosenkov/rust_clang_run/actions/runs/426337151.
Jobs that are called Run i686 do build and then run executable, other jobs do cargo run only.

@clauswilke clauswilke removed this from the 0.2.0 milestone Dec 16, 2020
@clauswilke
Copy link
Member

I have removed this issue from the milestone for the next libR-sys release. It isn't immediately show-stopping, as I've now a set of manually fixed-up bindings that work for now. We should still try to get the i686 libclang issue resolved, though.

@Ilia-Kosenkov
Copy link
Member

It feels like we have enough to go and ask 'rust' people about this issue. Clearly it is neither 'msys'/'mingw' nor 'clang' (I called the latter from x86 C++ app with no problem).

@clauswilke
Copy link
Member

Agreed. We can file an issue with cargo: https://github.com/rust-lang/cargo

Do you want to do this or do you prefer if I do so?

@Ilia-Kosenkov
Copy link
Member

@clauswilke,
If you can do that, please do.
I will participate and provide clarifications of what I found if needed.

@clauswilke
Copy link
Member

Issue filed with cargo: rust-lang/cargo#8990

@Ilia-Kosenkov
Copy link
Member

Following suggestions from rust/cargo people, I was able to build i686 and test* libR-sys without pre-built bindings.

rustup default stable-i686-pc-windows-msvc
rustup target add i686-pc-windows-gnu
LIBCLANG_PATH=path_to_msys\mingw32\bin
R_HOME=path_to_r

cargo build --release --target=i686-pc-windows-gnu --features use-bindgen

* because of PATH resolution issue (see #29), if PATH is updated to explicitly include only R_HOME\bin\i386, then

cargo test --release --target=i686-pc-windows-gnu --features use-bindgen -- --nocapture --test-threads=1

also works.

@mati865
Copy link

mati865 commented Dec 19, 2020

I think cross-compiling from any Windows toolchain, even x86_64-gnu would work here.

@Ilia-Kosenkov
Copy link
Member

With the answers we got from other teams & the successful deployment of our own GitHub CI, should this issue be closed?
README contains all the necessary information on how to build on windows, including i686.

@clauswilke
Copy link
Member

Yes, agreed. If new problems crop up we can open a new issue.

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

6 participants