Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip path off of variable in CMake

Tags:

cmake

Is there is way I can strip the path off of a variable in my CMakeLists.txt? Say I have a variable

RUNTIME_DEPENDENCIES = {
    src/frontend/solve.py
    src/frontend/other.py
}

these runtime dependencies are copied over to the bin for the executable to use during execution, but with the way I'm copying them they appear inside their respective folders. So my build folder looks like

    build
      | \
     src executable
      |
   frontend
   /       \
other.py  solve.py

i'd like the folder to simply contain

           build
    /       /         \
executable  other.py  solve.py

here's the actualy copy command used:

#copy runtime deps
add_custom_target(copy_runtime_dependencies ALL)
foreach(RUNTIME_DEPENDENCY ${RUNTIME_DEPENDENCIES})
    add_custom_command(TARGET copy_runtime_dependencies POST_BUILD
                       COMMAND ${CMAKE_COMMAND} -E copy
                       ${CMAKE_SOURCE_DIR}/${RUNTIME_DEPENDENCY}
                       $<TARGET_FILE_DIR:${EXECUTABLE_NAME}>/${RUNTIME_DEPENDENCY})
endforeach()

I realize I could simply change the 5th line above to

${CMAKE_SOURCE_DIR}/src/frontend/${RUNTIME_DEPENDENCY}

but in the case I have multiple dependencies outside of the src/frontend I don't want to have to write multiple seperate copy statements in my CMakeLists.txt

like image 736
Syntactic Fructose Avatar asked Dec 05 '22 04:12

Syntactic Fructose


1 Answers

The function get_filename_component does this:

get_filename_component(barename ${RUNTIME_DEPENDENCY} NAME)

barename is the output variable to hold the filename with path removed. For example:

get_filename_component(barename "src/frontend/solve.py" NAME)
message(STATUS ${barename})

would print solve.py

like image 102
Peter Avatar answered Dec 06 '22 18:12

Peter