Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cimport, error: dependency loop detected #18247

Open
jimying opened this issue Dec 10, 2023 · 3 comments
Open

cimport, error: dependency loop detected #18247

jimying opened this issue Dec 10, 2023 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@jimying
Copy link
Contributor

jimying commented Dec 10, 2023

Zig Version

0.12.0-dev.1808+69195d0cd

Steps to Reproduce and Observed Behavior

I want to call pjsip(open source: https://github.com/pjsip/pjproject) library in zig.

Here my simple sample : https://github.com/jimying/testpj.zig

steps to build sample (tested in linux x86_64):

# 1. install pjsip library
sh install_pjsip.sh

# 2. build, this step is fail, `error: dependency loop detected`.
zig build 

Expected Behavior

build and run ok

@jimying jimying added the bug Observed behavior contradicts documented or intended behavior label Dec 10, 2023
@jimying
Copy link
Contributor Author

jimying commented Jan 3, 2024

I build success by modify the declare sequeue and works fine.

At the beginning, I suspected that it's a problem with translate-c. But i compare the cimport.zig files, it's same!
This is a very special problem, hope this sample can help to debug.

Follow is the modify patch:

     // problem1: test memory pool  (error: dependency loop detected)
+    var pool: [*c]c.pj_pool_t = undefined;
     var cp: c.pj_caching_pool = undefined;
     c.pj_caching_pool_init(&cp, null, 0);

-    const pool = c.pj_pool_create(&cp.factory, "test", 1000, 1000, null);
+    pool = c.pj_pool_create(&cp.factory, "test", 1000, 1000, null);
     _ = c.pj_pool_alloc(pool, 100);
     c.pj_pool_release(pool);

@sphaerophoria
Copy link

Hit this while trying to use miniaudio. Found a stripped down repro which might be helpful

test.h

struct A;
typedef void (*function_callback)(struct A*);

struct A {
	function_callback callback;
};

struct B {
	function_callback callback;
};

.
test.zig

const c = @cImport({
    @cInclude("test.h");
});

pub fn main() void {
    const v: c.B = undefined;
    _ = v;
}

Run with zig run -I. test.zig

  • If we try to instantiate c.A instead of c.B, then it is not a problem
  • If we replace struct A* with void* in the function callback it is not a problem

Observed on zig 0.12.0 and nightly zig-linux-x86_64-0.13.0-dev.75+5c9eb4081

sphaerophoria added a commit to sphaerophoria/video-editor that referenced this issue May 13, 2024
Implement audio playback, hopefully motivation is self explanatory

The audio landscape is vast in linux, we have alsa, pulse, jack,
pipewire and alsa, pulse, jack interfaces into pipewire, alsa interfaces
into pulse, etc. Instead of trying to deal with this ourselves, pull in
an audio lib to deal with it for us

Just skimming online, miniaudio fits well. PortAudio and libsoundio also
seem to both be good candidates. No strong reasoning went into this
choice, the single header impl of miniaudio felt easy to work with.

We were tricked though, with the default miniaudio impl we hit a zig
compiler bug that triggers circular dependencies. Something about
using a struct pointer in a function pointer stored by the struct, see
ziglang/zig#18247 (comment)

Patch miniaudio to just use void pointers instead, this doesn't seem to
have any negative effects

Add an audio player abstraction that just injects a sin wave for now,
add test binary to try it

Potentially related issues...
ziglang/zig#12325
ziglang/zig#16419
sphaerophoria added a commit to sphaerophoria/video-editor that referenced this issue May 13, 2024
Implement audio playback, hopefully motivation is self explanatory

The audio landscape is vast in linux, we have alsa, pulse, jack,
pipewire and alsa, pulse, jack interfaces into pipewire, alsa interfaces
into pulse, etc. Instead of trying to deal with this ourselves, pull in
an audio lib to deal with it for us

Just skimming online, miniaudio fits well. PortAudio and libsoundio also
seem to both be good candidates. No strong reasoning went into this
choice, the single header impl of miniaudio felt easy to work with.

We were tricked though, with the default miniaudio impl we hit a zig
compiler bug that triggers circular dependencies. Something about
using a struct pointer in a function pointer stored by the struct, see
ziglang/zig#18247 (comment)

Patch miniaudio to just use void pointers instead, this doesn't seem to
have any negative effects

Add an audio player abstraction that just injects a sin wave for now,
add test binary to try it

Potentially related issues...
ziglang/zig#12325
ziglang/zig#16419
ziglang/zig#19392
sphaerophoria added a commit to sphaerophoria/video-editor that referenced this issue May 14, 2024
Implement audio playback, hopefully motivation is self explanatory

The audio landscape is vast in linux, we have alsa, pulse, jack,
pipewire and alsa, pulse, jack interfaces into pipewire, alsa interfaces
into pulse, etc. Instead of trying to deal with this ourselves, pull in
an audio lib to deal with it for us

Just skimming online, miniaudio fits well. PortAudio and libsoundio also
seem to both be good candidates. No strong reasoning went into this
choice, the single header impl of miniaudio felt easy to work with.

We were tricked though, with the default miniaudio impl we hit a zig
compiler bug that triggers circular dependencies. Something about
using a struct pointer in a function pointer stored by the struct, see
ziglang/zig#18247 (comment)

Patch miniaudio to just use void pointers instead, this doesn't seem to
have any negative effects

Extract audio frames from ffmepg, and feed them into our audio
subsystem. Not a ton to say here, everything is just using the APIs
provided by miniaudio and ffmpeg

Replace test video with a 20s segment of big buck bunny

Potentially related issues...
ziglang/zig#12325
ziglang/zig#16419
ziglang/zig#19392
@andrewrk andrewrk added the translate-c C to Zig source translation feature (@cImport) label Aug 15, 2024
@andrewrk andrewrk added this to the unplanned milestone Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

No branches or pull requests

4 participants
@andrewrk @sphaerophoria @jimying and others