qt实现linux下文件复制文件路径,qt-QMake-如何将文件复制到输出

这篇博客介绍了如何在项目构建过程中使用自定义函数进行文件和目录的操作,包括在构建前复制文件和目录,以及在构建完成后执行的文件复制。这些函数如`copyFileLater`和`copyDirLater`确保文件在链接操作后被复制,适用于需要在构建结束时执行的文件操作。此外,还提供了创建目录、删除文件和目录的功能,并演示了如何在构建后运行QMake脚本。
摘要由CSDN通过智能技术生成

首先,在以下位置(来自copyFileLater(source, destination)框架)进行定义,例如在XD文件内部:

# --------------------------------------

# This file defines few useful functions

# --------------------------------------

#copyDir(source, destination)

# using "shell_path()" to correct path depending on platform

# escaping quotes and backslashes for file paths

defineTest(copyDir) {

#append copy command

!isEmpty(xd_copydir.commands): xd_copydir.commands += && \\$$escape_expand(\n\t)

xd_copydir.commands += ( $(COPY_DIR) \"$$shell_path($$1)\" \"$$shell_path($$2)\" || echo \"copy failed\" )

#the qmake generated MakeFile contains "first" and we depend that on "xd_copydir"

first.depends *= xd_copydir

QMAKE_EXTRA_TARGETS *= first xd_copydir

export(first.depends)

export(xd_copydir.commands)

export(QMAKE_EXTRA_TARGETS)

}

#copy(source, destination) (i.e. the name "copyFile" was reserved)

defineTest(copyFile) {

#append copy command

!isEmpty(xd_copyfile.commands): xd_copyfile.commands += && \\$$escape_expand(\n\t)

xd_copyfile.commands += ( $(COPY_FILE) \"$$shell_path($$1)\" \"$$shell_path($$2)\" || echo \"copy failed\" )

#the qmake generated MakeFile contains "first" and we depend that on "xd_copyfile"

first.depends *= xd_copyfile

QMAKE_EXTRA_TARGETS *= first xd_copyfile

export(first.depends)

export(xd_copyfile.commands)

export(QMAKE_EXTRA_TARGETS)

}

并在您的项目中使用它,例如:

include($$PWD/functions.prf) #optional

copyFile($$PWD/myfile1.txt, $$DESTDIR/myfile1.txt)

copyFile($$PWD/README.txt, $$DESTDIR/README.txt)

copyFile($$PWD/LICENSE, $$DESTDIR/LICENSE)

copyDir($$PWD/redist, $$DESTDIR/redist) #copy "redist" folder to "$$DESTDIR"

请注意,在执行链接操作之前,所有文件都会被复制(这可能很有用)。

copyFileLater(source, destination)完整脚本

但是,当您需要类似copyFileLater(source, destination)的文件时,仅在构建完成后才复制文件,然后考虑使用以下代码(来自Apache 2.0许可下的XD框架):

# --------------------------------------

# This file defines few useful functions

# --------------------------------------

xd_command_count = 0

#xd_prebuild(prefix, command)

defineTest(xd_prebuild) {

#generate target name with number

xd_command_count = $$num_add($$xd_command_count, 1)

name = $$1$$xd_command_count

#append command

eval( $${name}.commands += ( \$\$2 ) );

#the qmake generated "MakeFile" should contain "first"

# and we depend that on new command

!contains( first.depends, $$name ) {

!isEmpty(first.depends): first.depends += \\$$escape_expand(\\n)

first.depends += $$name

}

QMAKE_EXTRA_TARGETS *= first $$name

export(xd_command_count)

export($${name}.commands)

export(first.depends)

export(QMAKE_EXTRA_TARGETS)

#eval( warning(xd_push_command: $${name}.commands += \$\${$${name}.commands}) )

}

#xd_postbuild(command)

defineTest(xd_postbuild) {

!isEmpty(QMAKE_POST_LINK): QMAKE_POST_LINK = $$QMAKE_POST_LINK$$escape_expand(\\n\\t)

QMAKE_POST_LINK = $${QMAKE_POST_LINK}$$quote(-$$1)

export(QMAKE_POST_LINK)

}

#xd_escape(path)

# resolves path like built-in functions (i.e. counts input relative to $$PWD)

defineReplace(xd_escape) {

1 = $$absolute_path($$1)

#using "shell_path()" to correct path depending on platform

# escaping quotes and backslashes for file paths

1 = $$shell_path($$1)

return($$quote($$1))

}

#copyFile(source, destination)

# this will both copy and rename "source" to "destination", However like "copy_file()":

# if "destination" is path to existing directory or ends with slash (i.e. "/" or "\\"),

# will just copy to existing "destination" directory without any rename

#

# note: this is executed before build, but after qmake did exit

# so use "copy_file(...)" instead if the output file is required in qmake script

# like for example if "write_file(...)" is called on the output...

defineTest(copyFile) {

#note that "$(COPY_FILE)" is generated by qmake from "$$QMAKE_COPY_FILE"

xd_prebuild(xd_copyfile, $(COPY_FILE) $$xd_escape($$1) $$xd_escape($$2) || echo copyFile-failed)

}

#copyFileLater(source, destination = $(DESTDIR))

# note: this is executed after build is done, hence the name copy-later

defineTest(copyFileLater) {

destDir = $$2

isEmpty(destDir): destDir = $(DESTDIR)

#append copy command

xd_postbuild($(COPY_FILE) $$xd_escape($$1) $$xd_escape($$destDir) || echo copyFileLater-failed)

#!build_pass:warning(copyFile: $$1 to: $$destDir)

}

#copyDir(source, destination)

defineTest(copyDir) {

xd_prebuild(xd_copydir, $(COPY_DIR) $$xd_escape($$1) $$xd_escape($$2) || echo copyDir-failed)

}

#copyDirLater(source, destination = $(DESTDIR))

# note: this is executed after build is done, hence the name copy-later

defineTest(copyDirLater) {

destDir = $$2

isEmpty(destDir): destDir = $(DESTDIR)

#append copy command

xd_postbuild($(COPY_DIR) $$xd_escape($$1) $$xd_escape($$destDir) || echo copyDirLater-failed)

#!build_pass:warning(copyFile: $$1 to: $$destDir)

}

#makeDir(destination)

defineTest(makeDir) {

xd_prebuild(xd_makedir, $(MKDIR) $$xd_escape($$1) || echo makeDir-failed: \"$$1\")

}

defineTest(makeDirLater) {

xd_postbuild( $(MKDIR) $$xd_escape($$1) || echo makeDirLater-failed )

#!build_pass:warning(makeDirLater: $$1)

}

defineTest(deleteFile) {

xd_prebuild(xd_delfile, $(DEL_FILE) $$xd_escape($$1) || echo deleteFile-failed)

}

defineTest(deleteFileLater) {

xd_postbuild( $(DEL_FILE) $$xd_escape($$1) || echo deleteFileLater-failed )

#!build_pass:warning(deleteFileLater: $$1)

}

defineTest(deleteDir) {

xd_prebuild(xd_delfile, $(DEL_DIR) $$xd_escape($$1) || echo deleteDir-failed)

}

defineTest(deleteDirLater) {

xd_postbuild( $(DEL_DIR) $$xd_escape($$1) || echo deleteDirLater-failed )

#!build_pass:warning(deleteFileLater: $$1)

}

#qmakeLater(qmake-script-file-path-to-run)

# note that inside the script runned by this method

# $$OUT_PWD will be same as original $$OUT_PWD of qmakeLater(...) caller project

# since there is the "Makefile" that executes our custom qmake

defineTest(qmakeRun) {

xd_postbuild( $(QMAKE) $$xd_escape($$1) -r -spec \"$$shell_path($$QMAKESPEC)\" )

#!build_pass:warning(qmakeLater: $$1)

}

defineTest(qmakeLater) {

xd_postbuild( $(QMAKE) $$xd_escape($$1) -r -spec \"$$shell_path($$QMAKESPEC)\" )

#!build_pass:warning(qmakeLater: $$1)

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值