Skip to content

Instantly share code, notes, and snippets.

@k-takata
Last active July 17, 2020 16:50
Show Gist options
  • Save k-takata/5124445 to your computer and use it in GitHub Desktop.
Save k-takata/5124445 to your computer and use it in GitHub Desktop.
Build The Silver Searcher

Build The Silver Searcher

the silver searcher

For Win32/64 using MSYS2

Install Packages

Install the following packages using pacman -S package-name:

  • base-devel
  • mingw-w64-{i686,x86_64}-toolchain
    (mingw-w64-{i686,x86_64}-gcc, mingw-w64-{i686,x86_64}-pkg-config)
  • mingw-w64-{i686,x86_64}-pcre
  • mingw-w64-{i686,x86_64}-xz
  • git

Build The Silver Searcher

Open "MSYS2 MinGW 32-bit" or "MSYS2 MinGW 64-bit" from the start menu.

$ git clone https://github.com/ggreer/the_silver_searcher.git
$ cd the_silver_searcher/
$ ./build.sh PCRE_CFLAGS=-DPCRE_STATIC LDFLAGS=-static
$ strip ag.exe

For Win32 using Cygwin-MinGW

(This is an old way. Using MSYS2 is easier.)

Install Packages

Install the following packages using Cygwin's setup-x86.exe:

  • mingw-gcc-g++
  • mingw-zlib-devel
  • pkg-config
  • autoconf
  • automake
  • gettext
  • gettext-devel
  • liblzma-devel
  • git
  • wget

Build PCRE

Build PCRE for static link. I don't want to install it to the system.

$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.bz2
$ tar xvf pcre-8.34.tar.bz2
$ cd pcre-8.34/
$ ./configure CC=i686-pc-mingw32-gcc CXX=i686-pc-mingw32-g++ --enable-jit --enable-unicode-properties --disable-shared
$ make
$ cd ..

Build XZ Utils

Build XZ Utils (liblzma) for static link. I don't want to install it to the system.

$ wget http://tukaani.org/xz/xz-5.0.5.tar.xz
$ tar xvf xz-5.0.5.tar.xz
$ cd xz-5.0.5
$ ./configure CC=i686-pc-mingw32-gcc
$ make
$ cd ..

Build The Silver Searcher

PCRE, zlib and liblzma are statically linked with this configuration but pthread is dynamically linked.

$ git clone https://github.com/ggreer/the_silver_searcher.git
$ cd the_silver_searcher/
$ aclocal && autoconf && autoheader && automake --add-missing
$ ./configure CC=i686-pc-mingw32-gcc PCRE_CFLAGS='-DPCRE_STATIC -I../pcre-8.34 -I../xz-5.0.5/src/liblzma/api' PCRE_LIBS='-static -L../pcre-8.34/.libs -lpcre' LZMA_LIBS='-L../xz-5.0.5/src/liblzma/.libs -llzma' LIBS='-lshlwapi'
$ make
$ strip ag

Pthread's DLL will be found at: C:\cygwin\usr\i686-pc-mingw32\sys-root\mingw\bin\pthreadGC2.dll

For Ubuntu

Build PCRE

Build PCRE and install it to $HOME/opt/pcre. Ubuntu has PCRE but sometimes it is old. I want to use the latest PCRE to enable JIT.

$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.bz2
$ tar xvf pcre-8.34.tar.bz2
$ cd pcre-8.34/
$ ./configure --prefix=$HOME/opt/pcre --enable-jit --enable-unicode-properties
$ make
$ make install

Build The Silver Searcher

PCRE is statically linked with this configuration.

$ aclocal && autoconf && autoheader && automake --add-missing
$ ./configure PCRE_CFLAGS="-I $HOME/opt/pcre/include" PCRE_LIBS="-L $HOME/opt/pcre/lib -Wl,-Bstatic -lpcre -Wl,-Bdynamic"
$ make
@diego898
Copy link

What do you mean "pthread is not"?

@driessen
Copy link

For Cygwin, it also required the LZMA lib.

@k-takata
Copy link
Author

What do you mean "pthread is not"?

