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

c project depends on pure c module failed to build #18493

Closed
binarycraft007 opened this issue Jan 9, 2024 · 5 comments · Fixed by #18622
Closed

c project depends on pure c module failed to build #18493

binarycraft007 opened this issue Jan 9, 2024 · 5 comments · Fixed by #18622
Labels
bug Observed behavior contradicts documented or intended behavior zig build system
Milestone

Comments

@binarycraft007
Copy link
Contributor

binarycraft007 commented Jan 9, 2024

Zig Version

0.12.0-dev.2076+8fd15c6ca

Steps to Reproduce and Observed Behavior

  1. Clone this minimal example: https://github.com/binarycraft007/c-module
  2. Run zig build
  3. Error output:
❯ zig build 
install
└─ install c-module
   └─ zig build-exe c-module Debug native failure
error: error: module 'main' depends on non-existent module 'add'

error: the following command exited with error code 1:
/usr/lib/zig/zig build-exe /home/user/c-module/src/main.c -ODebug --dep add /home/user/c-module/src/add.c -ODebug -I /home/user/.cache/zig/p/1220316855f1b90cb82a7c88fcf7d432450da9b2b62efdf9393a43b9b39e26856337/include -lc --cache-dir /home/user/c-module/zig-cache --global-cache-dir /home/user/.cache/zig --name c-module --listen=- 
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install c-module transitive failure
   └─ zig build-exe c-module Debug native failure
error: the following build command failed with exit code 1:
/home/user/c-module/zig-cache/o/2a7708358059b7f7aa43b7425266779e/build /usr/lib/zig/zig /home/user/c-module /home/user/c-module/zig-cache /home/user/.cache/zig --seed 0x4e13448

Expected Behavior

Building should succeed.

@binarycraft007 binarycraft007 added the bug Observed behavior contradicts documented or intended behavior label Jan 9, 2024
@matu3ba
Copy link
Contributor

matu3ba commented Jan 10, 2024

Looks like your .zon file in the dependency is missing the include directory https://github.com/binarycraft007/add/blob/main/build.zig.zon:

 .paths = .{
        "build.zig",
        "build.zig.zon",
        "src",
        "LICENSE",
        //"README.md",
    },

Is that intentional?

@binarycraft007
Copy link
Contributor Author

binarycraft007 commented Jan 10, 2024

Looks like your .zon file in the dependency is missing the include directory https://github.com/binarycraft007/add/blob/main/build.zig.zon:

 .paths = .{
        "build.zig",
        "build.zig.zon",
        "src",
        "LICENSE",
        //"README.md",
    },

Is that intentional?

No it's not, fixed it, thanks for pointing it out, but that's not the problem why the error is reported though

@binarycraft007
Copy link
Contributor Author

judging from the cli, the build system assumed that pure c module without root_source_file is a zig dependency, that's not true, by removing --dep add from --dep add /home/user/c-module/src/add.c, the building succeeds.

@andrewrk
Copy link
Member

Note that the bug here is that the build system creates an invalid command line to give to the compiler. Your project tree, as it currently exists, is not intended to work without any changes. After my patch in #18622, this is the (correct) result:

install
└─ install
   └─ install c-module-zig
      └─ zig build-exe c-module-zig Debug native 2 errors
/home/andy/.cache/zig/p/122011958020e30f221da170a2143bac2e30720c42ff7e4e4e4f38914dd499533c4b/src/main.zig:1:11: error: C import failed
const c = @cImport(@cInclude("add.h"));
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    main: /home/andy/.cache/zig/p/122011958020e30f221da170a2143bac2e30720c42ff7e4e4e4f38914dd499533c4b/src/main.zig:4:5
    callMain: /home/andy/dev/zig/lib/std/start.zig:575:17
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
/home/andy/dev/contrib-testing/zig-cache/o/58784ff95a1be73debc82b2981973621/cimport.h:1:10: error: 'add.h' file not found
#include <add.h>
         ^
error: the following command failed with 2 compilation errors:
/home/andy/dev/zig/build-release/stage4/bin/zig build-exe -ODebug --dep add -Mroot=/home/andy/.cache/zig/p/122011958020e30f221da170a2143bac2e30720c42ff7e4e4e4f38914dd499533c4b/src/main.zig /home/andy/.cache/zig/p/1220e1a95d351aaa972dfc1ba07c5ffb3fa5893632c6b907a367de67bcaf6ba068e4/src/add.c -ODebug -I /home/andy/.cache/zig/p/1220e1a95d351aaa972dfc1ba07c5ffb3fa5893632c6b907a367de67bcaf6ba068e4/include -Madd -lc --cache-dir /home/andy/dev/contrib-testing/zig-cache --global-cache-dir /home/andy/.cache/zig --name c-module-zig --listen=- 
install
└─ install
   └─ install c-module
      └─ zig build-exe c-module Debug native 1 errors
/home/andy/.cache/zig/p/122011958020e30f221da170a2143bac2e30720c42ff7e4e4e4f38914dd499533c4b/src/main.c:1:10: error: 'add.h' file not found
#include "add.h"
         ^~~~~~~~
error: the following command failed with 1 compilation errors:
/home/andy/dev/zig/build-release/stage4/bin/zig build-exe /home/andy/.cache/zig/p/122011958020e30f221da170a2143bac2e30720c42ff7e4e4e4f38914dd499533c4b/src/main.c -ODebug --dep add -Mroot /home/andy/.cache/zig/p/1220e1a95d351aaa972dfc1ba07c5ffb3fa5893632c6b907a367de67bcaf6ba068e4/src/add.c -ODebug -I /home/andy/.cache/zig/p/1220e1a95d351aaa972dfc1ba07c5ffb3fa5893632c6b907a367de67bcaf6ba068e4/include -Madd -lc --cache-dir /home/andy/dev/contrib-testing/zig-cache --global-cache-dir /home/andy/.cache/zig --name c-module --listen=- 
Build Summary: 0/6 steps succeeded; 2 failed (disable with --summary none)
install transitive failure
└─ install transitive failure
   ├─ install c-module transitive failure
   │  └─ zig build-exe c-module Debug native 1 errors
   └─ install c-module-zig transitive failure
      └─ zig build-exe c-module-zig Debug native 2 errors
error: the following build command failed with exit code 1:
/home/andy/dev/contrib-testing/zig-cache/o/f81498a5d06adebf0fb693d9f4aae510/build /home/andy/dev/zig/build-release/stage4/bin/zig /home/andy/dev/contrib-testing /home/andy/dev/contrib-testing/zig-cache /home/andy/.cache/zig --seed 0x90fb733e

The build system already provides a way to expose a static library with headers. You can find plenty of examples of this already. Zig modules in the build system are not a way to expose C headers to other compilations.

The use case for providing Zig API bindings for a C library looks like this:

  • create a static library which is the C library
  • create a zig module
    • give it a root source file which either has hand written API bindings, or publicly exposes the results of @cImport
    • add the static library to it with addObject

Then you can expose that zig module. That already worked before, and is not impacted by #18622 at all.

@binarycraft007
Copy link
Contributor Author

Note that the bug here is that the build system creates an invalid command line to give to the compiler. Your project tree, as it currently exists, is not intended to work without any changes. After my patch in #18622, this is the (correct) result:

install
└─ install
   └─ install c-module-zig
      └─ zig build-exe c-module-zig Debug native 2 errors
/home/andy/.cache/zig/p/122011958020e30f221da170a2143bac2e30720c42ff7e4e4e4f38914dd499533c4b/src/main.zig:1:11: error: C import failed
const c = @cImport(@cInclude("add.h"));
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    main: /home/andy/.cache/zig/p/122011958020e30f221da170a2143bac2e30720c42ff7e4e4e4f38914dd499533c4b/src/main.zig:4:5
    callMain: /home/andy/dev/zig/lib/std/start.zig:575:17
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
/home/andy/dev/contrib-testing/zig-cache/o/58784ff95a1be73debc82b2981973621/cimport.h:1:10: error: 'add.h' file not found
#include <add.h>
         ^
error: the following command failed with 2 compilation errors:
/home/andy/dev/zig/build-release/stage4/bin/zig build-exe -ODebug --dep add -Mroot=/home/andy/.cache/zig/p/122011958020e30f221da170a2143bac2e30720c42ff7e4e4e4f38914dd499533c4b/src/main.zig /home/andy/.cache/zig/p/1220e1a95d351aaa972dfc1ba07c5ffb3fa5893632c6b907a367de67bcaf6ba068e4/src/add.c -ODebug -I /home/andy/.cache/zig/p/1220e1a95d351aaa972dfc1ba07c5ffb3fa5893632c6b907a367de67bcaf6ba068e4/include -Madd -lc --cache-dir /home/andy/dev/contrib-testing/zig-cache --global-cache-dir /home/andy/.cache/zig --name c-module-zig --listen=- 
install
└─ install
   └─ install c-module
      └─ zig build-exe c-module Debug native 1 errors
/home/andy/.cache/zig/p/122011958020e30f221da170a2143bac2e30720c42ff7e4e4e4f38914dd499533c4b/src/main.c:1:10: error: 'add.h' file not found
#include "add.h"
         ^~~~~~~~
error: the following command failed with 1 compilation errors:
/home/andy/dev/zig/build-release/stage4/bin/zig build-exe /home/andy/.cache/zig/p/122011958020e30f221da170a2143bac2e30720c42ff7e4e4e4f38914dd499533c4b/src/main.c -ODebug --dep add -Mroot /home/andy/.cache/zig/p/1220e1a95d351aaa972dfc1ba07c5ffb3fa5893632c6b907a367de67bcaf6ba068e4/src/add.c -ODebug -I /home/andy/.cache/zig/p/1220e1a95d351aaa972dfc1ba07c5ffb3fa5893632c6b907a367de67bcaf6ba068e4/include -Madd -lc --cache-dir /home/andy/dev/contrib-testing/zig-cache --global-cache-dir /home/andy/.cache/zig --name c-module --listen=- 
Build Summary: 0/6 steps succeeded; 2 failed (disable with --summary none)
install transitive failure
└─ install transitive failure
   ├─ install c-module transitive failure
   │  └─ zig build-exe c-module Debug native 1 errors
   └─ install c-module-zig transitive failure
      └─ zig build-exe c-module-zig Debug native 2 errors
error: the following build command failed with exit code 1:
/home/andy/dev/contrib-testing/zig-cache/o/f81498a5d06adebf0fb693d9f4aae510/build /home/andy/dev/zig/build-release/stage4/bin/zig /home/andy/dev/contrib-testing /home/andy/dev/contrib-testing/zig-cache /home/andy/.cache/zig --seed 0x90fb733e

The build system already provides a way to expose a static library with headers. You can find plenty of examples of this already. Zig modules in the build system are not a way to expose C headers to other compilations.

The use case for providing Zig API bindings for a C library looks like this:

* create a static library which is the C library

* create a zig module
  
  * give it a root source file which either has hand written API bindings, or publicly exposes the results of `@cImport`
  * add the static library to it with `addObject`

Then you can expose that zig module. That already worked before, and is not impacted by #18622 at all.

Thanks for the explanation, I think this is due to my misunderstanding of the new build system design, it's great to finally have the clarification.

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 zig build system
Projects
None yet
3 participants