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

Build various target libraries from source #7

Merged
merged 11 commits into from
Jan 8, 2021
64 changes: 64 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[target.x86_64-pc-windows-msvc.glslang]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only applies when directly compiling this crate. It doesn't have any effect when using this crate as a dependency unfortunately. .cargo/config.toml is only read for the directory from which cargo was invoked, parent dirs and the home dir, not from arbitrary dependencies.

rustc-link-search = ["build/windows"]
rustc-link-lib = [
"HLSL.glsltospirv",
"OGLCompiler.glsltospirv",
"OSDependent.glsltospirv",
"SPIRV.glsltospirv",
"SPVRemapper.glsltospirv",
"glslang.glsltospirv"
]

[target.x86_64-unknown-linux-gnu.glslang]
rustc-link-search = ["build/linux"]
rustc-link-lib = [
"HLSL.glsltospirv",
"OGLCompiler.glsltospirv",
"OSDependent.glsltospirv",
"SPIRV.glsltospirv",
"SPVRemapper.glsltospirv",
"glslang.glsltospirv",
# linux and macOS-specific libraries
"SPIRV-Tools-opt.glsltospirv",
"SPIRV-Tools.glsltospirv"
]

[target.x86_64-apple-darwin.glslang]
rustc-link-search = ["build/osx"]
rustc-link-lib = [
"HLSL.glsltospirv",
"OGLCompiler.glsltospirv",
"OSDependent.glsltospirv",
"SPIRV.glsltospirv",
"SPVRemapper.glsltospirv",
"glslang.glsltospirv",
# linux and macOS-specific libraries
"SPIRV-Tools-opt.glsltospirv",
"SPIRV-Tools.glsltospirv"
]

[target.aarch64-linux-android.glslang]
rustc-link-search = ["build/android-arm64-v8a"]
rustc-link-lib = [
"HLSL.glsltospirv",
"OGLCompiler.glsltospirv",
"OSDependent.glsltospirv",
"SPIRV.glsltospirv",
"SPVRemapper.glsltospirv",
"glslang.glsltospirv",
# android-specific libraries
"c++_shared"
]

[target.armv7-linux-androideabi.glslang]
rustc-link-search = ["build/android-armeabi-v7a"]
rustc-link-lib = [
"HLSL.glsltospirv",
"OGLCompiler.glsltospirv",
"OSDependent.glsltospirv",
"SPIRV.glsltospirv",
"SPVRemapper.glsltospirv",
"glslang.glsltospirv",
# android-specific libraries
"c++_shared"
]
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
[package]
name = "bevy-glsl-to-spirv"
version = "0.2.0"
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>", "The vulkano contributors", "Carter Anderson <mcanders1@gmail.com>", "Nicholas Rishel <rishel.nick@gmail.com>"]
version = "0.2.2"
authors = ["Pierre Krieger <pierre.krieger1708@gmail.com>", "The vulkano contributors", "Carter Anderson <mcanders1@gmail.com>", "Nicholas Rishel <rishel.nick@gmail.com>", "Martin Krisnanto Putra <grygrflzr@hotmail.com>"]
description = "Deprecated. This crate is a temporary measure until native rust shader compilation like https://github.com/gfx-rs/naga lands."
repository = "https://github.com/cart/glsl-to-spirv"
license = "MIT/Apache-2.0"
build = "build/build.rs"
categories = ["rendering::graphics-api"]
edition = "2018"
links = "glslang"

[build-dependencies]
cmake = "0.1.45"
GrygrFlzr marked this conversation as resolved.
Show resolved Hide resolved
cc = { version = "1.0.66", features = ["parallel"] }
87 changes: 67 additions & 20 deletions build/build.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
extern crate cmake;

use std::env;
use std::fs::copy;
use std::path::Path;
use std::path::PathBuf;

const COMMON_FILES: &[&str] = &[
"glslang",
"HLSL",
"OGLCompiler",
"OSDependent",
"SPIRV",
"SPVRemapper",
];

fn main() {
let target = env::var("TARGET").unwrap();
let mut bin_dir = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap());
bin_dir.push("build");

if target.contains("x86_64-pc-windows-msvc") {
bin_dir.push("windows");
} else if target.contains("x86_64-unknown-linux-gnu") {
bin_dir.push("linux");
} else if target.contains("x86_64-apple-darwin") {
bin_dir.push("osx");
} else if target.contains("android") {
if target.contains("aarch64") {
bin_dir.push("android-arm64-v8a");
} else if target.contains("armv7") {
bin_dir.push("android-armeabi-v7a");
} else {
panic!("Missing Android target support {}", target);
}
} else {
panic!("Missing target support {}", target);
let target: &str = &env::var("TARGET").unwrap();
let bin_dir = match target {
"i686-pc-windows-msvc" => build_libraries(),
_ => panic!("Missing target support {}", target),
};

// Link order matters, make sure dependents are linked before their dependencies.
Expand All @@ -40,3 +37,53 @@ fn main() {
println!("cargo:rustc-link-lib=c++_shared");
}
}

/// Build target libraries and returns the path they're in
pub fn build_libraries() -> PathBuf {
// Prepare directories
let cargo_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let source_dir = cargo_dir.join("glslang");

let out_dir_env = env::var("OUT_DIR").unwrap().clone();
let out_dir = Path::new(&out_dir_env);
let install_dir = out_dir.join("install");
let library_dir = install_dir.join("lib");

// Re-use libraries if they exist
if library_dir.exists() {
return library_dir;
}

// Check glslang folder is initialized
let cmakelists = source_dir.join("CMakeLists.txt");
if !cmakelists.exists() {
panic!("Please make sure the glslang submodule is initialized");
}

// Set up "install" subdirectory
match std::fs::create_dir_all(&install_dir) {
Ok(_) => {}
Err(err) => panic!("Unable to create directory: {:?}", err),
}

// Configure and run build
cmake::Config::new(&source_dir)
.define("CMAKE_INSTALL_PREFIX", &install_dir)
.define("ENABLE_GLSLANG_BINARIES", "OFF")
.profile("Release")
.build_target("install")
.build();

// Rename library files
COMMON_FILES.iter().for_each(|file| {
match copy(
library_dir.join(file).with_extension("lib"),
library_dir.join(file).with_extension("glsltospirv.lib"),
) {
Ok(_) => {}
Err(err) => panic!("Error renaming glslang libaries: {}", err),
}
});

return library_dir;
}
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,19 @@ This crate is deprecated please use [shaderc-rs](https://github.com/google/shade


BEVY NOTE: This crate is a temporary measure until native rust shader compilation like https://github.com/gfx-rs/naga lands.

---

## Additional Dependencies for `i686-pc-windows-msvc`

Assuming an MSVC Windows host (either 32 or 64-bit):
- git
- [cmake](https://cmake.org/download/)
- [VS C++ Build Tools](https://aka.ms/buildtools) (2017 or higher)
- Select **Visual C++ build tools**
- Make sure **Visual C++ tools for CMake** is ticked on the right
- Restart the computer after installing build tools - will fail to build otherwise
- `glslang` submodule is assumed to be initialized
- Run `git submodule update --init` if you're checking out from git

`glslang` will be built from source the first time. Compiled libraries are re-used afterwards.