September 6th, 2024

How to wrap a C compiler and preprocessor, really

The article explores creating a wrapper for C compilers GCC and Clang, proposing a "back-room" approach to simplify modifications during compilation using existing compiler options and addressing integration challenges.

Read original articleLink Icon
How to wrap a C compiler and preprocessor, really

The article discusses the challenges and methodologies involved in creating a wrapper for C compilers, specifically focusing on GCC and Clang. The author aims to develop a drop-in replacement for the compiler that allows for modifications during the compilation process without needing to reimplement the compiler's driver. The proposed solution involves a "back-room" approach, which simplifies the process by utilizing existing compiler options like `-wrapper` and `-###` to manage subcommands generated during compilation. This method avoids the complexities of parsing command-line arguments and instead relies on the compiler's built-in functionalities to handle subcommands. The author also addresses the integration of preprocessing and compilation stages in modern compilers, noting that while GCC allows separation with the `-no-integrated-cpp` option, Clang does not support this directly but can be manipulated to achieve similar results. The article highlights the importance of understanding the command-line syntax and semantics of compilers to effectively implement such wrappers.

- The article focuses on creating a wrapper for C compilers like GCC and Clang.

- A "back-room" approach is proposed to simplify the process of modifying compiler behavior.

- The use of compiler options like `-wrapper` and `-###` is emphasized for managing subcommands.

- Challenges related to the integration of preprocessing and compilation stages are discussed.

- The author notes differences in handling these stages between GCC and Clang.

Link Icon 2 comments
By @fuhsnn - about 1 month
I have some experience getting several non-trivial projects to build with a small C compiler [1]. Generally, the more a toolchain resemblance, say GCC, the more GCC specific stuff build scripts will feed to it, this may send you down a rabbit hole of recreating GCC behavior distracting from the intended topic. There are simply too many varying factors in the internal messages between a toolchain's sub-fuctions: versioning, target system, the specific installation's default configuration etc. I have no idea what the author's intended modification are, but it may be actually simpler, and more reliable, to modify a compiler at source level than to provide a drop in injection of a certain stage.

[1] https://github.com/fuhsnn/widcc?tab=readme-ov-file#building-...