Cmake commands as macro arguments [duplicate] - cmake

This question already has answers here:
How to call a function in CMake whose name is stored in a variable
(5 answers)
Closed 11 months ago.
I want to use a CMake macro as a text replacement tool to call a command. This is something I am familiar with in C, but I am not sure if this is allowed in CMake.
what I want to be able to do:
MY_MACRO(
MY_COMMAND(ARGS)
)
I have tried a few different variations on this general idea on how to implement this, but I am not sure it is possible.
MACRO(MY_MACRO)
# do stuff
${ARGN} # Call MY_COMMAND
# do stuff
)

I figured this out based on this thread:
https://gitlab.kitware.com/cmake/cmake/-/issues/20800#note_951424
I haven't tested very thoroughly, but this is the gist at least:
MACRO(MY_MACRO)
#stuff
CMAKE_LANGUAGE(EVAL CODE ${ARGN})
#stuff
ENDMACRO(MY_MACRO)

Related

How to use Set directory properties [duplicate]

This question already has answers here:
How do I add a linker or compile flag in a CMake file?
(7 answers)
Closed last year.
I used Set_directory_properties(PROPERTIES COMPILE_DEFINITIONS "--saferc=none") to mask misra check in directory, but I got Warning:
Ccv850:Warning:option "-D--saferc=none" ignored due to invalid argument. Expected name or name=string.
You should basically never use directory properties, ever.
For whichever targets cannot compile without the flag, you can add it like so:
target_compile_options(
my_target
PRIVATE
"$<$<CXX_COMPILER_ID:GHS>:--saferc=none>"
"$<$<C_COMPILER_ID:GHS>:--saferc=none>"
)
My understanding is that --saferc=none is a Green Hills flag, hence the check for $<CXX_COMPILER_ID:GHS>. If you are only using one of C or C++, you can delete the flag for the other language.

How to uncomment multiple lines of a Cmake file in notepad++? [duplicate]

This question already has answers here:
Commenting code in Notepad++
(11 answers)
Closed 2 years ago.
My cmake file looks like this. I want to uncomment the lines 1 through 100 with a single shortcut
#line1
#line2
#line3
.
.
.
#line100
build(a_library
USING
b
c)
if(FALSE) # fake a block comment
endif()
Note that this works well for commenting out a block of valid CMake code, it doesn't allow you write plain text within the if block
As of CMake 3.0 there is a special syntax for block comments which start with #[[ and ends with ]] at the end of block comment.

Ternary generator expression not evaluating [duplicate]

This question already has an answer here:
CMake generator expression is not evaluated
(1 answer)
Closed 3 years ago.
Suppose I have an option:
option(TARGET_IS_ADDIN "Shared library target is an Addin" ON)
and a variable:
set(MyVar $<IF:$<BOOL:${TARGET_IS_ADDIN}>,addin,script>)
why is this getting stored as:
"$<IF:$<BOOL:ON>,addin,script>"
and not "addin"?
While the value for your option TARGET_IS_ADDIN is expanded at CMake's configuration stage, generator expressions are evaluated at CMake's buildsystem generation stage. Thus, when you attempt to print a generator expression using message(), you will see its un-expanded form, as the generation stage has not yet taken place.
Furthermore, not all of CMake's commands even accept generator expressions. The set() command is not one that can process generator expressions because it is processed at the configuration stage. If a command can process generator expressions, it will be explicitly mentioned in that command's documentation (e.g. target_link_libraries() or target_compile_options()).
For examples, see Tsyvarev's answer here.

How to put several paths in CMAKE_PREFIX_PATH? [duplicate]

This question already has an answer here:
Multiple Cmake_Prefix_Paths
(1 answer)
Closed 6 years ago.
How can I have several paths for cmake to look for needed libraries. I installed zlib and libpng under /usr/local/zlib and /usr/local/libpng however, what I'm currently doing is first cmake -DCMAKE_PREFIX_PATH=/usr/local/zlib, then issuing a second command `cmake -DCMAKE_PREFIX_PATH=/usr/local/libpng" in order for cmake to recognize both.
Is there a way to have both paths in the same variable?
I tried -DCMAKE_PREFIX_PATH=/usr/local/zlib:/usr/local/libpng but it didn't work.
You need to use ; character instead of : to define lists.

Makefile: updating variables [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
arithmetic in a Makefile
I'm very much a beginner using Makefile. I would like to update a variable, like this more or less:
LEVEL=0
train:
while (eval_score > previous_eval_score)
make iterate
iterate:
do stuff
write files to /path/to/$(LEVEL)/asdf.txt
$(LEVEL)++
In other words, every iteration writes some files to a directory, first to /path/to/0, then /path/to/1, etc. until some threshold is reached.
So apparently it's not allowed to update or reassign a variable inside a function. I have searched for a while now but can't find any satisfactory answers. Maybe I have but I don't understand everything. I have also tried stuff like calculating in bash but eventually I still have to update the variable. Perhaps I should try something like update a file instead and for every iteration, just read from the file?
Thanks for your time.
The main problem with your Makefile is that each make iterate spawns a sub-make, which could update its environment variables, but with no effect on the parent make and thus no effect on the next iteration. In addition, you cannot update variables from within rules, but that is a secondary problem.
You can do iterative make just like you suggested and increment the level via the shell:
train:
LEVEL=0; while need_more_iterations; do \
$(MAKE) LEVEL=$$LEVEL iterate; \
LEVEL=`echo $$LEVEL + 1 | bc`; \
done