Skip to content

Commit 675231a

Browse files
knappadorantiagainst
authored andcommitted
Create shaderc-sys for FFI, building, and linking (#46)
This commit creates a new shaderc-sys crate for Shaderc library FFI, building and linking. This change separates the concerns and is the recommended way of handling low-level C libraries. This commit also enhances Shaderc-sys with the ability to detect and use pre-built Shaderc libraries. It has the following building and linking behavior: 1. The option `--features build-from-source` will prevent automatic library detection and force building from source. 2. If the `SHADERC_LIB_DIR` environment variable is set to `/path/to/shaderc/libs/`, it will take precedence and `libshaderc_combined.a` (and the glsang and SPIRV libraries on Linux) will be searched in the `/path/to/shaderc/libs/` directory. 3. On Linux, `/usr/lib/` will be automatically searched for system libraries if none of the above were given. 4. If no other option was set or succeeded, shaderc-sys will fall back to checking out and compiling a copy of shaderc. This procedure is quite slow. This commit also bumps version to 0.5.0. Version 0.4.0 has been skipped and tag will not be used. History for a prospective split at 0.4.0 into two repos has been obliterated.
1 parent 367a4dd commit 675231a

File tree

15 files changed

+691
-396
lines changed

15 files changed

+691
-396
lines changed

.gitmodules

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[submodule "build/spirv-headers"]
2-
path = build/spirv-headers
2+
path = shaderc-sys/build/spirv-headers
33
url = https://github.com/KhronosGroup/SPIRV-Headers.git
44
[submodule "build/spirv-tools"]
5-
path = build/spirv-tools
5+
path = shaderc-sys/build/spirv-tools
66
url = https://github.com/KhronosGroup/SPIRV-Tools.git
77
[submodule "build/glslang"]
8-
path = build/glslang
8+
path = shaderc-sys/build/glslang
99
url = https://github.com/KhronosGroup/glslang.git
1010
[submodule "build/shaderc"]
11-
path = build/shaderc
11+
path = shaderc-sys/build/shaderc
1212
url = https://github.com/google/shaderc.git

Cargo.toml

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
1-
[package]
2-
name = "shaderc"
3-
version = "0.3.16"
4-
authors = ["Lei Zhang <[email protected]>"]
5-
6-
build = "build/build.rs"
7-
8-
description = "Rust bindings for shaderc"
9-
documentation = "https://docs.rs/shaderc"
10-
repository = "https://github.com/google/shaderc-rs"
11-
readme = "README.md"
12-
license = "Apache-2.0"
13-
keywords = ["shader", "compiler", "spirv", "glsl", "hlsl"]
14-
15-
[lib]
16-
path = "src/lib.rs"
17-
18-
[features]
19-
default = ["build-native-shaderc"]
20-
build-native-shaderc = [] # Clone and build the native C++ shaderc project
21-
22-
[dependencies]
23-
libc = "0.2"
24-
25-
[dev-dependencies]
26-
assert_matches = "1.3"
27-
28-
[build-dependencies]
29-
cmake = "0.1"
1+
[workspace]
2+
members = [
3+
"shaderc-rs",
4+
"shaderc-sys"
5+
]

README.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,14 @@ code that happens to be owned by Google.
1616
Usage
1717
-----
1818

19-
This library uses [`build.rs`](build/build.rs) to automatically check out
20-
and compile a copy of native C++ shaderc and link to the generated artifacts,
21-
which requires `git`, `cmake`, and `python` existing in the `PATH`.
22-
23-
To turn off this feature, specify `--no-default-features` when building.
24-
But then you will need to place a copy of the `shaderc_combined` static library
25-
to the location (printed out in the warning message) that is scanned by the
26-
linker.
19+
The included shaderc-sys crate uses [`build.rs`](shaderc-sys/build/build.rs) to
20+
discover or build a copy of shaderc libraries. See Setup section.
2721

2822
First add to your `Cargo.toml`:
2923

3024
```toml
3125
[dependencies]
32-
shaderc = "0.3"
26+
shaderc = "0.5"
3327
```
3428

3529
Then add to your crate root:
@@ -80,7 +74,34 @@ assert!(text_result.as_text().starts_with("; SPIR-V\n"));
8074
Setup
8175
-----
8276

83-
To build the shaderc-rs crate, the following tools must be installed and available on `PATH`:
77+
The order of preference in which the build script will attempt to obtain
78+
shaderc can be controlled by several options, which are passed through to
79+
shaderc-sys when building shaderc-rs:
80+
81+
1. The option `--features build-from-source` will prevent automatic library
82+
detection and force building from source.
83+
2. If the `SHADERC_LIB_DIR` environment variable is set to
84+
`/path/to/shaderc/libs/`, it will take precedence and `libshaderc_combined.a`
85+
(and the glsang and SPIRV libraries on Linux) will be searched in the
86+
`/path/to/shaderc/libs/` directory.
87+
3. On Linux, `/usr/lib/` will be automatically searched for system libraries
88+
if none of the above were given.
89+
4. If no other option was set or succeeded, shaderc-sys will fall back to
90+
checking out and compiling a copy of shaderc. This procedure is quite slow.
91+
92+
NOTE: `--no-default-features` still works on shaderc-rs, but shaderc-sys
93+
implements this behavior in a deprecated manner, and it will be removed in the
94+
next release. This method only works with a monolithic `libshaderc_combined.a`.
95+
Refer to pre-0.5 documentation for more information.
96+
Prefer `SHADERC_LIB_DIR="/path/to/shaderc/libs/"`.
97+
98+
Building from Source
99+
--------------------
100+
101+
The shaderc-sys [`build.rs`](shaderc-sys/build/build.rs) will automatically check out and compile a copy of native C++ shaderc and link to the generated artifacts,
102+
which requires `git`, `cmake`, and `python` existing in the `PATH`.
103+
104+
To build your own libshaderc for the shaderc-sys crate, the following tools must be installed and available on `PATH`:
84105
- [CMake](https://cmake.org/)
85106
- [Git](https://git-scm.com/)
86107
- [Python](https://www.python.org/) (works with both Python 2.x and 3.x, on windows the executable must be named `python.exe`)
@@ -132,6 +153,8 @@ For example on ubuntu:
132153
sudo apt-get install build-essential git python cmake
133154
```
134155

156+
On Arch linux, the [shaderc package](https://www.archlinux.org/packages/extra/x86_64/shaderc/) will include glsang and SPIRV libs in a detectable location.
157+
135158
### macOS Example Setup
136159

137160
Assuming Homebrew:
@@ -152,7 +175,7 @@ This project is initialized and mainly developed by Lei Zhang
152175
([@antiagainst][me]).
153176

154177
[shaderc]: https://github.com/google/shaderc
155-
[doc-compiler]: https://docs.rs/shaderc/0.3/shaderc/struct.Compiler.html
156-
[doc-options]: https://docs.rs/shaderc/0.3/shaderc/struct.CompileOptions.html
157-
[doc-artifact]: https://docs.rs/shaderc/0.3/shaderc/struct.CompilationArtifact.html
178+
[doc-compiler]: https://docs.rs/shaderc/0.5/shaderc/struct.Compiler.html
179+
[doc-options]: https://docs.rs/shaderc/0.5/shaderc/struct.CompileOptions.html
180+
[doc-artifact]: https://docs.rs/shaderc/0.5/shaderc/struct.CompilationArtifact.html
158181
[me]: https://github.com/antiagainst

build/build.rs

Lines changed: 0 additions & 95 deletions
This file was deleted.

shaderc-rs/Cargo.toml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[package]
2+
name = "shaderc"
3+
version = "0.5.0"
4+
authors = ["Lei Zhang <[email protected]>"]
5+
description = "Rust bindings for shaderc"
6+
documentation = "https://docs.rs/shaderc"
7+
repository = "https://github.com/google/shaderc-rs"
8+
readme = "../README.md"
9+
license = "Apache-2.0"
10+
keywords = ["shader", "compiler", "spirv", "glsl", "hlsl"]
11+
12+
[lib]
13+
path = "src/lib.rs"
14+
15+
[dependencies]
16+
libc = "0.2"
17+
shaderc-sys = { version = "0.5.0", path = "../shaderc-sys", features = ["check-inverted-no-defaults"] }
18+
19+
[dev-dependencies]
20+
assert_matches = "1.3"
21+
22+
[features]
23+
build-from-source = ["shaderc-sys/build-from-source"]
24+
25+
# NOTE: The following feature is deprecated. It will be removed in the next
26+
# release.
27+
28+
# The `inverted-no-defaults` behavior is enabled by setting this feature.
29+
check-inverted-no-defaults = ["shaderc-sys/check-inverted-no-defaults"]
30+
# This feature is logically inverted, so you "set" it by clearing it, by passing
31+
# the --no-defaults option to cargo when building this crate.
32+
default = ["shaderc-sys/inverted-no-defaults"]

0 commit comments

Comments
 (0)