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