# Piping the output of a script to the STDIN of a compiler

**URL:** https://discourse.cmake.org/t/piping-the-output-of-a-script-to-the-stdin-of-a-compiler/9614
**Category:** Code
**Created:** [December 12, 2023, 1:33am UTC](https://discourse.cmake.org/t/piping-the-output-of-a-script-to-the-stdin-of-a-compiler/9614 "2023-12-12T01:33:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Agent13](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/cdc98d/32.png) [@Agent13](https://discourse.cmake.org/u/Agent13)
#### Post date: [December 12, 2023, 1:33am UTC](https://discourse.cmake.org/t/piping-the-output-of-a-script-to-the-stdin-of-a-compiler/9614/1 "2023-12-12T01:33:37Z")

</div>

Hi,

I’m trying to move a project from GNU make to cmake. The old project has some make code shown below which does some “interesting” things. My port strategy is to basically re-implement the make steps within cmake.

Are there any suggestions on how to implement these targets in cmake?

I’ve tried various invocations of “add\_custom\_target” and “add\_custom\_command” along with the “COMMAND” and “VERBATIM” flags. These always produce errors. It appears that the shell and/or compiler aren’t happy by the way cmake quotes the parameters when the resultant make file is executed.

This is a tiny part of the build for retrobsd, in case you’re curious as to where this comes from.

Thanks

```make
all: ../libc.a

../libc.a: ../ar ../ranlib $(OBJS)
                ../ar rc $@ $(OBJS)
                ../ranlib $@

$(OBJS): $(LIBCDIR)/mips/sys/SYS.h
                @echo creating $*.o
                @printf '#include "SYS.h"\nSYS($*)\n' | $(AS) $(ASFLAGS) - -c -o $*.o

```

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [December 13, 2023, 11:06pm UTC](https://discourse.cmake.org/t/piping-the-output-of-a-script-to-the-stdin-of-a-compiler/9614/2 "2023-12-13T23:06:33Z")

</div>

Can you please format the make fragment as code? This is no valid makefile, I guess the `<` in `$<` were suppressed.

In general it looks like a normal static library with assembler compilation.  
And instead of having assembler files, the code is printed to STDOUT.

If that `SYS($)` is a `SYS($<)` if would say, you do compile the identical assembler code with different object names. I don’t know, why something like this should be done.  
It’s probably easier to understand, if you post some command lines, of what the make actually executed.

In CMake i would definitely generate the assembler source with `file(GENERATE)` or such.  
And add them to a normal static library, so CMake cares about the details of compilation and archiving.

```CMake
set(Header ${LIBCDIR}/mips/sys/SYS.h)
file(GENERATE
  OUTPUT x.asm
  CONTENT
    [[#include “SYS.h”]] "\n"
    "SYS(${Header})\n"
)
add_library(c
  PRIVATE
    ${CMAKE_CURRENT_BINARY_DIR}/x.asm
)

```

---

<div class="post-metadata">

### Author: ![Agent13](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/cdc98d/32.png) [@Agent13](https://discourse.cmake.org/u/Agent13)
#### Post date: [December 15, 2023, 1:07am UTC](https://discourse.cmake.org/t/piping-the-output-of-a-script-to-the-stdin-of-a-compiler/9614/3 "2023-12-15T01:07:41Z")

</div>

Hi Josef,

Thanks for replying to my post. As you indicated some of the characters were suppressed in my original post. It should have looked like the following:

```auto
all: ../libc.a

../libc.a: ../ar ../ranlib $(OBJS)
                ../ar rc $@ $(OBJS)
                ../ranlib $@

$(OBJS): $(LIBCDIR)/mips/sys/SYS.h
                @echo creating $*.o
                @printf '#include "SYS.h"\nSYS($*)\n' | $(AS) $(ASFLAGS) - -c -o $*.o

```

My bad ☹

---

<div class="post-metadata">

### Author: ![Agent13](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/cdc98d/32.png) [@Agent13](https://discourse.cmake.org/u/Agent13)
#### Post date: [December 15, 2023, 1:10am UTC](https://discourse.cmake.org/t/piping-the-output-of-a-script-to-the-stdin-of-a-compiler/9614/4 "2023-12-15T01:10:33Z")

</div>

It seems that one cannot edit something that has been posted? Anyhow

$(SYSOBJS) should be $(OBJS)

in my previous post

---

<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 19, 2023, 3:06am UTC](https://discourse.cmake.org/t/piping-the-output-of-a-script-to-the-stdin-of-a-compiler/9614/5 "2023-12-19T03:06:02Z")

</div>

> [@Agent13](#):
>
> It seems that one cannot edit something that has been posted?

It might be a new user restriction. I’ve updated the original post with formatting and your comment here with the `OBJS` fix.

---

<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 19, 2023, 3:08am UTC](https://discourse.cmake.org/t/piping-the-output-of-a-script-to-the-stdin-of-a-compiler/9614/6 "2023-12-19T03:08:07Z")

</div>

I would definitely go with file generation here (`configure_file` may suffice as well). These kinds of shenanigans don’t map well to all of CMake’s generators and, as such, there’s not really an abstraction for them. While I have seen some ways to smuggle things through, it’s not portable and I’d recommend adapting to CMake’s abstractions rather than trying to pierce them (they’re leaky enough already, but you may also interfere with the inner workings inadvertently).

---

<div class="post-metadata">

### Author: ![Agent13](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/cdc98d/32.png) [@Agent13](https://discourse.cmake.org/u/Agent13)
#### Post date: [December 20, 2023, 12:37am UTC](https://discourse.cmake.org/t/piping-the-output-of-a-script-to-the-stdin-of-a-compiler/9614/7 "2023-12-20T00:37:18Z")

</div>

Thanks for the info Ben.
