yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Lujin WangUpdate build to allow setting external mimalloc path (#8676)d0641da8c

master
15.2 KiB572 linesraw
1cmake_minimum_required(VERSION 3.22)
2
3# Our module dir, include that now so that we can get the version automatically
4# from git describe
5list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
6include(GitVersion)
7get_git_version(
8    SLANG_VERSION_NUMERIC
9    SLANG_VERSION_FULL
10    "${CMAKE_CURRENT_LIST_DIR}"
11)
12
13#
14# Our project
15#
16project(slang VERSION "${SLANG_VERSION_NUMERIC}" LANGUAGES)
17set(PROJECT_VERSION "${SLANG_VERSION_FULL}")
18
19#
20# Global CMake options
21#
22if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24")
23    cmake_policy(SET CMP0135 OLD)
24endif()
25cmake_policy(SET CMP0077 NEW)
26set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
27if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.25")
28    cmake_policy(SET CMP0141 NEW)
29endif()
30cmake_policy(SET CMP0091 NEW)
31
32# Don't use absolute paths to the build tree in RPATH, this makes the build
33# tree relocatable
34set(CMAKE_BUILD_RPATH_USE_ORIGIN TRUE)
35
36# Export the compile datebase as a json file, this can be used by VIM language server
37set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
38
39# Enable placing targets into a hierarchy for IDE generators
40set_property(GLOBAL PROPERTY USE_FOLDERS ON)
41
42#
43# CMake-supplied modules and our utils
44#
45enable_language(C CXX)
46
47include(FindPackageHandleStandardArgs)
48include(CMakeDependentOption)
49include(GNUInstallDirs)
50
51include(CCacheDebugInfoWorkaround)
52include(CompilerFlags)
53include(Glob)
54include(LLVM)
55include(SlangTarget)
56include(AutoOption)
57include(GitHubRelease)
58include(FetchedSharedLibrary)
59
60#
61# Options
62#
63
64auto_option(
65    SLANG_ENABLE_CUDA
66    CUDAToolkit
67    "Enable CUDA tests using CUDA found in CUDA_PATH"
68)
69# Check if SLANG_ENABLE_OPTIX was explicitly set by user before auto_option
70set(_SLANG_ENABLE_OPTIX_USER_SPECIFIED FALSE)
71get_property(
72    _SLANG_ENABLE_OPTIX_CACHE_TYPE
73    CACHE SLANG_ENABLE_OPTIX
74    PROPERTY TYPE
75)
76if(
77    DEFINED SLANG_ENABLE_OPTIX
78    AND NOT SLANG_ENABLE_OPTIX STREQUAL "AUTO"
79    AND _SLANG_ENABLE_OPTIX_CACHE_TYPE STREQUAL "BOOL"
80)
81    set(_SLANG_ENABLE_OPTIX_USER_SPECIFIED TRUE)
82endif()
83
84auto_option(
85    SLANG_ENABLE_OPTIX
86    OptiX
87    "Enable OptiX build/tests, requires SLANG_ENABLE_CUDA"
88)
89auto_option(
90    SLANG_ENABLE_NVAPI
91    NVAPI
92    "Enable NVAPI usage (Only available for builds targeting Windows)"
93)
94if(CMAKE_SYSTEM_NAME MATCHES "Linux")
95    auto_option(
96        SLANG_ENABLE_XLIB
97        X11
98        "Build gfx and platform with Xlib to support windowed apps on Linux"
99    )
100else()
101    set(SLANG_ENABLE_XLIB OFF)
102endif()
103auto_option(
104    SLANG_ENABLE_AFTERMATH
105    Aftermath
106    "Enable Aftermath in GFX, and add aftermath crash example to project"
107)
108advanced_option(
109    SLANG_ENABLE_DX_ON_VK
110    "Use dxvk and vkd3d-proton for DirectX support"
111    OFF
112)
113advanced_option(SLANG_ENABLE_SLANG_RHI "Use slang-rhi as dependency" ON)
114
115option(
116    SLANG_EMBED_CORE_MODULE_SOURCE
117    "Embed core module source in the binary"
118    ON
119)
120option(
121    SLANG_EMBED_CORE_MODULE
122    "Build slang with an embedded version of the core module"
123    ON
124)
125
126option(SLANG_ENABLE_DXIL "Enable generating DXIL with DXC" ON)
127
128option(SLANG_ENABLE_FULL_IR_VALIDATION "Enable full IR validation (SLOW!)")
129option(SLANG_ENABLE_IR_BREAK_ALLOC "Enable _debugUID on IR allocation")
130option(SLANG_ENABLE_ASAN "Enable ASAN (address sanitizer)")
131
132option(SLANG_ENABLE_PREBUILT_BINARIES "Enable using prebuilt binaries" ON)
133option(SLANG_ENABLE_GFX "Enable gfx targets" ON)
134option(SLANG_ENABLE_SLANGD "Enable language server target" ON)
135option(SLANG_ENABLE_SLANGC "Enable standalone compiler target" ON)
136option(SLANG_ENABLE_SLANGI "Enable Slang interpreter target" ON)
137option(SLANG_ENABLE_SLANGRT "Enable runtime target" ON)
138option(
139    SLANG_ENABLE_SLANG_GLSLANG
140    "Enable glslang dependency and slang-glslang wrapper target"
141    ON
142)
143option(
144    SLANG_ENABLE_TESTS
145    "Enable test targets, some tests may require SLANG_ENABLE_SLANG_RHI, SLANG_ENABLE_SLANGD or SLANG_ENABLE_SLANGRT"
146    ON
147)
148option(
149    SLANG_ENABLE_EXAMPLES
150    "Enable example targets, requires SLANG_ENABLE_SLANG_RHI"
151    ON
152)
153option(SLANG_ENABLE_REPLAYER "Enable slang-replay tool" ON)
154
155option(
156    SLANG_GITHUB_TOKEN
157    "Use a given token value for accessing Github REST API"
158    ""
159)
160
161advanced_option(SLANG_USE_SYSTEM_MINIZ "Build using system Miniz library" OFF)
162advanced_option(SLANG_USE_SYSTEM_LZ4 "Build using system LZ4 library" OFF)
163advanced_option(
164    SLANG_USE_SYSTEM_VULKAN_HEADERS
165    "Build using system Vulkan headers"
166    OFF
167)
168advanced_option(
169    SLANG_USE_SYSTEM_SPIRV_HEADERS
170    "Build using system SPIR-V headers"
171    OFF
172)
173advanced_option(
174    SLANG_USE_SYSTEM_UNORDERED_DENSE
175    "Build using system unordered dense"
176    OFF
177)
178advanced_option(
179    SLANG_USE_SYSTEM_SPIRV_TOOLS
180    "Build using system SPIR-V tools library"
181    OFF
182)
183advanced_option(
184    SLANG_USE_SYSTEM_GLSLANG
185    "Build using system glslang library"
186    OFF
187)
188if(WIN32)
189    set(SLANG_ENABLE_SPIRV_TOOLS_MIMALLOC_DEFAULT ON)
190else()
191    set(SLANG_ENABLE_SPIRV_TOOLS_MIMALLOC_DEFAULT OFF)
192endif()
193
194advanced_option(
195    SLANG_ENABLE_SPIRV_TOOLS_MIMALLOC
196    "Enable mimalloc for SPIRV-Tools to improve compilation performance"
197    ${SLANG_ENABLE_SPIRV_TOOLS_MIMALLOC_DEFAULT}
198)
199
200option(
201    SLANG_SPIRV_HEADERS_INCLUDE_DIR
202    "Provide a specific path for the SPIR-V headers and grammar files"
203)
204mark_as_advanced(SLANG_SPIRV_HEADERS_INCLUDE_DIR)
205
206# Options for user defined paths for external modules.
207#
208# Note that these paths point to a directory containing an subdirectory of the
209# dependency name, rather than to the dependency directory itself, for example
210# SLANG_OVERRIDE_LZ4_PATH points to a directory containing a directory called
211# `lz4` containing the lz4 source.
212#
213advanced_option(
214    SLANG_OVERRIDE_LZ4_PATH
215    "Build using user defined path for LZ4"
216    OFF
217)
218advanced_option(
219    SLANG_OVERRIDE_MINIZ_PATH
220    "Build using user defined path for Miniz"
221    OFF
222)
223advanced_option(
224    SLANG_OVERRIDE_UNORDERED_DENSE_PATH
225    "Build using user defined path for unordered_dense"
226    OFF
227)
228advanced_option(
229    SLANG_OVERRIDE_VULKAN_HEADERS_PATH
230    "Build using user defined path for Vulkan headers"
231    OFF
232)
233advanced_option(
234    SLANG_OVERRIDE_SPIRV_HEADERS_PATH
235    "Build using user defined path for SPIR-V headers"
236    OFF
237)
238advanced_option(
239    SLANG_OVERRIDE_SPIRV_TOOLS_PATH
240    "Build using user defined path for SPIR-V tools"
241    OFF
242)
243advanced_option(
244    SLANG_OVERRIDE_GLSLANG_PATH
245    "Build using user defined path for glslang"
246    OFF
247)
248advanced_option(
249    SLANG_OVERRIDE_GLM_PATH
250    "Build using user defined path for glm"
251    OFF
252)
253advanced_option(
254    SLANG_OVERRIDE_IMGUI_PATH
255    "Build using user defined path for imgui"
256    OFF
257)
258advanced_option(
259    SLANG_OVERRIDE_SLANG_RHI_PATH
260    "Build using user defined path for slang-rhi"
261    OFF
262)
263advanced_option(
264    SLANG_OVERRIDE_TINYOBJLOADER_PATH
265    "Build using user defined path for tinyobjloader"
266    OFF
267)
268advanced_option(
269    SLANG_OVERRIDE_LUA_PATH
270    "Build using user defined path for lua"
271    OFF
272)
273advanced_option(
274    SLANG_OVERRIDE_MIMALLOC_PATH
275    "Build using user defined path for mimalloc"
276    OFF
277)
278
279advanced_option(
280    SLANG_EXCLUDE_DAWN
281    "Optionally exclude webgpu_dawn from the build"
282    OFF
283)
284
285advanced_option(
286    SLANG_EXCLUDE_TINT
287    "Optionally exclude slang-tint from the build"
288    OFF
289)
290
291enum_option(
292    SLANG_LIB_TYPE
293    # Default
294    SHARED
295    "How to build the slang lib:"
296    # Options
297    SHARED
298    "Build slang as a shared library (default)"
299    STATIC
300    "Build slang as a static library"
301)
302
303option(
304    SLANG_ENABLE_RELEASE_DEBUG_INFO
305    "Generate debug info for Release builds"
306    ON
307)
308
309option(SLANG_ENABLE_RELEASE_LTO "Enable LTO for Release builds" OFF)
310
311option(
312    SLANG_ENABLE_SPLIT_DEBUG_INFO
313    "Generate split debug info for debug builds"
314    ON
315)
316
317set(SLANG_GENERATORS_PATH
318    ""
319    CACHE PATH
320    "An optional path to the outputs of the all-generators target compiled for the build platform, used when cross-compiling"
321)
322
323enum_option(
324    SLANG_SLANG_LLVM_FLAVOR
325    # Default
326    FETCH_BINARY_IF_POSSIBLE
327    "How to get or build slang-llvm:"
328    # Options
329    FETCH_BINARY
330    "Use a binary distribution of the slang-llvm library instead of building or using LLVM"
331    FETCH_BINARY_IF_POSSIBLE
332    "Like FETCH_BINARY, except falls back to DISABLE if a prebuilt slang-llvm can't be downloaded"
333    USE_SYSTEM_LLVM
334    "Build slang-llvm using system-provided LLVM and Clang binaries"
335    DISABLE
336    "Do not build llvm or fetch slang-llvm"
337)
338
339if(SLANG_SLANG_LLVM_FLAVOR MATCHES FETCH_BINARY)
340    # If the user didn't specify a URL, find the best one now
341    if(NOT SLANG_SLANG_LLVM_BINARY_URL)
342        get_best_slang_binary_release_url("${SLANG_GITHUB_TOKEN}" url)
343        if(NOT DEFINED url)
344            if(SLANG_SLANG_LLVM_FLAVOR STREQUAL FETCH_BINARY_IF_POSSIBLE)
345                message(
346                    WARNING
347                    "Unable to find a prebuilt binary for slang-llvm, Slang will be built without LLVM support. Please consider setting SLANG_SLANG_LLVM_BINARY_URL manually"
348                )
349            else()
350                message(
351                    FATAL_ERROR
352                    "Unable to find binary release for slang-llvm, please set a different SLANG_SLANG_LLVM_FLAVOR or set SLANG_SLANG_LLVM_BINARY_URL manually"
353                )
354            endif()
355        endif()
356    endif()
357    set(SLANG_SLANG_LLVM_BINARY_URL
358        ${url}
359        CACHE STRING
360        "URL specifying the location of the slang-llvm prebuilt library"
361    )
362endif()
363
364if(NOT SLANG_EXCLUDE_DAWN)
365    set(webgpu_dawn_release_tag "webgpu_dawn-0")
366    if(
367        CMAKE_SYSTEM_NAME MATCHES "Windows"
368        AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64"
369    )
370        set(SLANG_WEBGPU_DAWN_BINARY_URL
371            "https://github.com/shader-slang/dawn/releases/download/${webgpu_dawn_release_tag}/webgpu_dawn-windows-x64.zip"
372        )
373    endif()
374endif()
375
376if(NOT SLANG_EXCLUDE_TINT)
377    set(slang_tint_release_tag "slang-tint-0")
378    if(
379        CMAKE_SYSTEM_NAME MATCHES "Windows"
380        AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64"
381    )
382        set(SLANG_SLANG_TINT_BINARY_URL
383            "https://github.com/shader-slang/dawn/releases/download/${slang_tint_release_tag}/slang-tint-windows-x64.zip"
384        )
385    endif()
386endif()
387
388#
389# Option validation
390#
391
392if(NOT SLANG_EMBED_CORE_MODULE AND NOT SLANG_EMBED_CORE_MODULE_SOURCE)
393    message(
394        SEND_ERROR
395        "One of SLANG_EMBED_CORE_MODULE and SLANG_EMBED_CORE_MODULE_SOURCE must be enabled"
396    )
397endif()
398
399if(SLANG_ENABLE_OPTIX AND NOT SLANG_ENABLE_CUDA)
400    if(_SLANG_ENABLE_OPTIX_USER_SPECIFIED)
401        message(
402            SEND_ERROR
403            "SLANG_ENABLE_OPTIX is not supported without SLANG_ENABLE_CUDA"
404        )
405    else()
406        message(
407            STATUS
408            "OptiX is not supported without CUDA. Automatically disabling SLANG_ENABLE_OPTIX."
409        )
410        set(SLANG_ENABLE_OPTIX OFF)
411    endif()
412endif()
413
414if(SLANG_ENABLE_NVAPI AND NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
415    message(SEND_ERROR "SLANG_ENABLE_NVAPI is only supported on Windows")
416endif()
417
418if(SLANG_ENABLE_TESTS AND NOT SLANG_ENABLE_SLANG_RHI)
419    message(SEND_ERROR "SLANG_ENABLE_TESTS requires SLANG_ENABLE_SLANG_RHI")
420endif()
421
422#
423# Dependencies, most of these are however handled inside the "auto_option"
424# calls above
425#
426
427find_package(Threads REQUIRED)
428
429if(${SLANG_USE_SYSTEM_UNORDERED_DENSE})
430    find_package(unordered_dense CONFIG QUIET)
431endif()
432
433if(SLANG_USE_SYSTEM_SPIRV_HEADERS)
434    find_package(SPIRV-Headers REQUIRED)
435endif()
436
437if(${SLANG_USE_SYSTEM_MINIZ})
438    find_package(miniz REQUIRED)
439    add_library(miniz ALIAS miniz::miniz)
440endif()
441
442if(${SLANG_USE_SYSTEM_LZ4})
443    find_package(lz4 REQUIRED)
444    add_library(lz4_static ALIAS LZ4::lz4)
445endif()
446
447if(${SLANG_USE_SYSTEM_VULKAN_HEADERS})
448    find_package(VulkanHeaders REQUIRED)
449endif()
450
451if(${SLANG_USE_SYSTEM_SPIRV_TOOLS})
452    find_package(SPIRV-Tools REQUIRED)
453endif()
454
455if(${SLANG_USE_SYSTEM_GLSLANG})
456    find_package(glslang REQUIRED)
457    add_library(glslang ALIAS glslang::glslang)
458endif()
459
460add_subdirectory(external)
461
462# webgpu_dawn is only available as a fetched shared library, since Dawn's nested source
463# trees are too large and onerous for us to depend on.
464# We ignore the failure to fetch the library, since it's not required for the build to succeed.
465if(SLANG_WEBGPU_DAWN_BINARY_URL)
466    copy_fetched_shared_library(
467        "webgpu_dawn"
468        "${SLANG_WEBGPU_DAWN_BINARY_URL}"
469        IGNORE_FAILURE
470        SLANG_GITHUB_TOKEN ${SLANG_GITHUB_TOKEN}
471    )
472endif()
473
474# slang-tint is only available as a fetched shared library, since it's hosted in the Dawn
475# repository, and Dawn's nested source trees are too large and onerous for us to depend
476# on.
477# We ignore the failure to fetch the library, since it's not required for the build to succeed.
478if(SLANG_SLANG_TINT_BINARY_URL)
479    copy_fetched_shared_library(
480        "slang-tint"
481        "${SLANG_SLANG_TINT_BINARY_URL}"
482        IGNORE_FAILURE
483        SLANG_GITHUB_TOKEN ${SLANG_GITHUB_TOKEN}
484    )
485endif()
486
487fetch_or_build_slang_llvm()
488
489#
490# Our targets
491#
492
493add_subdirectory(source/core)
494add_subdirectory(source/slang-rt)
495add_subdirectory(source/compiler-core)
496add_subdirectory(source/slang-wasm)
497add_subdirectory(source/slang-glslang)
498add_subdirectory(tools)
499add_subdirectory(prelude)
500add_subdirectory(source/slang-core-module)
501add_subdirectory(source/slang-glsl-module)
502add_subdirectory(source/slang)
503add_subdirectory(source/slangc)
504add_subdirectory(examples)
505
506#
507# Packaging
508#
509set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
510set(CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE ON)
511set(CPACK_STRIP_FILES FALSE)
512install(
513    FILES "${slang_SOURCE_DIR}/README.md" "${slang_SOURCE_DIR}/LICENSE"
514    DESTINATION .
515    COMPONENT metadata
516    EXCLUDE_FROM_ALL
517)
518install(
519    DIRECTORY "${slang_SOURCE_DIR}/docs/"
520    DESTINATION share/doc/slang
521    PATTERN ".*" EXCLUDE
522)
523install(
524    DIRECTORY "${slang_SOURCE_DIR}/include"
525    DESTINATION .
526    PATTERN ".*" EXCLUDE
527)
528include(CPack)
529
530# Write basic package config version file using standard CMakePackageConfigHelpers utility
531include(CMakePackageConfigHelpers)
532write_basic_package_version_file(
533    "${PROJECT_NAME}ConfigVersion.cmake"
534    VERSION ${PROJECT_VERSION}
535    COMPATIBILITY SameMajorVersion
536)
537
538# Write SlangConfig.cmake which should allow find_package(SLANG) to work correctly
539# SlangConfig.cmake will define slang::slang target that can be linked with using
540# target_link_libraries. It will also define SLANG_EXECUTABLE export variable that
541# should point to slangc if SLANG_ENABLE_SLANGC is ON.
542if(WIN32)
543    set(SLANG_CMAKE_CONFIG_DIR cmake)
544else()
545    set(SLANG_CMAKE_CONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
546endif()
547configure_package_config_file(
548    "${PROJECT_SOURCE_DIR}/cmake/SlangConfig.cmake.in"
549    "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
550    INSTALL_DESTINATION ${SLANG_CMAKE_CONFIG_DIR}
551)
552
553# Conditionally handle the case for Emscripten where slang does not create
554# linkable targets. In this case do not export the targets. Otherwise, just
555# export the slang targets.
556if(NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
557    if(NOT ${SLANG_LIB_TYPE} STREQUAL "STATIC")
558        install(
559            EXPORT SlangTargets
560            FILE ${PROJECT_NAME}Targets.cmake
561            NAMESPACE ${PROJECT_NAME}::
562            DESTINATION ${SLANG_CMAKE_CONFIG_DIR}
563        )
564    endif()
565endif()
566
567install(
568    FILES
569        "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
570        "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
571    DESTINATION ${SLANG_CMAKE_CONFIG_DIR}
572)