CMakeCopyIfDifferent: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
m (Add CMakeMacro category)
(CMakeCopyIfDifferent: Use preformatted blocks)
Line 1: Line 1:
Generates a rule to copy each source file from source directory  
Generates a rule to copy each source file from source directory
to destination directory.  
to destination directory.


Typical use -  
Typical use -


SET(SRC_FILES head1.h head2.h head3.h)<br>
<pre>
COPY_IF_DIFFERENT( /from_dir /to_dir ${SRC_FILES} IncludeTargets "Includes")<br>
SET(SRC_FILES head1.h head2.h head3.h)
ADD_TARGET(CopyIncludes ALL DEPENDS ${IncludeTargets})<br>
COPY_IF_DIFFERENT( /from_dir /to_dir ${SRC_FILES} IncludeTargets "Includes")
ADD_TARGET(CopyIncludes ALL DEPENDS ${IncludeTargets})
</pre>


will generate rules that look  
will generate rules that look
----
----


CopyIncludes: $(CopyIncludes_DEPEND_LIBS) \<br>
<pre>
Include\head1.h \<br>
CopyIncludes: $(CopyIncludes_DEPEND_LIBS) \
Include\head2.h \<br>
  Include\head1.h \
Include\head3.h <br>
  Include\head2.h \
<br>
  Include\head3.h
Include\head1.h:<br>
 
@echo Building Copying head1.h /to_dir head1.h...<br>
Include\head1.h:
cmake.exe -E copy_if_different /from_dir/head1.h  /to_dir/head1.h <br>
@echo Building Copying head1.h /to_dir head1.h...
cmake.exe -E copy_if_different /from_dir/head1.h  /to_dir/head1.h
</pre>


----
----


    MACRO(COPY_IF_DIFFERENT FROM_DIR TO_DIR FILES TARGETS TAGS)
<pre>
    # Macro to implement copy_if_different for a list of files
MACRO(COPY_IF_DIFFERENT FROM_DIR TO_DIR FILES TARGETS TAGS)
    # Arguments -  
# Macro to implement copy_if_different for a list of files
    #  FROM_DIR - this is the source directory
# Arguments -
    #  TO_DIR  - this is the destination directory
#  FROM_DIR - this is the source directory
    #  FILES    - names of the files to copy  
#  TO_DIR  - this is the destination directory
    #              TODO: add globing.  
#  FILES    - names of the files to copy
    #  TARGETS  - List of targets
#              TODO: add globing.
    #  TAGS    - Since only the file name is used
#  TARGETS  - List of targets
    #              to generate rules, pre-pend a user  
#  TAGS    - Since only the file name is used
    #              supplied tag to prevent duplicate rule errors.  
#              to generate rules, pre-pend a user
    SET(AddTargets "")
#              supplied tag to prevent duplicate rule errors.
    FOREACH(SRC ${FILES})
SET(AddTargets "")
        GET_FILENAME_COMPONENT(SRCFILE ${SRC} NAME)  
FOREACH(SRC ${FILES})
        # Command to copy the files to ${STEP1}/src, if changed.
    GET_FILENAME_COMPONENT(SRCFILE ${SRC} NAME)
        SET(TARGET  "${TAGS}/${SRCFILE}")
    # Command to copy the files to ${STEP1}/src, if changed.
        IF("${FROM_DIR}" STREQUAL "")
    SET(TARGET  "${TAGS}/${SRCFILE}")
            SET(FROM ${SRC})
    IF("${FROM_DIR}" STREQUAL "")
        ELSE("${FROM_DIR}" STREQUAL "")
        SET(FROM ${SRC})
            SET(FROM ${FROM_DIR}/${SRC})
    ELSE("${FROM_DIR}" STREQUAL "")
        ENDIF("${FROM_DIR}" STREQUAL "")      
        SET(FROM ${FROM_DIR}/${SRC})
        IF("${TO_DIR}" STREQUAL "")
    ENDIF("${FROM_DIR}" STREQUAL "")
            SET(TO ${SRCFILE})
    IF("${TO_DIR}" STREQUAL "")
        ELSE("${TO_DIR}" STREQUAL "")
        SET(TO ${SRCFILE})
            SET(TO  ${TO_DIR}/${SRCFILE})
    ELSE("${TO_DIR}" STREQUAL "")
        ENDIF("${TO_DIR}" STREQUAL "")
        SET(TO  ${TO_DIR}/${SRCFILE})
        ADD_CUSTOM_COMMAND(
    ENDIF("${TO_DIR}" STREQUAL "")
            OUTPUT  ${TARGET}
    ADD_CUSTOM_COMMAND(
            COMMAND ${CMAKE_COMMAND}
        OUTPUT  ${TARGET}
            ARGS    -E copy_if_different ${FROM} ${TO}
        COMMAND ${CMAKE_COMMAND}
            COMMENT "Copying ${SRCFILE} ${TO_DIR}"
        ARGS    -E copy_if_different ${FROM} ${TO}
            )
        COMMENT "Copying ${SRCFILE} ${TO_DIR}"
        SET(AddTargets ${AddTargets} ${TARGET})
        )
    ENDFOREACH(SRC ${FILES})
    SET(AddTargets ${AddTargets} ${TARGET})
    SET(${TARGETS} ${AddTargets})
