简 述:Ubuntu 20.04 中,将基于 Qt5 + CMake 的个人项目 PicShot ,通过 dh_make 制作一个.deb 安装包。

[TOC]


本文初发于 “偕臧的小站“,同步转载于此。


步骤流程

💻: Ubuntu 20.04 中,从源码包构建一个 deb 文件,依次执行如下即可:

$ sudo apt install wget tar dh-make debmake lintian # 下载工具包
$ cd ~/Desktop                                 
$ mkdir DebExample && cd DebExample                 # 创建一个空文件夹并且进入
$ git clone --recursive https://github.com/xmuli/PicShot.git  # 下载仓库和子模块仓库

$ cp -r PicShot/ picshot-0.1.2                      # 复制重命名格式<name>-<version>
$ tar -cvzf picshot-0.1.2.tar.gz picshot-0.1.2      # 制作 .tar.gz 的压缩包

$ cat >> ~/.bashrc <<EOF                            # 配置环境变量,用于模板填充作者名和邮箱
DEBEMAIL="xmulitech@gmail.com"                      # 维护者的邮箱
DEBFULLNAME="XMuli"                                 # 维护者的姓名
export DEBEMAIL DEBFULLNAME
export PATH=~/Qt5.12.11/5.12.11/gcc_64/bin:$PATH    # 代替 QT5_DIR 作用,编译用;可省略
EOF
$ . ~/.bashrc                                       # 更新使修改后的配置文件立刻生效;重启生效太 low 了

$ cd picshot-0.1.2                                  # *** 后续所有命令均在此下操作【提示】 ***
$ dh_make -f ../picshot-0.1.2.tar.gz -s -c mit -y   # 源码根目录下,生成 debian 模板目录

$ sudo apt install devscripts
$ vim debian/changelog                              # 用 dch -i 或 dch -v version-revision
$ vim debian/control                                # 修改文件
$ vim debian/copyright                              # 修改版权信息

$ dpkg-buildpackage -us -uc                         # 制作 deb 包,通过源码编译
$ ls -al ../                                        # 确认 deb 包生成

$ lintian ../picshot_0.1.2-1_amd64.deb              # lintian 检查规范;若想进官方 apt 必须全部通过;可省略
$ dpkg-deb -c ../picshot_0.1.2-1_amd64.deb          # 查看 deb 文件的安装目录;可省略


# PS 补充:-------------------------------
sudo apt install devscripts       # 使用 dch -i 或用 dch -v version-revision 来指定版本

Linux 对应: /opt/Qt/5.15.2/gcc_64/bin    或    ~/Qt5.12.11/5.12.11/gcc_64/bin
MacOS 对应: /Users/winks/Qt/5.15.2/clang_64/bin

详细说明

源码包名有个格式规定

$ cp -r PicShot/ picshot-0.1.2

下载 PicShot 源码包后,若想用 dh_make 打包为 deb ,则需将 『文件夹』 重命名为指定格式 <package>-<version> ,这是官方强制的要求的 DESCRIPTION;例中我就直接 cp 一份改名。

dh_make is a tool that adds necessary files for making Debian source package from upstream source according to the requirements of the Debian Policy. dh_make must be invoked within a directory containing the source code, which must be named -. The must be all lowercase, The and must be all lowercase, digits and dashes. The can also contain digits, and the symbols plus, dot, tilde. The must start with a digit. If the directory name does not conform to this scheme, you must rename it before using dh_make. Alternatively, you may be able to use the –packagename option to force the package name.


配置环境变量

$ cat >> ~/.bashrc <<EOF                            # 配置环境变量,用于模板填充作者名和邮箱
DEBEMAIL="xmulitech@gmail.com"                      # 维护者的邮箱
DEBFULLNAME="XMuli"                                 # 维护者的姓名
export DEBEMAIL DEBFULLNAME
export PATH=~/Qt5.12.11/5.12.11/gcc_64/bin:$PATH    # 代替 QT5_DIR 作用,编译用;可省略
EOF
$ . ~/.bashrc                                       # 更新使修改后的配置文件立刻生效;

