Clang - Two-step compilation through a compiler wrapper

Hello!

I am currently working on a project from which I want to extract LLVM’s bitcode files. Despite attempting various methods, including gllvm, wllvm, and passing flags to clang, I have been unsuccessful in retrieving the bitcode files. There are different quirks to this build environment as it’s an EFI SDK.

My current approach involves a two-step compilation process, with the first step producing bitcode files and the second step executing the compiler as usual. Below is the definition of the wrapper used in my derivation:

let
  cc_wrapper = writeShellScript "ccwrapper.sh" ''
    #!/usr/bin/env bash
    # Get the original arguments
    shift
    args=$@

    echo "Args: $args"

    # Identify the source file and set the output bitcode file name
    src_file=""
    for arg in $args; do
      if [[ ''${arg} == *.c || ''${arg} == *.cpp ]]; then
        src_file="''${arg}"
        echo "Source file: ''${src_file}"
        break
      fi
    done

    if [[ -n ''${src_file} ]]; then
      bitcode_file="$(basename "''${src_file}").bc"
      # Call the original clang with the modified arguments to generate bitcode
      ${clangStdenv.cc}/bin/cc -c -emit-llvm -o ''${bitcode_file} $args
    fi

    # If no source file is found, just call the original clang with the original arguments
    #${clangStdenv.cc}/bin/cc $args
  '';
  wrappedStdenv = overrideCC clangStdenv cc_wrapper;
in
wrappedStdenv.mkDerivation {
...}

I am encountering two primary issues:

  1. The wrapper implementation may not be ideal, as it seems not to preserve the original compilation commands.
  2. The project is unable to locate clang during the compilation process, despite overriding clangStdenv.

I would greatly appreciate any insights or suggestions on how to resolve these issues and successfully implement a two-step compilation with a compiler wrapper.

Thank you!