Skip to content

Commit

Permalink
declutter top level CMakeLists.txt (shader-slang#5391)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
expipiplus1 authored Oct 24, 2024
1 parent fb50c03 commit 61aa670
Show file tree
Hide file tree
Showing 12 changed files with 597 additions and 614 deletions.
624 changes: 17 additions & 607 deletions CMakeLists.txt

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions cmake/LLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}/$<CONFIG>/${runtime_subdir}
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${prebuilt_binaries}
${CMAKE_BINARY_DIR}/$<CONFIG>/${runtime_subdir}
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/$<CONFIG>/${runtime_subdir}/D3D12
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${prebuilt_d3d12_binaries}
${CMAKE_BINARY_DIR}/$<CONFIG>/${runtime_subdir}/D3D12
VERBATIM
)
set_target_properties(copy-prebuilt-binaries PROPERTIES FOLDER external)
endif()
endif()
endfunction()
91 changes: 91 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<$<BOOL:${SLANG_ENABLE_CUDA}>:CUDA::cuda_driver>
EXTRA_COMPILE_DEFINITIONS_PRIVATE
$<$<BOOL:${SLANG_ENABLE_XLIB}>: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
$<$<BOOL:${SLANG_ENABLE_CUDA}>: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()
10 changes: 5 additions & 5 deletions prelude/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions source/compiler-core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
10 changes: 10 additions & 0 deletions source/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
16 changes: 16 additions & 0 deletions source/slang-glslang/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 15 additions & 0 deletions source/slang-rt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 3 additions & 2 deletions source/slang-stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down
17 changes: 17 additions & 0 deletions source/slang-wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
10 changes: 10 additions & 0 deletions source/slangc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit 61aa670

Please sign in to comment.