命令含义为,直接将 『DEBEMAIL=”xmulitech@gmail.com“ ..省略.. :$PATH』 这段文本,追加在 ~/.bashrc 文件的尾部,然后更新使此新加的环境变量 立刻 生效。DEBEMAILDEBFULLNAME 两个变量,是填充自动生成模板使用的。


dh_make 生成 debian 目录

$ dh_make -f ../picshot-0.1.2.tar.gz -s -c mit -y

dh_make 命令用来在源码根目录下 debian 的;问了下 debian 的官方某维护者,他也是教我用这个命令。我这儿直接指定了几个参数: 压缩包 路径和 packageclass 为 single,和 copyright 为 mit 许可证。

默认生成的 debian 文件夹下又如此文件,以 .ex 结尾代表模板的含义,除了标记出来的几个必须 章 4. debian 目錄中的必須內容 ,其余都是可删除的,放心大胆的删,屡试不爽。

xmuli@xmuli-virtual-machine:~/Desktop/DebExample/picshot-0.1.2$ tree debian
debian
├── changelog                # 必须
├── control                  # 必须
├── copyright                # 必须
├── manpage.1.ex
├── manpage.sgml.ex
├── manpage.xml.ex
├── picshot.cron.d.ex
├── picshot.doc-base.EX
├── picshot-docs.docs
├── postinst.ex
├── postrm.ex
├── preinst.ex
├── prerm.ex
├── README.Debian
├── README.source
├── rules                    # 必须
├── salsa-ci.yml.ex
├── source                   # 必须
│   └── format
└── watch.ex

我们只需要简单修改 changelogcontrolcopyright 这三个文件即可;rules 为构建脚本,通常采用默认即可。下面将我修改的提供大家参考。


修改 debian/changelog
xmuli@xmuli-virtual-machine:~/Desktop/DebExample/picshot-0.1.2$ cat debian/changelog   # 修改后的参考
picshot (0.1.2-1) unstable; urgency=medium

  * try build .deb on Linux(ubuntu 20.04)                     #『改写』随便写一句替换默认的

 -- XMuli <xmulitech@gmail.com>  Sun, 27 Mar 2022 16:16:38 +0800
 
 
 //----------------------------------------------------
 sudo apt install devscripts       # 使用 dch -i 或用 dch -v version-revision 来指定版本

修改 debian/control
xmuli@xmuli-virtual-machine:~/Desktop/DebExample/picshot-0.1.2$ cat debian/control   # 修改后的参考
Source: picshot
Section: unknown
Priority: optional
Maintainer: XMuli <xmulitech@gmail.com>                    #『已修改』维护者姓名和联系方式
Build-Depends: debhelper-compat (= 12)                     #『未修改』构建依赖
Standards-Version: 4.4.1
Homepage: https://github.com/XMuli                         #『已修改』首页网站
#Vcs-Browser: https://salsa.debian.org/debian/picshot
#Vcs-Git: https://salsa.debian.org/debian/picshot.git

Package: picshot                                           #『未修改』包名称
Architecture: any                                          #『未修改』平台
Depends: ${shlibs:Depends}, ${misc:Depends}                #『未修改』依赖
Description: Open source cross-platform screenshots        #『已修改』简短的一句描述
 Qt5-based open source cross-platform screenshot tool      #『已修改』可以书写详细的描述

注意: 关键词后紧贴着冒号,填写的内容需要先填一个空格,格式正确,否则后面会失败。


xmuli@xmuli-virtual-machine:~/Desktop/DebExample/picshot-0.1.2$ cat debian/copyright  # 修改后的参考
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: picshot
Upstream-Contact: https://github.com/XMuli/PicShot         #『已修改』
Source: https://github.com/XMuli/PicShot                   #『已修改』

Files: *
Copyright: 2022 XMuli <xmulitech@gmail.com>
License: MIT

