Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake and configure_file: what if I need $ONLY instead of @ONLY?

Tags:

As from the documentation of configure_file, it has an useful @ONLY parameter used to:

Restrict variable replacement to references of the form @VAR@. This is useful for configuring scripts that use ${VAR} syntax.

So far so good.

Now I'm in the case where I've a file that contains both @FOO@ and ${BAR} and I want to replace S{BAR} with a variable that comes from my CMakeLists.tx. On the other side I don't want that configure_file tries to replace @FOO@.
In other terms, I need something like a $ONLY parameter that works as @ONLY but for the opposite case.

Is there any chance I can do that with what I have right now?
I tried putting backslashes in front of my @, but this way I obtain a file that contains a couple of \@ and this is not exactly what I need.

like image 971
skypjack Avatar asked Jul 26 '17 08:07

skypjack


1 Answers

At least I found a solution that works, even if I'm not sure that's the best approach ever.
I leave it as an answer for future readers in case no solutions come up.

Suppose the line to be elaborated looks like this:

@DontTouchMe@/${PROJECT_NAME}

To be able to skip the @DontTouchMe@ part when you pass the file to configure_file, you can change the line as it follows:

@@Workaround@DontTouchMe@Workaround@@/${PROJECT_NAME}

configure_file will match and replace @Workaround@ parts with an empty string and forget about the other @ around, so that you obtain the following line after the substitution:

@DontTouchMe@/TheNameOfTheProject

Quite dirty and it could break out with future releases of cmake for it actually exploits a bug (uhm, I'm not sure I can call it this way actually) of the tool.
Anyway, it works and can be a viable approach for those out there that are still looking for a possible solution.

like image 147
skypjack Avatar answered Sep 24 '22 15:09

skypjack