Skip to content

Commit

Permalink
Control building from feature flag
Browse files Browse the repository at this point in the history
Properly skips optional dependencies pending rust-lang/cargo#7914
More extensible for addition of new platforms
  • Loading branch information
GrygrFlzr committed Jan 5, 2021
1 parent 9ee8a3c commit dad973a
Show file tree
Hide file tree
Showing 45 changed files with 425 additions and 435 deletions.
64 changes: 0 additions & 64 deletions .cargo/config.toml

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
**/*.rs.bk
Cargo.lock
.cargo/config
.cargo/config
glslang_actual_builder/glslang/
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "glslang"]
path = glslang
path = glslang_actual_builder/glslang
url = https://github.com/KhronosGroup/glslang.git
22 changes: 17 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ authors = ["Pierre Krieger <[email protected]>", "The vulkano contrib
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"
cc = { version = "1.0.66", features = ["parallel"] }
[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"] }
89 changes: 0 additions & 89 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 }
110 changes: 110 additions & 0 deletions glslang_actual_builder/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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/windows"),
"x86_64-unknown-linux-gnu" => cargo_dir.join("build/linux"),
"x86_64-apple-darwin" => cargo_dir.join("build/osx"),
"aarch64-linux-android" => cargo_dir.join("build/android-arm64-v8a"),
"armv7-linux-androideabi" => cargo_dir.join("build/android-armeabi-v7a"),
"i686-pc-windows-msvc" => build::build_libraries(),
_ => 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");
}
}

#[cfg(feature = "build-from-source")]
mod build {
extern crate cmake;

use std::path::Path;
use std::path::PathBuf;

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

/// Build target libraries if required,
/// and returns the location of library files
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 = std::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 std::fs::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;
}
}

#[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() -> PathBuf {
panic!("This platform must build glslang from source.");
}
}
Loading

0 comments on commit dad973a

Please sign in to comment.