Files: debian/*
Copyright: 2022 XMuli <xmulitech@gmail.com>
License: MIT

License: MIT
 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
 to deal in the Software without restriction, including without limitation
 the rights to use, copy, modify, merge, publish, distribute, sublicense,
 and/or sell copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following conditions:
 .
 The above copyright notice and this permission notice shall be included
 in all copies or substantial portions of the Software.
 .
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Please also look if there are files or directories which have a
# different copyright/license attached and list them here.
# Please avoid picking licenses with terms that are more restrictive than the
# packaged work, as it may make Debian's contributions unacceptable upstream.
#
# If you need, there are some extra license texts available in two places:
#   /usr/share/debhelper/dh_make/licenses/
#   /usr/share/common-licenses/

注意开源软件的许可证,对于想要进入 debian 官方上游 apt 仓库,对于这些都管控很严格,提交不正确或者错误,是会发邮件告知不i通过原因,按要求修改即可。


dh_make 的参数含义

感觉有必要列举出来,含义自己看;几个关键的参数:-c-fs|i|l|p ,没必要全部都熟记,二八原则。 -y 参数的最大优点就是后面写 yml 构建 CI/CD 的时候优势就出来了,届时在写一篇教程;

xmuli@xmuli-virtual-machine:~/Desktop/DebExample/PicShot$ dh_make --help
usage: dh_make [-h] [-a] [-c <type>] [--copyrightfile <file>] [-d] [--docs] [-e <address>] [-f <file>]
               [-n] [-o <dir>] [-p <name>] [-t <dir>] [-y] [--createorig] [--with-emacs]
               [-C <cls> | -s | -i | -l | --python] [-v]

prepare Debian packaging from an original source archive

optional arguments:
  -h, --help            show this help message and exit
  -a, --addmissing      reprocess package and add missing files
  -c <type>, --copyright <type>
                        use <type> of license in copyright file (apache|artistic|bsd|gpl|gpl2|gpl3|isc|lgpl|lgpl2|lgpl3|mit|custom)
  --copyrightfile <file>
                        Template to use for custom copyright
  -d, --defaultless     skip the default Debian and package class templates
  --docs                create a separate docs package
  -e <address>, --email <address>
                        use <address> as the maintainer e-mail address
  -f <file>, --file <file>
                        use <file> as the original source archive
  -n, --native          the program is Debian native, don't generate .orig
  -o <dir>, --overlay <dir>
                        reprocess package using templates in <dir>
  -p <name>, --packagename <name>
                        force package name to be <name>
  -t <dir>, --templates <dir>
                        apply customizing templates from <dir>
  -y, --yes             automatic yes to prompts and run non-interactively
  --createorig          create orig.tar.xz file
  --with-emacs          add files for emacsen
  -C <cls>, --packageclass <cls>
                        set package class (s|i|l|p)
  -s, --single          set package class to single
  -i, --indep           set package class to arch-independent
  -l, --library         set package class to library
  --python              set package class to python
  -v, --version         show program's version number and exit

dpkg-buildpackage 生成 deb 包

$ dpkg-buildpackage -us -uc

会自动生成如下中间过程和最后的 picshot_0.1.2-1_amd64.deb 包;已然成功,双击安装即可。

双击即可安装;


lintian 检查包

$ lintian ../picshot_0.1.2-1_amd64.deb

此步骤通过 lintian deb 进行一个标准的格式检查,如果想要进入上游 apt 仓库,就必须全部通过。标记有 E 只要不 apt 都是无所谓的,就如同编译的 警告一样。

当时倘若你有心走的更远,那么怎么也不能耽误了武学奇才,给出一个具体案例讲解 Linux上面使用lintian检测deb包报错的解决方案


dpkg-deb 查看 deb 的安装路径

$ dpkg-deb -c ../picshot_0.1.2-1_amd64.deb

执行一下会看到你想要的信息♥。


其它有用命令

再补充两个命令

  • ldd 可以查看依赖

  • locate 命令用于查找符合条件的文档

    sudo apt install mlocate                 # 所在包
    locate Qt5X11Extras.so.5                 # 用  例

系列地址

QtExamples

欢迎 star ⭐ 和 fork 🍴 这个系列的 C++ / QT / DTK 学习,附学习由浅入深的目录,这里你可以学到如何亲自编写这类软件的经验,这是一系列完整的教程,并且永久免费!”