Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake : softlink resource ( such as GLSL shaders ) or copy each complilation

The simplest way how to copy resources from source directory into build directory with CMake is

file( COPY ${CMAKE_CURRENT_SOURCE_DIR}/resources DESTINATION ${CMAKE_CURRENT_BINARY_DIR} )

however, this updates the resources in build directory only when I call cmake. I need something which update resources each time I call make.

E.g. now I develop some GLSL shaders. I need to change simultaneously both the C++ code and GLSL code, and I need everything is synchronized each time I hit compile or run in my IDE ( I use CodeBlocks with project files generated by CMake )

The simple solution would be to make softlink from source directory to build directory. But I don't want to do it manually (it would be by-passing of CMake and would make project more fragile ). Can CMake do it for me ?


just for completness, this is how my whole CMakeList.txt looks like

# ==== common header

cmake_minimum_required ( VERSION 2.8 )
project ( SimpleSimulationEngine )

if( UNIX )
    SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++0x" )
endif()

SET( AXULIARY_COMPILE_FLAGS "-w -O2")
SET( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${AXULIARY_COMPILE_FLAGS}" )
SET( COMMON_SRCS "${CMAKE_SOURCE_DIR}/common" )
include_directories(
        ${COMMON_SRCS}
        ${COMMON_SRCS}/algorithms
        ${COMMON_SRCS}/math
        ${COMMON_SRCS}/SDL2OGL3
)

set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake_utils )

find_package( OpenGL REQUIRED )
find_package( GLEW   REQUIRED )
find_package( GLU    REQUIRED )
find_package( SDL2   REQUIRED )

# ==== Particular build target

add_executable       ( test_SphereShader test_SphereShader.cpp )
target_link_libraries( test_SphereShader ${OpenGL_LIBRARY} ${GLU_LIBRARY} ${GLEW_LIBRARY} ${SDL2_LIBRARY} )
file( COPY ${CMAKE_CURRENT_SOURCE_DIR}/shaders DESTINATION ${CMAKE_CURRENT_BINARY_DIR} )
like image 824
Prokop Hapala Avatar asked Apr 07 '16 17:04

Prokop Hapala


1 Answers

You can provide a custom target with you turn into a dependency for your binary test_SphereShader:

ADD_CUSTOM_TARGET(
        copy_shader_files
        ${CMAKE_COMMAND}
        -D SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}
        -D DESTINATION_DIR=${CMAKE_CURRENT_BINARY_DIR} 
        -P CopyFile.cmake
        COMMENT "Copying Files for target: test_SphereShader" VERBATIM 
        )

ADD_DEPENDENCIES ( test_SphereShader copy_shader_files )

Using the custom target invokes a new CMake instance with a script file CopyFile.cmake which contains your copy command:

file( COPY ${CMAKE_CURRENT_SOURCE_DIR}/shaders DESTINATION ${CMAKE_CURRENT_BINARY_DIR} )

This way the file is copied every time make or make test_SphereShader is invoked. You can even copy manually be calling make copy_shader_files.

like image 70
ToniBig Avatar answered Oct 19 '22 09:10

ToniBig