From 1db7ecb9c0235b9317b5c318685bbbfa8a2309d1 Mon Sep 17 00:00:00 2001 From: yum Date: Sat, 11 Oct 2025 10:10:30 -0700 Subject: bugfix c# script, respect include guards --- CMakeLists.txt | 3 -- build.ps1 | 13 ++++++ main.cc | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 146 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 25032e6..f289121 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,4 @@ add_custom_command(TARGET modular_slang POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${SLANG_SDK_ROOT}/bin/gfx.dll" "$/gfx.dll" - COMMAND ${CMAKE_COMMAND} -E copy_if_different - "${CMAKE_CURRENT_SOURCE_DIR}/test.slang" - "$/test.slang" ) diff --git a/build.ps1 b/build.ps1 index 95d2683..efa46c9 100644 --- a/build.ps1 +++ b/build.ps1 @@ -8,3 +8,16 @@ if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } cmake --build build --config Release if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } +# Package up into ./dist dir +if (Test-Path -Path ./dist) { + rm -Recurse ./dist +} +mkdir ./dist +cp -Recurse build/bin/Release/* dist +cp -Recurse ./Scripts ./dist/ + +if (Test-Path -Path ../Assets/yum_food/3ner/Modular_Slang) { + rm -Recurse ../Assets/yum_food/3ner/Modular_Slang +} +cp -Recurse ./dist ../Assets/yum_food/3ner/Modular_Slang/ + diff --git a/main.cc b/main.cc index 2521e07..3163fee 100644 --- a/main.cc +++ b/main.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -77,6 +78,15 @@ struct FunctionInfo std::vector parameters; }; +struct IncludeGuardInfo +{ + bool present = false; + std::string macro; + std::string ifndefLine; + std::string defineLine; + std::string endifLine; +}; + std::string trim(std::string_view text) { size_t start = 0; @@ -543,6 +553,91 @@ std::string rewriteHLSLWithWrappers( return result; } +IncludeGuardInfo detectIncludeGuard(const std::filesystem::path& sourcePath) +{ + IncludeGuardInfo info; + + std::ifstream input(sourcePath); + if (!input) + { + return info; + } + + std::vector lines; + std::string line; + while (std::getline(input, line)) + { + lines.push_back(line); + } + + size_t ifndefIndex = std::numeric_limits::max(); + for (size_t i = 0; i < lines.size(); ++i) + { + std::string trimmed = trim(lines[i]); + if (trimmed.rfind("#ifndef", 0) == 0) + { + std::istringstream stream(trimmed); + std::string directive; + std::string macro; + stream >> directive >> macro; + if (!macro.empty()) + { + info.macro = macro; + info.ifndefLine = lines[i]; + ifndefIndex = i; + } + break; + } + } + + if (info.macro.empty()) + { + return info; + } + + for (size_t i = ifndefIndex + 1; i < lines.size(); ++i) + { + std::string trimmed = trim(lines[i]); + if (trimmed.rfind("#define", 0) == 0) + { + std::istringstream stream(trimmed); + std::string directive; + std::string macro; + stream >> directive >> macro; + if (macro == info.macro) + { + info.defineLine = lines[i]; + break; + } + } + } + + if (info.defineLine.empty()) + { + info = IncludeGuardInfo{}; + return info; + } + + for (size_t i = lines.size(); i-- > 0;) + { + std::string trimmed = trim(lines[i]); + if (trimmed.rfind("#endif", 0) == 0) + { + info.endifLine = lines[i]; + break; + } + } + + if (info.endifLine.empty()) + { + info = IncludeGuardInfo{}; + return info; + } + + info.present = true; + return info; +} + int main(int argc, char** argv) { if (argc < 2) @@ -572,6 +667,8 @@ int main(int argc, char** argv) std::filesystem::path outputPath = modulePath; outputPath.replace_extension(".hlsl"); + IncludeGuardInfo includeGuard = detectIncludeGuard(modulePath); + // 1. Session Creation Slang::ComPtr globalSession; checkResult("slang::createGlobalSession", slang::createGlobalSession(globalSession.writeRef())); @@ -711,7 +808,42 @@ int main(int argc, char** argv) static_cast(targetCodeBlob->getBufferPointer()), static_cast(targetCodeBlob->getBufferSize())); - const std::string finalHlsl = rewriteHLSLWithWrappers(hlslSource, functions); + std::string finalHlsl = rewriteHLSLWithWrappers(hlslSource, functions); + + if (includeGuard.present) + { + const std::string guardIfndefToken = "#ifndef " + includeGuard.macro; + const std::string guardDefineToken = "#define " + includeGuard.macro; + const bool alreadyGuarded = + finalHlsl.find(guardIfndefToken) != std::string::npos && + finalHlsl.find(guardDefineToken) != std::string::npos; + + if (!alreadyGuarded) + { + std::string body = finalHlsl; + if (!body.empty() && body.back() != '\n') + { + body += '\n'; + } + + std::ostringstream wrapped; + wrapped << includeGuard.ifndefLine << '\n'; + wrapped << includeGuard.defineLine << '\n'; + wrapped << '\n'; + wrapped << body; + if (!body.empty() && body.back() != '\n') + { + wrapped << '\n'; + } + wrapped << includeGuard.endifLine; + if (!includeGuard.endifLine.empty() && includeGuard.endifLine.back() != '\n') + { + wrapped << '\n'; + } + + finalHlsl = wrapped.str(); + } + } // 6. Write HLSL output to a sibling .hlsl file std::ofstream outputFile(outputPath, std::ios::binary); -- cgit v1.2.3