From 61aa670237e2d51f7144322bf56b8ce3633c2db9 Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Thu, 24 Oct 2024 11:37:50 +0800 Subject: [PATCH] declutter top level CMakeLists.txt (#5391) * Split examples cmake desc * declutter top level CMakeLists.txt * fail if building tests without gfx * Move llvm fetching to another cmake file * Further split CMakeLists.txt * Neaten llvm fetching * Remove last premake remnant * correct cross builds * Neaten * Neaten project organization in vs --- CMakeLists.txt | 624 +--------------------------- cmake/LLVM.cmake | 75 ++++ examples/CMakeLists.txt | 91 ++++ prelude/CMakeLists.txt | 10 +- source/compiler-core/CMakeLists.txt | 16 + source/core/CMakeLists.txt | 10 + source/slang-glslang/CMakeLists.txt | 16 + source/slang-rt/CMakeLists.txt | 15 + source/slang-stdlib/CMakeLists.txt | 5 +- source/slang-wasm/CMakeLists.txt | 17 + source/slangc/CMakeLists.txt | 10 + tools/CMakeLists.txt | 322 ++++++++++++++ 12 files changed, 597 insertions(+), 614 deletions(-) create mode 100644 examples/CMakeLists.txt create mode 100644 source/compiler-core/CMakeLists.txt create mode 100644 source/core/CMakeLists.txt create mode 100644 source/slang-glslang/CMakeLists.txt create mode 100644 source/slang-rt/CMakeLists.txt create mode 100644 source/slang-wasm/CMakeLists.txt create mode 100644 source/slangc/CMakeLists.txt create mode 100644 tools/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 35279de088..94d0147e6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -219,21 +219,9 @@ if(SLANG_ENABLE_NVAPI AND NOT CMAKE_SYSTEM_NAME MATCHES "Windows") message(SEND_ERROR "SLANG_ENABLE_NVAPI is only supported on Windows") endif() -# -# Clean files from premake -# -# The premake builds places generated files in the source tree, make sure these -# aren't present -glob_append( - premake_generated_files - "source/slang/*.meta.slang.h" - "source/slang/*-generated-*.h" - "source/slang/slang-lookup-*.cpp" -) -if(premake_generated_files) - file(REMOVE ${premake_generated_files}) +if(SLANG_ENABLE_TESTS AND NOT SLANG_ENABLE_GFX) + message(SEND_ERROR "SLANG_ENABLE_TESTS requires SLANG_ENABLE_GFX") endif() -file(REMOVE external/miniz/miniz_export.h) # # Dependencies, most of these are however handled inside the "auto_option" @@ -242,620 +230,42 @@ file(REMOVE external/miniz/miniz_export.h) find_package(Threads REQUIRED) -if(SLANG_SLANG_LLVM_FLAVOR STREQUAL "USE_SYSTEM_LLVM") - find_package(LLVM 13.0 REQUIRED CONFIG) - find_package(Clang REQUIRED CONFIG) -endif() - if (${SLANG_USE_SYSTEM_UNORDERED_DENSE}) find_package(unordered_dense CONFIG QUIET) endif() add_subdirectory(external) -set(SLANG_CORE_LIBRARIES "") -list(APPEND SLANG_CORE_LIBRARIES miniz lz4_static Threads::Threads ${CMAKE_DL_LIBS}) - -# -# Our targets -# - -slang_add_target( - source/core - STATIC - EXCLUDE_FROM_ALL - USE_EXTRA_WARNINGS - LINK_WITH_PRIVATE ${SLANG_CORE_LIBRARIES} - LINK_WITH_PUBLIC unordered_dense::unordered_dense - INCLUDE_DIRECTORIES_PUBLIC source include -) - -if(SLANG_ENABLE_SLANGRT) - slang_add_target( - source/slang-rt - SHARED - # This compiles 'core' again with the SLANG_RT_DYNAMIC_EXPORT macro defined - EXTRA_SOURCE_DIRS source/core - USE_EXTRA_WARNINGS - LINK_WITH_PRIVATE ${SLANG_CORE_LIBRARIES} - LINK_WITH_PUBLIC unordered_dense::unordered_dense - EXPORT_MACRO_PREFIX SLANG_RT - INCLUDE_DIRECTORIES_PUBLIC include - INSTALL - ) -endif() - -slang_add_target( - source/compiler-core - STATIC - EXCLUDE_FROM_ALL - USE_EXTRA_WARNINGS - LINK_WITH_PRIVATE core - INCLUDE_FROM_PUBLIC SPIRV-Headers -) -if(NOT MSVC) - # This is necessary to compile the DXC headers - set_source_files_properties( - source/compiler-core/slang-dxc-compiler.cpp - PROPERTIES COMPILE_OPTIONS "-fms-extensions" - DIRECTORY ${slang_SOURCE_DIR} - ) -endif() - -# -# Tools used to generate source during the build: -# - -add_custom_target( - all-generators - COMMENT "meta target which depends on all generators" -) - -if(CMAKE_HOST_WIN32) - set(CMAKE_HOST_EXECUTABLE_SUFFIX ".exe") -else() - set(CMAKE_HOST_EXECUTABLE_SUFFIX "") -endif() - -set_target_properties(all-generators PROPERTIES FOLDER generators) -function(generator dir) - if(SLANG_GENERATORS_PATH) - cmake_parse_arguments(ARG "" "TARGET_NAME" "" ${ARGN}) - if(ARG_TARGET_NAME) - set(target ${ARG_TARGET_NAME}) - else() - get_filename_component(target ${dir} NAME) - endif() - add_executable(${target} IMPORTED) - set_property( - TARGET ${target} - PROPERTY - IMPORTED_LOCATION - "${SLANG_GENERATORS_PATH}/${target}${CMAKE_HOST_EXECUTABLE_SUFFIX}" - ) - else() - slang_add_target( - ${dir} - EXECUTABLE - EXCLUDE_FROM_ALL - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE core - OUTPUT_DIR generators - REQUIRED_BY all-generators - FOLDER generators - INSTALL_COMPONENT generators - ${ARGN} - ) - endif() -endfunction() - -generator(tools/slang-cpp-extractor USE_FEWER_WARNINGS LINK_WITH_PRIVATE compiler-core) -generator(tools/slang-embed) -generator(tools/slang-generate USE_FEWER_WARNINGS) -generator(tools/slang-lookup-generator LINK_WITH_PRIVATE compiler-core) -generator(tools/slang-capability-generator LINK_WITH_PRIVATE compiler-core) -generator(tools/slang-spirv-embed-generator LINK_WITH_PRIVATE compiler-core) - -generator( - source/slangc - TARGET_NAME slang-bootstrap - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE - prelude - slang-without-embedded-stdlib - slang-capability-lookup - slang-lookup-tables - Threads::Threads -) - -# -# The compiler itself -# - -# keep these non-trivial targets in their own directories so as not to clutter -# this file -add_subdirectory(prelude) -add_subdirectory(source/slang-stdlib) -add_subdirectory(source/slang) - -if(SLANG_ENABLE_SLANGD) - slang_add_target( - tools/slangd - EXECUTABLE - LINK_WITH_PRIVATE - core - compiler-core - slang - slang-reflect-headers - slang-capability-defs - Threads::Threads - INSTALL - ) -endif() -if(SLANG_ENABLE_SLANGC) - slang_add_target( - source/slangc - EXECUTABLE - USE_FEWER_WARNINGS - DEBUG_DIR ${slang_SOURCE_DIR} - LINK_WITH_PRIVATE core slang Threads::Threads - INSTALL - ) -endif() - -# -# WebAssembly bindings for Slang -# -# This is an executable target because emcmake produces .a files without bindings if you just create a static library -# https://stackoverflow.com/questions/63622009/static-library-built-with-cmake-as-a-with-emscripten-instead-of-wasm-js -if (EMSCRIPTEN) - slang_add_target( - source/slang-wasm - EXECUTABLE - EXCLUDE_FROM_ALL - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE miniz lz4_static slang core compiler-core - INCLUDE_DIRECTORIES_PUBLIC include source/slang-wasm - ) - # To generate binding code - target_link_options(slang-wasm PUBLIC "--bind") -endif() -# -# Our wrappers for glslang and llvm -# -if(SLANG_ENABLE_SLANG_GLSLANG) - slang_add_target( - source/slang-glslang - MODULE - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE glslang SPIRV SPIRV-Tools-opt - INCLUDE_DIRECTORIES_PRIVATE include - INSTALL - ) - # Our only interface is through what we define in source/slang-glslang, in the - # interests of hygiene, hide anything else we link in. - add_supported_cxx_linker_flags(slang-glslang PRIVATE "-Wl,--exclude-libs,ALL") -endif() - # webgpu_dawn is only available as a fetched shared library, since Dawn's nested source # trees are too large and onerous for us to depend on. if(SLANG_WEBGPU_DAWN_BINARY_URL) - copy_fetched_shared_library("webgpu_dawn" "${SLANG_WEBGPU_DAWN_BINARY_URL}") + copy_fetched_shared_library("webgpu_dawn" "${SLANG_WEBGPU_DAWN_BINARY_URL}") endif() # slang-tint is only available as a fetched shared library, since it's hosted in the Dawn # repository, and Dawn's nested source trees are too large and onerous for us to depend # on. if(SLANG_SLANG_TINT_BINARY_URL) - copy_fetched_shared_library("slang-tint" "${SLANG_SLANG_TINT_BINARY_URL}") -endif() - -if(SLANG_SLANG_LLVM_FLAVOR STREQUAL "FETCH_BINARY") - install_fetched_shared_library("slang-llvm" "${SLANG_SLANG_LLVM_BINARY_URL}") -elseif(SLANG_SLANG_LLVM_FLAVOR STREQUAL "FETCH_BINARY_IF_POSSIBLE") - if(SLANG_SLANG_LLVM_BINARY_URL) - install_fetched_shared_library("slang-llvm" "${SLANG_SLANG_LLVM_BINARY_URL}" IGNORE_FAILURE) - if(NOT TARGET slang-llvm) - message(WARNING "Unable to fetch slang-llvm prebuilt binary, configuring without LLVM support") - endif() - endif() -elseif(SLANG_SLANG_LLVM_FLAVOR STREQUAL "USE_SYSTEM_LLVM") - llvm_target_from_components(llvm-dep filecheck native orcjit) - clang_target_from_libs( - clang-dep - clangBasic - clangCodeGen - clangDriver - clangLex - clangFrontend - clangFrontendTool - ) - slang_add_target( - source/slang-llvm - MODULE - LINK_WITH_PRIVATE core compiler-core llvm-dep clang-dep - # We include slang.h, but don't need to link with it - INCLUDE_FROM_PRIVATE slang - # This uses the SLANG_DLL_EXPORT macro from slang.h, so make sure to set - # SLANG_DYNAMIC and SLANG_DYNAMIC_EXPORT - EXPORT_MACRO_PREFIX SLANG - INSTALL - INSTALL_COMPONENT slang-llvm - ) - # If we don't include this, then the symbols in the LLVM linked here may - # conflict with those of other LLVMs linked at runtime, for instance in mesa. - add_supported_cxx_linker_flags(slang-llvm PRIVATE "-Wl,--exclude-libs,ALL") - - # The LLVM headers need a warning disabling, which somehow slips through \external - if(MSVC) - target_compile_options(slang-llvm PRIVATE -wd4244) - endif() - - # TODO: Put a check here that libslang-llvm.so doesn't have a 'NEEDED' - # directive for libLLVM-13.so, it's almost certainly going to break at - # runtime in surprising ways when linked alongside Mesa (or anything else - # pulling in libLLVM.so) -endif() - -if(SLANG_ENABLE_PREBUILT_BINARIES) - if(CMAKE_SYSTEM_NAME MATCHES "Windows") - # DX Agility SDK requires the D3D12*.DLL files to be placed under a sub-directory, "D3D12". - # https://devblogs.microsoft.com/directx/gettingstarted-dx12agility/#d3d12sdkpath-should-not-be-the-same-directory-as-the-application-exe - file(GLOB prebuilt_binaries "${CMAKE_SOURCE_DIR}/external/slang-binaries/bin/windows-x64/*") - file(GLOB prebuilt_d3d12_binaries "${CMAKE_SOURCE_DIR}/external/slang-binaries/bin/windows-x64/[dD]3[dD]12*") - list(REMOVE_ITEM prebuilt_binaries ${prebuilt_d3d12_binaries}) - add_custom_target( - copy-prebuilt-binaries ALL - COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/$/${runtime_subdir} - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${prebuilt_binaries} - ${CMAKE_BINARY_DIR}/$/${runtime_subdir} - COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/$/${runtime_subdir}/D3D12 - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${prebuilt_d3d12_binaries} - ${CMAKE_BINARY_DIR}/$/${runtime_subdir}/D3D12 - VERBATIM - ) - endif() + copy_fetched_shared_library("slang-tint" "${SLANG_SLANG_TINT_BINARY_URL}") endif() -if(SLANG_ENABLE_GFX) - # - # `platform` contains all the platform abstractions for a GUI application. - # - slang_add_target( - tools/platform - SHARED - EXCLUDE_FROM_ALL - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE - core - imgui - $<$:X11::X11> - "$<$:-framework Cocoa>" - "$<$:-framework QuartzCore>" - ${CMAKE_DL_LIBS} - LINK_WITH_FRAMEWORK - Foundation - Cocoa - QuartzCore - EXTRA_COMPILE_DEFINITIONS_PRIVATE - $<$:SLANG_ENABLE_XLIB=1> - INCLUDE_FROM_PRIVATE gfx - INCLUDE_DIRECTORIES_PUBLIC tools/platform - EXPORT_MACRO_PREFIX SLANG_PLATFORM - ) - - # - # GFX - # - slang_add_target( - tools/gfx - SHARED - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE - core - slang - Vulkan-Headers - metal-cpp - $<$:X11::X11> - $<$:CUDA::cuda_driver> - LINK_WITH_FRAMEWORK - Foundation - Cocoa - QuartzCore - Metal - EXTRA_COMPILE_DEFINITIONS_PRIVATE - $<$:GFX_ENABLE_CUDA> - $<$:GFX_OPTIX> - $<$:GFX_NVAPI> - $<$:SLANG_ENABLE_XLIB> - # This is a shared library, so we need to set a preprocessor macro to mark - # exported symbols - EXPORT_MACRO_PREFIX SLANG_GFX - # slang-gfx is in this directory, anything which depends on gfx should include - # this - INCLUDE_DIRECTORIES_PUBLIC . include - REQUIRES copy-gfx-slang-modules - INSTALL - FOLDER gfx - ) - set(modules_dest_dir $) - add_custom_target( - copy-gfx-slang-modules - COMMAND ${CMAKE_COMMAND} -E make_directory ${modules_dest_dir} - COMMAND - ${CMAKE_COMMAND} -E copy tools/gfx/gfx.slang - ${modules_dest_dir}/gfx.slang - COMMAND - ${CMAKE_COMMAND} -E copy tools/gfx/slang.slang - ${modules_dest_dir}/slang.slang - WORKING_DIRECTORY ${slang_SOURCE_DIR} - VERBATIM - ) - set_target_properties(copy-gfx-slang-modules PROPERTIES FOLDER generators) - install( - FILES ${modules_dest_dir}/gfx.slang ${modules_dest_dir}/slang.slang - DESTINATION ${runtime_subdir} - ) - - slang_add_target( - tools/gfx-util - STATIC - LINK_WITH_PRIVATE core - INCLUDE_FROM_PRIVATE gfx - # The headers are included with 'include "gfx-util/blah.h"' which is found - # in the tools directory - INCLUDE_DIRECTORIES_PUBLIC tools - FOLDER gfx - ) -endif() +fetch_or_build_slang_llvm() # -# The test executables and runtime-loaded modules -# -if(SLANG_ENABLE_TESTS) - slang_add_target( - tools/test-server - EXECUTABLE - EXCLUDE_FROM_ALL - LINK_WITH_PRIVATE core compiler-core slang - FOLDER test - ) - slang_add_target( - tools/test-process - EXECUTABLE - EXCLUDE_FROM_ALL - LINK_WITH_PRIVATE core compiler-core - FOLDER test - ) - - slang_add_target( - tools/slang-test - EXECUTABLE - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE core compiler-core slang ${CMAKE_DL_LIBS} Threads::Threads - REQUIRES - # Shared libraries dlopened by slang-test - slang-reflection-test - slang-unit-test - # Used by some tests when they run - slangd - test-server - test-process - OPTIONAL_REQUIRES - gfx - slang-rt - slang-glslang - slang-llvm - copy-webgpu_dawn - copy-slang-tint - FOLDER test - DEBUG_DIR ${slang_SOURCE_DIR} - ) - set_property( - DIRECTORY ${slang_SOURCE_DIR} - PROPERTY VS_STARTUP_PROJECT slang-test - ) - - slang_add_target( - tools/unit-test - OBJECT - EXCLUDE_FROM_ALL - INCLUDE_FROM_PRIVATE slang core - INCLUDE_DIRECTORIES_PRIVATE include - FOLDER test - ) - - # These are libraries loaded at runtime from the test executable: - if(SLANG_ENABLE_GFX) - slang_add_target( - tools/gfx-unit-test - MODULE - EXCLUDE_FROM_ALL - EXTRA_COMPILE_DEFINITIONS_PRIVATE SLANG_SHARED_LIBRARY_TOOL - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE core slang unit-test gfx gfx-util platform - OUTPUT_NAME gfx-unit-test-tool - REQUIRED_BY slang-test - FOLDER test/tools - ) - if(SLANG_ENABLE_SLANG_RHI) - slang_add_target( - tools/render-test - MODULE - EXCLUDE_FROM_ALL - EXTRA_COMPILE_DEFINITIONS_PRIVATE SLANG_SHARED_LIBRARY_TOOL - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE - core - compiler-core - slang - slang-rhi - platform - $<$:CUDA::cuda_driver> - EXTRA_COMPILE_DEFINITIONS_PRIVATE - $<$:RENDER_TEST_CUDA> - $<$:RENDER_TEST_OPTIX> - OUTPUT_NAME render-test-tool - REQUIRED_BY slang-test - FOLDER test/tools - ) - endif() - endif() - slang_add_target( - tools/slang-unit-test - MODULE - EXCLUDE_FROM_ALL - EXTRA_COMPILE_DEFINITIONS_PRIVATE SLANG_SHARED_LIBRARY_TOOL - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE core compiler-core unit-test slang Threads::Threads - OUTPUT_NAME slang-unit-test-tool - FOLDER test/tools - ) - slang_add_target( - tools/slang-reflection-test - MODULE - EXCLUDE_FROM_ALL - EXTRA_COMPILE_DEFINITIONS_PRIVATE SLANG_SHARED_LIBRARY_TOOL - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE core slang Threads::Threads - OUTPUT_NAME slang-reflection-test-tool - FOLDER test/tools - ) - - slang_add_target( - tools/slang-profile - EXECUTABLE - EXCLUDE_FROM_ALL - LINK_WITH_PRIVATE core slang - FOLDER test - ) -endif() - -if (SLANG_ENABLE_EXAMPLES AND SLANG_ENABLE_GFX) - # - # Examples - # - slang_add_target( - examples/example-base - STATIC - LINK_WITH_PRIVATE - core - slang - gfx - platform - $<$:CUDA::cuda_driver> - FOLDER examples - ) - - add_custom_target( - all-examples - COMMENT "meta target which depends on all examples" - ) - set_target_properties(all-examples PROPERTIES FOLDER examples) - function(example dir) - set(debug_dir ${CMAKE_BINARY_DIR}/${dir}) - - slang_add_target( - ${dir} - EXECUTABLE - USE_FEWER_WARNINGS - LINK_WITH_PRIVATE - core - example-base - slang - gfx - gfx-util - platform - $<$:CUDA::cuda_driver> - EXTRA_COMPILE_DEFINITIONS_PRIVATE - $<$:SLANG_ENABLE_XLIB> - REQUIRED_BY all-examples - FOLDER examples - DEBUG_DIR ${debug_dir} - ${ARGN} - ) - - 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" - "${CMAKE_SOURCE_DIR}/${dir}/*.h" - ) - - 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 (MSVC) - if (SLANG_ENABLE_PREBUILT_BINARIES) - add_dependencies(${example_target} copy-prebuilt-binaries) - endif() - endif() - endif() - endfunction() - - example(examples/autodiff-texture WIN32_EXECUTABLE) - example(examples/cpu-com-example ) - example(examples/cpu-hello-world ) - example(examples/gpu-printing ) - example(examples/hello-world LINK_WITH_PRIVATE Vulkan-Headers) - example(examples/model-viewer WIN32_EXECUTABLE) - example(examples/platform-test WIN32_EXECUTABLE) - example(examples/ray-tracing WIN32_EXECUTABLE) - example(examples/ray-tracing-pipeline WIN32_EXECUTABLE) - example(examples/shader-object ) - example(examples/shader-toy WIN32_EXECUTABLE) - example(examples/triangle WIN32_EXECUTABLE) - if(SLANG_ENABLE_AFTERMATH) - example(examples/nv-aftermath-example WIN32_EXECUTABLE) - endif() -endif() - -# -# slang-replay tool for replaying the record files +# Our targets # -if (SLANG_ENABLE_REPLAYER) - slang_add_target( - tools/slang-replay - EXECUTABLE - EXTRA_SOURCE_DIRS source/slang-record-replay/replay source/slang-record-replay/util - LINK_WITH_PRIVATE core compiler-core slang - INCLUDE_DIRECTORIES_PUBLIC source/slang-record-replay - FOLDER test - ) -endif() -# -# Testing -# -if(SLANG_ENABLE_TESTS) - include(CTest) - add_test( - NAME slang-test - COMMAND - slang-test -bindir ${slang_SOURCE_DIR}/build/$/${runtime_subdir} - -expected-failure-list ${slang_SOURCE_DIR}/tests/expected-failure.txt - -expected-failure-list - ${slang_SOURCE_DIR}/tests/expected-failure-github.txt - WORKING_DIRECTORY ${slang_SOURCE_DIR} - ) -endif() +add_subdirectory(source/core) +add_subdirectory(source/slang-rt) +add_subdirectory(source/compiler-core) +add_subdirectory(source/slang-wasm) +add_subdirectory(source/slang-glslang) +add_subdirectory(tools) +add_subdirectory(prelude) +add_subdirectory(source/slang-stdlib) +add_subdirectory(source/slang) +add_subdirectory(source/slangc) +add_subdirectory(examples) # # Packaging diff --git a/cmake/LLVM.cmake b/cmake/LLVM.cmake index 10a31e2d16..a9afdb3cb0 100644 --- a/cmake/LLVM.cmake +++ b/cmake/LLVM.cmake @@ -38,3 +38,78 @@ function(clang_target_from_libs target_name) add_supported_cxx_flags(${target_name} INTERFACE -fno-rtti /GR-) endif() endfunction() + +function(fetch_or_build_slang_llvm) + if(SLANG_SLANG_LLVM_FLAVOR STREQUAL "FETCH_BINARY") + install_fetched_shared_library("slang-llvm" "${SLANG_SLANG_LLVM_BINARY_URL}") + elseif(SLANG_SLANG_LLVM_FLAVOR STREQUAL "FETCH_BINARY_IF_POSSIBLE") + if(SLANG_SLANG_LLVM_BINARY_URL) + install_fetched_shared_library("slang-llvm" "${SLANG_SLANG_LLVM_BINARY_URL}" IGNORE_FAILURE) + if(NOT TARGET slang-llvm) + message(WARNING "Unable to fetch slang-llvm prebuilt binary, configuring without LLVM support") + endif() + endif() + elseif(SLANG_SLANG_LLVM_FLAVOR STREQUAL "USE_SYSTEM_LLVM") + find_package(LLVM 13.0 REQUIRED CONFIG) + find_package(Clang REQUIRED CONFIG) + + llvm_target_from_components(llvm-dep filecheck native orcjit) + clang_target_from_libs( + clang-dep + clangBasic + clangCodeGen + clangDriver + clangLex + clangFrontend + clangFrontendTool + ) + slang_add_target( + source/slang-llvm + MODULE + LINK_WITH_PRIVATE core compiler-core llvm-dep clang-dep + # We include slang.h, but don't need to link with it + INCLUDE_FROM_PRIVATE slang + # This uses the SLANG_DLL_EXPORT macro from slang.h, so make sure to set + # SLANG_DYNAMIC and SLANG_DYNAMIC_EXPORT + EXPORT_MACRO_PREFIX SLANG + INSTALL + INSTALL_COMPONENT slang-llvm + ) + # If we don't include this, then the symbols in the LLVM linked here may + # conflict with those of other LLVMs linked at runtime, for instance in mesa. + add_supported_cxx_linker_flags(slang-llvm PRIVATE "-Wl,--exclude-libs,ALL") + + # The LLVM headers need a warning disabling, which somehow slips through \external + if(MSVC) + target_compile_options(slang-llvm PRIVATE -wd4244) + endif() + + # TODO: Put a check here that libslang-llvm.so doesn't have a 'NEEDED' + # directive for libLLVM-13.so, it's almost certainly going to break at + # runtime in surprising ways when linked alongside Mesa (or anything else + # pulling in libLLVM.so) + endif() + + if(SLANG_ENABLE_PREBUILT_BINARIES) + if(CMAKE_SYSTEM_NAME MATCHES "Windows") + # DX Agility SDK requires the D3D12*.DLL files to be placed under a sub-directory, "D3D12". + # https://devblogs.microsoft.com/directx/gettingstarted-dx12agility/#d3d12sdkpath-should-not-be-the-same-directory-as-the-application-exe + file(GLOB prebuilt_binaries "${slang_SOURCE_DIR}/external/slang-binaries/bin/windows-x64/*") + file(GLOB prebuilt_d3d12_binaries "${slang_SOURCE_DIR}/external/slang-binaries/bin/windows-x64/[dD]3[dD]12*") + list(REMOVE_ITEM prebuilt_binaries ${prebuilt_d3d12_binaries}) + add_custom_target( + copy-prebuilt-binaries ALL + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/$/${runtime_subdir} + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${prebuilt_binaries} + ${CMAKE_BINARY_DIR}/$/${runtime_subdir} + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/$/${runtime_subdir}/D3D12 + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${prebuilt_d3d12_binaries} + ${CMAKE_BINARY_DIR}/$/${runtime_subdir}/D3D12 + VERBATIM + ) + set_target_properties(copy-prebuilt-binaries PROPERTIES FOLDER external) + endif() + endif() +endfunction() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000000..ad2d6f3194 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,91 @@ +function(example dir) + set(debug_dir ${CMAKE_CURRENT_BINARY_DIR}/${dir}) + + file( + GLOB asset_files + CONFIGURE_DEPENDS + "${dir}/*.slang" + "${dir}/*.jpg" + "${dir}/*.obj" + "${dir}/*.mtl" + "${dir}/*.h" + ) + + list(LENGTH asset_files asset_files_length) + if(asset_files_length GREATER 0) + set(copy_assets_target "${dir}-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" + ) + endif() + + slang_add_target( + ${dir} + EXECUTABLE + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE + core + example-base + slang + gfx + gfx-util + platform + $<$:CUDA::cuda_driver> + EXTRA_COMPILE_DEFINITIONS_PRIVATE + $<$:SLANG_ENABLE_XLIB> + REQUIRED_BY all-examples + OPTIONAL_REQUIRES ${copy_assets_target} copy-prebuilt-binaries + FOLDER examples + DEBUG_DIR ${debug_dir} + ${ARGN} + ) +endfunction() + +if(SLANG_ENABLE_EXAMPLES) + # + # Examples + # + slang_add_target( + example-base + STATIC + LINK_WITH_PRIVATE + core + slang + gfx + platform + $<$:CUDA::cuda_driver> + FOLDER examples + ) + + add_custom_target( + all-examples + COMMENT "meta target which depends on all examples" + ) + set_target_properties(all-examples PROPERTIES FOLDER examples) + example(autodiff-texture WIN32_EXECUTABLE) + example(cpu-com-example) + example(cpu-hello-world) + example(gpu-printing) + example(hello-world LINK_WITH_PRIVATE Vulkan-Headers) + example(model-viewer WIN32_EXECUTABLE) + example(platform-test WIN32_EXECUTABLE) + example(ray-tracing WIN32_EXECUTABLE) + example(ray-tracing-pipeline WIN32_EXECUTABLE) + example(shader-object) + example(shader-toy WIN32_EXECUTABLE) + example(triangle WIN32_EXECUTABLE) + if(SLANG_ENABLE_AFTERMATH) + example(nv-aftermath-example WIN32_EXECUTABLE) + endif() +endif() diff --git a/prelude/CMakeLists.txt b/prelude/CMakeLists.txt index 49503ddf7e..b9a763f035 100644 --- a/prelude/CMakeLists.txt +++ b/prelude/CMakeLists.txt @@ -3,10 +3,10 @@ # Construct a library called 'prelude' to be linked with by slang # -glob_append(SLANG_PRELUDE_HEADERS "*-prelude.h") +glob_append(prelude_headers "*-prelude.h") -set(SLANG_PRELUDE_SOURCE) -foreach(input ${SLANG_PRELUDE_HEADERS}) +set(prelude_source) +foreach(input ${prelude_headers}) get_filename_component(input_name ${input} NAME) set(output "${CMAKE_CURRENT_BINARY_DIR}/${input_name}.cpp") add_custom_command( @@ -15,13 +15,13 @@ foreach(input ${SLANG_PRELUDE_HEADERS}) DEPENDS ${input} slang-embed VERBATIM ) - list(APPEND SLANG_PRELUDE_SOURCE ${output}) + list(APPEND prelude_source ${output}) endforeach() slang_add_target( . OBJECT - EXPLICIT_SOURCE ${SLANG_PRELUDE_SOURCE} + EXPLICIT_SOURCE ${prelude_source} EXCLUDE_FROM_ALL TARGET_NAME prelude INCLUDE_DIRECTORIES_PUBLIC ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/../include diff --git a/source/compiler-core/CMakeLists.txt b/source/compiler-core/CMakeLists.txt new file mode 100644 index 0000000000..4d1bff17b1 --- /dev/null +++ b/source/compiler-core/CMakeLists.txt @@ -0,0 +1,16 @@ +slang_add_target( + . + STATIC + EXCLUDE_FROM_ALL + USE_EXTRA_WARNINGS + LINK_WITH_PRIVATE core + INCLUDE_FROM_PUBLIC SPIRV-Headers +) +if(NOT MSVC) + # This is necessary to compile the DXC headers + set_source_files_properties( + slang-dxc-compiler.cpp + PROPERTIES COMPILE_OPTIONS "-fms-extensions" + DIRECTORY ${slang_SOURCE_DIR} + ) +endif() diff --git a/source/core/CMakeLists.txt b/source/core/CMakeLists.txt new file mode 100644 index 0000000000..15da2abfb1 --- /dev/null +++ b/source/core/CMakeLists.txt @@ -0,0 +1,10 @@ +slang_add_target( + . + STATIC + EXCLUDE_FROM_ALL + USE_EXTRA_WARNINGS + LINK_WITH_PRIVATE + miniz lz4_static Threads::Threads ${CMAKE_DL_LIBS} + LINK_WITH_PUBLIC unordered_dense::unordered_dense + INCLUDE_DIRECTORIES_PUBLIC ${slang_SOURCE_DIR}/source ${slang_SOURCE_DIR}/include +) diff --git a/source/slang-glslang/CMakeLists.txt b/source/slang-glslang/CMakeLists.txt new file mode 100644 index 0000000000..37bb93ca08 --- /dev/null +++ b/source/slang-glslang/CMakeLists.txt @@ -0,0 +1,16 @@ +# +# Our wrapper for glslang +# +if(SLANG_ENABLE_SLANG_GLSLANG) + slang_add_target( + . + MODULE + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE glslang SPIRV SPIRV-Tools-opt + INCLUDE_DIRECTORIES_PRIVATE ${slang_SOURCE_DIR}/include + INSTALL + ) + # Our only interface is through what we define in source/slang-glslang, in the + # interests of hygiene, hide anything else we link in. + add_supported_cxx_linker_flags(slang-glslang PRIVATE "-Wl,--exclude-libs,ALL") +endif() diff --git a/source/slang-rt/CMakeLists.txt b/source/slang-rt/CMakeLists.txt new file mode 100644 index 0000000000..fc37237cd7 --- /dev/null +++ b/source/slang-rt/CMakeLists.txt @@ -0,0 +1,15 @@ +if(SLANG_ENABLE_SLANGRT) + slang_add_target( + . + SHARED + # This compiles 'core' again with the SLANG_RT_DYNAMIC_EXPORT macro defined + EXTRA_SOURCE_DIRS ${slang_SOURCE_DIR}/source/core + USE_EXTRA_WARNINGS + LINK_WITH_PRIVATE + miniz lz4_static Threads::Threads ${CMAKE_DL_LIBS} + LINK_WITH_PUBLIC unordered_dense::unordered_dense + EXPORT_MACRO_PREFIX SLANG_RT + INCLUDE_DIRECTORIES_PUBLIC ${slang_SOURCE_DIR}/include + INSTALL + ) +endif() diff --git a/source/slang-stdlib/CMakeLists.txt b/source/slang-stdlib/CMakeLists.txt index 6303bc1b91..a6d646cf30 100644 --- a/source/slang-stdlib/CMakeLists.txt +++ b/source/slang-stdlib/CMakeLists.txt @@ -87,7 +87,8 @@ add_custom_command( VERBATIM ) -add_custom_target(generate_stdlib_headers DEPENDS ${stdlib_meta_generated_headers}) +add_custom_target(generate-stdlib-headers DEPENDS ${stdlib_meta_generated_headers}) +set_target_properties(generate-stdlib-headers PROPERTIES FOLDER generated) # # Generate embedded stdlib source @@ -108,7 +109,7 @@ set(stdlib_source_common_args EXPLICIT_SOURCE ./slang-embedded-stdlib-source.cpp ${stdlib_meta_generated_headers} - REQUIRES generate_stdlib_headers + REQUIRES generate-stdlib-headers EXTRA_COMPILE_DEFINITIONS_PRIVATE SLANG_EMBED_STDLIB_SOURCE INCLUDE_DIRECTORIES_PRIVATE ${stdlib_meta_output_dir} diff --git a/source/slang-wasm/CMakeLists.txt b/source/slang-wasm/CMakeLists.txt new file mode 100644 index 0000000000..d821415e74 --- /dev/null +++ b/source/slang-wasm/CMakeLists.txt @@ -0,0 +1,17 @@ +# +# WebAssembly bindings for Slang +# +# This is an executable target because emcmake produces .a files without bindings if you just create a static library +# https://stackoverflow.com/questions/63622009/static-library-built-with-cmake-as-a-with-emscripten-instead-of-wasm-js +if (EMSCRIPTEN) + slang_add_target( + . + EXECUTABLE + EXCLUDE_FROM_ALL + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE miniz lz4_static slang core compiler-core + INCLUDE_DIRECTORIES_PUBLIC ${slang_SOURCE_DIR}/include . + ) + # To generate binding code + target_link_options(slang-wasm PUBLIC "--bind") +endif() diff --git a/source/slangc/CMakeLists.txt b/source/slangc/CMakeLists.txt new file mode 100644 index 0000000000..fba390b31f --- /dev/null +++ b/source/slangc/CMakeLists.txt @@ -0,0 +1,10 @@ +if(SLANG_ENABLE_SLANGC) + slang_add_target( + . + EXECUTABLE + USE_FEWER_WARNINGS + DEBUG_DIR ${slang_SOURCE_DIR} + LINK_WITH_PRIVATE core slang Threads::Threads + INSTALL + ) +endif() diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt new file mode 100644 index 0000000000..8c2221a2b5 --- /dev/null +++ b/tools/CMakeLists.txt @@ -0,0 +1,322 @@ +# +# Tools used to generate source during the build: +# + +add_custom_target( + all-generators + COMMENT "meta target which depends on all generators" +) +set_target_properties(all-generators PROPERTIES FOLDER generators) + +if(CMAKE_HOST_WIN32) + set(CMAKE_HOST_EXECUTABLE_SUFFIX ".exe") +else() + set(CMAKE_HOST_EXECUTABLE_SUFFIX "") +endif() +function(generator dir) + if(SLANG_GENERATORS_PATH) + cmake_parse_arguments(ARG "" "TARGET_NAME" "" ${ARGN}) + if(ARG_TARGET_NAME) + set(target ${ARG_TARGET_NAME}) + else() + get_filename_component(target ${dir} NAME) + endif() + add_executable(${target} IMPORTED GLOBAL) + set_property( + TARGET ${target} + PROPERTY + IMPORTED_LOCATION + "${SLANG_GENERATORS_PATH}/${target}${CMAKE_HOST_EXECUTABLE_SUFFIX}" + ) + else() + slang_add_target( + ${dir} + EXECUTABLE + EXCLUDE_FROM_ALL + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE core + OUTPUT_DIR generators + REQUIRED_BY all-generators + FOLDER generators + INSTALL_COMPONENT generators + ${ARGN} + ) + endif() +endfunction() + +generator(slang-cpp-extractor USE_FEWER_WARNINGS LINK_WITH_PRIVATE compiler-core) +generator(slang-embed) +generator(slang-generate USE_FEWER_WARNINGS) +generator(slang-lookup-generator LINK_WITH_PRIVATE compiler-core) +generator(slang-capability-generator LINK_WITH_PRIVATE compiler-core) +generator(slang-spirv-embed-generator LINK_WITH_PRIVATE compiler-core) +generator( + ${slang_SOURCE_DIR}/source/slangc + TARGET_NAME slang-bootstrap + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE + prelude + slang-without-embedded-stdlib + slang-capability-lookup + slang-lookup-tables + Threads::Threads +) + +# +# Language Server +# +if(SLANG_ENABLE_SLANGD) + slang_add_target( + slangd + EXECUTABLE + LINK_WITH_PRIVATE + core + compiler-core + slang + slang-reflect-headers + slang-capability-defs + Threads::Threads + INSTALL + ) +endif() + +if(SLANG_ENABLE_GFX) + # + # `platform` contains all the platform abstractions for a GUI application. + # + slang_add_target( + platform + SHARED + EXCLUDE_FROM_ALL + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE + core + imgui + $<$:X11::X11> + "$<$:-framework Cocoa>" + "$<$:-framework QuartzCore>" + ${CMAKE_DL_LIBS} + LINK_WITH_FRAMEWORK + Foundation + Cocoa + QuartzCore + EXTRA_COMPILE_DEFINITIONS_PRIVATE + $<$:SLANG_ENABLE_XLIB=1> + INCLUDE_FROM_PRIVATE gfx + INCLUDE_DIRECTORIES_PUBLIC platform + EXPORT_MACRO_PREFIX SLANG_PLATFORM + ) + + # + # GFX + # + slang_add_target( + gfx + SHARED + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE + core + slang + Vulkan-Headers + metal-cpp + $<$:X11::X11> + $<$:CUDA::cuda_driver> + LINK_WITH_FRAMEWORK + Foundation + Cocoa + QuartzCore + Metal + EXTRA_COMPILE_DEFINITIONS_PRIVATE + $<$:GFX_ENABLE_CUDA> + $<$:GFX_OPTIX> + $<$:GFX_NVAPI> + $<$:SLANG_ENABLE_XLIB> + # This is a shared library, so we need to set a preprocessor macro to mark + # exported symbols + EXPORT_MACRO_PREFIX SLANG_GFX + # slang-gfx is in this directory, anything which depends on gfx should include + # this + INCLUDE_DIRECTORIES_PUBLIC ${slang_SOURCE_DIR} ${slang_SOURCE_DIR}/include + REQUIRES copy-gfx-slang-modules + INSTALL + FOLDER gfx + ) + set(modules_dest_dir $) + add_custom_target( + copy-gfx-slang-modules + COMMAND ${CMAKE_COMMAND} -E make_directory ${modules_dest_dir} + COMMAND + ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_LIST_DIR}/gfx/gfx.slang + ${modules_dest_dir}/gfx.slang + COMMAND + ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_LIST_DIR}/gfx/slang.slang + ${modules_dest_dir}/slang.slang + WORKING_DIRECTORY ${slang_SOURCE_DIR} + VERBATIM + ) + set_target_properties(copy-gfx-slang-modules PROPERTIES FOLDER generators) + install( + FILES ${modules_dest_dir}/gfx.slang ${modules_dest_dir}/slang.slang + DESTINATION ${runtime_subdir} + ) + + slang_add_target( + gfx-util + STATIC + LINK_WITH_PRIVATE core + INCLUDE_FROM_PRIVATE gfx + # The headers are included with 'include "gfx-util/blah.h"' which is found + # in the tools directory + INCLUDE_DIRECTORIES_PUBLIC . + FOLDER gfx + ) +endif() + +# +# The test executables and runtime-loaded modules +# +if(SLANG_ENABLE_TESTS) + slang_add_target( + test-server + EXECUTABLE + EXCLUDE_FROM_ALL + LINK_WITH_PRIVATE core compiler-core slang + FOLDER test + ) + slang_add_target( + test-process + EXECUTABLE + EXCLUDE_FROM_ALL + LINK_WITH_PRIVATE core compiler-core + FOLDER test + ) + + slang_add_target( + slang-test + EXECUTABLE + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE core compiler-core slang ${CMAKE_DL_LIBS} Threads::Threads + REQUIRES + # Shared libraries dlopened by slang-test + slang-reflection-test + slang-unit-test + # Used by some tests when they run + slangd + test-server + test-process + OPTIONAL_REQUIRES + gfx + slang-rt + slang-glslang + slang-llvm + copy-webgpu_dawn + copy-slang-tint + FOLDER test + DEBUG_DIR ${slang_SOURCE_DIR} + ) + set_property( + DIRECTORY ${slang_SOURCE_DIR} + PROPERTY VS_STARTUP_PROJECT slang-test + ) + include(CTest) + add_test( + NAME slang-test + COMMAND + slang-test -bindir ${slang_SOURCE_DIR}/build/$/${runtime_subdir} + -expected-failure-list ${slang_SOURCE_DIR}/tests/expected-failure.txt + -expected-failure-list + ${slang_SOURCE_DIR}/tests/expected-failure-github.txt + WORKING_DIRECTORY ${slang_SOURCE_DIR} + ) + + + slang_add_target( + unit-test + OBJECT + EXCLUDE_FROM_ALL + INCLUDE_FROM_PRIVATE slang core + INCLUDE_DIRECTORIES_PRIVATE ${slang_SOURCE_DIR}/include + FOLDER test + ) + + # These are libraries loaded at runtime from the test executable: + if(SLANG_ENABLE_GFX) + slang_add_target( + gfx-unit-test + MODULE + EXCLUDE_FROM_ALL + EXTRA_COMPILE_DEFINITIONS_PRIVATE SLANG_SHARED_LIBRARY_TOOL + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE core slang unit-test gfx gfx-util platform + OUTPUT_NAME gfx-unit-test-tool + REQUIRED_BY slang-test + FOLDER test/tools + ) + if(SLANG_ENABLE_SLANG_RHI) + slang_add_target( + render-test + MODULE + EXCLUDE_FROM_ALL + EXTRA_COMPILE_DEFINITIONS_PRIVATE SLANG_SHARED_LIBRARY_TOOL + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE + core + compiler-core + slang + slang-rhi + platform + $<$:CUDA::cuda_driver> + EXTRA_COMPILE_DEFINITIONS_PRIVATE + $<$:RENDER_TEST_CUDA> + $<$:RENDER_TEST_OPTIX> + OUTPUT_NAME render-test-tool + REQUIRED_BY slang-test + FOLDER test/tools + ) + endif() + endif() + slang_add_target( + slang-unit-test + MODULE + EXCLUDE_FROM_ALL + EXTRA_COMPILE_DEFINITIONS_PRIVATE SLANG_SHARED_LIBRARY_TOOL + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE core compiler-core unit-test slang Threads::Threads + OUTPUT_NAME slang-unit-test-tool + FOLDER test/tools + ) + slang_add_target( + slang-reflection-test + MODULE + EXCLUDE_FROM_ALL + EXTRA_COMPILE_DEFINITIONS_PRIVATE SLANG_SHARED_LIBRARY_TOOL + USE_FEWER_WARNINGS + LINK_WITH_PRIVATE core slang Threads::Threads + OUTPUT_NAME slang-reflection-test-tool + FOLDER test/tools + ) + + slang_add_target( + slang-profile + EXECUTABLE + EXCLUDE_FROM_ALL + LINK_WITH_PRIVATE core slang + FOLDER test + ) +endif() + +# +# slang-replay tool for replaying the record files +# +if (SLANG_ENABLE_REPLAYER) + slang_add_target( + slang-replay + EXECUTABLE + EXTRA_SOURCE_DIRS + ${slang_SOURCE_DIR}/source/slang-record-replay/replay + ${slang_SOURCE_DIR}/source/slang-record-replay/util + LINK_WITH_PRIVATE core compiler-core slang + INCLUDE_DIRECTORIES_PUBLIC ${slang_SOURCE_DIR}/source/slang-record-replay + FOLDER test + ) +endif()