Generating `compile_commands.json` with `zig build`?
I'm still pretty new to this, so I might be missing something terribly obvious. I'm trying to use c2rust with FastFEC to generate Rust code to start porting. It is relatively small, so in reality hand-rewriting it might be easiest, but I tried to give this a try first.
FastFEC seems to just be using Zig for building, but is otherwise in C. I fear this step might be outside the currently supported and expected path.
It uses Zig to build, with zig build as the command, and that works well and quickly. When I try the intercept build command as suggested to generate the compile_commands.json file however I come up with empty output.
FastFEC % intercept-build -vvvv zig build
intercept-build: DEBUG: parse_args_for_intercept_build: Raw arguments ['/opt/homebrew/bin/intercept-build', '-vvvv', 'zig', 'build']
intercept-build: DEBUG: parse_args_for_intercept_build: Parsed arguments: Namespace(verbose=4, cdb='compile_commands.json', override_compiler=False, cc='cc', cxx='c++', append=False, build=['zig', 'build'])
intercept-build: DEBUG: run_command: exec command ['csrutil', 'status'] in /Users/davidfisher/code/FastFEC
intercept-build: DEBUG: run_build: run build ['zig', 'build'], in environment:
{'CC': 'intercept-cc',
'COLORFGBG': '7;0',
'COLORTERM': 'truecolor',
'COMMAND_MODE': 'unix2003',
'CXX': 'intercept-c++',
'HOME': '/Users/davidfisher',
'HOMEBREW_CELLAR': '/opt/homebrew/Cellar',
'HOMEBREW_PREFIX': '/opt/homebrew',
'HOMEBREW_REPOSITORY': '/opt/homebrew',
'INFOPATH': '/opt/homebrew/share/info:',
'INTERCEPT_BUILD': '{"verbose": 4, "cc": ["cc"], "cxx": ["c++"]}',
'INTERCEPT_BUILD_TARGET_DIR': '/var/folders/5w/2v1mgyl54tj7yjcym5s055yw0000gq/T/intercept-s_k51ey8',
'ITERM_PROFILE': 'Default',
'LANG': 'en_US.UTF-8',
'LC_TERMINAL': 'iTerm2',
'LC_TERMINAL_VERSION': '3.4.15',
'LOGNAME': 'davidfisher',
'MANPATH': '/opt/homebrew/share/man::',
'OLDPWD': '/Users/davidfisher/code',
'PATH': '/Users/davidfisher/.rbenv/shims:/Users/davidfisher/.rbenv/bin:/Users/david/.ebcli-virtual-env/executables:/Users/davidfisher/.krew/bin:/usr/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/davidfisher/.cargo/bin:/Users/davidfisher/.rbenv/bin:/Users/david/.ebcli-virtual-env/executables',
'PWD': '/Users/davidfisher/code/FastFEC',
'PYENV_SHELL': 'zsh',
'RBENV_SHELL': 'zsh',
'SHELL': '/bin/zsh',
'SHLVL': '1',
'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.1Wb4rd0hK7/Listeners',
'TERM': 'xterm-256color',
'TERM_PROGRAM': 'iTerm.app',
'TERM_PROGRAM_VERSION': '3.4.15',
'TMPDIR': '/var/folders/5w/2v1mgyl54tj7yjcym5s055yw0000gq/T/',
'USER': 'davidfisher',
'XPC_FLAGS': '0x0',
'XPC_SERVICE_NAME': '0',
'_': '/opt/homebrew/bin/intercept-build',
'__CFBundleIdentifier': 'com.googlecode.iterm2',
'__CF_USER_TEXT_ENCODING': '0x1F7:0x0:0x0'}
intercept-build: DEBUG: run_build: build finished with exit code: 0
compile_commands.json then has content of []. Any ideas? I'm on MacOS 12.4 (21F79) on an M1 machine.
I believe intercept-build overrides your environment variables CXX and CC in order to do its job. I'm not sure if zig build does anything with those. zig cc might, and if you're willing to build with that then it's possible --overwrite-compiler may work for you with some combination of export CC="zig cc ..."
To your core issue, though, you can compile this with clang based on build.zig:
clang src/buffer.c src/memory.c src/encoding.c src/csv.c src/writer.c src/fec.c src/urlopen.c src/main.c -I deps/pcre -I src/ -lcurl -lpcre -o fastfec
Put that in a CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(fastfec)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)
# add the executable
add_executable(fastfec src/buffer.c src/memory.c src/encoding.c src/csv.c src/writer.c src/fec.c src/urlopen.c src/main.c)
target_include_directories(fastfec PUBLIC
deps/pcre
src/
)
target_link_libraries(fastfec PUBLIC curl pcre)
and then run intercept-build with make. I like bear, so I ran with bear -- make.
If I made a mistake, apologies, but this should at least be enough to get you started.
Running
strace -etrace=execve -f zig build
it appears zig build invokes zig clang as the clang driver, so you may be able to intercept those calls.
@tibbon I had an itch to try to figure this out -- these are the steps I took after generating the compile_commands.json as explained above:
c2rust transpile -o rust --binary main compile_commands.json --verbose -- -I <path/to/stddef.h>
and after that change the generated rust/build.rs to link libpcre and libcurl:
#[cfg(all(unix, not(target_os = "macos")))]
fn main() {
// add unix dependencies below
println!("cargo:rustc-flags=-lcurl -lpcre");
}
#[cfg(target_os = "macos")]
fn main() {
// add macos dependencies below
// println!("cargo:rustc-flags=-l edit");
}
and after that it should build and run from the rust directory:
$ cargo run
...
Usage:
target/debug/main [flags] <id, file, or url> [output directory=output] [override id]
or: [some command] | target/debug/main [flags] <id> [output directory=output]
Optional flags:
--include-filing-id, -i: include a filing_id column at the beginning of
every output CSV
--silent, -s : suppress all stdout messages
--warn, -w : show warning messages
--no-stdin, -x : disable piped input
Clang/zig cc generate json file:
-MJ
Write a compilation database entry per input
src: clang doc
compile_commans.json
File Contents
{
"directory": "/home/kassane/FastFEC",
"file": "src/main.c",
"output": "/home/kassane/.cache/zig/tmp/cc83b769addb89b2-main.o",
"arguments": [
"/home/kassane/.local/bin/zig",
"-xc",
"src/main.c",
"-D",
"__GLIBC_MINOR__=19",
"--target=x86_64-unknown-linux-gnu",
"-nostdinc",
"-fno-spell-checking",
"-isystem",
"/home/kassane/.local/lib/zig/include",
"-isystem",
"/usr/include",
"-Xclang",
"-target-cpu",
"-Xclang",
"bdver2",
"-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",
"-avx512fp16",
"-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",
"+crc32",
"-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-movbe",
"-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-crosslane-shuffle",
"-Xclang",
"-target-feature",
"-Xclang",
"-fast-variable-perlane-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",
"-tagged-globals",
"-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",
"-use-slm-arith-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",
"-widek",
"-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",
"-g",
"-fsanitize=undefined",
"-fsanitize-trap=undefined",
"-mred-zone",
"-fno-omit-frame-pointer",
"-D",
"_DEBUG",
"-O0",
"-fstack-protector-strong",
"--param",
"ssp-buffer-size=4",
"-fPIC",
"-fno-unwind-tables",
"-I",
"deps/pcre",
"-I",
"src/",
"-isystem",
"/usr/local/include",
"-isystem",
"/usr/include/x86_64-linux-gnu",
"-isystem",
"/usr/include",
"-c",
"-o",
"/home/kassane/.cache/zig/tmp/cd20399c74ed84fa-urlopen.o",
"--target=x86_64-unknown-linux-gnu"
]
}
@aneksteind Here's what I tried. I got closer with your help, and I super appreciate this!
Created CMakeLists.txt in FastFEC directory
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(fastfec)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)
# add the executable
add_executable(fastfec src/buffer.c src/memory.c src/encoding.c src/csv.c src/writer.c src/fec.c src/urlopen.c src/main.c)
target_include_directories(fastfec PUBLIC
deps/pcre
src/
)
target_link_libraries(fastfec PUBLIC curl pcre)
$ c2rust transpile -o rust --binary main compile_commands.json --verbose -- -I /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/stddef.h
$ intercept-build bear -- make
make: *** No targets specified and no makefile found. Stop.
Running clang src/buffer.c src/memory.c src/encoding.c src/csv.c src/writer.c src/fec.c src/urlopen.c src/main.c -I deps/pcre -I src/ -lcurl -lpcre -o fastfec does seem to compile fastfec
I'm going to attempt to update the paths in @kassane 's output they posted and see if that works. Otherwise my compile_commands.json still seems empty.
It appears intercept-build bear -- make actually clears my compile_command.json file resetting it to []
A step I left out is to first run cmake . (in the FastFec dir) before running bear -- make (or intercept-build make, you don't need both)
@tibbon I've been able to make this public: https://github.com/immunant/FastFEC-rust, hopefully it helps you.