undeflife / libreoffice-rs

Rust binding to LibreOfficeKit
Apache License 2.0
15 stars 7 forks source link

Alpine Compilation - Errors #14

Closed inzanez closed 2 years ago

inzanez commented 2 years ago

Hi

I am just trying to get things compiled on Alpine, but for some reason it doesn't want to build:

thread 'main' panicked at 'Unable to find libclang: "the `libclang` shared library at /usr/lib/libclang.so.10 could not be opened: Dynamic loading not supported"', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.59.2/src/lib.rs:2144:31

Not sure if that's an issue of how libreoffice-rs is built or maybe clang, ... or Alpine itself. Any idea how to fix that?

yveszoundi commented 2 years ago

I'm usually both building and running libreoffice-rs applications inside an Alpine Linux container image (musl).

yveszoundi commented 2 years ago

Based on the applicable README instructions in the GitHub project page (Ubuntu apt-get install notes), you're "likely" missing the equivalent clang-dev apk package. Ubuntu libclang-dev == Alpine clang-dev

inzanez commented 2 years ago

I did actually install all of these. I now created a fresh Alpine container with Rust installed in it, cloned libreoffice-rs and installed pretty much all your dependencies (except the OCR stuff). I just cloned your repo and tried to build it:

/libreoffice-rs # apk add clang clang-dev clang-libs
OK: 1883 MiB in 278 packages
/libreoffice-rs # cargo build
   Compiling libreoffice-rs v0.3.2 (/libreoffice-rs)
error: failed to run custom build command for `libreoffice-rs v0.3.2 (/libreoffice-rs)`

Caused by:
  process didn't exit successfully: `/libreoffice-rs/target/debug/build/libreoffice-rs-3c006341663d6d25/build-script-build` (exit status: 101)
  --- stdout
  cargo:rustc-link-search=native=/libreoffice-rs/target/debug/build/libreoffice-rs-f55ec8fbedc916ad/out

  --- stderr
  ar: `u' modifier ignored since `D' is the default (see `U')
  thread 'main' panicked at 'Unable to find libclang: "the `libclang` shared library at /usr/lib/libclang.so.13.0.1 could not be opened: Dynamic loading not supported"', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.59.2/src/lib.rs:2144:31
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Not really sure why that would happen.

yveszoundi commented 2 years ago

If following the notes below still results into problems, you'll need to provide more details about your setup (using Raspberry Pi or special CPU architecture, custom RUSTFLAGS or ~/.cargo/config.toml, custom Alpine image, etc.). I've been building libreoffice-rs almost daily for amd64 and more recently arm64 (cross-compilation) without issues.

A) Detailed notes

  1. Clone the repository: git clone https://github.com/undeflife/libreoffice-rs/
  2. Switch to the project folder cd libreoffice-rs
  3. Create a Dockerfile at the root of the project (with contents to follow)
  4. Build the Alpine container image (podman, docker, etc.) podman build -t libreoffice-build .

B) Dockerfile mentioned at step 3 in previous section

FROM docker.io/alpine:3.16

RUN apk -U upgrade && \
    apk add \
    libreofficekit \
    clang gcc clang-dev gcc cargo rust

WORKDIR /home/rust/entrusted-container
COPY . .
RUN LO_INCLUDE_PATH=/usr/include/LibreOfficeKit cargo build

C) Screenshot of build results

libreoffice-rs-build

inzanez commented 2 years ago

I am running on Docker alpine:3.16 on a normal AMD64 machine. It seems to be the Rust environment that I am using. I have not installed the default package rust or cargo, but installed it in my build-image like this:

...
 url="https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-musl/rustup-init"; \
    wget "$url"; \
    chmod +x rustup-init; \
    ./rustup-init -y --no-modify-path --default-toolchain nightly-2022-08-08 ; \
    rm rustup-init;
...

Not sure yet what implications that has, but I could imagine that the toolchain was built to do static-linking initially.

yveszoundi commented 2 years ago

I don't think that you can build with the musl toolchain... You need to use x86_64-unknown-linux-gnu

inzanez commented 2 years ago

@yveszoundi yes,...although I must admit I am not seeing through this right now. I was always under the impression that the only toolchain running on Alpine would be the musl toolchain, as it is a musl based system. Installing x86_64-unknown-linux-gnu and doing an override doesn't do the trick as cargo cannot be executed anymore. I guess I will have to dig into the cargo package in Alpine,...

inzanez commented 2 years ago

Ok, so it seems that Alpine created their own target by applying patches to the installed Rust toolchain. Like that, they are creating a toolchain called x86_64-linux-alpine-musl; this toolchain does not seem to link fully static anymore but therefore allows to build things like libreoffice-rs in a dynamically linked way.

undeflife commented 2 years ago

https://github.com/undeflife/libreoffice-rs/blob/master/build.rs#L9 Looks like this causes your compile error, it need to be musl-gcc for alpine

inzanez commented 2 years ago

@undeflife I am not sure actually; I am building on Alpine, so it should be gcc I think. At least there is no musl-gcc binary here. But I guess there is a reason that Alpine made the effort creating their own package for the Rust toolchain. Using that one works quite well,...

undeflife commented 2 years ago

issues on Alpine is all about glibc mostly , using dynamically link is a quick workaround. musl-gcc i mentioned is another option which need install manually, see wiki

yveszoundi commented 2 years ago

For reference, I managed to compile libreoffice-rs with a pristine x86_64-linux-alpine-musl stable toolchain:

1. Changes to make it work on Alpine Musl

Cargo manifest updates

 [build-dependencies]
-bindgen = "0.59"
-
+bindgen = {version = "0.59.0", default_features = false, features = ["static"]}

Dockerfile for building libreoffice-rs

The Dockerfile file below was used to build ONLY libreoffice-rs, not a full-blown project. clang-static is required on Alpine Linux, among other dependencies.

FROM docker.io/alpine:3.16

RUN apk -U upgrade && \
    apk add curl build-base ca-certificates clang-static \
    libreofficekit libstdc++ libffi-dev zlib-dev llvm-static ncurses-dev llvm-dev \
    clang gcc clang-dev gcc cargo musl-dev musl

WORKDIR /home/rust/entrusted-container
COPY . .

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
  . $HOME/.cargo/env
ENV PATH=$PATH:/root/.cargo/bin

RUN LO_INCLUDE_PATH=/usr/include/LibreOfficeKit cargo build

2. Potential issues of making it work just for libreoffice-rs

libreoffice-rs would likely need a way to optionally enable the static feature for bindgen (not sure if that's possible for the build-dependencies section in cargo: not all Linux distributions have an equivalent clang-static package.

My understanding (I could be wrong), is that cargo dependencies features are additive across dependencies (transitive dependencies included). That could be a problem with other dependencies leveraging bindgen for a given application.