It means "pthread is not statically linked". So when you run ag.exe, pthreadGC2.dll is needed.

For Cygwin, it also required the LZMA lib.

Yes. After I wrote this, ag supports lzma. But I haven't compiled the latest ag yet.

@k-takata
Copy link
Author

k-takata commented Jan 6, 2014

I have updated this memo for the latest ag (0.18.1).

@k-takata
Copy link
Author

Updated for ag 0.19.

@kaushalmodi
Copy link

Hi, what CC flag should I use for 64-bit Cygwin? Thanks!

Update: For 64-bit Cygwin, I also installed the following from Cygwin setup: mingw64-x86_64-gcc-core, mingw64-x86_64-gcc-g++ and then used the prefix x86_64-w64-mingw32 instead of i686-pc-mingw32. And then the build PCRE and build XZ steps finished fine. Now proceeding to further steps...

Update 2: Static linking didn't work for me. This is what helped successfully compile ag on 64-bit Cygwin:

  • Follow the steps to compile PCRE and XZ Utils, and then do make install in the very end.
  • For finally installing ag, follow the instructions to download it, extract it and then simply do ./build.sh and make install.

@lel4866
Copy link

lel4866 commented Jun 9, 2017

Since it's been awhile, I just thought I'd like to comment on my experience.
First of all, after carefully following your exact instructions, it worked perfectly on my Windows 7 system at work. But not on the first try and here's why:

When you install MSYS2, it installs 3 subsystems: msys2, mingw32, and mingw64. You do the installs with msys2. But if you read carefully,the instructions above for Build the Silver Searcher say: Open "MSYS2 MinGW 32-bit" or "MSYS2 MinGW 64-bit" from the start menu. YOU MUST MAKE SURE AND OPEN MSYS2 MinGW 32-bit or MSYS2 MinGW 64-bit, NOT MSYS2 MSYS. If you don't, it won't find gcc, and things go downhill from there. If you do, everything works fine.

Now, on Windows 7, when you enter msys2 on the Start menu prompt, it conveniently gives you all 3 and you can select the proper one. But on Windows 10, it only shows you one choice. You have to select the proper one some other way (like opening Windows Explorer, and going to the msys2 installation directory).

One other issue, but not with this sites build instructions. The Windows version of ag doesn't seem to recurse into subdirectories. At least I couldn't figure out what to type to make it happen:

ag testpattern *.c works if you're in the proper directory
ag testpattern *.c doesn't find anything if you're in the parent directory. same with **/*.c

Anyways, really great job! thanks!

@i-give-up
Copy link

Nice instructions. The project's Readme and/or "Windows" wiki page could definitely use the updated instructions you have in this gist.

Also, you should use --needed with pacman so that pacman won't re-install packages if you already have them (I don't know why that's pacman's default behavior. It doesn't make sense to me).


@lel4866

You do the installs with msys2.

That's not true. You can use any subsystem to install packages with pacman and those packages can then be used in any of the three subsystems.

YOU MUST MAKE SURE AND OPEN MSYS2 MinGW 32-bit or MSYS2 MinGW 64-bit, NOT MSYS2 MSYS. If you don't, it won't find gcc, and things go downhill from there. If you do, everything works fine.

That's because mingw32 subsystem is for compiling 32-bit Windows programs, mingw64 subsystem is for compiling 64-bit programs, and msys subsystem is for compiling programs for use in MSYS2.

ag testpattern *.c works if you're in the proper directory
ag testpattern *.c doesn't find anything if you're in the parent directory. same with **/*.c

I just tried ag testpattern *.c in Linux and that doesn't do recursive search either. ag testpattern **/*.c would work if you are using zsh or if you set shopt -s globstar in bash. In any case, I think it's the shell that expands the wildcards into filenames and passes them to ag as argument instead of ag itself expanding the wildcards. On Windows, the shell probably just pass **/*.c literally to ag which is why you'll get errors like

ERR: Error opening directory */*.c: No such file or directory

Anyway, to do what you intend (search a pattern in .c files recursively), use this instead

ag -G "\.c$" testpattern

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment