为什么CMake会删除add_custom_command的源文件

时间:2019-10-19 12:46:45

标签: makefile cmake glsl

我有一个CMake函数,可以将我拥有的某些(GLSL)代码编译为运行我的源代码所需的格式。该函数接收一些输入文件,在它们上运行一些脚本,并输出一些相应的文件。该函数在Windows上运行良好,并且生成了我的输出。但是,在Mac(Unix?)上,生成输出后,我的源(输入)被删除了。为什么会发生这种情况?

这是我的功能:

# adds a custom target that compiles the GLSL code using the compile-glsl-code.py python script
function(build_glsl)
    set(optionArgs "")
    set(oneValueArgs "TARGET" "VULKAN")
    set(multiValueArgs "SOURCES")
    cmake_parse_arguments(FUNCTION_ARGS "${optionArgs}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    # Make sure python is installed
    find_package(Python3 COMPONENTS Interpreter)
    if (NOT Python3_FOUND)
        message(FATAL_ERROR "Could not find python3!")
    endif()

    if(WIN32)
        set(glslangValidator ${FUNCTION_ARGS_VULKAN}/Bin/glslangValidator.exe)
    else()
        set(glslangValidator ${FUNCTION_ARGS_VULKAN}/bin/glslangValidator)
    endif()
    set(spvConverter ${CMAKE_SOURCE_DIR}/src/spv-converter.py)
    set(entryPoint "main")
    set(GLSL_COMPILED_SOURCES "")
    foreach(file IN LISTS FUNCTION_ARGS_SOURCES)
        if(file MATCHES ".*.vert.glsl$" OR file MATCHES ".*.frag.glsl$" OR file MATCHES ".*.geom.glsl$" OR file MATCHES ".*.tesc.glsl$" OR file MATCHES ".*.tese.glsl$" OR file MATCHES ".*.comp.glsl$")
            set(tmpFile ${file}.tmp.spv)
            set(outputFile ${file}.spv)
            list(APPEND GLSL_COMPILED_SOURCES ${outputFile})
            add_custom_command(
                OUTPUT ${outputFile}
                COMMAND ${glslangValidator} "-V" "--entry-point" "${entryPoint}" "${file}" "-o" "${tmpFile}" # compile ${file} into ${tmpFile}
                COMMAND ${Python3_EXECUTABLE} ${spvConverter} "${tmpFile}" "${outputFile}" # compile ${tmpFile} into ${outputFile}
                COMMAND ${CMAKE_COMMAND} -E remove -f ${tmpFile} # delete ${tmpFile}
                SOURCES ${file}
                VERBATIM
            )
        endif()
    endforeach()

    add_custom_target(${FUNCTION_ARGS_TARGET}
        SOURCES ${FUNCTION_ARGS_SOURCES}
        DEPENDS ${GLSL_COMPILED_SOURCES}
        VERBATIM
    )
endfunction()

更简洁地说,这部分:

add_custom_command(
    OUTPUT ${outputFile}
    COMMAND ${glslangValidator} "-V" "--entry-point" "${entryPoint}" "${file}" "-o" "${tmpFile}" # compile ${file} into ${tmpFile}
    COMMAND ${Python3_EXECUTABLE} ${spvConverter} "${tmpFile}" "${outputFile}" # compile ${tmpFile} into ${outputFile}
    COMMAND ${CMAKE_COMMAND} -E remove -f ${tmpFile} # delete ${tmpFile}
    SOURCES ${file}
    VERBATIM
)

glslangValidator和python脚本不会删除源文件。如果我删除COMMAND ${CMAKE_COMMAND} -E remove -f ${tmpFile}行,那么它不会删除我的源文件(也不删除tmp文件),但是我确实要删除tmp文件。可能是什么原因造成的?

0 个答案:

没有答案