Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
zig-cache/
.zig-cache/
zig-out/
zig-pkg/
60 changes: 36 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
# OpenAssetImporter Library Binding for Zig

This repo is a build sdk for [Assimp](https://github.com/assimp/assimp) to be used with the Zig build system:
This repo packages [Assimp](https://github.com/assimp/assimp) 5.4.0 for the Zig build system (requires Zig 0.16.0+).

## Add the dependency

```sh
zig fetch --save git+https://github.com/allyourcodebase/assimp.git
```

Or add it manually to `build.zig.zon`:

```zig
.dependencies = .{
.zig_assimp = .{
.url = "https://github.com/allyourcodebase/assimp/archive/<commit>.tar.gz",
.hash = "...",
},
},
```

## Use it in `build.zig`

```zig
const std = @import("std");

// Import the SDK
const Assimp = @import("Sdk.zig");

pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions();

const exe = b.addExecutable("static-example", null);
exe.setBuildMode(mode);
exe.addCSourceFile("src/example.cpp", &[_][]const u8{"-std=c++17"});
exe.linkLibC();
exe.linkLibCpp();
exe.install();

// Create a new instance
var sdk = Assimp.init(b);

// And link Assimp statically to our exe and enable a default set of
// formats.
sdk.addTo(exe, .static, Assimp.FormatSet.default);
}
```
const assimp_dep = b.dependency("zig_assimp", .{
.target = target,
.optimize = optimize,
.formats = @as([]const u8, "STL,Obj,FBX"), // or "all"
.double = false,
});

exe.linkLibrary(assimp_dep.artifact("assimp"));
```

Headers (including the generated `assimp/config.h`) are installed on the artifact, so `linkLibrary` is enough — no extra include paths needed.

## Options

- `formats` — comma-separated list of importers/exporters to compile, or `all`. Empty (default) builds none. See `build.zig` for the full list.
- `double` — store data as `double` instead of `float`.
29 changes: 10 additions & 19 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
const std = @import("std");
const builtin = @import("builtin");

const zig_version = builtin.zig_version;
fn lazy_from_path(path_chars: []const u8, owner: *std.Build) std.Build.LazyPath {
if (zig_version.major > 0 or zig_version.minor >= 13) {
return std.Build.LazyPath{ .src_path = .{ .sub_path = path_chars, .owner = owner } };
} else if (zig_version.minor >= 12) {
return std.Build.LazyPath{ .path = path_chars };
} else unreachable;
}

pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
Expand All @@ -32,9 +22,9 @@ pub fn build(b: *std.Build) !void {
lib.root_module.addCMacro("OPENDDL_STATIC_LIBARY", "");
}

lib.linkLibC();
lib.root_module.link_libc = true;
if (target.result.abi != .msvc) {
lib.linkLibCpp();
lib.root_module.link_libcpp = true;
}

const config_h = b.addConfigHeader(
Expand All @@ -46,7 +36,7 @@ pub fn build(b: *std.Build) !void {
);
lib.root_module.addConfigHeader(config_h);
lib.root_module.addIncludePath(assimp.path("include"));
lib.root_module.addIncludePath(lazy_from_path("include", b));
lib.root_module.addIncludePath(b.path("include"));

lib.root_module.addIncludePath(assimp.path(""));
lib.root_module.addIncludePath(assimp.path("contrib"));
Expand All @@ -56,6 +46,7 @@ pub fn build(b: *std.Build) !void {
lib.root_module.addIncludePath(assimp.path("contrib/unzip"));
lib.root_module.addIncludePath(assimp.path("contrib/zlib"));
lib.root_module.addIncludePath(assimp.path("contrib/openddlparser/include"));
lib.root_module.addIncludePath(assimp.path("contrib/utf8cpp/source"));

lib.root_module.addCMacro("RAPIDJSON_HAS_STDSTRING", "1");

Expand All @@ -67,7 +58,7 @@ pub fn build(b: *std.Build) !void {
);

lib.installHeadersDirectory(
lazy_from_path("include", b),
b.path("include"),
"",
.{ .include_extensions = &.{ ".h", ".inl", ".hpp" } },
);
Expand Down Expand Up @@ -152,10 +143,10 @@ pub fn build(b: *std.Build) !void {
.files = &[_][]const u8{"src/example.cpp"},
.flags = &[_][]const u8{"-std=c++17"},
});
example_cpp.linkLibrary(lib);
example_cpp.linkLibC();
example_cpp.root_module.linkLibrary(lib);
example_cpp.root_module.link_libc = true;
if (target.result.abi != .msvc) {
example_cpp.linkLibCpp();
example_cpp.root_module.link_libcpp = true;
}
example_cpp.root_module.addIncludePath(assimp.path("include"));
if (target.result.os.tag == .windows) {
Expand All @@ -175,8 +166,8 @@ pub fn build(b: *std.Build) !void {
.files = &[_][]const u8{"src/example.c"},
.flags = &[_][]const u8{"-std=c99"},
});
example_c.linkLibrary(lib);
example_c.linkLibC();
example_c.root_module.linkLibrary(lib);
example_c.root_module.link_libc = true;
example_c.root_module.addIncludePath(assimp.path("include"));
if (target.result.os.tag == .windows) {
example_c.root_module.addCMacro("_WINDOWS", "");
Expand Down
8 changes: 4 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = .zig_assimp,
.version = "5.3.1",
.version = "5.4.0",
.fingerprint = 0x3e92a7cfbee36ef8,
.paths = .{
"build.zig",
Expand All @@ -10,11 +10,11 @@
"include",
"src",
},
.minimum_zig_version = "0.15.1",
.minimum_zig_version = "0.16.0",
.dependencies = .{
.assimp = .{
.url = "https://github.com/assimp/assimp/archive/v5.3.1.tar.gz",
.hash = "N-V-__8AACI1agiq-JJtHIb-h7Td-FFZ-HgD1WOpv1g_Vl3w",
.url = "https://github.com/assimp/assimp/archive/v5.4.0.tar.gz",
.hash = "N-V-__8AACoWewhH5OBlKGMhvR9XlApEzxGY07K8ZCn7TlH9",
},
},
}