immunant / c2rust

Migrate C code to Rust
https://c2rust.com/
Other
3.95k stars 234 forks source link

Build fails on ARM Mac with brew binutils installed #969

Closed miakizz closed 1 year ago

miakizz commented 1 year ago

Running cargo install c2rust fails on my M1 Mac with the following error:

```shell `error: failed to run custom build command for `c2rust-ast-exporter v0.18.0` Caused by: process didn't exit successfully: `/var/folders/3g/r3hmdls10wq2rbdghv32ygqh0000gn/T/cargo-installN4Ai2d/release/build/c2rust-ast-exporter-71773e650c2d1038/build-script-build` (exit status: 101) --- stdout CMAKE_TOOLCHAIN_FILE_aarch64-apple-darwin = None CMAKE_TOOLCHAIN_FILE_aarch64_apple_darwin = None HOST_CMAKE_TOOLCHAIN_FILE = None CMAKE_TOOLCHAIN_FILE = None CMAKE_GENERATOR_aarch64-apple-darwin = None CMAKE_GENERATOR_aarch64_apple_darwin = None HOST_CMAKE_GENERATOR = None CMAKE_GENERATOR = None CMAKE_PREFIX_PATH_aarch64-apple-darwin = None CMAKE_PREFIX_PATH_aarch64_apple_darwin = None HOST_CMAKE_PREFIX_PATH = None CMAKE_PREFIX_PATH = None CMAKE_aarch64-apple-darwin = None CMAKE_aarch64_apple_darwin = None HOST_CMAKE = None CMAKE = None running: "cmake" "/Users/nkizz/.cargo/registry/src/github.com-1ecc6299db9ec823/c2rust-ast-exporter-0.18.0/src" "-DCMAKE_OSX_ARCHITECTURES=arm64" "-DLLVM_DIR=/opt/homebrew/Cellar/llvm/16.0.6/lib/cmake/llvm" "-DClang_DIR=/opt/homebrew/Cellar/llvm/16.0.6/lib/cmake/clang" "-DCMAKE_INSTALL_PREFIX=/var/folders/3g/r3hmdls10wq2rbdghv32ygqh0000gn/T/cargo-installN4Ai2d/release/build/c2rust-ast-exporter-507aaf8b5625b01d/out" "-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC -arch arm64" "-DCMAKE_C_COMPILER=/usr/bin/cc" "-DCMAKE_CXX_FLAGS= -ffunction-sections -fdata-sections -fPIC -arch arm64" "-DCMAKE_CXX_COMPILER=/usr/bin/c++" "-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC -arch arm64" "-DCMAKE_ASM_COMPILER=/usr/bin/cc" "-DCMAKE_BUILD_TYPE=Release" -- The C compiler identification is AppleClang 14.0.3.14030022 -- The CXX compiler identification is AppleClang 14.0.3.14030022 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Performing Test HAVE_FFI_CALL -- Performing Test HAVE_FFI_CALL - Success -- Found FFI: /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/lib/libffi.tbd -- Performing Test Terminfo_LINKABLE -- Performing Test Terminfo_LINKABLE - Success -- Found Terminfo: /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/lib/libcurses.tbd -- Found ZLIB: /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/lib/libz.tbd (found version "1.2.11") -- Found zstd: /opt/homebrew/lib/libzstd.dylib -- Found LibXml2: /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/lib/libxml2.tbd (found version "2.9.13") -- Configuring done (2.3s) -- Generating done (0.0s) -- Build files have been written to: /private/var/folders/3g/r3hmdls10wq2rbdghv32ygqh0000gn/T/cargo-installN4Ai2d/release/build/c2rust-ast-exporter-507aaf8b5625b01d/out/build running: "cmake" "--build" "." "--target" "clangAstExporter" "--config" "Release" [ 8%] Creating directories for 'tinycbor_build' [ 16%] Performing download step (git clone) for 'tinycbor_build' [ 25%] Performing update step for 'tinycbor_build' [ 33%] Performing patch step for 'tinycbor_build' patching file Makefile patching file Makefile.configure [ 41%] Performing configure step for 'tinycbor_build' .config file not yet created [ 50%] Performing build step for 'tinycbor_build' --- stderr CMake Warning: Manually-specified variables were not used by the project: CMAKE_ASM_COMPILER CMAKE_ASM_FLAGS Cloning into 'tinycbor_build'... HEAD is now at d393c16 Parser: fix reading it->extra on big endian when bytesNeeded == 1 ld: warning: ignoring file lib/libtinycbor.a, building for macOS-arm64 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 ) Undefined symbols for architecture arm64: "_cbor_error_string", referenced from: _printerror in cbordump.o "_cbor_parser_init", referenced from: _dumpFile in cbordump.o "_cbor_value_to_json_advance", referenced from: _dumpFile in cbordump.o "_cbor_value_to_pretty_advance_flags", referenced from: _dumpFile in cbordump.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[4]: *** [bin/cbordump] Error 1 gmake[3]: *** [CMakeFiles/tinycbor_build.dir/build.make:86: tinycbor/src/tinycbor_build-stamp/tinycbor_build-build] Error 2 gmake[2]: *** [CMakeFiles/Makefile2:92: CMakeFiles/tinycbor_build.dir/all] Error 2 gmake[1]: *** [CMakeFiles/Makefile2:281: CMakeFiles/clangAstExporter.dir/rule] Error 2 gmake: *** [Makefile:215: clangAstExporter] Error 2 thread 'main' panicked at ' command did not execute successfully, got: exit status: 2 build script failed, must exit now', /Users/nkizz/.cargo/registry/src/github.com-1ecc6299db9ec823/cmake-0.1.50/src/lib.rs:1098:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace warning: build failed, waiting for other jobs to finish... error: failed to compile `c2rust v0.18.0`, intermediate artifacts can be found at /var/folders/3g/r3hmdls10wq2rbdghv32ygqh0000gn/T/cargo-installN4Ai2 ```

Inspired by https://stackoverflow.com/a/74159799, I ran brew uninstall binutils, and c2rust installed without any errors. I would dig into a fix, but I'm not exactly sure which ld is being used where. Maybe just explicitly using the system ld would be good? Or throwing an error if it detects the bintools package is installed.

Thanks!

thedataking commented 1 year ago

Hi @nkizz Thanks for the report. The problem relates to the build of tinycbor which uses its own build system that we can't easily control. I think what we'll do is point anyone else who hits this problem to this issue since it contains the resolution.

kkysen commented 1 year ago

ld: warning: ignoring file lib/libtinycbor.a, building for macOS-arm64 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 )

says the file starts with !<arch>\n/. What does file say it is? That doesn't appear to be a Mach-O archive, which I think should start with 0xFEEDFACF, so I'm not sure what's going on.

thedataking commented 1 year ago

The archive contains .o files which file report as Mach-O 64-bit object arm64. I think it is simply the case that binutils is not compatible with the Apple host toolchain (e.g. Apple clang) so the right solution is to make sure binutils is not on path.

brew emits a warning when installing binutils:

==> Caveats
binutils is keg-only, which means it was not symlinked into /opt/homebrew,
because it shadows the host toolchain.

If you need to have binutils first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/binutils/bin:$PATH"' >> /Users/perl/.bash_profile

For compilers to find binutils you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/binutils/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/binutils/include"