0

I want to have a cmake target that updates a specific file. I tried following but it seems that I cannot put all what I want into a custom target.

ADD_CUSTOM_TARGET(update
            file(READ "file.h" myFile)
            string(REPLACE "originalString" "newString" myFile"${myFile}"))
            )

I'm quite new to cmake so I may be missing something basic. How could I update the file on request ?

Julien
  • 1,810
  • 1
  • 16
  • 34

1 Answers1

2

What I personally use for this type of problem is use configure_file. Essentially, you can make what you consider a template file and wrap variable names in the files.

Example.h.template
Here is my @VAR@

Then if you have VAR defined in your CMake project, it will replace VAR in your destination.

configure_file(<input> <output> NEWLINE_STYLE WIN32)

If input (your template) is modified, CMake will be re-run to keep your file up to date. So in your cmake you can

set(VAR "program")
configure_file("Path/To/Example.h.template" "Path/To/File.h" NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF])

The result should be:

File.h
Here is my program
Chris
  • 2,254
  • 8
  • 22