|
1 | 1 | const std = @import("std"); |
2 | 2 |
|
3 | 3 | pub fn build(b: *std.Build) !void { |
4 | | - const lib = b.addLibrary(.{ |
5 | | - .name = "VulkanMemoryAllocator", |
6 | | - .linkage = b.option(std.builtin.LinkMode, "linkage", "defaults to static") orelse .static, |
7 | | - .root_module = b.createModule(.{ |
8 | | - .target = b.standardTargetOptions(.{}), |
9 | | - .optimize = b.standardOptimizeOption(.{}), |
10 | | - }), |
11 | | - }); |
12 | | - lib.linkLibCpp(); |
| 4 | + const target = b.standardTargetOptions(.{}); |
| 5 | + const optimize = b.standardOptimizeOption(.{}); |
13 | 6 |
|
14 | | - const install_vulkan_headers = b.option(bool, "install-vulkan-headers", "(defaults to false)") orelse false; |
| 7 | + const options = .{ |
| 8 | + .{ "VMA_STATIC_VULKAN_FUNCTIONS", bool, "Use statically linked Vulkan functions" }, |
| 9 | + .{ "VMA_DYNAMIC_VULKAN_FUNCTIONS", bool, "Dynamically load Vulkan functions" }, |
| 10 | + .{ "VMA_VULKAN_VERSION", i64, "Target specific Vulkan API version" }, |
| 11 | + .{ "VMA_STATS_STRING_ENABLED", bool, "Enable vmaBuildStatsString / vmaFreeStatsString" }, |
| 12 | + .{ "VMA_DEDICATED_ALLOCATION", bool, "Enable KHR dedicated allocation support" }, |
| 13 | + .{ "VMA_BIND_MEMORY2", bool, "Enable KHR bind memory 2 support" }, |
| 14 | + .{ "VMA_MEMORY_BUDGET", bool, "Enable memory budget tracking" }, |
| 15 | + .{ "VMA_BUFFER_DEVICE_ADDRESS", bool, "Enable buffer device address support" }, |
| 16 | + .{ "VMA_MEMORY_PRIORITY", bool, "Enable memory priority support" }, |
| 17 | + .{ "VMA_KHR_MAINTENANCE4", bool, "Enable KHR maintenance4 support" }, |
| 18 | + .{ "VMA_KHR_MAINTENANCE5", bool, "Enable KHR maintenance5 support" }, |
| 19 | + .{ "VMA_EXTERNAL_MEMORY", bool, "Enable external memory support" }, |
| 20 | + .{ "VMA_EXTERNAL_MEMORY_WIN32", bool, "Enable Win32 external memory handle support" }, |
15 | 21 |
|
16 | | - if (b.option(std.Build.LazyPath, "vulkan-headers-path", "Path to Vulkan headers (defaults to bundled headers)")) |vulkan_headers_path| { |
17 | | - lib.addIncludePath(vulkan_headers_path); |
18 | | - if (install_vulkan_headers) lib.installHeadersDirectory(vulkan_headers_path, "", .{}); |
19 | | - } else { |
20 | | - if (b.lazyDependency("vulkan_headers", .{})) |vulkan_headers| { |
21 | | - lib.addIncludePath(vulkan_headers.namedLazyPath("vulkan-headers")); |
22 | | - if (install_vulkan_headers) lib.installHeadersDirectory(vulkan_headers.namedLazyPath("vulkan-headers"), "", .{}); |
| 22 | + .{ "VMA_DEBUG_ALWAYS_DEDICATED_MEMORY", bool, "Force every allocation into its own VkDeviceMemory" }, |
| 23 | + .{ "VMA_DEBUG_INITIALIZE_ALLOCATIONS", bool, "Fill new/destroyed allocations with a bit pattern" }, |
| 24 | + .{ "VMA_DEBUG_DETECT_CORRUPTION", bool, "Enable corruption detection (requires VMA_DEBUG_MARGIN > 0)" }, |
| 25 | + .{ "VMA_DEBUG_GLOBAL_MUTEX", bool, "Single global mutex protecting all entry points" }, |
| 26 | + .{ "VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT", bool, "Respect maxMemoryAllocationCount" }, |
| 27 | + .{ "VMA_DEBUG_DONT_EXCEED_HEAP_SIZE_WITH_ALLOCATION_SIZE", bool, "Refuse allocations exceeding heap size" }, |
| 28 | + .{ "VMA_MAPPING_HYSTERESIS_ENABLED", bool, "Enable mapping hysteresis to avoid frequent map/unmap" }, |
| 29 | + .{ "VMA_MIN_ALIGNMENT", i64, "Minimum alignment of all allocations in bytes (power of two)" }, |
| 30 | + .{ "VMA_DEBUG_MARGIN", i64, "Margin in bytes after every allocation for corruption detection" }, |
| 31 | + .{ "VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY", i64, "Override minimum bufferImageGranularity" }, |
| 32 | + .{ "VMA_SMALL_HEAP_MAX_SIZE", i64, "Max heap size in bytes to consider 'small'" }, |
| 33 | + .{ "VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE", i64, "Default block size in bytes for large heap allocations" }, |
| 34 | + }; |
| 35 | + |
| 36 | + const config_h = b.addConfigHeader(.{ .include_path = "vk_mem_alloc_config.h" }, .{}); |
| 37 | + |
| 38 | + inline for (options) |entry| { |
| 39 | + const name, const T, const desc = entry; |
| 40 | + if (b.option(T, name, desc)) |value| { |
| 41 | + config_h.addValue(name, T, value); |
23 | 42 | } |
24 | 43 | } |
25 | 44 |
|
26 | | - const upstream = b.dependency("VulkanMemoryAllocator", .{}); |
27 | | - lib.installHeader(upstream.path("include/vk_mem_alloc.h"), "vk_mem_alloc.h"); |
28 | | - lib.addIncludePath(upstream.path("include/")); |
| 45 | + const root_module = b.createModule(.{ |
| 46 | + .target = target, |
| 47 | + .optimize = optimize, |
| 48 | + .link_libcpp = true, |
| 49 | + }); |
29 | 50 |
|
30 | | - lib.addCSourceFile(.{ |
31 | | - .file = b.addWriteFiles().add("vk_mem_alloc.cpp", "#include \"vk_mem_alloc.h\""), |
32 | | - .flags = &.{ "-DVMA_IMPLEMENTATION", "-std=c++17" }, |
| 51 | + root_module.addCSourceFile(.{ |
| 52 | + .file = b.addWriteFiles().add("stub.cpp", |
| 53 | + \\#include "vk_mem_alloc_config.h" |
| 54 | + \\#define VMA_IMPLEMENTATION |
| 55 | + \\#include "vk_mem_alloc.h" |
| 56 | + ), |
| 57 | + .flags = &.{"-std=c++17"}, |
33 | 58 | }); |
34 | 59 |
|
35 | | - { |
36 | | - const allocPrint = std.fmt.allocPrint; |
37 | | - const bool_options = [_][2][]const u8{ |
38 | | - .{ "macro_static_vulkan_functions", "VMA_STATIC_VULKAN_FUNCTIONS" }, |
39 | | - .{ "macro_dynamic_vulkan_functions", "VMA_DYNAMIC_VULKAN_FUNCTIONS" }, |
40 | | - .{ "macro_stats_string_enabled", "VMA_STATS_STRING_ENABLED" }, |
41 | | - .{ "macro_debug_initialize_allocations", "VMA_DEBUG_INITIALIZE_ALLOCATIONS" }, |
42 | | - .{ "macro_debug_detect_corruption", "VMA_DEBUG_DETECT_CORRUPTION" }, |
43 | | - .{ "macro_debug_global_mutex", "VMA_DEBUG_GLOBAL_MUTEX" }, |
44 | | - .{ "macro_use_stl_shared_mutex", "VMA_USE_STL_SHARED_MUTEX" }, |
45 | | - .{ "macro_debug_always_dedicated_memory", "VMA_DEBUG_ALWAYS_DEDICATED_MEMORY" }, |
46 | | - .{ "macro_debug_dont_exceed_max_memory_allocation_count", "VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT" }, |
47 | | - .{ "macro_mapping_hysteresis_enabled", "VMA_MAPPING_HYSTERESIS_ENABLED" }, |
48 | | - }; |
49 | | - for (bool_options) |bool_option| { |
50 | | - if (b.option(bool, bool_option[0], "")) |opt| |
51 | | - lib.root_module.addCMacro(bool_option[1], try allocPrint(b.allocator, "{}", .{@intFromBool(opt)})); |
52 | | - } |
| 60 | + root_module.addIncludePath(config_h.getOutputDir()); |
53 | 61 |
|
54 | | - if (b.option(i64, "macro_debug_min_buffer_image_granularity", "")) |opt| |
55 | | - lib.root_module.addCMacro("VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY", try allocPrint(b.allocator, "{}", .{opt})); |
56 | | - if (b.option(i64, "macro_debug_margin", "")) |opt| |
57 | | - lib.root_module.addCMacro("VMA_DEBUG_MARGIN", try allocPrint(b.allocator, "{}", .{opt})); |
58 | | - if (b.option(i64, "macro_min_alignment", "")) |opt| |
59 | | - lib.root_module.addCMacro("VMA_MIN_ALIGNMENT", try allocPrint(b.allocator, "{}", .{opt})); |
| 62 | + const upstream = b.dependency("VulkanMemoryAllocator", .{}); |
| 63 | + root_module.addIncludePath(upstream.path("include/")); |
60 | 64 |
|
61 | | - if (b.option(u64, "macro_small_heap_max_size", "")) |opt| |
62 | | - lib.root_module.addCMacro("VMA_SMALL_HEAP_MAX_SIZE", try allocPrint(b.allocator, "{}ull", .{opt})); |
63 | | - if (b.option(u64, "macro_default_large_heap_block_size", "")) |opt| |
64 | | - lib.root_module.addCMacro("VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE", try allocPrint(b.allocator, "{}ull", .{opt})); |
| 65 | + if (b.option(std.Build.LazyPath, "vulkan-include-path", "Path to Vulkan headers")) |vulkan_include_path| { |
| 66 | + root_module.addIncludePath(vulkan_include_path); |
65 | 67 |
|
66 | | - if (b.option([]const u8, "macro_null", "")) |opt| |
67 | | - lib.root_module.addCMacro("VMA_NULL", opt); |
68 | | - if (b.option([]const u8, "macro_configuration_user_includes_h", "")) |opt| |
69 | | - lib.root_module.addCMacro("VMA_CONFIGURATION_USER_INCLUDES_H", try allocPrint(b.allocator, "\"{s}\"", .{opt})); |
| 68 | + const lib = b.addLibrary(.{ |
| 69 | + .name = "VulkanMemoryAllocator", |
| 70 | + .linkage = b.option(std.builtin.LinkMode, "linkage", "defaults to static") orelse .static, |
| 71 | + .root_module = root_module, |
| 72 | + }); |
| 73 | + b.installArtifact(lib); |
| 74 | + lib.installHeader(upstream.path("include/vk_mem_alloc.h"), "vk_mem_alloc.h"); |
| 75 | + lib.installHeader(config_h.getOutputFile(), "vk_mem_alloc_config.h"); |
| 76 | + } else { |
| 77 | + const fail = b.addFail("missing vulkan headers, specify a path to vulkan headers using the vulkan-include-path option"); |
| 78 | + b.getInstallStep().dependOn(&fail.step); |
70 | 79 | } |
71 | | - |
72 | | - b.installArtifact(lib); |
73 | 80 | } |
0 commit comments