Linux Kernel Considers Enabling Microsoft C Extensions

linux kernel development

The Linux kernel is moving towards enabling Microsoft C Extensions (via -fms-extensions) in its build process, potentially for kernel 6.19. This aims to simplify C code and offer constructs like anonymous structs/unions, with Linus Torvalds' apparent approval.

Two key patches are currently in the kbuild-next development tree, signaling a significant shift for the Linux kernel. These patches aim to universally enable the -fms-extensions compiler argument, allowing both GCC and LLVM/Clang to utilize Microsoft C Extensions during kernel compilation. With their presence in kbuild-next, these changes are expected to be proposed for the Linux 6.19 kernel merge window next month, though potential objections could still arise.

The -fms-extensions option, supported by both the GNU Compiler Collection and LLVM/Clang, facilitates the use of certain non-standard C/C++ constructs prevalent in Microsoft header files and recognized by the Microsoft Visual C/C++ compiler. For Linux kernel development, this means developers could, for example, embed a tagged struct or union anonymously within another struct or union, leading to more concise code.

While proposals to unconditionally enable -fms-extensions have surfaced over the years, they have historically failed to gain traction on the Linux kernel mailing list. However, with the current patches now in kbuild-next, their inclusion in the Linux 6.19 kernel merge window seems probable, pending any strong opposition from leading Linux kernel developers or Linus Torvalds himself.

Rasmus Villemoes, in his argument for "Kbuild: enable -fms-extensions", highlighted that these extensions could lead to "prettier code." Others have previously pointed out the potential for stack space savings and the general advantage of leveraging Microsoft C behavior. Villemoes elaborated:

"Once in a while, it turns out that enabling -fms-extensions could allow some slightly prettier code. But every time it has come up, the code that had to be used instead has been deemed 'not too awful' and not worth introducing another compiler flag for. That's probably true for each individual case, but then it's somewhat of a chicken/egg situation. If we just 'bite the bullet' as Linus says and enable it once and for all, it is available whenever a use case turns up, and no individual case has to justify it."

He also noted that numerous other areas in the codebase could benefit from these extensions, even if they haven't been explicitly discussed.

The second patch, titled "kbuild: Add '-fms-extensions' to areas with dedicated CFLAGS," ensures that -fms-extensions is correctly applied to CPU architectures that utilize their own CFLAGS rather than the primary KBUILD_CFLAGS.

Linus Torvalds himself participated in earlier mailing list discussions on this topic and appears to be open to enabling -fms-extensions starting with the Linux 6.19 kernel.

Ultimately, enabling -fms-extensions is expected to lead to cleaner and potentially more elegant C code within the kernel, even if some developers might initially hesitate at the idea of incorporating Microsoft C behavior into the mainline Linux kernel.