| 1 | [](https://github.com/KWARC/rust-libxml/actions/workflows/CI.yml) |
| 2 | [](https://github.com/KWARC/rust-libxml/actions/workflows/windows.yml) |
| 3 | [](http://KWARC.github.io/rust-libxml/libxml/index.html) |
| 4 | [](https://raw.githubusercontent.com/KWARC/rust-libxml/master/LICENSE) |
| 5 | [](https://crates.io/crates/libxml) |
| 6 | |
| 7 | Rust wrapper for [libxml2](http://xmlsoft.org/). |
| 8 | |
| 9 | The main goal of this project is to benefit from libxml2's maturity and stability while the native Rust XML crates mature to be near-drop-in replacements. |
| 10 | |
| 11 | As of the `0.2.0` release of the crate, there are some modest safety guarantees: |
| 12 | |
| 13 | * Mutability, as well as ownership - we use `Rc<RefCell<T>>` wrappers to ensure runtime safety of libxml2 operations already in the Rust layer. |
| 14 | * Memory safety guarantees - in particular `Node` and `Document` objects have automatic bookkeeping and deallocation on drop, for leak-free wrapper use. |
| 15 | * No thread safety - libxml2's global memory management is a challenge to adapt in a thread-safe way with minimal intervention |
| 16 | |
| 17 | **Coverage**: Only covers a subset of libxml2 at the moment, contributions are welcome. We try to increase support with each release. |
| 18 | |
| 19 | **Welcome!** With these caveats, the contributors to the project are migrating production work towards Rust and find a continuing reliance on libxml2 a helpful relief for initial ports. As such, contributions to this crate are welcome, if your workflow is not yet fully supported. |
| 20 | |
| 21 | ## Installation prerequisites |
| 22 | |
| 23 | Before performing the usual cargo build/install steps, you need to have the relevant components for using the original libxml2 code. These may become gradually outdated with time - please do let us know by opening a new issue/PR whenever that's the case. |
| 24 | |
| 25 | ### Linux/Debian |
| 26 | |
| 27 | On linux systems you'd need the development headers of libxml2 (e.g. `libxml2-dev` in Debian), as well as `pkg-config`. |
| 28 | |
| 29 | Starting with `0.3.4`, build requires `bindgen` (see [`bindgen` requirements](https://rust-lang.github.io/rust-bindgen/requirements.html#clang)). |
| 30 | So you have to install CLang 9.0 or greater: |
| 31 | |
| 32 | - Alpine: `$ apk add clang-dev` |
| 33 | - Arch Linux: `$ pacman -S clang` |
| 34 | - Debian / Ubuntu: `$ apt install libclang-dev` |
| 35 | - Fedora: `$ dnf install clang-devel` |
| 36 | |
| 37 | ### Static linking (musl / Alpine / fully static binaries) |
| 38 | |
| 39 | Two independent knobs build a binary with no runtime libxml2 / libclang |
| 40 | dependency (see [#110](https://github.com/KWARC/rust-libxml/issues/110)): |
| 41 | |
| 42 | * **`static` cargo feature** — selects how `bindgen` finds libclang at build |
| 43 | time. The default `runtime` feature `dlopen`s libclang, which is unavailable |
| 44 | in fully static / musl environments; `static` links it via `clang-sys/static` |
| 45 | instead: |
| 46 | |
| 47 | ``` |
| 48 | cargo build --no-default-features --features static |
| 49 | ``` |
| 50 | |
| 51 | * **`LIBXML2_STATIC` env var** — statically links libxml2 *itself*. Point |
| 52 | `PKG_CONFIG_PATH` at a prefix holding a PIC `libxml2.a` and set |
| 53 | `LIBXML2_STATIC=1`; `build.rs` then emits `cargo:rustc-link-lib=static=xml2`, |
| 54 | so the binary carries no `libxml2.so` SONAME dependency. |
| 55 | |
| 56 | ### MacOS |
| 57 | [Community contributed](https://github.com/KWARC/rust-libxml/issues/88#issuecomment-890876895): |
| 58 | |
| 59 | ``` |
| 60 | $ brew install libxml2 # e.g. version 2.9.12 |
| 61 | $ ln -s /usr/local/Cellar/libxml2/2.9.12/lib/libxml2.2.dylib /usr/local/lib/libxml-2.0.dylib |
| 62 | $ export LIBXML2=/usr/local/Cellar/libxml2/2.9.12/lib/pkgconfig/libxml-2.0.pc |
| 63 | ``` |
| 64 | |
| 65 | ### FreeBSD |
| 66 | [Community contributed](https://github.com/KWARC/rust-libxml/issues/130#issuecomment-1976348349) |
| 67 | |
| 68 | ``` |
| 69 | $ pkg install libxml2 pkgconf |
| 70 | ``` |
| 71 | |
| 72 | ### Windows |
| 73 | |
| 74 | #### msvc |
| 75 | |
| 76 | [Community contributed](https://github.com/KWARC/rust-libxml/issues/81#issuecomment-760364976): |
| 77 | |
| 78 | * manually install builds tools c++ and english language by visiting [BuildTools](https://visualstudio.microsoft.com/fr/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16) |
| 79 | * launch cmd prompt with admin privileges and execute these commands sequentially: |
| 80 | ``` |
| 81 | C:\> git clone https://github.com/microsoft/vcpkg |
| 82 | C:\> .\vcpkg\bootstrap-vcpkg.bat |
| 83 | C:\> setx /M PATH "%PATH%;c:\vcpkg" && setx VCPKGRS_DYNAMIC "1" /M |
| 84 | C:\> refreshenv |
| 85 | C:\> vcpkg install libxml2:x64-windows |
| 86 | C:\> vcpkg integrate install |
| 87 | ``` |
| 88 | |
| 89 | #### gnu |
| 90 | |
| 91 | On mingw64 environment you could install libxml2 with `pacman -S mingw64/mingw-w64-x86_64-libxml2`. |