# How to implement Make pattern rules in CMake?

**URL:** <https://discourse.cmake.org/t/how-to-implement-make-pattern-rules-in-cmake/12792>\
**Category:** Code\
**Created:** [October 17, 2024, 3:35pm UTC](https://discourse.cmake.org/t/how-to-implement-make-pattern-rules-in-cmake/12792 "2024-10-17T15:35:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![petk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/petk/32/4555_2.png) [@petk](https://discourse.cmake.org/u/petk)\
**Post date:** [October 17, 2024, 3:35pm UTC](https://discourse.cmake.org/t/how-to-implement-make-pattern-rules-in-cmake/12792/1 "2024-10-17T15:35:58Z")

</div>

Hello, what would be the recommended approach in CMake to implement the so-called pattern rules as known in Make:

> **[Pattern Rules (GNU make)](https://www.gnu.org/software/make/manual/html_node/Pattern-Rules.html)**
>
> Pattern Rules (GNU make)

For example, the source files `*.stub.php` are used to generate the `_arginfo.h` files with PHP.

A follow up question:

1. Would an unknown language extension in `add_library()`, `add_executable()` or `target_sources()` cause any issues in CMake somewhere?

For example:

```cmake
add_library(foo)

target_sources(foo PRIVATE foo.c bar.c foo.stub.php)

```

Because, I was aiming a bit to add the `CMakeDeterminePHPCompiler`, `CMakePHPInformation` and other files to use `enable_language(PHP)` but the main issue is still figuring out proper dependency relations between the C objects in the linked library and the `*.stub.php` files. For example, the `*_arginfo.h` files from their accompanying `*.stub.php` files in the context of the library should be generated before compiling C files as they include the `*_arginfo.h` files.

I’m guessing that using `CMakeDeterminePHPCompiler` for generating files would be a bit of a cheating as there wouldn’t be any compilation here specifically, only generation.

And adding `add_custom_command()` gets a bit heavy IMHO in the build output phase and there comes an issue of retrieving the source files from either library source directory or the library target sources a bit.

Any suggestion is most welcome. Thank you.
