# Chained compilation

**URL:** https://discourse.cmake.org/t/chained-compilation/2365
**Category:** Usage
**Created:** [December 14, 2020, 7:39am UTC](https://discourse.cmake.org/t/chained-compilation/2365 "2020-12-14T07:39:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![robiwano](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robiwano/32/1027_2.png) [@robiwano](https://discourse.cmake.org/u/robiwano)
#### Post date: [December 14, 2020, 7:39am UTC](https://discourse.cmake.org/t/chained-compilation/2365/1 "2020-12-14T07:39:09Z")

</div>

I’m looking for a way to create a new language, let’s call it LLVM :), and the compilation from C++ to object file would go through:  
C++ -\> LLVM -\> C -\> object file  
where LLVM -\> C compilation is defined via [https://github.com/Kitware/CMake/blob/master/Modules/CMakeAddNewLanguage.txt](https://github.com/Kitware/CMake/blob/master/Modules/CMakeAddNewLanguage.txt).

So given that C++ outputs LLVM, I’d like CMake to “understand” to apply the compilation chain above, implicitly. Ideas on how to accomplish this, if possible at all?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 14, 2020, 1:03pm UTC](https://discourse.cmake.org/t/chained-compilation/2365/2 "2020-12-14T13:03:58Z")

</div>

I think you’d have to basically skip the `C++ -> object file` logic that CMake knows about and add your own `CXXLLVM` language for that step. The outputs might need some coaxing to be understood as source files as well, but I think ISPC likely has codepaths for that these days. Not sure how easy it would be to repurpose them.

Cc: @brad.king @robert.maynard

---

<div class="post-metadata">

### Author: ![robiwano](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robiwano/32/1027_2.png) [@robiwano](https://discourse.cmake.org/u/robiwano)
#### Post date: [December 14, 2020, 1:12pm UTC](https://discourse.cmake.org/t/chained-compilation/2365/3 "2020-12-14T13:12:15Z")

</div>

I was more thinking of specifying a toolchain file, which basically overrides CMAKE\_CXX\_OUTPUT\_EXTENSION with the input extension of the LLVM language (".llvm"). So all C++ files would go through the compilation chain. The CXX flags would have “-S -emit-llvm” so that the C++ object file, with ending .llvm, would contain LLVM IR code.  
This is how I envisioned CMake being able to deduce the compilation order 🙂

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [December 14, 2020, 2:03pm UTC](https://discourse.cmake.org/t/chained-compilation/2365/4 "2020-12-14T14:03:00Z")

</div>

ISPC code model is `C++ -> N object files + headers`. As long as CMake compiles all ISPC files before any other language ( per target ) we are good since that makes sure headers exist before consumers.

> [@robiwano](#):
>
> So all C++ files would go through the compilation chain. The CXX flags would have “-S -emit-llvm” so that the C++ object file, with ending .llvm, would contain LLVM IR code.

If I am understanding this correctly you want to pass the output of the `C++` compiler to the `C` compiler. This will be tricky for CMake as in general languages are considered to be fully independent, so a project might only enable `C++` and therefore you won’t have `C` .

This type of transformation is done by other current languages via the compiler driver it self. Since the end result of `C++ -> object` hasn’t changed, this allows for drop-in replacement as the C++ compiler for numerous build-systems.
