If this is going to be in a package’s config file, make sure you unset any temporary variables so they don’t leak into the calling scope. You should also name any temporary variables with names that are specific to your package so they don’t clash with any variables in the consuming project.
set(ChangelogGenerator_INCLUDE_DIR @PACKAGE_${current_project_name}_INCLUDE_DIR@) #incorrect
set(A_LIBRARY_DIR PROJECT_NAME) #incorrect
set(A_INCLUDE_DIR2 "C:/Users/xxxx/Desktop/Coding/cmakeLib/include") #correct (do not care about absolute path this is for the example)
Sorry, I was in a rush when I replied yesterday. My suggestion was nonsense.
For the specific example you gave, don’t try to overuse PROJECT_NAME. In fact, I would suggest you should not have used it at all anywhere in your example code. The reality is that you should not really ever change the project name once the project is more than just a quick prototype that only one person uses. As soon as others might be using your project as a dependency, the project name should stay the same because consumers may rely on it.
Another reason is that many people confuse the concept of “project” and “target”. They are two different things, but it is a common mistake that people use ${PROJECT_NAME} throughout their code in places where a target name is expected. This makes the code confusing to read. Even for projects having just one main target, you gain nothing by putting ${PROJECT_NAME} instead of the actual target name directly. Either way, you’re copying around a string that has to be identical in all places. You may as well put the actual target name there to improve readability.
Interestingly, your original problem is another case of using ${PROJECT_NAME} in places where something else is expected. While the majority of projects do use the same name for the project name and their package name (and that’s usually a sensible thing to do), it also isn’t required to be that way. And the same argument applies as for targets - once you have consumers of your package, you shouldn’t change the package name because consumers will depend on it not changing. So once again, you can and should just hard-code the package name in places where you need to use it.
If you follow the above advice, your original problem goes away. Your code line would reduce to (if the package name is A):
set_and_check(@MYVAR@ "@PACKAGE_A@")
Now it also becomes clear that this probably doesn’t make sense either. Why prefix A with PACKAGE_...? That would only be needed for something that evaluates to a path of something in your install area. Your expanded example in your latest reply makes a bit more sense, and it reduces down to something much clearer when you get rid of all the ${PROJECT_NAME} uses:
How about configuring that file twice, once for @@ style with @ONLY and once for ${} style.
However, I think you did not make your use case clear. It looks like an unusual approach that may be usually solved completely different. Maybe, you can show us what you actually want to achieve?
is that it is not well-defined. You could also have @PACKAGE_@ and @_INCLUDE_DIR@ as replacements. I already mentioned the two-stage approach. This also solves the ambiguity.