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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
.cargo/config
.cargo/config
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "glslang"]
path = glslang
[submodule "glslang_actual_builder/glslang"]
path = glslang_actual_builder/glslang
url = https://github.com/KhronosGroup/glslang.git
34 changes: 31 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
[package]
name = "bevy-glsl-to-spirv"
version = "0.2.0"
authors = ["Pierre Krieger <[email protected]>", "The vulkano contributors", "Carter Anderson <[email protected]>", "Nicholas Rishel <[email protected]>"]
version = "0.2.2"
authors = ["Pierre Krieger <[email protected]>", "The vulkano contributors", "Carter Anderson <[email protected]>", "Nicholas Rishel <[email protected]>", "Martin Krisnanto Putra <[email protected]>"]
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"

[dependencies]
# Default panic message handling
glslang_actual_builder = { path = "glslang_actual_builder", default-features = false }

# Supported Platforms
[target.x86_64-pc-windows-msvc.dependencies]
glslang_actual_builder = { path = "glslang_actual_builder", default-features = false }

[target.x86_64-unknown-linux-gnu.dependencies]
glslang_actual_builder = { path = "glslang_actual_builder", default-features = false }

[target.x86_64-apple-darwin.dependencies]
glslang_actual_builder = { path = "glslang_actual_builder", default-features = false }

[target.aarch64-linux-android.dependencies]
glslang_actual_builder = { path = "glslang_actual_builder", default-features = false }

[target.armv7-linux-androideabi.dependencies]
glslang_actual_builder = { path = "glslang_actual_builder", default-features = false }

[target.i686-pc-windows-msvc.dependencies]
glslang_actual_builder = { path = "glslang_actual_builder", features = ["build-from-source"] }

[target.x86_64-pc-windows-gnu.dependencies]
glslang_actual_builder = { path = "glslang_actual_builder", features = ["build-from-source"] }

[target.i686-pc-windows-gnu.dependencies]
glslang_actual_builder = { path = "glslang_actual_builder", features = ["build-from-source"] }
42 changes: 0 additions & 42 deletions build/build.rs

This file was deleted.

1 change: 0 additions & 1 deletion glslang
Submodule glslang deleted from bcf6a2
18 changes: 18 additions & 0 deletions glslang_actual_builder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "glslang_actual_builder"
version = "0.1.0"
authors = ["Pierre Krieger <[email protected]>", "The vulkano contributors", "Carter Anderson <[email protected]>", "Nicholas Rishel <[email protected]>", "Martin Krisnanto Putra <[email protected]>"]
description = "Internal builder"
repository = "https://github.com/cart/glsl-to-spirv"
license = "MIT/Apache-2.0"
build = "build.rs"
categories = ["rendering::graphics-api"]
edition = "2018"

[features]
default = []
build-from-source = ["cmake", "cc"]

[build-dependencies]
cmake = { version = "0.1.45", optional = true }
cc = { version = "1.0.66", features = ["parallel"], optional = true }
116 changes: 116 additions & 0 deletions glslang_actual_builder/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use std::path::Path;

fn main() {
let target: &str = &std::env::var("TARGET").unwrap();
let cargo_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let bin_dir = match target {
"x86_64-pc-windows-msvc" => cargo_dir.join("build").join("windows"),
"x86_64-unknown-linux-gnu" => cargo_dir.join("build").join("linux"),
"x86_64-apple-darwin" => cargo_dir.join("build").join("osx"),
"aarch64-linux-android" => cargo_dir.join("build").join("android-arm64-v8a"),
"armv7-linux-androideabi" => cargo_dir.join("build").join("android-armeabi-v7a"),
"i686-pc-windows-msvc" => build::build_libraries(&target),
"x86_64-pc-windows-gnu" => build::build_libraries(&target),
"i686-pc-windows-gnu" => build::build_libraries(&target),
_ => panic!("Unsupported target {}", target),
};

// Link order matters, make sure dependants are linked before their dependencies.
println!("cargo:rustc-link-search={}", bin_dir.to_str().unwrap());
println!("cargo:rustc-link-lib=glslang.glsltospirv");
println!("cargo:rustc-link-lib=HLSL.glsltospirv");
println!("cargo:rustc-link-lib=OGLCompiler.glsltospirv");
println!("cargo:rustc-link-lib=OSDependent.glsltospirv");
println!("cargo:rustc-link-lib=SPIRV.glsltospirv");
println!("cargo:rustc-link-lib=SPVRemapper.glsltospirv");
if target.contains("x86_64-unknown-linux-gnu") || target.contains("x86_64-apple-darwin") {
println!("cargo:rustc-link-lib=SPIRV-Tools-opt.glsltospirv");
println!("cargo:rustc-link-lib=SPIRV-Tools.glsltospirv");
}
if target.contains("android") {
println!("cargo:rustc-link-lib=c++_shared");
}
if target.ends_with("-pc-windows-gnu") {
println!("cargo:rustc-link-lib=dylib=stdc++");
}
}

#[cfg(feature = "build-from-source")]
mod build {
use std::path::Path;
use std::path::PathBuf;

/// Build target libraries if required,
/// and returns the location of library files
pub fn build_libraries(_target: &str) -> PathBuf {
// Prepare directories
let cargo_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let source_dir = cargo_dir.join("glslang");

let out_dir_env = std::env::var("OUT_DIR").unwrap();
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 let Ok(mut entry) = library_dir.read_dir() {
if entry.next().is_some() {
// a file exists in the path
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),
}

// Common configuration
let mut config = cmake::Config::new(&source_dir);
config
.define("CMAKE_INSTALL_PREFIX", &install_dir)
.define("CMAKE_TRY_COMPILE_TARGET_TYPE", "STATIC_LIBRARY")
.define("ENABLE_GLSLANG_BINARIES", "OFF")
.profile("Release")
.build_target("install");

// Build config
config.build();

// Add vendor suffix to all library names
for path in library_dir
.read_dir()
.expect("Unable to locate compiled glslang libraries")
{
let filename = path.unwrap().path();
let metadata = std::fs::metadata(&filename).unwrap();
if metadata.is_file() {
let extension = filename.extension().unwrap().to_str().unwrap();
let new_extension = format!("glsltospirv.{}", extension);
let new_name = filename.with_extension(new_extension);
std::fs::copy(&filename, &new_name)
.expect("Failed to rename a glslang library for linking");
}
}

library_dir
}
}

#[cfg(not(feature = "build-from-source"))]
mod build {
use std::path::PathBuf;

/// Build target libraries if required,
/// and returns the location of library files
pub fn build_libraries(target: &str) -> PathBuf {
panic!("Platform {} must build glslang from source.", &target);
}
}
Loading