Issue
I have a project that is configured via CMake, which I have imported into Eclipse and I can compile without issue.
I would like to check the size of certain files and to do so, I am invoking the defined size utility via Cmake command as follows
add_custom_command(
TARGET myTarget
POST_BUILD
COMMENT "Unit sizes"
COMMAND ${CMAKE_SIZE_UTIL} "C:/LocalData/Projects/build_dir/debug/apps/myApp/code/CMakeFiles/units.dir/myUnit/src/*.o"
)
Above command work fine since I am providing the hard-coded path to the *.o
files
Unit sizes
text data bss dec hex filename
xxxx x xx xx xxx C:/LocalData/Projects/build_dir/debug/apps/myApp/code/CMakeFiles/units.dir/myUnit/src/a.o
xxx x xx xx xxx C:/LocalData/Projects/build_dir/debug/apps/myApp/code/CMakeFiles/units.dir/myUnit/src/b.o
However, I would like to replace the hardcoded path in the Cmake custom_command by a variable myPath
defined within Eclipse that contains this path, i.e.myPath=C:/LocalData/Projects/build_dir/debug/apps/myApp
, thus it would be more flexible.
add_custom_command(
TARGET myTarget
POST_BUILD
COMMENT "Unit sizes"
COMMAND ${CMAKE_SIZE_UTIL} "${myPath}/code/CMakeFiles/units.dir/myUnit/src/*.o"
)
Can I achieve this? If so, how can I do this?
Thanks in advance
Solution
You should not use an environment variable for this. It's brittle because a user can forget to set it, and redundant because $<TARGET_OBJECTS:...>
exists.
Based on seeing code/CMakeFiles/units.dir
in your code, I'm assuming you have a target named units
declared somewhere. If this is the case, then the fully correct thing to do is just to have CMake paste in the target objects directly via a generator expression:
add_custom_command(
TARGET myTarget POST_BUILD
COMMAND ${CMAKE_SIZE_UTIL} "$<TARGET_OBJECTS:units>"
COMMAND_EXPAND_LISTS
)
That will list exactly the object files of units
on the command line as separate arguments (COMMAND_EXPAND_LISTS
does that), no matter where they are located on disk. This is definitely better practice than munging around inside CMakeFiles
directories (whose layouts are technically allowed to change without notice).
Answered By - Alex Reinking