ENDFOREACH(SRC ${FILES})
    ENDMACRO(COPY_IF_DIFFERENT FROM_DIR TO_DIR FILES TARGETS TAGS)
SET(${TARGETS} ${AddTargets})
ENDMACRO(COPY_IF_DIFFERENT FROM_DIR TO_DIR FILES TARGETS TAGS)
</pre>


{{CMake/Template/Footer}}
{{CMake/Template/Footer}}
[[Category:CMakeMacro]]
[[Category:CMakeMacro]]

Revision as of 14:19, 20 April 2018

Generates a rule to copy each source file from source directory to destination directory.

Typical use -

SET(SRC_FILES head1.h head2.h head3.h)
COPY_IF_DIFFERENT( /from_dir /to_dir ${SRC_FILES} IncludeTargets "Includes")
ADD_TARGET(CopyIncludes ALL DEPENDS ${IncludeTargets})

will generate rules that look


CopyIncludes: $(CopyIncludes_DEPEND_LIBS) \
  Include\head1.h \
  Include\head2.h \
  Include\head3.h

Include\head1.h:
	@echo Building Copying head1.h /to_dir head1.h...
	cmake.exe -E copy_if_different /from_dir/head1.h  /to_dir/head1.h

MACRO(COPY_IF_DIFFERENT FROM_DIR TO_DIR FILES TARGETS TAGS)
# Macro to implement copy_if_different for a list of files
# Arguments -
#   FROM_DIR - this is the source directory
#   TO_DIR   - this is the destination directory
#   FILES    - names of the files to copy
#              TODO: add globing.
#   TARGETS  - List of targets
#   TAGS     - Since only the file name is used
#              to generate rules, pre-pend a user
#              supplied tag to prevent duplicate rule errors.
SET(AddTargets "")
FOREACH(SRC ${FILES})
    GET_FILENAME_COMPONENT(SRCFILE ${SRC} NAME)
    # Command to copy the files to ${STEP1}/src, if changed.
    SET(TARGET  "${TAGS}/${SRCFILE}")
    IF("${FROM_DIR}" STREQUAL "")
        SET(FROM ${SRC})
    ELSE("${FROM_DIR}" STREQUAL "")
        SET(FROM ${FROM_DIR}/${SRC})
    ENDIF("${FROM_DIR}" STREQUAL "")
    IF("${TO_DIR}" STREQUAL "")
        SET(TO ${SRCFILE})
    ELSE("${TO_DIR}" STREQUAL "")
        SET(TO   ${TO_DIR}/${SRCFILE})
    ENDIF("${TO_DIR}" STREQUAL "")
    ADD_CUSTOM_COMMAND(
        OUTPUT  ${TARGET}
        COMMAND ${CMAKE_COMMAND}
        ARGS    -E copy_if_different ${FROM} ${TO}
        COMMENT "Copying ${SRCFILE} ${TO_DIR}"
        )
    SET(AddTargets ${AddTargets} ${TARGET})
ENDFOREACH(SRC ${FILES})
SET(${TARGETS} ${AddTargets})
ENDMACRO(COPY_IF_DIFFERENT FROM_DIR TO_DIR FILES TARGETS TAGS)



CMake: [Welcome | Site Map]