文章目录
- 一、coreutils 源码下载
- 二、MSYS2 安装编译工具
- 1. pacman -Suy 更新软件仓库
- 2. pacman -S msys/gcc 安装gcc
- 3. pacman -S msys/make 安装make工具
- 4. 解压缩 coreutils 压缩包
- 5. 执行 ./configure 命令
- 6. 执行 make 进行编译(cygwin-3.4.8版本的一个bug导致的报错)
- 7. 重新执行 make 进行编译(因为一些依赖包未安装,仍报错)
- 8. 再次执行 make 进行编译(成功)
- 9. 打包生成的可执行程序
- 10. 测试 coreutils 工具包中的实用程序
- 三、最后&总结
上一篇文章总结了:MSYS2 介绍、下载与安装、Pacman常用命令
本文将演示如何使用 MSYS2 编译 coreutils 源码。
一、coreutils 源码下载
官网镜像仓库:Index of /gnu/coreutils
Github 仓库:coreutils/coreutils/tags
这里下载目前最新版本:coreutils-9.4.tar.gz
虽然但是我还是要提一下:MSYS2 安装完成后,默认会安装coreutils软件包,版本为 8.32
Administrator@GC MSYS ~
$ pacman -Qs coreutils
local/coreutils 8.32-5
The basic file, shell and text manipulation utilities of the GNU operating system
一般情况下也没必要使用最新版,这里仅是为了演示如何使用MSYS2编译项目,所以就拿coreutils试试。
二、MSYS2 安装编译工具
使用 MSYS 环境进行编译 coreutils
1. pacman -Suy 更新软件仓库
Administrator@GC MSYS ~
$ pacman -Suy
:: Synchronizing package databases...
clangarm64 is up to date
mingw32 is up to date
mingw64 is up to date
ucrt64 is up to date
clang32 is up to date
clang64 is up to date
msys is up to date
:: Starting core system upgrade...
there is nothing to do
:: Starting full system upgrade...
there is nothing to do
我这里已经是最新了。
2. pacman -S msys/gcc 安装gcc
Administrator@GC MSYS ~
$ pacman -S msys/gcc
resolving dependencies...
looking for conflicting packages...
Packages (8) binutils-2.41-2 isl-0.26-1 mpc-1.3.1-1 msys2-runtime-devel-3.4.8-1
msys2-w32api-headers-11.0.1.r0.gc3e587c06-1
msys2-w32api-runtime-11.0.1.r0.gc3e587c06-1 windows-default-manifest-6.4-1
gcc-11.3.0-4
Total Download Size: 41.38 MiB
Total Installed Size: 318.84 MiB
:: Proceed with installation? [Y/n] y
:: Retrieving packages...
isl-0.26-1-x86_64 745.0 KiB 260 KiB/s 00:03 [###############################] 100%
msys2-w32api-headers-11.0.... 5.2 MiB 1131 KiB/s 00:05 [###############################] 100%
mpc-1.3.1-1-x86_64 78.5 KiB 147 KiB/s 00:01 [###############################] 100%
binutils-2.41-2-x86_64 5.4 MiB 979 KiB/s 00:06 [###############################] 100%
windows-default-manifest-6... 1388.0 B 3.45 KiB/s 00:00 [###############################] 100%
msys2-runtime-devel-3.4.8-... 338.8 KiB 68.2 KiB/s 00:05 [###############################] 100%
gcc-11.3.0-4-x86_64 27.6 MiB 2.63 MiB/s 00:10 [###############################] 100%
msys2-w32api-runtime-11.0.... 2.0 MiB 194 KiB/s 00:11 [###############################] 100%
Total (8/8) 41.4 MiB 3.86 MiB/s 00:11 [###############################] 100%
(8/8) checking keys in keyring [###############################] 100%
(8/8) checking package integrity [###############################] 100%
(8/8) loading package files [###############################] 100%
(8/8) checking for file conflicts [###############################] 100%
(8/8) checking available disk space [###############################] 100%
:: Processing package changes...
(1/8) installing binutils [###############################] 100%
(2/8) installing isl [###############################] 100%
(3/8) installing mpc [###############################] 100%
(4/8) installing msys2-runtime-devel [###############################] 100%
(5/8) installing msys2-w32api-headers [###############################] 100%
(6/8) installing msys2-w32api-runtime [###############################] 100%
(7/8) installing windows-default-manifest [###############################] 100%
(8/8) installing gcc [###############################] 100%
:: Running post-transaction hooks...
(1/1) Updating the info directory file...
3. pacman -S msys/make 安装make工具
Administrator@GC MSYS ~
$ pacman -S msys/make
resolving dependencies...
looking for conflicting packages...
Packages (1) make-4.4.1-1
Total Download Size: 0.49 MiB
Total Installed Size: 1.61 MiB
:: Proceed with installation? [Y/n] y
:: Retrieving packages...
make-4.4.1-1-x86_64 505.9 KiB 129 KiB/s 00:04 [###############################] 100%
(1/1) checking keys in keyring [###############################] 100%
(1/1) checking package integrity [###############################] 100%
(1/1) loading package files [###############################] 100%
(1/1) checking for file conflicts [###############################] 100%
(1/1) checking available disk space [###############################] 100%
:: Processing package changes...
(1/1) installing make [###############################] 100%
:: Running post-transaction hooks...
(1/1) Updating the info directory file...
4. 解压缩 coreutils 压缩包
Administrator@GC MSYS ~
$ cd C:/Chen/Mytools/
Administrator@GC MSYS /c/Chen/Mytools
$ tar -xzf coreutils-9.4.tar.gz coreutils-9.4/
Administrator@GC MSYS /c/Chen/Mytools
$ cd coreutils-9.4
Administrator@GC MSYS /c/Chen/Mytools/coreutils-9.4
$
5. 执行 ./configure 命令
在解压后的coreutils-9.4
根目录下,执行.configure
命令,将生成Makefile
文件。
这个可能需要执行数十分钟。
Administrator@GC MSYS /c/Chen/Mytools/coreutils-9.4
$ ./configure
configure: loading site script /etc/config.site
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a race-free mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
...
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating po/Makefile.in
config.status: creating gnulib-tests/Makefile
config.status: creating lib/config.h
config.status: executing depfiles commands
config.status: executing po-directories commands
config.status: creating po/POTFILES
config.status: creating po/Makefile
Administrator@GC MSYS /c/Chen/Mytools/coreutils-9.4
$
6. 执行 make 进行编译(cygwin-3.4.8版本的一个bug导致的报错)
Administrator@GC MSYS /c/Chen/Mytools/coreutils-9.4
$ make
GEN lib/alloca.h
GEN lib/arpa/inet.h
GEN lib/configmake.h
GEN lib/ctype.h
GEN lib/dirent.h
GEN lib/error.h
...
In file included from /usr/include/sys/signal.h:23,
from /usr/include/signal.h:6,
from ./lib/signal.h:52,
from /usr/include/time.h:158,
from ./lib/time.h:54,
from ./lib/sys/stat.h:51,
from ./lib/fcntl.h:64,
from ./lib/unistd.h:105,
from ./lib/stdlib.h:104,
from /usr/include/sys/cpuset.h:12,
from /usr/include/sys/_pthreadtypes.h:12,
from /usr/include/sys/types.h:221,
from ./lib/sys/types.h:46,
from ./lib/stdio.h:75,
from lib/long-options.c:27:
/usr/include/cygwin/signal.h:121:3: error: unknown type name 'pthread_attr_t'
121 | pthread_attr_t *sigev_notify_attributes; /* notification attributes */
| ^~~~~~~~~~~~~~
In file included from /usr/include/signal.h:6,
from ./lib/signal.h:52,
from /usr/include/time.h:158,
from ./lib/time.h:54,
from ./lib/sys/stat.h:51,
from ./lib/fcntl.h:64,
from ./lib/unistd.h:105,
from ./lib/stdlib.h:104,
from /usr/include/sys/cpuset.h:12,
from /usr/include/sys/_pthreadtypes.h:12,
from /usr/include/sys/types.h:221,
from ./lib/sys/types.h:46,
from ./lib/stdio.h:75,
from lib/long-options.c:27:
/usr/include/sys/signal.h:227:29: error: expected ')' before 'int'
227 | int pthread_kill (pthread_t, int);
| ^~~~
| )
In file included from /usr/include/sys/stat.h:22,
from ./lib/sys/stat.h:54,
from ./lib/fcntl.h:64,
from ./lib/unistd.h:105,
from ./lib/stdlib.h:104,
from /usr/include/sys/cpuset.h:12,
from /usr/include/sys/_pthreadtypes.h:12,
from /usr/include/sys/types.h:221,
from ./lib/sys/types.h:46,
from ./lib/stdio.h:75,
from lib/long-options.c:27:
/usr/include/cygwin/stat.h:27:3: error: unknown type name 'timestruc_t'
27 | timestruc_t st_atim;
| ^~~~~~~~~~~
/usr/include/cygwin/stat.h:28:3: error: unknown type name 'timestruc_t'
28 | timestruc_t st_mtim;
| ^~~~~~~~~~~
/usr/include/cygwin/stat.h:29:3: error: unknown type name 'timestruc_t'
29 | timestruc_t st_ctim;
| ^~~~~~~~~~~
/usr/include/cygwin/stat.h:32:3: error: unknown type name 'timestruc_t'
32 | timestruc_t st_birthtim;
| ^~~~~~~~~~~
CC lib/libcoreutils_a-mbrlen.o
make[2]: *** [Makefile:15547: lib/libcoreutils_a-long-options.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[2]: Leaving directory '/c/Chen/Mytools/coreutils-9.4'
make[1]: *** [Makefile:22396: all-recursive] Error 1
make[1]: Leaving directory '/c/Chen/Mytools/coreutils-9.4'
make: *** [Makefile:8835: all] Error 2
我这里执行make后有报错(见上述示例),这个错误困扰我两天了,在网上查阅了多资料,最终在 can’t compile coreutils-9.3 any more after upgrade to cygwin-3.4.8 找到了原因。原因是这个问题是cygwin-3.4.8版本的一个bug,而我的MSYS2依赖的刚好是cygwin-3.4.8版本,真的是好巧不巧。。。
MSYS2
中依赖的cygwin
,对应的是 msys2-runtime
、msys2-runtime-devel
软件包。
Administrator@GC MSYS ~
$ pacman -Qs msys2-runtime
local/msys2-runtime 3.4.8-1
Posix emulation engine for Windows
local/msys2-runtime-devel 3.4.8-1
MSYS2 headers and libraries
另外写这篇文章的前一天,cygwin官网已经修复了这个问题,在cygwin-3.4.9版本。
cygwin/cygwin/tree/cygwin-3.4.9
cygwin/cygwin/compare/cygwin-3.4.7…cygwin-3.4.8
cygwin/cygwin/compare/cygwin-3.4.8…cygwin-3.4.9
cygwin 3.4.9-1 问题修复
msys2/msys2-rumtime 仓库也同步了 cygwin-3.4.9 版本:
msys2/msys2-runtime
msys2/msys2-runtime/compare/cygwin-3.4.8…cygwin-3.4.9
msys2/MSYS2-packages 软件包代码仓库也同步了 msys2-runtime 为 3.4.9 版本:
msys2/MSYS2-packages
msys2/MSYS2-packages/tree/master/msys2-runtime
但是有个问题是,MSYS2 软件仓库的msys2-runtime
、msys2-runtime-devel
目前仍是 3.4.8 版本,比源码要落后(具体什么时候更新到3.4.9,我也不清楚)。
https://mirror.umd.edu/msys2/msys/x86_64/
也就是说目前,无法通过 pacman -Syu msys2-runtime
更新到3.4.9
版本。
当然你可以自己编译最新版本,然后通过pacman -U <本地软件包>
进行安装,可参考:Creating Packages
自己编译还是有些麻烦的,这里找到了一种简便方法供参考:
方案:修改 /usr/include/sys/cpuset.h
文件
在MSYS2安装路径
的usr/include/sys/
目录下,找到cpuset.h
文件(前提是已安装了msys2-runtime-devel
,安装gcc
时会自动安装这个)
将文件中的#include <stdlib.h>
、#include <string.h>
注释掉。
7. 重新执行 make 进行编译(因为一些依赖包未安装,仍报错)
修改完上述的问题后,(先执行下make clean
),重新执行make
命令,又遇到了如下报错。
In file included from src/system.h:172,
from src/chroot.c:26:
./lib/gettext.h:26:11: fatal error: libintl.h: No such file or directory
26 | # include <libintl.h>
| ^~~~~~~~~~~
compilation terminated.
make[2]: *** [Makefile:12465: src/chroot.o] Error 1
make[2]: Leaving directory '/c/Chen/Mytools/coreutils-9.4'
make[1]: *** [Makefile:22396: all-recursive] Error 1
make[1]: Leaving directory '/c/Chen/Mytools/coreutils-9.4'
make: *** [Makefile:8835: all] Error 2
这个报错也困扰我了好久,因为我不知道编译 coreutils 都需要哪些依赖。后来通过
Package: coreutils | MSYS2 Packages 找到了MSYS2官网在构建coreutils-8.32
时依赖项
啥也别说了,把这些依赖项都安装上吧。
(有些我已经装过了,我这里通过 pacman -Qs 查看,autotools gmp-devel help2man
还没有装)
Administrator@GC MSYS ~
$ pacman -S autotools gmp-devel help2man
8. 再次执行 make 进行编译(成功)
安装完依赖软件包后,(先执行下make clean
),再次执行make
命令。
这次很顺利,编译生成了Windows可执行的exe实用程序
Administrator@GC MSYS /c/Chen/Mytools/coreutils-9.4/
$ make -j4
...
Administrator@GC MSYS /c/Chen/Mytools/coreutils-9.4
$ ls src/*.exe
'src/[.exe' src/expr.exe src/nproc.exe src/stat.exe
src/b2sum.exe src/factor.exe src/numfmt.exe src/stty.exe
src/base32.exe src/false.exe src/od.exe src/sum.exe
src/base64.exe src/fmt.exe src/paste.exe src/sync.exe
src/basename.exe src/fold.exe src/pathchk.exe src/tac.exe
src/basenc.exe src/getlimits.exe src/pinky.exe src/tail.exe
src/cat.exe src/ginstall.exe src/pr.exe src/tee.exe
src/chcon.exe src/groups.exe src/printenv.exe src/test.exe
src/chgrp.exe src/head.exe src/printf.exe src/timeout.exe
src/chmod.exe src/hostid.exe src/ptx.exe src/touch.exe
src/chown.exe src/id.exe src/pwd.exe src/tr.exe
src/chroot.exe src/join.exe src/readlink.exe src/true.exe
src/cksum.exe src/kill.exe src/realpath.exe src/truncate.exe
src/comm.exe src/link.exe src/rm.exe src/tsort.exe
src/cp.exe src/ln.exe src/rmdir.exe src/tty.exe
src/csplit.exe src/logname.exe src/runcon.exe src/uname.exe
src/cut.exe src/ls.exe src/seq.exe src/unexpand.exe
src/date.exe src/make-prime-list.exe src/sha1sum.exe src/uniq.exe
src/dd.exe src/md5sum.exe src/sha224sum.exe src/unlink.exe
src/df.exe src/mkdir.exe src/sha256sum.exe src/uptime.exe
src/dir.exe src/mkfifo.exe src/sha384sum.exe src/users.exe
src/dircolors.exe src/mknod.exe src/sha512sum.exe src/vdir.exe
src/dirname.exe src/mktemp.exe src/shred.exe src/wc.exe
src/du.exe src/mv.exe src/shuf.exe src/who.exe
src/echo.exe src/nice.exe src/sleep.exe src/whoami.exe
src/env.exe src/nl.exe src/sort.exe src/yes.exe
src/expand.exe src/nohup.exe src/split.exe
9. 打包生成的可执行程序
执行make install
命令,打包编译生成的可执行程序。
需要注意的是,在执行 make install 命令时,要以 root 用户或者使用 sudo 命令来执行,以便拥有足够的权限进行安装。
说明:
我这里不想改动MSYS2
自带的coreutils-8.32-5
版本,所以就不执行make install
了。因为前面我在执行./configure
时忘记添加 --prefix
参数指定安装路径了,所以这里手动搞一下。
Administrator@GC MSYS /c/Chen/Mytools/coreutils-9.4
$ mkdir bin
Administrator@GC MSYS /c/Chen/Mytools/coreutils-9.4
$ cp src/*.exe bin
Administrator@GC MSYS /c/Chen/Mytools/coreutils-9.4
$ cp /usr/bin/msys-2.0.dll bin
注意:通过MSYS2
编译的exe程序,依赖msys-2.0.dll
库,因此在脱离MSYS2
环境使用时,需要将msys-2.0.dll
一并打包。
10. 测试 coreutils 工具包中的实用程序
Administrator@GC MSYS /c/Chen/Mytools/coreutils-9.4/bin
$ expr --version
expr (GNU coreutils) 8.32
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Parker, James Youngman, and Paul Eggert.
Administrator@GC MSYS /c/Chen/Mytools/coreutils-9.4/bin
$ cmd
Microsoft Windows [版本 10.0.19045.3208]
(c) Microsoft Corporation。保留所有权利。
C:\Chen\Mytools\coreutils-9.4\bin>expr --version
expr (GNU coreutils) 9.4
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Parker, James Youngman, and Paul Eggert.
C:\Chen\Mytools\coreutils-9.4\bin>expr ( 2 + 6 )
8
C:\Chen\Mytools\coreutils-9.4\bin>ls | grep 'whoami'
whoami.exe
C:\Chen\Mytools\coreutils-9.4\bin>whoami
Administrator
C:\Chen\Mytools\coreutils-9.4\bin>
注意:
因为我没有在MSYS环境安装最新版本的coreutils,所以在MSYS环境下仍是自带的coreutils-8.32
版本。通过cmd命令,切换出MSYS环境,进行了如上验证。可以看到最新编译的coreutils-9.4
版本中的expr、ls、grep、whoami
命令可以正常使用。
完美~
另外可以将C:\Chen\Mytools\coreutils-9.4\bin>
添加到环境变量PATH中,这样就可以在cmd、powershell 的任何工作路径下使用新编译的coreutils
工具包中的命令了(这里就不演示了)
三、最后&总结
最后,在我即将发布这篇文章的时候,我又看了一眼 MSYS2 软件包仓库的msys2-runtime、msys2-runtime-devel
版本,已经更新到了 3.4.9 ,所以你们是幸福的,直接更新到 3.4.9 ,就不会遇到我前面遇到的第一个问题了。
Administrator@GC MSYS ~
$ pacman -Syu
:: Synchronizing package databases...
clangarm64 1728.7 KiB 787 KiB/s 00:02 [####################################################################] 100%
mingw32 1803.3 KiB 1088 KiB/s 00:02 [####################################################################] 100%
mingw64 1944.2 KiB 921 KiB/s 00:02 [####################################################################] 100%
ucrt64 1984.0 KiB 731 KiB/s 00:03 [####################################################################] 100%
clang32 1781.6 KiB 838 KiB/s 00:02 [####################################################################] 100%
clang64 1940.5 KiB 2.31 MiB/s 00:01 [####################################################################] 100%
msys 460.5 KiB 540 KiB/s 00:01 [####################################################################] 100%
:: Starting core system upgrade...
warning: terminate other MSYS2 programs before proceeding
resolving dependencies...
looking for conflicting packages...
Packages (2) msys2-runtime-3.4.9-1 msys2-runtime-devel-3.4.9-1
Total Download Size: 2.12 MiB
Total Installed Size: 10.08 MiB
Net Upgrade Size: 0.00 MiB
:: Proceed with installation? [Y/n] y
:: Retrieving packages...
msys2-runtime-devel-3.4.9-1-x86_64 338.9 KiB 664 KiB/s 00:01 [####################################################################] 100%
msys2-runtime-3.4.9-1-x86_64 1834.9 KiB 3.22 MiB/s 00:01 [####################################################################] 100%
Total (2/2) 2.1 MiB 3.60 MiB/s 00:01 [####################################################################] 100%
(2/2) checking keys in keyring [####################################################################] 100%
(2/2) checking package integrity [####################################################################] 100%
(2/2) loading package files [####################################################################] 100%
(2/2) checking for file conflicts [####################################################################] 100%
(2/2) checking available disk space [####################################################################] 100%
:: Processing package changes...
(1/2) upgrading msys2-runtime [####################################################################] 100%
(2/2) upgrading msys2-runtime-devel [####################################################################] 100%
:: To complete this update all MSYS2 processes including this terminal will be closed. Confirm to proceed [Y/n]
最后总结:
在编译coreutils-9.4
的过程中,虽然遇到了问题,中途几次都想放弃,但是最终还是找到了解决方案,在解决问题的过程中,收获蛮多的。比如:
- 更切实的理解了,MSYS2 底层依赖了 Cygwin,对应的是 msys2-runtime、msys2-runtime-devel 包;
- 可通过 https://packages.msys2.org/updates 查看软件包的详细信息,比如上述 coreutils 构建时依赖有哪些依赖,就是通过这个网站查到的。package/coreutils