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

zig cc -MJ (for compile_commands.json) does not work well with multiple files #9323

Open
alexrp opened this issue Jul 7, 2021 · 10 comments
Open
Labels
bug Observed behavior contradicts documented or intended behavior zig cc Zig as a drop-in C compiler feature
Milestone

Comments

@alexrp
Copy link
Member

alexrp commented Jul 7, 2021

$ ls *.c
bar.c  foo.c  main.c
$ clang *.c -MJ o.json
$ cat o.json
{ "directory": "/home/alexrp/tests/c", "file": "bar.c", "output": "/tmp/bar-ebe0be.o", "arguments": ["/usr/lib/llvm-12/bin/clang", "-xc", "bar.c", "--target=x86_64-pc-linux-gnu"]},
{ "directory": "/home/alexrp/tests/c", "file": "foo.c", "output": "/tmp/foo-a01038.o", "arguments": ["/usr/lib/llvm-12/bin/clang", "-xc", "foo.c", "--target=x86_64-pc-linux-gnu"]},
{ "directory": "/home/alexrp/tests/c", "file": "main.c", "output": "/tmp/main-09a1d0.o", "arguments": ["/usr/lib/llvm-12/bin/clang", "-xc", "main.c", "--target=x86_64-pc-linux-gnu"]},
$ zig cc *.c -MJ o.json
$ cat o.json
{ "directory": "/home/alexrp/tests/c", "file": "bar.c", "output": "/home/alexrp/.cache/zig/tmp/14ea1c3c9c9678b9-bar.o", "arguments": ["/home/alexrp/zig/build/bin/zig", "-xc", "bar.c", "--target=x86_64-unknown-linux-gnu", "-nostdinc", "-fno-spell-checking", "-isystem", "/home/alexrp/zig/build/lib/zig/include", "-isystem", "/home/alexrp/zig/build/lib/zig/libc/include/x86_64-linux-gnu", "-isystem", "/home/alexrp/zig/build/lib/zig/libc/include/generic-glibc", "-isystem", "/home/alexrp/zig/build/lib/zig/libc/include/x86_64-linux-any", "-isystem", "/home/alexrp/zig/build/lib/zig/libc/include/any-linux-any", "-Xclang", "-target-cpu", "-Xclang", "skylake", "-Xclang", "-target-feature", "-Xclang", "-16bit-mode", "-Xclang", "-target-feature", "-Xclang", "-32bit-mode", "-Xclang", "-target-feature", "-Xclang", "-3dnow", "-Xclang", "-target-feature", "-Xclang", "-3dnowa", "-Xclang", "-target-feature", "-Xclang", "+64bit", "-Xclang", "-target-feature", "-Xclang", "+adx", "-Xclang", "-target-feature", "-Xclang", "+aes", "-Xclang", "-target-feature", "-Xclang", "-amx-bf16", "-Xclang", "-target-feature", "-Xclang", "-amx-int8", "-Xclang", "-target-feature", "-Xclang", "-amx-tile", "-Xclang", "-target-feature", "-Xclang", "+avx", "-Xclang", "-target-feature", "-Xclang", "+avx2", "-Xclang", "-target-feature", "-Xclang", "-avx512bf16", "-Xclang", "-target-feature", "-Xclang", "-avx512bitalg", "-Xclang", "-target-feature", "-Xclang", "-avx512bw", "-Xclang", "-target-feature", "-Xclang", "-avx512cd", "-Xclang", "-target-feature", "-Xclang", "-avx512dq", "-Xclang", "-target-feature", "-Xclang", "-avx512er", "-Xclang", "-target-feature", "-Xclang", "-avx512f", "-Xclang", "-target-feature", "-Xclang", "-avx512ifma", "-Xclang", "-target-feature", "-Xclang", "-avx512pf", "-Xclang", "-target-feature", "-Xclang", "-avx512vbmi", "-Xclang", "-target-feature", "-Xclang", "-avx512vbmi2", "-Xclang", "-target-feature", "-Xclang", "-avx512vl", "-Xclang", "-target-feature", "-Xclang", "-avx512vnni", "-Xclang", "-target-feature", "-Xclang", "-avx512vp2intersect", "-Xclang", "-target-feature", "-Xclang", "-avx512vpopcntdq", "-Xclang", "-target-feature", "-Xclang", "-avxvnni", "-Xclang", "-target-feature", "-Xclang", "+bmi", "-Xclang", "-target-feature", "-Xclang", "+bmi2", "-Xclang", "-target-feature", "-Xclang", "-branchfusion", "-Xclang", "-target-feature", "-Xclang", "-cldemote", "-Xclang", "-target-feature", "-Xclang", "+clflushopt", "-Xclang", "-target-feature", "-Xclang", "-clwb", "-Xclang", "-target-feature", "-Xclang", "-clzero", "-Xclang", "-target-feature", "-Xclang", "+cmov", "-Xclang", "-target-feature", "-Xclang", "+cx16", "-Xclang", "-target-feature", "-Xclang", "+cx8", "-Xclang", "-target-feature", "-Xclang", "-enqcmd", "-Xclang", "-target-feature", "-Xclang", "+ermsb", "-Xclang", "-target-feature", "-Xclang", "+f16c", "-Xclang", "-target-feature", "-Xclang", "-false-deps-lzcnt-tzcnt", "-Xclang", "-target-feature", "-Xclang", "+false-deps-popcnt", "-Xclang", "-target-feature", "-Xclang", "-fast-11bytenop", "-Xclang", "-target-feature", "-Xclang", "+fast-15bytenop", "-Xclang", "-target-feature", "-Xclang", "-fast-7bytenop", "-Xclang", "-target-feature", "-Xclang", "-fast-bextr", "-Xclang", "-target-feature", "-Xclang", "+fast-gather", "-Xclang", "-target-feature", "-Xclang", "-fast-hops", "-Xclang", "-target-feature", "-Xclang", "-fast-lzcnt", "-Xclang", "-target-feature", "-Xclang", "+fast-scalar-fsqrt", "-Xclang", "-target-feature", "-Xclang", "-fast-scalar-shift-masks", "-Xclang", "-target-feature", "-Xclang", "+fast-shld-rotate", "-Xclang", "-target-feature", "-Xclang", "+fast-variable-shuffle", "-Xclang", "-target-feature", "-Xclang", "+fast-vector-fsqrt", "-Xclang", "-target-feature", "-Xclang", "-fast-vector-shift-masks", "-Xclang", "-target-feature", "-Xclang", "+fma", "-Xclang", "-target-feature", "-Xclang", "-fma4", "-Xclang", "-target-feature", "-Xclang", "+fsgsbase", "-Xclang", "-target-feature", "-Xclang", "-fsrm", "-Xclang", "-target-feature", "-Xclang", "+fxsr", "-Xclang", "-target-feature", "-Xclang", "-gfni", "-Xclang", "-target-feature", "-Xclang", "-hreset", "-Xclang", "-target-feature", "-Xclang", "-idivl-to-divb", "-Xclang", "-target-feature", "-Xclang", "+idivq-to-divl", "-Xclang", "-target-feature", "-Xclang", "+invpcid", "-Xclang", "-target-feature", "-Xclang", "-kl", "-Xclang", "-target-feature", "-Xclang", "-lea-sp", "-Xclang", "-target-feature", "-Xclang", "-lea-uses-ag", "-Xclang", "-target-feature", "-Xclang", "-lvi-cfi", "-Xclang", "-target-feature", "-Xclang", "-lvi-load-hardening", "-Xclang", "-target-feature", "-Xclang", "-lwp", "-Xclang", "-target-feature", "-Xclang", "+lzcnt", "-Xclang", "-target-feature", "-Xclang", "+macrofusion", "-Xclang", "-target-feature", "-Xclang", "+mmx", "-Xclang", "-target-feature", "-Xclang", "+movbe", "-Xclang", "-target-feature", "-Xclang", "-movdir64b", "-Xclang", "-target-feature", "-Xclang", "-movdiri", "-Xclang", "-target-feature", "-Xclang", "-mwaitx", "-Xclang", "-target-feature", "-Xclang", "+nopl", "-Xclang", "-target-feature", "-Xclang", "-pad-short-functions", "-Xclang", "-target-feature", "-Xclang", "+pclmul", "-Xclang", "-target-feature", "-Xclang", "-pconfig", "-Xclang", "-target-feature", "-Xclang", "-pku", "-Xclang", "-target-feature", "-Xclang", "+popcnt", "-Xclang", "-target-feature", "-Xclang", "-prefer-128-bit", "-Xclang", "-target-feature", "-Xclang", "-prefer-256-bit", "-Xclang", "-target-feature", "-Xclang", "-prefer-mask-registers", "-Xclang", "-target-feature", "-Xclang", "-prefetchwt1", "-Xclang", "-target-feature", "-Xclang", "+prfchw", "-Xclang", "-target-feature", "-Xclang", "-ptwrite", "-Xclang", "-target-feature", "-Xclang", "-rdpid", "-Xclang", "-target-feature", "-Xclang", "+rdrnd", "-Xclang", "-target-feature", "-Xclang", "+rdseed", "-Xclang", "-target-feature", "-Xclang", "-retpoline", "-Xclang", "-target-feature", "-Xclang", "-retpoline-external-thunk", "-Xclang", "-target-feature", "-Xclang", "-retpoline-indirect-branches", "-Xclang", "-target-feature", "-Xclang", "-retpoline-indirect-calls", "-Xclang", "-target-feature", "-Xclang", "-rtm", "-Xclang", "-target-feature", "-Xclang", "+sahf", "-Xclang", "-target-feature", "-Xclang", "-serialize", "-Xclang", "-target-feature", "-Xclang", "-seses", "-Xclang", "-target-feature", "-Xclang", "-sgx", "-Xclang", "-target-feature", "-Xclang", "-sha", "-Xclang", "-target-feature", "-Xclang", "-shstk", "-Xclang", "-target-feature", "-Xclang", "+slow-3ops-lea", "-Xclang", "-target-feature", "-Xclang", "-slow-incdec", "-Xclang", "-target-feature", "-Xclang", "-slow-lea", "-Xclang", "-target-feature", "-Xclang", "-slow-pmaddwd", "-Xclang", "-target-feature", "-Xclang", "-slow-pmulld", "-Xclang", "-target-feature", "-Xclang", "-slow-shld", "-Xclang", "-target-feature", "-Xclang", "-slow-two-mem-ops", "-Xclang", "-target-feature", "-Xclang", "-slow-unaligned-mem-16", "-Xclang", "-target-feature", "-Xclang", "-slow-unaligned-mem-32", "-Xclang", "-target-feature", "-Xclang", "-soft-float", "-Xclang", "-target-feature", "-Xclang", "+sse", "-Xclang", "-target-feature", "-Xclang", "+sse2", "-Xclang", "-target-feature", "-Xclang", "+sse3", "-Xclang", "-target-feature", "-Xclang", "+sse4.1", "-Xclang", "-target-feature", "-Xclang", "+sse4.2", "-Xclang", "-target-feature", "-Xclang", "-sse4a", "-Xclang", "-target-feature", "-Xclang", "-sse-unaligned-mem", "-Xclang", "-target-feature", "-Xclang", "+ssse3", "-Xclang", "-target-feature", "-Xclang", "-tbm", "-Xclang", "-target-feature", "-Xclang", "-tsxldtrk", "-Xclang", "-target-feature", "-Xclang", "-uintr", "-Xclang", "-target-feature", "-Xclang", "-use-aa", "-Xclang", "-target-feature", "-Xclang", "-use-glm-div-sqrt-costs", "-Xclang", "-target-feature", "-Xclang", "-vaes", "-Xclang", "-target-feature", "-Xclang", "-vpclmulqdq", "-Xclang", "-target-feature", "-Xclang", "+vzeroupper", "-Xclang", "-target-feature", "-Xclang", "-waitpkg", "-Xclang", "-target-feature", "-Xclang", "-wbnoinvd", "-Xclang", "-target-feature", "-Xclang", "-widekl", "-Xclang", "-target-feature", "-Xclang", "+x87", "-Xclang", "-target-feature", "-Xclang", "-xop", "-Xclang", "-target-feature", "-Xclang", "+xsave", " "-Xclang", "-target-feature", "-Xclang", "+xsavec", "-Xclang", "-target-feature", "-Xclang", "+xsaveopt", "-Xclang", "-target-feature", "-Xclang", "+xsaves", "-fomit-frame-pointer", "-fsanitize=undefined", "-fsanitize-trap=undefined", "-mred-zone", "-D", "_DEBUG", "-Og", "-fstack-protector-strong", "--param", "ssp-buffer-size=4", "-fPIC", "-fno-unwind-tables", "-isystem", "/usr/local/include", "-isystem", "/usr/include/x86_64-linux-gnu", "-isystem", "/usr/include", "-c", "-o", "/home/alexrp/.cache/zig/tmp/abd94befe6eeb02-main.o", "--target=x86_64-unknown-linux-gnu"]},

Note how only one file entry is emitted by Zig.

@alexrp
Copy link
Member Author

alexrp commented Jul 19, 2021

For anyone else running into this and looking for a workaround, -gen-cdb-fragment-path cdb will cause Clang to output JSON fragment files to the cdb directory that you can then stitch together to form a complete compilation database. This comes with the caveat that you have to clear both that directory and the Zig cache directory completely when compiler flags change or .c/.cxx files are added/removed.

@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior zig cc Zig as a drop-in C compiler feature labels Aug 6, 2021
@Vexu Vexu added this to the 0.10.0 milestone Aug 6, 2021
@g-berthiaume
Copy link

g-berthiaume commented Apr 30, 2023

+1 for this feature.
That would be very helpful to integrate zig with other parts of the C ecosystem.
For example, using clang-tidy.

@VisenDev
Copy link

VisenDev commented Jun 8, 2023

+1

1 similar comment
@mundusnine
Copy link

+1

@VisenDev
Copy link

I would add that this LSP integration issue is the main reason keeping me from integrating into my existing C projects

It would really be nice to have clangd or some other LSP work with zig built C projects

@neurocyte
Copy link

I have builds produce a working compile_commands.json for my zig-built-cpp projects. I do this:

  • Add "-gen-cdb-fragment-path", ".cache/cdb" to my addCSourceFiles flags (make sure you don't have -MJ too)
  • Run (echo \[ ; cat .cache/cdb/* ; echo {}\]) | perl -0777 -pe 's/,\n\{\}//igs' | jq . | grep -v 'no-default-config' > compile_commands.json (or something similar) after each build

I guess this could be better integrated into Zig's build system, but it works just fine for me without any special support from zig.

@kassane
Copy link
Contributor

kassane commented Jul 22, 2023

Recently a user made a compile_commands.json generator which may help this issue.
https://github.com/the-argus/raylib-chipmunk-crossplatform-template/blob/main/build/compile_commands.zig

@the-argus
Copy link

@kassane i have no clue how you found and noticed my little repo but thank you :)

I have refined and packaged that functionality for easy use with zig v0.11.0 now: https://github.com/the-argus/zig-compile-commands

It doesn't know anything about what zig cc commands actually get executed, so it's more of a mimicry of what the zig compiler is actually doing in order to put enough into compile_commands to satisfy clangd specifically for my project. Currently it doesn't support config headers and I'm sure there will be other holes to patch.

@thezealousfool
Copy link

+1

@jonathanmarvens
Copy link

jonathanmarvens commented Dec 28, 2023

Hey, please check out my PR for this feature: #18391. We can continue the discussion there.

Update: @alexrp pointed out in a comment that #18391 doesn't actually address this particular issue. #18391 covers the zig build use case while this issue is focused on zig cc which has to do with the Clang driver.

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 cc Zig as a drop-in C compiler feature
Projects
None yet
Development

No branches or pull requests

10 participants