Chih-Hung Hsieh | fab4380 | 2020-04-07 14:24:01 -0700 | [diff] [blame] | 1 | # clang-sys |
| 2 | |
| 3 | [](https://crates.io/crates/clang-sys) |
| 4 | [](https://travis-ci.org/KyleMayes/clang-sys) |
| 5 | [](https://ci.appveyor.com/project/KyleMayes/clang-sys-vtvy5/branch/master) |
| 6 | |
| 7 | Rust bindings for `libclang`. |
| 8 | |
| 9 | If you are interested in a Rust wrapper for these bindings, see |
| 10 | [clang-rs](https://github.com/KyleMayes/clang-rs). |
| 11 | |
| 12 | Supported on the stable, beta, and nightly Rust channels.<br/> |
| 13 | Minimum supported Rust version: **1.36.0** |
| 14 | |
| 15 | Released under the Apache License 2.0. |
| 16 | |
| 17 | ## Documentation |
| 18 | |
| 19 | There are two versions of the documentation, one for the API exposed when |
| 20 | linking dynamically or statically and one for the API exposed when linking at |
| 21 | runtime (see the |
| 22 | [Dependencies](https://github.com/KyleMayes/clang-sys#dependencies) section |
| 23 | of the README for more information on the linking options). |
| 24 | |
| 25 | The only difference between the APIs exposed is that when linking at runtime a |
| 26 | few additional types and functions are exposed to manage the loaded `libclang` |
| 27 | shared library. |
| 28 | |
| 29 | * Runtime - [Documentation](https://kylemayes.github.io/clang-sys/runtime/clang_sys) |
| 30 | * Dynamic / Static - [Documentation](https://kylemayes.github.io/clang-sys/default/clang_sys) |
| 31 | |
| 32 | ## Supported Versions |
| 33 | |
| 34 | To target a version of `libclang`, enable one of the following Cargo features: |
| 35 | |
| 36 | * `clang_3_5` - requires `libclang` 3.5 or later |
| 37 | * `clang_3_6` - requires `libclang` 3.6 or later |
| 38 | * `clang_3_7` - requires `libclang` 3.7 or later |
| 39 | * `clang_3_8` - requires `libclang` 3.8 or later |
| 40 | * `clang_3_9` - requires `libclang` 3.9 or later |
| 41 | * `clang_4_0` - requires `libclang` 4.0 or later |
| 42 | * `clang_5_0` - requires `libclang` 5.0 or later |
| 43 | * `clang_6_0` - requires `libclang` 6.0 or later |
| 44 | * `clang_7_0` - requires `libclang` 7.0 or later |
| 45 | * `clang_8_0` - requires `libclang` 8.0 or later |
| 46 | * `clang_9_0` - requires `libclang` 9.0 or later |
| 47 | |
| 48 | If you do not enable one of these features, the API provided by `libclang` 3.5 will be available by |
| 49 | default. |
| 50 | |
| 51 | ## Dependencies |
| 52 | |
| 53 | By default, this crate will attempt to link to `libclang` dynamically. In this case, this crate |
| 54 | depends on the `libclang` shared library (`libclang.so` on Linux, `libclang.dylib` on macOS, |
| 55 | `libclang.dll` on Windows). If you want to link to `libclang` statically instead, enable the |
| 56 | `static` Cargo feature. In this case, this crate depends on the LLVM and Clang static libraries. If |
| 57 | you don't want to link to `libclang` at compiletime but instead want to load it at runtime, enable |
| 58 | the `runtime` Cargo feature. |
| 59 | |
| 60 | These libraries can be either be installed as a part of Clang or downloaded |
| 61 | [here](http://llvm.org/releases/download.html). |
| 62 | |
| 63 | **Note:** The downloads for LLVM and Clang 3.8 and later do not include the `libclang.a` static |
| 64 | library. This means you cannot link to any of these versions of `libclang` statically unless you |
| 65 | build it from source. |
| 66 | |
| 67 | ### Versioned Dependencies |
| 68 | |
| 69 | This crate supports finding versioned instances of `libclang.so` (e.g.,`libclang-3.9.so`). |
| 70 | In the case where there are multiple instances to choose from, this crate will prefer instances with |
| 71 | higher versions. For example, the following instances of `libclang.so` are listed in descending |
| 72 | order of preference: |
| 73 | |
| 74 | 1. `libclang-4.0.so` |
| 75 | 2. `libclang-4.so` |
| 76 | 3. `libclang-3.9.so` |
| 77 | 4. `libclang-3.so` |
| 78 | 5. `libclang.so` |
| 79 | |
| 80 | **Note:** On BSD distributions, versioned instances of `libclang.so` matching the pattern |
| 81 | `libclang.so.*` (e.g., `libclang.so.7.0`) are also included. |
| 82 | |
| 83 | **Note:** On Linux distributions when the `runtime` features is enabled, versioned instances of |
| 84 | `libclang.so` matching the pattern `libclang.so.*` (e.g., `libclang.so.1`) are also included. |
| 85 | |
| 86 | ## Environment Variables |
| 87 | |
| 88 | The following environment variables, if set, are used by this crate to find the required libraries |
| 89 | and executables: |
| 90 | |
| 91 | * `LLVM_CONFIG_PATH` **(compiletime)** - provides a full path to an `llvm-config` executable |
| 92 | (including the executable itself [i.e., `/usr/local/bin/llvm-config-8.0`]) |
| 93 | * `LIBCLANG_PATH` **(compiletime)** - provides a path to a directory containing a `libclang` shared |
| 94 | library or a full path to a specific `libclang` shared library |
| 95 | * `LIBCLANG_STATIC_PATH` **(compiletime)** - provides a path to a directory containing LLVM and |
| 96 | Clang static libraries |
| 97 | * `CLANG_PATH` **(runtime)** - provides a path to a `clang` executable |
| 98 | |
| 99 | ## Linking |
| 100 | |
| 101 | ### Dynamic |
| 102 | |
| 103 | `libclang` shared libraries will be searched for in the following directories: |
| 104 | |
| 105 | * the directory provided by the `LIBCLANG_PATH` environment variable |
| 106 | * the `bin` and `lib` directories in the directory provided by `llvm-config --libdir` |
| 107 | * the directories provided by `LD_LIBRARY_PATH` environment variable |
| 108 | * a list of likely directories for the target platform (e.g., `/usr/local/lib` on Linux) |
| 109 | * **macOS only:** the toolchain directory in the directory provided by `xcode-select --print-path` |
| 110 | |
| 111 | On Linux, running an executable that has been dynamically linked to `libclang` may require you to |
| 112 | add a path to `libclang.so` to the `LD_LIBRARY_PATH` environment variable. The same is true on OS |
| 113 | X, except the `DYLD_LIBRARY_PATH` environment variable is used instead. |
| 114 | |
| 115 | On Windows, running an executable that has been dynamically linked to `libclang` requires that |
| 116 | `libclang.dll` can be found by the executable at runtime. See |
| 117 | [here](https://msdn.microsoft.com/en-us/library/7d83bc18.aspx) for more information. |
| 118 | |
| 119 | ### Static |
| 120 | |
| 121 | The availability of `llvm-config` is not optional for static linking. Ensure that an instance of |
| 122 | this executable can be found on your system's path or set the `LLVM_CONFIG_PATH` environment |
| 123 | variable. The required LLVM and Clang static libraries will be searched for in the same way as |
| 124 | shared libraries are searched for, except the `LIBCLANG_STATIC_PATH` environment variable is used in |
| 125 | place of the `LIBCLANG_PATH` environment variable. |
| 126 | |
| 127 | ### Runtime |
| 128 | |
| 129 | The `clang_sys::load` function is used to load a `libclang` shared library for use in the thread in |
| 130 | which it is called. The `clang_sys::unload` function will unload the `libclang` shared library. |
| 131 | `clang_sys::load` searches for a `libclang` shared library in the same way one is searched for when |
| 132 | linking to `libclang` dynamically at compiletime. |