Skip to content

Building on Windows

devjeonghwan edited this page Jun 28, 2022 · 11 revisions

This little guide will help you to compile Javacpp-Presets for Windows.

It has been written with Windows 7 64 bit in mind so be carefully when using it on a different Windows version. Especially, avoid installing 32bit software on 64bit systems because it will trigger 32bit environment emulation known as WOW64 and mess things up. This is a major source of subtle bugs! Due to the fact we're targeting the Java Native Interface under the hood, it's important to keep in mind that 32bit JVM won't run 64bit native libraries and vice versa!

Prepare the build environment

You'll need to install the following software packages first:

  • MSYS2 - a lightweight Unix-like shell environment for Windows

    Download the appropriate package for your system architecture, run it and choose the following installation path:

    C:\msys64

    After installation, please follow the instructions given on the MSYS2 site in order to initialize the package manager pacman and update system packages.

    Various projects will require additional tools to build properly - in particular, you'll need tar, unzip and patch. Open the MSYS2 console from "Start" menu and type in the following command:

    pacman -S git tar unzip patch

  • Windows 7 Platform SDK - You may actually need another SDK if you're running another Windows version.

  • MingW-w64 compiler toolchain - this is a port of of the GNU compiler collection to Windows

    Download the installer from the above-mentioned site, run it, choose the installation path to be

    C:\mingw64

    and architecture = x86_64, threading model = win32, exception handling = SEH and build revision = 2.

  • 7zip decompression package - required for uncompressing source code delivered as RPM packages, ZIP or GZIP archives and tarballs.

    Please ensure that 7zip executable has been properly installed to

    C:\Program Files\7-Zip\7z.exe

  • Java 7 JDK

  • Maven - one of the most widespread build tools for Java

    Detailed installation instructions for Maven can be found here.

  • Now you'll need to add the following code to your Windows PATH:

    C:\msys64\usr\bin;C:\mingw64\mingw64\bin

    Please ensure that Java and Maven are in your Windows PATH as well.

Build Environment Sanity checks

Fire up the Windows SDK Command Prompt (not the regular command prompt!) and issue the following commands:

bash --version
g++ --version
java -version
mvn --version

If it results in a meaningful output without errors you're ready to proceed with the compilation.

Note on Windows 8.1: Windows SDK 8.1 no longer ships with a complete command-line build environment. Therefore there is no "Windows SDK Command Prompt" under Windows 8.1. Please use the MSYS2 command prompt instead.

Compilation of Javacpp-presets

Please note that you'll first need to compile and install the parent project - Javacpp.

Then proceed with the compilation of Javacpp-presets as follows:

  • download or clone the latest source code and extract it to said javacpp-presets
  • open the Windows SDK Command Prompt (sic!) and issue the following command:
cd javacpp-presets
bash cppbuild.sh -platform windows-x86_64 install leptonica tesseract etc

Specifying the correct target platform via -platform option is obligatory because the uname command used for platform identification in the cppbuild scripts does report details on the MSYS2 platform instead of Windows. TARGET_CPU: unbound variable error! usually indicates a wrong command prompt. Retry with the Windows SDK Command Prompt.

Note on Windows 8.1: Because there is NO "Windows SDK Command Prompt" in Windows 8.1 you'll need to use the MSYS2 command prompt instead. The required environment variable TARGET_CPU should be set manually before calling the build script as follows:

set TARGET_CPU=x64

If everything went well during the previous step, build the Java part as follows:

mvn install --projects .,leptonica,tesseract,etc

Don't forget the period in the beginning of the projects list!

Happy building!

Installation Recipe for Visual Studio 2019 and Windows 10

• Download and install msys2-x86_64-20161025 to C:\msys64
http://msys2.github.io/

• Start msys2 console and run:
pacman -Sy pacman

• close msys2 and start again to run:
pacman -Syu

• close msys2 and start again to run:
pacman -Su

• Next install some necessary packages (press Enter afterwards to accept the installation of all modules):
pacman -S base-devel tar patch make git unzip zip nasm yasm pkg-config

• Now we can install mingw
pacman -Su mingw-w64-x86_64-cmake mingw-w64-x86_64-gcc mingw-w64-i686-gcc mingw-w64-x86_64-gcc-fortran mingw-w64-i686-gcc-fortran mingw-w64-x86_64-libwinpthread-git mingw-w64-i686-libwinpthread-git

• As of 2018-05-18 there is a problem with the gcc compiler when building openblas (maybe other libs too). Therefore download and store the following packages 1 2 3 in the C:\msys64\var\cache\pacman\pkg\ directory. Afterwards start the msys2 console, move to the directory and install the packages:

cd \var\cache\pacman\pkg\
pacman -U mingw-w64-x86_64-crt-git-6.0.0.5100.739199f8-1-any.pkg.tar.xz
pacman -U mingw-w64-x86_64-headers-git-6.0.0.5102.2f16a8c7-1-any.pkg.tar.xz
pacman -U mingw-w64-x86_64-tools-git-6.0.0.5079.3b7a42fd-1-any.pkg.tar.xz

• Inside the C:\msys64 edit the new files mingw32.ini and mingw64.ini. Remove the # at line 4 resulting in:
MSYS2_PATH_TYPE=inherit

• Install Visual Studio 2019 Community Edition with "Desktop development with C++" workload

https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community&rel=16

• Install Windows 10 SDK

https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk

• Start the command line tool "x64 Native Tools Command Prompt for VS 2019" inside the directory:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\VC

• Navigate to the C:\msys64 directory and run mingw64.exe

• Check the path (look for /c/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools)
echo $PATH

• Check the Visual Studio environment variables (should be 16.0)
echo $VisualStudioVersion

• Check the include path (look for C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include and C:\Program Files (x86)\Windows Kits\10\include\10.0.20348.0\ucrt)

• Navigate to a directory where you would like to build the libs. e.g. cd /c/Lang/Java/javacpp/

• There run

git clone https://github.com/bytedeco/javacpp.git 
git clone https://github.com/bytedeco/javacpp-presets.git  
cd javacpp  
mvn clean install

• At the end of the process the javacpp library has been installed into your local maven repository
C:\Users\%USERNAME%\.m2\repository\org\bytedeco\javacpp\

• Now you can build the javacpp-preset default project

cd ..
cd javacpp-presets
mvn clean install -Djavacpp.platform=windows-x86_64 --projects .

• ... and afterwards any other preset you like (e.g openblas)

bash cppbuild.sh -platform windows-x86_64 install openblas  
mvn install -Djavacpp.platform=windows-x86_64 -Djavacpp.cppbuild.skip=true --projects openblas

• In case the library needs to be compiled with GPU support use

bash cppbuild.sh -platform windows-x86_64 -extension "-gpu" install tensorflow
mvn install -Djavacpp.platform=windows-x86_64 -Djavacpp.platform.extension="-gpu" -Djavacpp.cppbuild.skip=true --projects tensorflow