# How to write a new generator?

**URL:** https://discourse.cmake.org/t/how-to-write-a-new-generator/5584
**Category:** Development
**Created:** [April 30, 2022, 10:38pm UTC](https://discourse.cmake.org/t/how-to-write-a-new-generator/5584 "2022-04-30T22:38:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![PeterBocan](https://discourse.cmake.org/user_avatar/discourse.cmake.org/peterbocan/32/2394_2.png) [@PeterBocan](https://discourse.cmake.org/u/PeterBocan)
#### Post date: [April 30, 2022, 10:38pm UTC](https://discourse.cmake.org/t/how-to-write-a-new-generator/5584/1 "2022-04-30T22:38:00Z")

</div>

Hello,

I am looking into contributing and extending CMake with a new kind of generator for a Please build system ([https://please.build](https://please.build)). I have been looking into the codebase but I am lost a bit. Namely, can you describe the architecture you use to create new generators? What are `local` and `global` generators used for? What is the difference?

---

<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: [May 2, 2022, 1:52pm UTC](https://discourse.cmake.org/t/how-to-write-a-new-generator/5584/2 "2022-05-02T13:52:02Z")

</div>

Just taking a glance, it doesn’t look to me like Please is suitable backend. However, it’s only at a glance and you’ve not provided details into how you expect it to work, so feel free to correct me.

CMake has its own model of what targets are and how they interact. There’s almost certainly not a way to put something like this into a generated Please file other than as a series of commands to perform (i.e., `c_library` and `cc_library` are not usable in general):

```cmake
add_library(foo STATIC
  foo.c
  bar.c
  impl.cxx # C++ code
  fort.f90 # some Fortran source code
)
target_link_library(foo
  PRIVATE
    Some::Target # has complicated genex requirements
)

```

As for the question you asked:

- local generators represent a build directory added by `add_subdirectory` and are “homes” for targets
- the global generator coordinates all of the local generators to store global information and the like

---

<div class="post-metadata">

### Author: ![PeterBocan](https://discourse.cmake.org/user_avatar/discourse.cmake.org/peterbocan/32/2394_2.png) [@PeterBocan](https://discourse.cmake.org/u/PeterBocan)
#### Post date: [May 2, 2022, 2:07pm UTC](https://discourse.cmake.org/t/how-to-write-a-new-generator/5584/3 "2022-05-02T14:07:35Z")

</div>

Interesting you mention that. Although `c_library` or `cc_library` invoke “C compiler” or “C++ compiler” that does not have to be a strict rule as you can always configure what is executed. You can always go a layer below - meaning what that target does can be redefined.

In other words, if we need separate build definitions such that they map easily to CMake ones that is of no problem to create ones (all rules at the end call `genrule` or `build_rule` internal which are very generic)

I am not that well-versed in the internals of the build targets of CMake so I have no idea what `add_library` does internally with the different file types and file formats.

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [May 2, 2022, 2:48pm UTC](https://discourse.cmake.org/t/how-to-write-a-new-generator/5584/4 "2022-05-02T14:48:31Z")

</div>

In fact, what are you searching to achieve?

From my quick lookup to `please.build`, it is more a high-level build tool (so more or less equivalent, conceptually speaking, to CMake) than a low-level one like `Makefile` or `Ninja`.

So, as noted by @ben.boeckel, it will be very complex to do a correct matching of the concepts between the two tools. Except, as you note, to do a complete rewrite of the rules of builds in `please`.

The result will be a complex development of this generator, and what will be the added-value compared to `Ninja` for example?

---

<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: [May 2, 2022, 2:57pm UTC](https://discourse.cmake.org/t/how-to-write-a-new-generator/5584/5 "2022-05-02T14:57:07Z")

</div>

> [@marc.chevrier](#):
>
> what will be the added-value compared to `Ninja` for example?

From what I can tell, Please tries to isolate each command. Given that CMake currently has no model to inform any tool caring about such things what belongs in that isolated environment, I don’t know how well it could be leveraged without CMake developments.

---

<div class="post-metadata">

### Author: ![PeterBocan](https://discourse.cmake.org/user_avatar/discourse.cmake.org/peterbocan/32/2394_2.png) [@PeterBocan](https://discourse.cmake.org/u/PeterBocan)
#### Post date: [May 2, 2022, 3:03pm UTC](https://discourse.cmake.org/t/how-to-write-a-new-generator/5584/6 "2022-05-02T15:03:54Z")

</div>

build rules are Starlark (Python-like) functions and there will be support for plugins in the future, so it’s not that bad, IMO. Although matching the concepts be an impossible task it would mean that I can use

> **[GitHub - thought-machine/please: High-performance extensible build system for...](https://github.com/thought-machine/please)**
>
> High-performance extensible build system for reproducible multi-language builds. - GitHub - thought-machine/please: High-performance extensible build system for reproducible multi-language builds.

The general built-in definitions are here:

> <https://github.com/thought-machine/please/blob/master/rules/misc_rules.build_defs>
