Linux

Linux软件包管理方式和手工编译源代码安装软件时究竟做了哪些操作?

一软件包管理分类

CentOS,RHEL,OEL,Fedora:rpm,redhat package management; rpm格式软件包;

Debian,Ubuntu:apt,advanced package tools,deb格式软件包。

关于apt:https://en.wikipedia.org/wiki/APT_(software)

Advanced Package Tool, or APT, is a free-software user interface that works with core libraries to handle the installation and removal of software on Debian, Ubuntu, and related Linux distributions.[4] APT simplifies the process of managing software on Unix-like computer systems by automating the retrieval, configuration and installation of software packages, either from precompiled files or by compiling source code.

二手工编译源码安装软件

1 configure

执行这一步的作用是,按照源代码文件中已经配置好的环境信息,来配置软件,并且在源代码路径下生成Makefile,用于给下一步make命令使用。

举例说明:https://ftp.postgresql.org/pub/source/v14beta2/postgresql-14beta2.tar.gz 这个PostgreSQL数据库软件的源代码中,在打包生成该源代码的tarball文件时,就已经支持了CentOS 8 内核版本4.18.0-147.5.1.el8_1.x86_64的操作系统「当然,该源码同时还支持其它多种操作系统」,并且指定了软件默认安装在/usr/local路径下。现在,我们在CentOS 8 内核版本4.18.0-147.5.1.el8_1.x86_64的操作系统上通过编译该源码的方式来安装PostgreSQL数据库软件,我们不想让软件安装在/usr/local路径下,想把软件安装在/postgres/pg14路径下。于是,我们在源代码的解压路径下,可以执行

[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ ./configure --help
`configure' configures PostgreSQL 14beta2 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages
      --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
      --srcdir=DIR        find the sources in DIR [configure dir or `..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local/pgsql]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, `make install' will install all the files in
`/usr/local/pgsql/bin', `/usr/local/pgsql/lib' etc.  You can specify
an installation prefix other than `/usr/local/pgsql' using `--prefix',
for instance `--prefix=$HOME'.

For better control, use the options below.
....
....

[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ ./configure --prefix=/postgres/pg14
...
configure: using CPPFLAGS= -D_GNU_SOURCE 
configure: using LDFLAGS=  -Wl,--as-needed
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/include/pg_config_ext.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.c
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port
[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ 

从上述的帮助信息可以看到:

`configure’ configures PostgreSQL 14beta2 to adapt to many kinds of systems.

同时,

To assign environment variables (e.g., CC, CFLAGS…), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.

还有:

By default, make install' will install all the files in /usr/local/pgsql/bin’, /usr/local/pgsql/lib' etc. You can specify an installation prefix other than/usr/local/pgsql’ using --prefix', for instance–prefix=$HOME’.

当然,这一份源代码文件可以支持many kinds of systems。并且,其默认是是把文件安装在/usr/local/pgsql下了。

再者:从configure的执行结果,看到生成了GNUmakefile以及其它新的几个文件:

[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ ls -lrt
total 1240
-rw-r--r--  1 pg14 pg14   1213 Jun 22 05:07 README
-rw-r--r--  1 pg14 pg14   1665 Jun 22 05:07 Makefile
-rw-r--r--  1 pg14 pg14    277 Jun 22 05:07 HISTORY
-rw-r--r--  1 pg14 pg14   4278 Jun 22 05:07 GNUmakefile.in
-rw-r--r--  1 pg14 pg14   1192 Jun 22 05:07 COPYRIGHT
-rw-r--r--  1 pg14 pg14  83700 Jun 22 05:07 configure.ac
-rwxr-xr-x  1 pg14 pg14 580505 Jun 22 05:07 configure
-rw-r--r--  1 pg14 pg14    490 Jun 22 05:07 aclocal.m4
drwxrwxr-x 58 pg14 pg14   4096 Jun 22 05:11 contrib
drwxrwxr-x  2 pg14 pg14   4096 Jun 22 05:11 config
drwxrwxr-x  3 pg14 pg14     87 Jun 22 05:11 doc
-rw-r--r--  1 pg14 pg14  63887 Jun 22 05:12 INSTALL
-rwxrwxr-x  1 pg14 pg14  40177 Jul 25 08:41 config.status
-rw-rw-r--  1 pg14 pg14   4278 Jul 25 08:41 GNUmakefile
drwxrwxr-x 16 pg14 pg14   4096 Jul 25 08:41 src
-rw-rw-r--  1 pg14 pg14 443551 Jul 25 08:41 config.log
[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ date
Sun Jul 25 08:47:53 CST 2021
[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ 

从文件的生成时间,就可以看出来了。这里的一个小疑问,前面不是说,这一步骤会生产Makefile的吗?可是明明这个文件本身在执行configure命令之前,就已经存在源码路径下呢?为什么要生成一个新的GNUmakefile呢?其实,查看一下Makefile的内容,就知道了:

[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ vi Makefile 
# The PostgreSQL make files exploit features of GNU make that other
# makes do not have. Because it is a common mistake for users to try
# to build Postgres with a different make, we have this make file
# that, as a service, will look for a GNU make and invoke it, or show
# an error message if none could be found.

# If the user were using GNU make now, this file would not get used
# because GNU make uses a make file named "GNUmakefile" in preference
# to "Makefile" if it exists. PostgreSQL is shipped with a
# "GNUmakefile". If the user hasn't run the configure script yet, the
# GNUmakefile won't exist yet, so we catch that case as well.


# AIX make defaults to building *every* target of the first rule.  Start with
# a single-target, empty rule to make the other targets non-default.
all:
2 make

==将源代码文件,编译成可以执行的二进制文件,生成的可执行二进制文件,依然位于当前的源代码路径下「在contrib路径下」。==这一步骤依赖于上一步的configure命令的成功执行。当然,这里的make world命令是PostgreSQL数据库软件特有的编译选项,一般的源代码编译安装,只需要执行make即可。

[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ make world
...
interfaces/libpq -lpq -L../../src/port -L../../src/common   -Wl,--as-needed -Wl,-rpath,'/postgres/pg14/lib',--enable-new-dtags   -lpgcommon -lpgport -lz -lreadline -lpthread -lrt -ldl -lm  -o vacuumlo
make[2]: Leaving directory '/home/pg14/postgresql-14beta2/contrib/vacuumlo'
make[1]: Leaving directory '/home/pg14/postgresql-14beta2/contrib'
PostgreSQL, contrib, and documentation successfully made. Ready to install.
[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$
3 make install

最后,将上述步骤中生成的可以执行的二进制文件,copy到目标路径下,/postgres/pg14。

[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ du -sh /postgres/
0       /postgres/
[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ make install-world
...
/usr/bin/mkdir -p '/postgres/pg14/lib'
/usr/bin/mkdir -p '/postgres/pg14/share/extension'
/usr/bin/mkdir -p '/postgres/pg14/share/extension'
/usr/bin/install -c -m 755  tsm_system_time.so '/postgres/pg14/lib/tsm_system_time.so'
/usr/bin/install -c -m 644 ./tsm_system_time.control '/postgres/pg14/share/extension/'
/usr/bin/install -c -m 644 ./tsm_system_time--1.0.sql  '/postgres/pg14/share/extension/'
make[2]: Leaving directory '/home/pg14/postgresql-14beta2/contrib/tsm_system_time'
make -C unaccent install
make[2]: Entering directory '/home/pg14/postgresql-14beta2/contrib/unaccent'
/usr/bin/mkdir -p '/postgres/pg14/lib'
/usr/bin/mkdir -p '/postgres/pg14/share/extension'
/usr/bin/mkdir -p '/postgres/pg14/share/extension'
/usr/bin/mkdir -p '/postgres/pg14/share/tsearch_data'
/usr/bin/install -c -m 755  unaccent.so '/postgres/pg14/lib/unaccent.so'
/usr/bin/install -c -m 644 ./unaccent.control '/postgres/pg14/share/extension/'
/usr/bin/install -c -m 644 ./unaccent--1.1.sql ./unaccent--1.0--1.1.sql  '/postgres/pg14/share/extension/'
/usr/bin/install -c -m 644 ./unaccent.rules '/postgres/pg14/share/tsearch_data/'
make[2]: Leaving directory '/home/pg14/postgresql-14beta2/contrib/unaccent'
make -C vacuumlo install
make[2]: Entering directory '/home/pg14/postgresql-14beta2/contrib/vacuumlo'
/usr/bin/mkdir -p '/postgres/pg14/bin'
/usr/bin/install -c  vacuumlo '/postgres/pg14/bin'
make[2]: Leaving directory '/home/pg14/postgresql-14beta2/contrib/vacuumlo'
make[1]: Leaving directory '/home/pg14/postgresql-14beta2/contrib'
PostgreSQL, contrib, and documentation installation complete.
[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ du -sh /postgres/
52M     /postgres/
[pg14@iZbp1anc2b2vggfj0i0oovZ postgresql-14beta2]$ 

从上,可以看到make install的过程中,的确是把源代码路径下的编译生成的可执行的二进制文件,copy到目标路径/postgres/pg14下了。

三小结

Linux系统上软件的安装常用的是rpm,apt,这取决于操作系统平台;

结合PostgreSQL的编译源代码的安装方式,理清楚了configure,make,make install到底做了哪些基本工作。

参考:鸟哥的Linux私房菜 第2版,2007年在项目组出差时,购买于辽宁沈阳。

如何在CentOS上源码安装PostgreSQL数据库