How to use MSYS2 to compile the latest version of coreutils source code (the latest version currently is coreutils-9.4)

The previous article summarized: MSYS2 introduction, download and installation, Pacman common commands

This article will demonstrate how to use MSYS2 to compile coreutils source code.

1. coreutils source code download

Official website mirror warehouse: Index of /gnu/coreutils
Github warehouse: coreutils/coreutils/tagsDownload
the latest version here: coreutils-9.4.tar.gz

Although I still have to mention it: After the MSYS2 installation is completed, the coreutils package will be installed by default, version 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

Under normal circumstances, there is no need to use the latest version. This is just to demonstrate how to use MSYS2 to compile a project, so let's try coreutils.

2. MSYS2 installation and compilation tools

Compile coreutils using MSYS environment

1. pacman -Suy updates the software repository

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

What I have here is already the latest.

2. pacman -S msys/gcc install 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 installs the make tool

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. Unzip the coreutils compressed package

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. Execute the ./configure command

In the decompressed coreutils-9.4root directory, execute .configurethe command and the file will be generated Makefile.
This may take tens of minutes to execute.

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. Execute make to compile (error reported due to a bug in cygwin-3.4.8 version)

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

I got an error after executing make here (see the above example). This error has been bothering me for two days. I checked a lot of information on the Internet and finally found the reason in can't compile coreutils-9.3 any more after upgrade to cygwin-3.4.8 . The reason is that this problem is a bug in the cygwin-3.4.8 version, and my MSYS2 happens to depend on the cygwin-3.4.8 version. It is really a coincidence. . .

MSYS2The dependencies in cygwincorrespond to msys2-runtimesoftware msys2-runtime-develpackages.

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

In addition, the day before writing this article, the cygwin official website has fixed this problem in cygwin-3.4.9 version.
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 bug fix

The msys2/msys2-rumtime warehouse is also synchronized with the cygwin-3.4.9 version:
msys2/msys2-runtime
msys2/msys2-runtime/compare/cygwin-3.4.8…cygwin-3.4.9

The msys2/MSYS2-packages software package code repository has also been synchronized with msys2-runtime to version 3.4.9:
msys2/MSYS2-packages
msys2/MSYS2-packages/tree/master/msys2-runtime

Insert image description here
But one problem is that the MSYS2 software repository msys2-runtimeis msys2-runtime-develstill at version 3.4.8, which is lagging behind the source code (I don’t know exactly when it will be updated to 3.4.9).
https://mirror.umd.edu/msys2/msys/x86_64/That
Insert image description here
is to say, currently, it is not possible pacman -Syu msys2-runtimeto update to 3.4.9the version through .
Of course, you can compile the latest version yourself and then pacman -U <本地软件包>install it, please refer to: Creating Packages

Compiling by yourself is still a bit troublesome. Here is a simple method for reference:
Plan: modify /usr/include/sys/cpuset.hthe file . Find the file
in the MSYS2安装路径directory (provided it has been installed , this will be automatically installed during installation)usr/include/sys/cpuset.hmsys2-runtime-develgcc

#include <stdlib.h>Comment out the in the file #include <string.h>.
Insert image description here

7. Execute make again to compile (because some dependent packages are not installed, an error is still reported)

After correcting the above problem, (execute it first make clean), I re-executed makethe command and encountered the following error again.

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

This error has troubled me for a long time because I don't know what dependencies are needed to compile coreutils. Later, I found the build- time dependencies of the MSYS2 official website through
Package: coreutils | MSYS2 Packagescoreutils-8.32

Insert image description here

Don’t say anything else, just install all these dependencies.
(I have already installed some of them. I checked here through pacman -Qs and autotools gmp-devel help2manthey have not been installed yet)

Administrator@GC MSYS ~
$ pacman -S autotools gmp-devel help2man

8. Execute make again to compile (success)

After installing the dependent software packages (execute first make clean), execute makethe command again.
This time it went smoothly. The Windows executable exe utility was compiled and generated.

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. Package the generated executable program

Execute make installthe command to package and compile the generated executable program.
It should be noted that when executing the make install command, you must execute it as the root user or use the sudo command so that you have sufficient permissions to install.

Note: I don’t want to change the built-in version
here , so I won’t execute it. Because I forgot to add the parameter to specify the installation path when executing, so I will do it manually here.MSYS2coreutils-8.32-5make 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

Note: MSYS2The compiled exe program relies on msys-2.0.dlllibraries, so when MSYS2used outside the environment, it needs to be msys-2.0.dllpackaged together.

10. Testing the utilities in the coreutils toolkit

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>

Note:
Because I did not install the latest version of coreutils in the MSYS environment, it is still the built-in version in the MSYS environment coreutils-8.32. Use the cmd command to switch out of the MSYS environment and conduct the above verification. coreutils-9.4You can see that the commands in the latest compiled version expr、ls、grep、whoamican be used normally.
Perfect~
In addition, you can C:\Chen\Mytools\coreutils-9.4\bin>add to the environment variable PATH, so that you can use coreutilsthe commands in the newly compiled toolkit in any working path of cmd and powershell (not demonstrated here)

3. Final & Summary

Finally, when I was about to publish this article, I took another look at msys2-runtime、msys2-runtime-develthe version of the MSYS2 software package warehouse and it has been updated to 3.4.9, so you are happy. If you directly update to 3.4.9, you will not encounter me. The first problem encountered earlier.

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]

Final summary:
During the compilation coreutils-9.4process, although I encountered problems and wanted to give up several times in the middle, I finally found a solution. In the process of solving the problem, I gained a lot. for example:

  • A more practical understanding is that the bottom layer of MSYS2 relies on Cygwin, which corresponds to the msys2-runtime and msys2-runtime-devel packages;
  • You can check the detailed information of the software package through https://packages.msys2.org/updates . For example, the dependencies of the above coreutils when building are found through this website. package/coreutils

Guess you like

Origin blog.csdn.net/B11050729/article/details/132709325