Clang - Two-step compilation through a compiler wrapper


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:

  cc_wrapper = writeShellScript "" ''
    #!/usr/bin/env bash
    # Get the original arguments

    echo "Args: $args"

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

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

    # If no source file is found, just call the original clang with the original arguments
    #${}/bin/cc $args
  wrappedStdenv = overrideCC clangStdenv cc_wrapper;
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!