What I’m currently doing is making me unhappy.
hip.h:
#ifndef HIP
#define HIP
#include <stdint.h>
typedef void *HipStream;
#ifdef __cplusplus
extern "C" {
#endif
uint32_t add(uint32_t a, uint32_t b);
uint32_t createHipStream(HipStream *stream);
void destroyHipStream(HipStream stream);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // HIP
hip.cpp:
#include "hip.h"
#include <hip/hip_runtime.h>
uint32_t add(uint32_t a, uint32_t b) { return a + b; }
uint32_t createHipStream(HipStream *stream) {
hipStream_t hip_stream = nullptr;
const hipError_t error = hipStreamCreate(&hip_stream);
if (error != hipSuccess) {
return 1;
}
*stream = (void *)hip_stream;
return 0;
}
void destroyHipStream(HipStream stream) {
static_cast<void>(hipStreamDestroy((hipStream_t)stream));
}
The cpp code gets compiled into an object file with hipcc, that I link to and I also use the header in translate c:
const hip_translated_c = b.addTranslateC(.{
.root_source_file = b.path("libs/hip/hip.h"),
.optimize = optimize,
.target = target,
.link_libc = true,
});
const hip_library = b.addLibrary(.{
.name = "hip",
.root_module = init: {
const hip_module = b.createModule(.{
.optimize = optimize,
.target = target,
.link_libcpp = true,
});
hip_module.addObjectFile(b.path("libs/hip/hip.o"));
break :init hip_module;
},
.linkage = .static,
});
b.installArtifact(hip_library);
const exe = b.addExecutable(.{
.name = "ZigWithHip",
.root_module = init: {
const exe_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "hip", .module = hip_translated_c.createModule() },
},
});
exe_module.linkLibrary(hip_library);
exe_module.linkSystemLibrary("amdhip64", .{});
break :init exe_module;
},
});
b.installArtifact(exe);
// ETC..
Any tips on how to make the cpp side better so it translates “nicely” to zig? I tried to avoid having any HIP stuff in the outfacing headers by casting to void ptrs, but should I should I just include the HIP header in the hip.h so they also get translated? Any examples or links are greatly appreciated.
Thank you!