From 32e66fd29c37c76313d937a2b082ec198005dc39 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sat, 28 Dec 2024 23:24:57 -0500 Subject: [PATCH] feat: Initial GlobalSession --- .clang-format | 1 + .gitignore | 3 + .gitmodules | 3 + CMakeLists.txt | 39 +++++++ Cargo.lock | 302 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 12 ++ build.rs | 25 ++++ slang | 1 + src/lib.cpp | 18 +++ src/lib.h | 25 ++++ src/lib.rs | 19 ++++ 11 files changed, 448 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 build.rs create mode 160000 slang create mode 100644 src/lib.cpp create mode 100644 src/lib.h create mode 100644 src/lib.rs diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..468ceea --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +BasedOnStyle: LLVM \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..573461a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea +/target +/cmake-build-* \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..2d62ece --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "slang"] + path = slang + url = https://github.com/shader-slang/slang.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..89fc06b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,39 @@ +# NOTE: This file is _not_ used as part of the main build process. +# It exists to help IDE tools provide auto-hinting while developing the C++ bridge +cmake_minimum_required(VERSION 3.22) +project(slang_compiler) + +find_program(CXXBRIDGE cxxbridge) +set(SLANG_COMPILER_CXXBRIDGE_DIR ${CMAKE_CURRENT_BINARY_DIR}/cxxbridge) + +# Generate the Rust bridge stubs header +# This is used by the C++ header to define an interface for Rust +file(MAKE_DIRECTORY ${SLANG_COMPILER_CXXBRIDGE_DIR}/rust) +set(SLANG_COMPILER_CXXBRIDGE_RUST ${SLANG_COMPILER_CXXBRIDGE_DIR}/rust/cxx.h) +add_custom_command( + OUTPUT ${SLANG_COMPILER_CXXBRIDGE_RUST} + COMMAND ${CXXBRIDGE} --header -o ${SLANG_COMPILER_CXXBRIDGE_RUST} +) + +# Generate the Rust bridge header +# This is used by the C++ implementation to interact with Rust +file(MAKE_DIRECTORY ${SLANG_COMPILER_CXXBRIDGE_DIR}/slang-compiler-sys/src) +set(SLANG_COMPILER_CXXBRIDGE_INPUT ${CMAKE_CURRENT_LIST_DIR}/src/lib.rs) +set(SLANG_COMPILER_CXXBRIDGE_OUTPUT ${SLANG_COMPILER_CXXBRIDGE_DIR}/slang-compiler-sys/src/lib.rs.h) +add_custom_command( + OUTPUT ${SLANG_COMPILER_CXXBRIDGE_OUTPUT} + COMMAND ${CXXBRIDGE} ${SLANG_COMPILER_CXXBRIDGE_INPUT} --header -o ${SLANG_COMPILER_CXXBRIDGE_OUTPUT} + DEPENDS ${SLANG_COMPILER_CXXBRIDGE_INPUT} +) + +add_custom_target(slang_compiler_bridge DEPENDS ${SLANG_COMPILER_CXXBRIDGE_RUST} ${SLANG_COMPILER_CXXBRIDGE_OUTPUT}) + +add_library(slang_compiler "src/lib.cpp") + +# Find the bridge headers +add_dependencies(slang_compiler slang_compiler_bridge) +target_include_directories(slang_compiler PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src ${SLANG_COMPILER_CXXBRIDGE_DIR}) + +# Find the slang headers +add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/slang" EXCLUDE_FROM_ALL) +target_link_libraries(slang_compiler PRIVATE slang) \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..e5077c6 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,302 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "cc" +version = "1.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d6dbb628b8f8555f86d0323c2eb39e3ec81901f4b83e091db8a6a76d316a333" +dependencies = [ + "shlex", +] + +[[package]] +name = "clap" +version = "4.5.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.5.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" +dependencies = [ + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "cmake" +version = "0.1.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c682c223677e0e5b6b7f63a64b9351844c3f1b1678a68b7ee617e30fb082620e" +dependencies = [ + "cc", +] + +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + +[[package]] +name = "cxx" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad7c7515609502d316ab9a24f67dc045132d93bfd3f00713389e90d9898bf30d" +dependencies = [ + "cc", + "cxxbridge-cmd", + "cxxbridge-flags", + "cxxbridge-macro", + "foldhash", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bfd16fca6fd420aebbd80d643c201ee4692114a0de208b790b9cd02ceae65fb" +dependencies = [ + "cc", + "codespan-reporting", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-cmd" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c33fd49f5d956a1b7ee5f7a9768d58580c6752838d92e39d0d56439efdedc35" +dependencies = [ + "clap", + "codespan-reporting", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be0f1077278fac36299cce8446effd19fe93a95eedb10d39265f3bf67b3036c9" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3da7e4d6e74af6b79031d264b2f13c3ea70af1978083741c41ffce9308f1f24f" +dependencies = [ + "proc-macro2", + "quote", + "rustversion", + "syn", +] + +[[package]] +name = "foldhash" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" + +[[package]] +name = "link-cplusplus" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d240c6f7e1ba3a28b0249f774e6a9dd0175054b52dfbb61b16eb8505c3785c9" +dependencies = [ + "cc", +] + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustversion" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" + +[[package]] +name = "scratch" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3cf7c11c38cb994f3d40e8a8cde3bbd1f72a435e4c49e85d6553d8312306152" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "slang-compiler-sys" +version = "0.1.0" +dependencies = [ + "cmake", + "cxx", + "cxx-build", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c786062daee0d6db1132800e623df74274a0a87322d8e183338e01b3d98d058" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..12de9d2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "slang-compiler-sys" +version = "0.1.0" +edition = "2021" +resolver = "2" + +[dependencies] +cxx = "1.0" + +[build-dependencies] +cmake = "0.1" +cxx-build = "1.0" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..461c00d --- /dev/null +++ b/build.rs @@ -0,0 +1,25 @@ +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=src/lib.rs"); + println!("cargo:rerun-if-changed=src/lib.cpp"); + + // Build libslang.so for use with the Rust/C++ bridge + let cmake_output = cmake::Config::new("./slang") + // https://github.com/shader-slang/slang/issues/5832#issuecomment-2533324982 + .define("CMAKE_SKIP_INSTALL_RULES", "ON") + .build_target("slang") + .build(); + + // Build the C++ bridge + cxx_build::bridge("src/lib.rs") + .include(format!("{}/build/Debug/include", cmake_output.display())) + .include(format!("{}/build/Release/include", cmake_output.display())) + .include("src") + .file("src/lib.cpp") + .compile("slang-compiler"); + + // Link the C++ bridge + println!("cargo:rustc-link-search=native={}/build/Debug/lib", cmake_output.display()); + println!("cargo:rustc-link-search=native={}/build/Release/lib", cmake_output.display()); + println!("cargo:rustc-link-lib=dylib=slang"); +} \ No newline at end of file diff --git a/slang b/slang new file mode 160000 index 0000000..772c48a --- /dev/null +++ b/slang @@ -0,0 +1 @@ +Subproject commit 772c48af91951bda9ff7064fc7a089550c759f76 diff --git a/src/lib.cpp b/src/lib.cpp new file mode 100644 index 0000000..fb2afb5 --- /dev/null +++ b/src/lib.cpp @@ -0,0 +1,18 @@ +// +// Created by bspeice on 12/28/24. +// +#include "lib.h" + +#include "slang-compiler-sys/src/lib.rs.h" + +namespace slang_compiler { + +GlobalSession::GlobalSession() { + slang::createGlobalSession(global_session_.writeRef()); +} + +std::unique_ptr create_global_session() { + return std::make_unique(); +} + +} // namespace slang_compiler diff --git a/src/lib.h b/src/lib.h new file mode 100644 index 0000000..c5e7c42 --- /dev/null +++ b/src/lib.h @@ -0,0 +1,25 @@ +// +// Created by bspeice on 12/28/24. +// +#pragma once + +#include +#include + +#include + +#include "rust/cxx.h" + +namespace slang_compiler { + +class GlobalSession { +public: + explicit GlobalSession(); + +private: + Slang::ComPtr global_session_; +}; + +std::unique_ptr create_global_session(); + +} // namespace slang_compiler diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..491f734 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,19 @@ +#[cxx::bridge(namespace = "slang_compiler")] +mod ffi { + unsafe extern "C++" { + include!("lib.h"); + + type GlobalSession; + fn create_global_session() -> UniquePtr; + } +} + +#[cfg(test)] +mod tests { + use crate::ffi::create_global_session; + + #[test] + fn can_create_global_session() { + let _global_session = create_global_session(); + } +} \ No newline at end of file