|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +pub fn build(b: *std.Build) void { |
| 4 | + const upstream = b.dependency("nativefiledialog-extended", .{}); |
| 5 | + const target = b.standardTargetOptions(.{}); |
| 6 | + const optimize = b.standardOptimizeOption(.{}); |
| 7 | + |
| 8 | + const strip = b.option(bool, "strip", "Omit debug information"); |
| 9 | + const pic = b.option(bool, "pie", "Produce Position Independent Code"); |
| 10 | + |
| 11 | + const portal = b.option(bool, "portal", "Use xdg-desktop-portal instead of GTK") orelse false; |
| 12 | + const use_allowedcontenttypes_if_available = b.option(bool, "use-allowedcontenttypes-if-available", "Use allowedContentTypes for filter lists on macOS >= 11.0") orelse true; |
| 13 | + const append_extension = b.option(bool, "append-extension", "Automatically append file extension to an extensionless selection in SaveDialog()") orelse false; |
| 14 | + |
| 15 | + const nfd = b.addStaticLibrary(.{ |
| 16 | + .name = "nfd", |
| 17 | + .target = target, |
| 18 | + .optimize = optimize, |
| 19 | + .pic = pic, |
| 20 | + .strip = strip, |
| 21 | + }); |
| 22 | + b.installArtifact(nfd); |
| 23 | + nfd.addIncludePath(upstream.path("src/include")); |
| 24 | + nfd.installHeadersDirectory(upstream.path("src/include"), "", .{ .include_extensions = &.{ ".h", ".hpp" } }); |
| 25 | + if (target.result.os.tag == .windows) { |
| 26 | + nfd.linkLibCpp(); |
| 27 | + nfd.addCSourceFile(.{ .file = upstream.path("src/nfd_win.cpp") }); |
| 28 | + nfd.linkSystemLibrary("ole32"); |
| 29 | + nfd.linkSystemLibrary("uuid"); |
| 30 | + nfd.linkSystemLibrary("shell32"); |
| 31 | + } else if (target.result.os.tag.isDarwin()) { |
| 32 | + nfd.addCSourceFile(.{ .file = upstream.path("src/nfd_cocoa.m") }); |
| 33 | + nfd.linkFramework("AppKit"); |
| 34 | + // Whether this is correct is completely untested since I don't use macOS. |
| 35 | + nfd.root_module.addCMacro("NFD_MACOS_ALLOWEDCONTENTTYPES", if (use_allowedcontenttypes_if_available) "1" else "0"); |
| 36 | + if (use_allowedcontenttypes_if_available and target.result.os.isAtLeast(.macos, .{ .major = 11, .minor = 0, .patch = 0 }).?) { |
| 37 | + nfd.linkFramework("UniformTypeIdentifiers"); |
| 38 | + } |
| 39 | + } else { |
| 40 | + nfd.linkLibCpp(); |
| 41 | + if (append_extension) nfd.root_module.addCMacro("NFD_APPEND_EXTENSION", "1"); |
| 42 | + if (portal) { |
| 43 | + nfd.addCSourceFile(.{ .file = upstream.path("src/nfd_portal.cpp") }); |
| 44 | + nfd.linkSystemLibrary("dbus-1"); |
| 45 | + nfd.root_module.addCMacro("NFD_PORTAL", "1"); |
| 46 | + } else { |
| 47 | + nfd.addCSourceFile(.{ .file = upstream.path("src/nfd_gtk.cpp") }); |
| 48 | + nfd.linkSystemLibrary("gtk+-3.0"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + const install_tests_step = b.step("install-tests", "Install all test executables"); |
| 53 | + |
| 54 | + for (test_sources) |sub_path| { |
| 55 | + const name = b.dupe(std.fs.path.stem(sub_path)); |
| 56 | + std.mem.replaceScalar(u8, name, '.', '-'); |
| 57 | + std.mem.replaceScalar(u8, name, '_', '-'); |
| 58 | + |
| 59 | + const test_exe = b.addExecutable(.{ |
| 60 | + .name = name, |
| 61 | + .target = target, |
| 62 | + .optimize = optimize, |
| 63 | + .pic = pic, |
| 64 | + .strip = strip, |
| 65 | + }); |
| 66 | + test_exe.addCSourceFile(.{ .file = upstream.path("test").path(b, sub_path) }); |
| 67 | + test_exe.linkLibrary(nfd); |
| 68 | + |
| 69 | + install_tests_step.dependOn(&b.addInstallArtifact(test_exe, .{}).step); |
| 70 | + |
| 71 | + const run_test_exe = b.addRunArtifact(test_exe); |
| 72 | + |
| 73 | + const test_step = b.step(name, b.fmt("Run {s}", .{sub_path})); |
| 74 | + test_step.dependOn(&run_test_exe.step); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +const test_sources: []const []const u8 = &.{ |
| 79 | + "test_opendialog.c", |
| 80 | + "test_opendialog_cpp.cpp", |
| 81 | + "test_opendialog_native.c", |
| 82 | + "test_opendialog_with.c", |
| 83 | + "test_opendialog_native_with.c", |
| 84 | + "test_opendialogmultiple.c", |
| 85 | + "test_opendialogmultiple_cpp.cpp", |
| 86 | + "test_opendialogmultiple_native.c", |
| 87 | + "test_opendialogmultiple_enum.c", |
| 88 | + "test_opendialogmultiple_enum_native.c", |
| 89 | + "test_pickfolder.c", |
| 90 | + "test_pickfolder_cpp.cpp", |
| 91 | + "test_pickfolder_native.c", |
| 92 | + "test_pickfolder_with.c", |
| 93 | + "test_pickfolder_native_with.c", |
| 94 | + "test_pickfoldermultiple.c", |
| 95 | + "test_pickfoldermultiple_native.c", |
| 96 | + "test_savedialog.c", |
| 97 | + "test_savedialog_native.c", |
| 98 | + "test_savedialog_with.c", |
| 99 | + "test_savedialog_native_with.c", |
| 100 | +}; |
0 commit comments