|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +const rmq_version: std.SemanticVersion = .{ |
| 4 | + .major = 0, |
| 5 | + .minor = 15, |
| 6 | + .patch = 0, |
| 7 | +}; |
| 8 | + |
| 9 | +pub fn build(b: *std.Build) void { |
| 10 | + const target = b.standardTargetOptions(.{}); |
| 11 | + const optimize = b.standardOptimizeOption(.{}); |
| 12 | + |
| 13 | + const ssl = b.option(bool, "ssl", "Build with SSL support") orelse false; |
| 14 | + const shared = b.option(bool, "shared", "Build shared library") orelse false; |
| 15 | + const examples = b.option(bool, "examples", "Build examples") orelse false; |
| 16 | + |
| 17 | + const generated_export_header = b.addWriteFile("rabbitmq-c/export.h", export_h); |
| 18 | + |
| 19 | + const lib = if (shared) b.addSharedLibrary(.{ |
| 20 | + .name = "rabbitmq-c", |
| 21 | + .target = target, |
| 22 | + .optimize = optimize, |
| 23 | + .version = rmq_version, |
| 24 | + }) else b.addStaticLibrary(.{ |
| 25 | + .name = "rabbitmq-c-static", |
| 26 | + .target = target, |
| 27 | + .optimize = optimize, |
| 28 | + .version = rmq_version, |
| 29 | + }); |
| 30 | + if (!shared) { |
| 31 | + lib.pie = true; |
| 32 | + lib.defineCMacro("AMQP_STATIC", ""); |
| 33 | + } |
| 34 | + const configH = b.addConfigHeader(.{ |
| 35 | + .style = .blank, |
| 36 | + .include_path = "config.h", |
| 37 | + }, .{ |
| 38 | + .HAVE_SELECT = if (lib.rootModuleTarget().os.tag == .windows) {} else null, |
| 39 | + .HAVE_POLL = if (lib.rootModuleTarget().os.tag != .windows) {} else null, |
| 40 | + .AMQ_PLATFORM = switch (lib.rootModuleTarget().os.tag) { |
| 41 | + .linux => "Linux", |
| 42 | + .macos => "Darwin", |
| 43 | + .windows => "Win32", |
| 44 | + else => @panic("Unsupported platform"), |
| 45 | + }, |
| 46 | + .ENABLE_SSL_ENGINE_API = if (ssl) {} else null, |
| 47 | + }); |
| 48 | + lib.defineCMacro("HAVE_CONFIG_H", null); |
| 49 | + lib.addConfigHeader(configH); |
| 50 | + lib.addIncludePath(generated_export_header.getDirectory()); |
| 51 | + lib.addIncludePath(b.path("include")); |
| 52 | + lib.addIncludePath(b.path("librabbitmq")); |
| 53 | + if (lib.rootModuleTarget().os.tag == .windows) { |
| 54 | + lib.addIncludePath(b.path("librabbitmq/win32")); |
| 55 | + lib.addCSourceFile(.{ |
| 56 | + .file = b.path("librabbitmq/win32/threads.c"), |
| 57 | + }); |
| 58 | + lib.linkSystemLibrary("ws2_32"); |
| 59 | + } else lib.addIncludePath(b.path("librabbitmq/unix")); |
| 60 | + lib.addCSourceFiles(.{ |
| 61 | + .root = b.path("librabbitmq"), |
| 62 | + .files = &.{ |
| 63 | + "amqp_api.c", "amqp_connection.c", "amqp_consumer.c", "amqp_framing.c", |
| 64 | + "amqp_mem.c", "amqp_socket.c", "amqp_table.c", "amqp_tcp_socket.c", |
| 65 | + "amqp_time.c", "amqp_url.c", |
| 66 | + }, |
| 67 | + }); |
| 68 | + if (ssl) { |
| 69 | + lib.addCSourceFiles(.{ |
| 70 | + .root = b.path("librabbitmq"), |
| 71 | + .files = &.{ |
| 72 | + "amqp_openssl.c", |
| 73 | + "amqp_openssl_bio.c", |
| 74 | + }, |
| 75 | + }); |
| 76 | + lib.linkSystemLibrary("ssl"); |
| 77 | + lib.linkSystemLibrary("crypto"); |
| 78 | + } |
| 79 | + lib.linkLibC(); |
| 80 | + lib.installHeadersDirectory(b.path("include"), "", .{}); |
| 81 | + b.installArtifact(lib); |
| 82 | + |
| 83 | + if (examples) { |
| 84 | + inline for (&.{ |
| 85 | + "amqp_bind.c", |
| 86 | + if (ssl) |
| 87 | + "amqp_ssl_connect.c" |
| 88 | + else |
| 89 | + "amqp_connect_timeout.c", |
| 90 | + "amqp_consumer.c", |
| 91 | + "amqp_exchange_declare.c", |
| 92 | + "amqp_listen.c", |
| 93 | + "amqp_listenq.c", |
| 94 | + "amqp_producer.c", |
| 95 | + "amqp_rpc_sendstring_client.c", |
| 96 | + "amqp_sendstring.c", |
| 97 | + "amqp_unbind.c", |
| 98 | + }) |file| { |
| 99 | + buildExamples(b, .{ |
| 100 | + .name = file[0 .. file.len - 2], |
| 101 | + .filepaths = &.{file}, |
| 102 | + .target = target, |
| 103 | + .optimize = optimize, |
| 104 | + .lib = lib, |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +const buildOptions = struct { |
| 111 | + name: []const u8, |
| 112 | + target: std.Build.ResolvedTarget, |
| 113 | + optimize: std.builtin.OptimizeMode, |
| 114 | + filepaths: []const []const u8, |
| 115 | + lib: *std.Build.Step.Compile, |
| 116 | +}; |
| 117 | + |
| 118 | +fn buildExamples(b: *std.Build, options: buildOptions) void { |
| 119 | + const example = b.addExecutable(.{ |
| 120 | + .name = options.name, |
| 121 | + .target = options.target, |
| 122 | + .optimize = options.optimize, |
| 123 | + }); |
| 124 | + for (options.lib.root_module.include_dirs.items) |include_dir| { |
| 125 | + example.root_module.include_dirs.append(b.allocator, include_dir) catch unreachable; |
| 126 | + } |
| 127 | + example.addIncludePath(b.path("examples")); |
| 128 | + example.addCSourceFile(.{ |
| 129 | + .file = b.path("examples/utils.c"), |
| 130 | + }); |
| 131 | + if (example.rootModuleTarget().os.tag == .windows) |
| 132 | + example.addCSourceFile(.{ |
| 133 | + .file = b.path("examples/win32/platform_utils.c"), |
| 134 | + }) |
| 135 | + else |
| 136 | + example.addCSourceFile(.{ |
| 137 | + .file = b.path("examples/unix/platform_utils.c"), |
| 138 | + }); |
| 139 | + example.addCSourceFiles(.{ |
| 140 | + .root = b.path("examples"), |
| 141 | + .files = options.filepaths, |
| 142 | + }); |
| 143 | + example.linkLibrary(options.lib); |
| 144 | + example.linkLibC(); |
| 145 | + b.installArtifact(example); |
| 146 | +} |
| 147 | + |
| 148 | +const export_h = |
| 149 | + \\ #ifndef RABBITMQ_C_EXPORT_H |
| 150 | + \\ #define RABBITMQ_C_EXPORT_H |
| 151 | + \\ |
| 152 | + \\ #ifdef AMQP_STATIC |
| 153 | + \\ # define AMQP_EXPORT |
| 154 | + \\ # define AMQP_NO_EXPORT |
| 155 | + \\ #else |
| 156 | + \\ # ifndef AMQP_EXPORT |
| 157 | + \\ # ifdef rabbitmq_EXPORTS |
| 158 | + \\ /* We are building this library */ |
| 159 | + \\ # define AMQP_EXPORT __attribute__((visibility("default"))) |
| 160 | + \\ # else |
| 161 | + \\ /* We are using this library */ |
| 162 | + \\ # define AMQP_EXPORT __attribute__((visibility("default"))) |
| 163 | + \\ # endif |
| 164 | + \\ # endif |
| 165 | + \\ |
| 166 | + \\ # ifndef AMQP_NO_EXPORT |
| 167 | + \\ # define AMQP_NO_EXPORT __attribute__((visibility("hidden"))) |
| 168 | + \\ # endif |
| 169 | + \\ #endif |
| 170 | + \\ |
| 171 | + \\ #ifndef AMQP_DEPRECATED |
| 172 | + \\ # define AMQP_DEPRECATED __attribute__ ((__deprecated__)) |
| 173 | + \\ #endif |
| 174 | + \\ |
| 175 | + \\ #ifndef AMQP_DEPRECATED_EXPORT |
| 176 | + \\ # define AMQP_DEPRECATED_EXPORT AMQP_EXPORT AMQP_DEPRECATED |
| 177 | + \\ #endif |
| 178 | + \\ |
| 179 | + \\ #ifndef AMQP_DEPRECATED_NO_EXPORT |
| 180 | + \\ # define AMQP_DEPRECATED_NO_EXPORT AMQP_NO_EXPORT AMQP_DEPRECATED |
| 181 | + \\ #endif |
| 182 | + \\ |
| 183 | + \\/* NOLINTNEXTLINE(readability-avoid-unconditional-preprocessor-if) */ |
| 184 | + \\ #if 0 /* DEFINE_NO_DEPRECATED */ |
| 185 | + \\ # ifndef AMQP_NO_DEPRECATED |
| 186 | + \\ # define AMQP_NO_DEPRECATED |
| 187 | + \\ # endif |
| 188 | + \\ #endif |
| 189 | + \\ |
| 190 | + \\ #endif /* RABBITMQ_C_EXPORT_H */ |
| 191 | + \\ |
| 192 | +; |
0 commit comments