diff options
| author | Hai Nguyen <379079+chaoticbob@users.noreply.github.com> | 2024-05-16 10:40:58 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-16 07:40:58 -0700 |
| commit | 0a6180299352d7a2ec850004564c7a95b37a41c4 (patch) | |
| tree | 574e345669ed34ec96bd3009ff8c98d1a14ed427 /CMakeLists.txt | |
| parent | 1b89f78cd1762aa08402bd656e807b66833b11d0 (diff) | |
Fixes running examples from generated SLN (#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>
Diffstat (limited to 'CMakeLists.txt')
| -rw-r--r-- | CMakeLists.txt | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6793a9f6f..049fbf742 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -448,10 +448,12 @@ endif() if(SLANG_ENABLE_PREBUILT_BINARIES) if(CMAKE_SYSTEM_NAME MATCHES "Windows") + file(GLOB prebuilt_binaries "${CMAKE_SOURCE_DIR}/external/slang-binaries/bin/windows-x64/*") add_custom_target( copy-prebuilt-binaries ALL - COMMAND ${CMAKE_COMMAND} -E copy_directory - ${CMAKE_SOURCE_DIR}/external/slang-binaries/bin/windows-x64 + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/$<CONFIG>/${runtime_subdir} + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${prebuilt_binaries} ${CMAKE_BINARY_DIR}/$<CONFIG>/${runtime_subdir} VERBATIM ) @@ -699,6 +701,11 @@ if (SLANG_ENABLE_EXAMPLES AND SLANG_ENABLE_GFX) ) set_target_properties(all-examples PROPERTIES FOLDER examples) function(example dir) + set(debug_dir ${dir}) + if (MSVC) + set(debug_dir ${CMAKE_BINARY_DIR}/${dir}) + endif() + slang_add_target( ${dir} EXECUTABLE @@ -716,9 +723,40 @@ if (SLANG_ENABLE_EXAMPLES AND SLANG_ENABLE_GFX) $<$<BOOL:${SLANG_ENABLE_XLIB}>:SLANG_ENABLE_XLIB> REQUIRED_BY all-examples FOLDER examples - DEBUG_DIR ${dir} + DEBUG_DIR ${debug_dir} ${ARGN} ) + + if (MSVC) + get_filename_component(example_target ${dir} NAME) + file(GLOB asset_files + "${CMAKE_SOURCE_DIR}/${dir}/*.slang" + "${CMAKE_SOURCE_DIR}/${dir}/*.jpg" + "${CMAKE_SOURCE_DIR}/${dir}/*.obj" + "${CMAKE_SOURCE_DIR}/${dir}/*.mtl" + ) + + list(LENGTH asset_files asset_files_length) + if (asset_files_length GREATER 0) + set(copy_assets_target "${example_target}-copy-assets") + + add_custom_target( + ${copy_assets_target} + COMMAND ${CMAKE_COMMAND} -E make_directory ${debug_dir} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${asset_files} ${debug_dir} + COMMENT "Copy example assets to ${debug_dir}" + ) + + set_target_properties(${copy_assets_target} PROPERTIES FOLDER "examples/copy_assets") + + add_dependencies(${example_target} ${copy_assets_target}) + + # Copy DirectX shader binaries so signing doesn't fail when running from Visual Studio + if (SLANG_ENABLE_PREBUILT_BINARIES) + add_dependencies(${example_target} copy-prebuilt-binaries) + endif() + endif() + endif() endfunction() example(examples/autodiff-texture WIN32_EXECUTABLE) |
