Skip to content

Commit 0a61802

Browse files
chaoticbobjkwak-workcsyonghe
authored
Fixes running examples from generated SLN (shader-slang#4173)
* Fixes running examples from generated SLN This CL contains changes to CMakeLists.txt that enables the examples to run from within Visual Studio when using CMake generated solution. Previously the working directory was set to examples/<example name> and which resulted in an invalid path in the generated project files. Additionally, the assets (shaders, images, models) were not in location that was accessible to the executable when ran from within Visual Studio. - Changed examples to use ${CMAKE_BINARY_DIR}/${dir} instead of ${dir} if generator is MSVC. - Add custom target to assets (shaders, images, models, etc) to example subdir under ${CMAKE_BINARY_DIR} - Add dependency to copy prebuilt binaries if building examples in MSVC so DirectX shader signing doesn't fail - Changed copy-prebuilt-binaries to use copy_if_different to avoid redundant copies The initial build time is increased by 20 seconds (16%) from 2m3s to 2m23s, due to the asset copy. The incremental build time remained same at 4 seconds. * Corrected tabs to spaces Corrected unintentional use of tabs instead of spaces. --------- Co-authored-by: Jay Kwak <82421531+jkwak-work@users.noreply.github.com> Co-authored-by: Yong He <yonghe@outlook.com>
1 parent 1b89f78 commit 0a61802

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

CMakeLists.txt

+41-3
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,12 @@ endif()
448448

449449
if(SLANG_ENABLE_PREBUILT_BINARIES)
450450
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
451+
file(GLOB prebuilt_binaries "${CMAKE_SOURCE_DIR}/external/slang-binaries/bin/windows-x64/*")
451452
add_custom_target(
452453
copy-prebuilt-binaries ALL
453-
COMMAND ${CMAKE_COMMAND} -E copy_directory
454-
${CMAKE_SOURCE_DIR}/external/slang-binaries/bin/windows-x64
454+
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/$<CONFIG>/${runtime_subdir}
455+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
456+
${prebuilt_binaries}
455457
${CMAKE_BINARY_DIR}/$<CONFIG>/${runtime_subdir}
456458
VERBATIM
457459
)
@@ -699,6 +701,11 @@ if (SLANG_ENABLE_EXAMPLES AND SLANG_ENABLE_GFX)
699701
)
700702
set_target_properties(all-examples PROPERTIES FOLDER examples)
701703
function(example dir)
704+
set(debug_dir ${dir})
705+
if (MSVC)
706+
set(debug_dir ${CMAKE_BINARY_DIR}/${dir})
707+
endif()
708+
702709
slang_add_target(
703710
${dir}
704711
EXECUTABLE
@@ -716,9 +723,40 @@ if (SLANG_ENABLE_EXAMPLES AND SLANG_ENABLE_GFX)
716723
$<$<BOOL:${SLANG_ENABLE_XLIB}>:SLANG_ENABLE_XLIB>
717724
REQUIRED_BY all-examples
718725
FOLDER examples
719-
DEBUG_DIR ${dir}
726+
DEBUG_DIR ${debug_dir}
720727
${ARGN}
721728
)
729+
730+
if (MSVC)
731+
get_filename_component(example_target ${dir} NAME)
732+
file(GLOB asset_files
733+
"${CMAKE_SOURCE_DIR}/${dir}/*.slang"
734+
"${CMAKE_SOURCE_DIR}/${dir}/*.jpg"
735+
"${CMAKE_SOURCE_DIR}/${dir}/*.obj"
736+
"${CMAKE_SOURCE_DIR}/${dir}/*.mtl"
737+
)
738+
739+
list(LENGTH asset_files asset_files_length)
740+
if (asset_files_length GREATER 0)
741+
set(copy_assets_target "${example_target}-copy-assets")
742+
743+
add_custom_target(
744+
${copy_assets_target}
745+
COMMAND ${CMAKE_COMMAND} -E make_directory ${debug_dir}
746+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${asset_files} ${debug_dir}
747+
COMMENT "Copy example assets to ${debug_dir}"
748+
)
749+
750+
set_target_properties(${copy_assets_target} PROPERTIES FOLDER "examples/copy_assets")
751+
752+
add_dependencies(${example_target} ${copy_assets_target})
753+
754+
# Copy DirectX shader binaries so signing doesn't fail when running from Visual Studio
755+
if (SLANG_ENABLE_PREBUILT_BINARIES)
756+
add_dependencies(${example_target} copy-prebuilt-binaries)
757+
endif()
758+
endif()
759+
endif()
722760
endfunction()
723761

724762
example(examples/autodiff-texture WIN32_EXECUTABLE)

0 commit comments

Comments
 (0)