QMake复制文件/目录方法

2020-08-04 11:59:36 浏览数 (1)

使用QMake语法方便复制文件或文件夹。

使用

代码语言:javascript复制
# 配置file_copies
CONFIG  = file_copies

# 创建examples变量并配置
# 配置需要复制的文件或目录(支持通配符)
examples.files = $$PWD/MyExamples
# 配置需要复制的目标目录, $$OUT_PWD为QMake内置变量,含义为程序输出目录
examples.path = $$OUT_PWD

# 配置COPIES
COPIES  = examples

来源

  从Qt5.6引入,该方法是非官方文档说明,后续可能不支持。

代码语言:javascript复制
https://codereview.qt-project.org/c/qt/qtbase/ /156784

  以Qt 5.12.1的MSVC版本文件路径。

代码语言:javascript复制
C:QtQt5.12.15.12.1msvc2017_64mkspecsfeaturesfile_copies.prf

实现

file_copies.prf对应源码:

代码语言:javascript复制
isEmpty(COPIES): return()
contains(TEMPLATE, .*subdirs): error("COPIES does not work with TEMPLATE=subdirs")

build_pass:build_all:!isEqual(BUILD_PASS, $$first(BUILDS)) {
    # Avoid that multiple build passes race with each other.
    # This will fail to copy anything if the user explicitly invokes
    # only the non-primary build. This is unfixable, as at qmake time
    # we cannot possibly know how make will be invoked, yet we must
    # predict it here.
    return()
}

defineReplace(qtStripProPwd) {
    return($$relative_path($$1, $$_PRO_FILE_PWD_))
}

for (cp, COPIES) {
    isEmpty($${cp}.files): next()
    pfx = copy_$${cp}
    notdir = false
    dir = false
    for (f, $${cp}.files) {
        fil = $$absolute_path($$f, $$_PRO_FILE_PWD_)
        tfiles = $$files($$fil/*)
        isEmpty(tfiles): 
            notdir = true
        else: 
            dir = true
        $${pfx}.files  = $$fil
    }
    $$dir:$$notdir: 
        error("COPIES entry $$cp lists both files and directories.")
    path = $$eval($${cp}.path)
    isEmpty(path): error("COPIES entry $$cp defines no .path")
    base = $$eval($${cp}.base)
    isEmpty(base) {
        $${pfx}.output = $$path/${QMAKE_FILE_IN_NAME}
    } else: isEqual(base, $$_PRO_FILE_PWD_) {
        $${pfx}.output = $$path/${QMAKE_FUNC_FILE_IN_qtStripProPwd}
    } else {
        eval(defineReplace(qtStripSrcDir_$$cp) { 
            return($$relative_path($$1, $$val_escape(base))) 
        })
        $${pfx}.output = $$path/${QMAKE_FUNC_FILE_IN_qtStripSrcDir_$$cp}
    }
    $${pfx}.input = $${pfx}.files
    $${pfx}.commands = $(QINSTALL) ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT}
    $${pfx}.name = COPY ${QMAKE_FILE_IN}
    $${pfx}.CONFIG = no_link no_clean target_predeps
    QMAKE_EXTRA_COMPILERS  = $${pfx}
}
qt

0 人点赞