>

msys2, abbreviated from Minimal SYStem 2, is a Cygwin-derived software distro on Windows, it uses Arch Linux’s Pacman as a package manager.
As described in its home page, msys2 is an independent rewrite of MSYS, based on modern Cygwin (POSIX compatibility layer) and MinGW-w64 with the aim of better interoperability with native Windows software.

Since my personal laptop becomes slow as more applications are loaded, especially when a virtual vm starts up, I decide to try out msys2 to lighten
the loads. msys2 together with the MinGW-w64 simulate a Linux environment in Windows help me perform tasks that need Linux environment.

Installation

HomePage
Download: msys2 64bit

It will be installed into directory C:\msys64. Files that are installed look like this:

Environment Variables

Utilities that are provided by msys2 are located in C:\msys64\usr\bin, we can add this directory into windows environment variable PATH like this,

We benefit from this since we can now use those Linux commands or utils (e.g. which, ls, etc.) in Windows cmd command line also.

Home Path

The default $HOME directory is C:\msys64\home\username after install msys2. To use the Windows $HOME, which is C:\Users\username, there are several ways:

  1. Add a HOME variable in Windows Environment Variable settings, and set its value to C:\Users\username.

  2. Modify /etc/fstab and add the below line at the end of file:

    C:/Users /home ntfs binary,noacl,auto 1 1

this will automatically mount C:\Users as /home directory.

  1. Modify /etc/nsswitch.conf
    add windows into db_home variables, and put it before other variables. like this:

    db_home: windows cygwin desc

Warnning:
Mind the risk of mis-operation in this $HOME directory as it will affect the real user environment in Windows,
executing rm -f in msys2 shell will make you unable to recover the files that are removed.

Setting Shell Style/Theme

msys2 use mitty as the shell window, its style/theme can be configured through ~/.mittyrc. Go to http://ciembor.github.io/4bit/#, configure a theme that we like, copy the configuration settings into our
local ~/.mittyrc.

Get Windows Environment to msys2

Sometimes we want to execute applications in Windows $Path (but not in msys2’s $PATH) in msys2 shell (e.g. we’ve installed Python in Windows but
we want to test some python scripts in msys2). We need to inherit the Windows $Path value into msys2’s $PATH. Fortunately, msys2 provides a way to do
this. msys2 provides a script msys2_shell.cmd to initialize the environment, if we want to get Windows environment variables, we can modify this file.

Take a look at this:

As it described, we can either launch the shell by executing $ msys2_shell.cmd -use-full-path or
set a new environment variable MSYS2_PATH_TYPE with value inherit in Windows.

Some recommendations
When using the shells, try to remove as many entries from PATH as you can, ideally only leaving something likeC:\Windows\system32.
Mixing in programs from other MSYS2 installations, Cygwin installations or compiler toolchains is not supported and will probably break things in unexpected ways.
Do not have these things in PATH when running MSYS2 unless you know exactly what you’re doing.

Install base tools in msys2

$ pacman -S --needed base

Install packages using pacman

$ pacman -Sy git

To install (build from source) off-line packages from https://github.com/Alexpux/MSYS2-packages

$ cd package_dir
$ makepkg

Note: we need to run pacman -S --needed base first to make sure command makepkg works.

More things about msys2

keep in mind that MSYS2 doesn’t intend to compete with Cygwin or duplicate their efforts. The set of things that belong to the msys2 subsystem is pretty small:

  • essential POSIX stuff: filesystem, msys2-runtime, …
  • the native toolchain: gcc, binutils, gdb, …
  • supporting programs that are hard to port to Windows: bash, automake, make, …
  • supporting programs, even though they’re portable: mintty, winpty, python, man, vim, git, …
  • random, highly useful stuff: mc, ssh, rsync, lftp, …
  • dependencies of these packages

In other words, if a program is needed to build native software, but is itself hard to port, it can be made into a msys2 package. Anything else needs to be done as a mingw package or vetted individually.

Introduction of MinGw-w64
MinGw-w64 provides a GNU C Compiler toolchains in Windows, it helps to build native Windows applications without the dependencies on third party DLLs.

Install MinGW-GCC

GCC in Windows is provided by MinGW tool chains, there are two ways to install it:

  1. Use msys2’s package manager pacman:

    $ pacman -S mingw-w64-x86_64-gcc

  2. Download an offline installer to install a specific version of MinGW-GCC:

https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/dongsheng-daily/


Development in msys2

Check cross-platform definitions

It’s common that we see a conditional include directive in a header files like below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32) && !defined(__SYMBIAN32__)
#define WIN32
#endif

#include <stdio.h>
#include <limits.h>

#if defined(__FreeBSD__) && (__FreeBSD__ >= 2)
// Needed for __FreeBSD_version symbol definition
#include <osreldate.h>
#endif

#if defined(WIN32) && !defined(_WIN32_WCE) && !defined(__CYGWIN__)
#if !(defined(_WINSOCKAPI_) || defined(_WINSOCK_H) || \
defined(__LWIP_OPT_H__) || defined(LWIP_HDR_OPT_H))
/* The check above prevents the winsock2 inclusion if winsock.h already was
included, since they can't co-exist without problems */
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#if !defined(WIN32) && !defined(_WIN32_WCE)
#include <sys/socket.h>
#endif

#if !defined(WIN32) && !defined(__WATCOMC__) && !defined(__VXWORKS__)
#include <sys/time.h>
#endif

#ifdef __BEOS__
#include <support/SupportDefs.h>
#endif

We can use below commands to check whether these variables are defined:

$ gcc -m64 -dM -E -x c /dev/null | grep "WIN"
#define _WIN32 1
#define _WIN64 1
#define __WINT_MAX__ 0xffff
#define __WINT_MIN__ 0
#define __WIN32 1
#define __WIN64 1
#define __WINNT 1
#define __WINNT__ 1
#define __WIN32__ 1
#define __SIZEOF_WINT_T__ 2
#define WIN32 1
#define WIN64 1
#define __WINT_TYPE__ short unsigned int
#define WINNT 1
#define __WIN64__ 1

$ gcc -mwin32 -dM -E -x c /dev/null | grep "WIN"
...



Reference
https://sourceforge.net/p/msys2/wiki/Home/
https://sourceforge.net/p/msys2/wiki/MSYS2%20introduction/