Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
115 lines (97 loc) · 4.33 KB
/
Copy pathbuild.zig
File metadata and controls
115 lines (97 loc) · 4.33 KB
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
const std = @import("std");
const PapiConfig = struct {
include_path: ?[]const u8,
lib_path: ?[]const u8,
use_pkg_config: bool,
add_rpath: bool,
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const papi = resolvePapiConfig(b);
const papi_module = b.addModule("zig_papi", .{
.root_source_file = b.path("src/papi.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
configurePapiModule(papi_module, papi);
_ = addExample(b, papi_module, target, optimize, "basic_ipc", "examples/basic_ipc.zig", "run-example", "Run the basic IPC example");
_ = addExample(b, papi_module, target, optimize, "load_store", "examples/load_store.zig", "run-load-store", "Run the load/store example");
_ = addExample(b, papi_module, target, optimize, "native_cache_misses", "examples/native_cache_misses.zig", "run-native-cache-misses", "Run the native cache-miss example");
_ = addExample(b, papi_module, target, optimize, "availability", "examples/availability.zig", "run-availability", "Run the availability example");
const tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/papi.zig"),
.target = target,
.optimize = optimize,
}),
});
configurePapiModule(tests.root_module, papi);
const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run the wrapper tests");
test_step.dependOn(&run_tests.step);
}
fn resolvePapiConfig(b: *std.Build) PapiConfig {
const prefix_option = b.option([]const u8, "papi-prefix", "Path to the local PAPI install prefix");
const include_option = b.option([]const u8, "papi-include", "Path to the directory containing papi.h");
const lib_option = b.option([]const u8, "papi-lib", "Path to the directory containing libpapi.so");
const has_explicit_paths = prefix_option != null or include_option != null or lib_option != null;
const use_pkg_config = b.option(bool, "papi-use-pkg-config", "Use pkg-config to find PAPI instead of the explicit include/lib paths") orelse !has_explicit_paths;
const add_rpath = b.option(bool, "papi-rpath", "Embed the selected PAPI library directory into built artifacts") orelse true;
const default_prefix = blk: {
const home = b.graph.env_map.get("HOME") orelse @panic("HOME is not set; pass -Dpapi-prefix=/path/to/papi explicitly");
break :blk b.fmt("{s}/.local/papi", .{home});
};
const prefix = if (use_pkg_config) null else prefix_option orelse default_prefix;
return .{
.include_path = include_option orelse if (prefix) |value| b.fmt("{s}/include", .{value}) else null,
.lib_path = lib_option orelse if (prefix) |value| b.fmt("{s}/lib", .{value}) else null,
.use_pkg_config = use_pkg_config,
.add_rpath = add_rpath,
};
}
fn configurePapiModule(module: *std.Build.Module, config: PapiConfig) void {
module.link_libc = true;
if (config.include_path) |include_path| {
module.addIncludePath(.{ .cwd_relative = include_path });
}
if (config.lib_path) |lib_path| {
module.addLibraryPath(.{ .cwd_relative = lib_path });
if (config.add_rpath) {
module.addRPath(.{ .cwd_relative = lib_path });
}
}
module.linkSystemLibrary("papi", .{
.use_pkg_config = if (config.use_pkg_config) .yes else .no,
.preferred_link_mode = .dynamic,
.search_strategy = .paths_first,
});
}
fn addExample(
b: *std.Build,
papi_module: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
name: []const u8,
path: []const u8,
run_step_name: []const u8,
run_step_description: []const u8,
) *std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = name,
.root_module = b.createModule(.{
.root_source_file = b.path(path),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "zig_papi", .module = papi_module },
},
}),
});
b.installArtifact(exe);
const run_exe = b.addRunArtifact(exe);
const run_step = b.step(run_step_name, run_step_description);
run_step.dependOn(&run_exe.step);
return exe;
}