Skip to content

Building Tutorial

Will Hedgecock edited this page Dec 1, 2021 · 42 revisions

Building this library yourself is not advised (at least not for distribution) as it requires native compilation across multiple platforms. It is recommended to simply use the pre-built jSerialComm.jar library in your application. For installation instructions, please refer to the Installation wiki.


Required Tools

If you do choose to build this library yourself, the recommended methodology is to use Crosstool-NG, Clang, and Gradle, coupled with two Java SDKs, versions 1.6 and 9.0.4 (for backward compatibility). These tools can be obtained from the following locations:

Please note that there may be alternate methods of obtaining each of these tools based on your operating system (especially certain Linux distributions).

If you would like to edit any of the source code or view it in an IDE (such as Eclipse), you can automatically create the Eclipse project files by entering the following instruction on a command line or terminal from the base directory of this project:

gradle eclipse

You can then import the project into Eclipse using the built-in "Import->Existing Project into Workspace" tool. (Note that if you use Eclipse as an IDE, you will probably want to install the Eclipse CDT plugin for proper handling of the C source code)

Once the required tools have been installed, refer to the next sections for instructions on how to cross-compile the various native libraries.


Building Linux Cross Compilers

In order to cross-compile for all applicable Linux architectures, we recommend using the Crosstool-NG tool from a Linux (or WSL) platform that can be downloaded from here. Installation of this tool can be achieved by running the following commands from a Linux terminal:

cd ~
wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.24.0.tar.bz2
tar xjf crosstool-ng-1.24.0.tar.bz2
rm -rf crosstool-ng-1.24.0.tar.bz2
cd crosstool-ng-1.24.0
./configure --prefix=/usr
make
sudo make install
cd ..
rm -rf crosstool-ng-1.24.0

In order to configure this tool to build for the various Linux architectures, you will need the Crosstool-NG configuration files (*.config) that can be downloaded from here. For the following instructions, assume that these files are located in a subdirectory at {CONFIG_SUBDIR}. Run the following commands from a Linux terminal to build the required toolchains:

mkdir build
cd build
cp -f {CONFIG_SUBDIR}/jSerialComm32.config .config
ct-ng build
cp -f {CONFIG_SUBDIR}/jSerialComm32HF.config .config
ct-ng build
cp -f {CONFIG_SUBDIR}/jSerialComm64.config .config
ct-ng build
cp -f {CONFIG_SUBDIR}/jSerialCommPPC64LE.config .config
ct-ng build
cp -f {CONFIG_SUBDIR}/jSerialCommx86.config .config
ct-ng build
cp -f {CONFIG_SUBDIR}/jSerialCommx86_64.config .config
ct-ng build
cd ..
rm -rf build

Finally, include the new toolchains in your path by adding $HOME/x-tools/i486-unknown-linux-gnu/bin, $HOME/x-tools/x86_64-unknown-linux-gnu/bin, $HOME/x-tools/arm-unknown-linux-gnueabi/bin, $HOME/x-tools/arm-unknown-linux-gnueabihf/bin, $HOME/x-tools/aarch64-unknown-linux-gnu/bin, and $HOME/x-tools/powerpc64le-unknown-linux-gnu/bin to your PATH variable in the ~/.bash_profile or ~/.bashrc file.


Building Android Cross Compilers

In order to build the Android native libraries, the Android NDK must be downloaded from here and extracted somewhere on your local hard drive (e.g., extract to /opt/ to store the toolchain in /opt/android-ndk-rxxx, where rxxx denotes the current version of the toolchain). This directory should also be added to your PATH variable in the ~/.bash_profile or ~/.bashrc file.


Building Solaris Cross Compilers

In order to build for the Solaris Operating System, you will need one Linux-based cross-compiler for the x86_64 hardware architecture and one for all Sparc-based architectures. Before starting, you will need to obtain the Solaris system header files for both architectures, either from here or from the Solaris 10 Installation DVDs. You will also need to obtain a copy of GCC v7.3.0 and Binutils v.2.30.0. Once these items have been acquired, place them in a directory called ~/buildcc, and prepare your host system to build the cross-compilers by issuing the following commands:

mkdir ~/x-tools && cd ~/x-tools
tar xvhf ../buildcc/SolarisSystemHeaders.tgz
cd ~/buildcc

Please Note: If you are obtaining the Solaris system headers directly from the Solaris Installation DVDs instead of the provided tarball, you will need to manually extract the contents of SUNWhea and SUNWarc to ~/x-tools/$TARGET/sysroot for both architectures, where $TARGET is one of either x86_64-sun-solaris2.10 or sparc-sun-solaris2.10. Additionally, you will need to extract the various versions of libc.so.1 from SUNWcslr to ~/x-tools/$TARGET/sysroot/lib and create symlinks to them as libc.so in ~/x-tools/$TARGET/sysroot/usr/lib.

Once your host build system has been set up, prepare to build the x86_64 Solaris cross-compiler by issuing the following commands in order:

export TARGET=x86_64-sun-solaris2.10
export PREFIX="$HOME/x-tools/$TARGET"
export SYSROOT="$PREFIX/sysroot"
export PATH="$PREFIX/bin:$PATH"

First, build the binutils package from source via the following commands:

mkdir build-binutils && cd build-binutils
../binutils-2.30/configure --prefix="$PREFIX" --target="$TARGET" --with-sysroot="$SYSROOT" --disable-nls --disable-werror
make && make install
cd .. && rm -rf build-binutils

Next, build the GCC cross-compiler by issuing the following:

mkdir build-gcc && cd build-gcc
../gcc-7.3.0/configure --prefix="$PREFIX" --target="$TARGET" --with-sysroot="$SYSROOT" --disable-nls --enable-languages=c --with-gnu-as --with-gnu-ld
make all-gcc all-target-libgcc
make install-gcc install-target-libgcc
cd .. && rm -rf build-gcc

Repeat the previous steps to build the Solaris cross-compiler for the Sparc system architecture, like so:

export TARGET=sparc-sun-solaris2.10
export PREFIX="$HOME/x-tools/$TARGET"
export SYSROOT="$PREFIX/sysroot"
export PATH="$PREFIX/bin:$PATH"

Continue by building the binutils package followed by GCC. At this point, both working cross-compilers should be fully installed in ~/x-tools/$TARGET/bin. Add the new toolchains to your path by adding $HOME/x-tools/x86_64-sun-solaris2.10/bin and $HOME/x-tools/sparc-sun-solaris2.10/bin to your PATH variable in the ~/.bash_profile or ~/.bashrc file.


Building FreeBSD Cross Compilers

In order to build for the various FreeBSD architectures, you will need Clang and LLD installed on your system (usually available via your OS's package manager under the package names clang and lld). The rest of the setup process involves obtaining and setting up the OS-specific headers and libraries which can be done by executing the following commands:

mkdir -p $HOME/x-tools/x86_64-unknown-freebsd11.2 && cd $HOME/x-tools/x86_64-unknown-freebsd11.2
wget http://ftp.plusline.de/FreeBSD/releases/amd64/11.2-RELEASE/base.txz
tar -xf base.txz ./lib/ ./usr/lib/ ./usr/include/
rm base.txz
mkdir -p $HOME/x-tools/arm64-unknown-freebsd11.2 && cd $HOME/x-tools/arm64-unknown-freebsd11.2
wget http://ftp.plusline.de/FreeBSD/releases/arm64/11.2-RELEASE/base.txz
tar -xf base.txz ./lib/ ./usr/lib/ ./usr/include/
rm base.txz
mkdir -p $HOME/x-tools/i386-unknown-freebsd11.2 && cd $HOME/x-tools/i386-unknown-freebsd11.2
wget http://ftp.plusline.de/FreeBSD/releases/i386/11.2-RELEASE/base.txz
tar -xf base.txz ./lib/ ./usr/lib/ ./usr/include/
rm base.txz

Ensure that clang is present in your PATH and accessible via the command line. If it is not, the location of the clang binary should be added to your PATH variable in the ~/.bash_profile or ~/.bashrc file.


Building Mac OS Cross Compilers

In order to build for Mac OS X or later, you will need one Linux-based cross-compiler for 32-bit architectures and one for 64-bit architectures. Before starting, you will need to obtain the Mac OS system header files for both architectures from here and here. Once these items have been acquired, place them in a directory called ~/mac-headers, and prepare your host system to build the cross-compilers by issuing the following commands:

sudo apt install cmake
git clone https://github.com/tpoechtrager/osxcross.git
cd osxcross
sudo tools/get_dependencies.sh

Once your host build system has been set up, build the 32-bit Mac OS cross-compiler by issuing the following commands in order:

cp -f ~/mac-headers/MacOSX10.13.sdk.tar.xz tarballs/
TARGET_DIR=$HOME/x-tools/osx32 OSX_VERSION_MIN=10.6 ./build.sh
rm -rf $HOME/x-tools/osx32/bin/o64*
cd .. && rm -rf osxcross

Next, build the 64-bit Mac OS cross-compiler by issuing the following commands in order:

git clone https://github.com/tpoechtrager/osxcross.git
cd osxcross
cp -f ~/mac-headers/MacOSX12.0.sdk.tar.xz tarballs/
TARGET_DIR=$HOME/x-tools/osxcross OSX_VERSION_MIN=10.9 ./build.sh
cd .. && rm -rf osxcross

Include the new toolchains in your path by adding $HOME/x-tools/osxcross/bin and $HOME/x-tools/osx32/bin to your PATH variable in the ~/.bash_profile or ~/.bashrc file.


Cross-Compiling Native Libraries

Before beginning compilation of the various libraries, ensure that the JDK_HOME environment variable has been set for the 9.0.4 version of your Java SDK. The correct directory can usually be found by entering the following command:

readlink -f /usr/bin/java

Export the result of this command up to BUT NOT INCLUDING the "/jre/bin/java" portion using the 'export' command. For example, if 'readlink' produced "/usr/lib/jvm/java-9.0.4/jre/bin/java" as an output, the export command would look like:

export JDK_HOME=/usr/lib/jvm/java-9.0.4

If you installed the 9.0.4 version of the Java JDK to non-standard location, you may need to manually specify its location (e.g., /usr/local/jdk-9.0.4). It is a good idea to add this export command to your ~/.bash_profile or ~/.bashrc file to avoid having to repeat this step in the future.

Change directories to jSerialComm/src/main/c/Posix/, and run the following commands in order:

make linux
make arm
make powerpc
make solaris
make freebsd
make osx
make android
cd ../../../..
gradle build

The resulting jSerialComm library can be found in the project directory jSerialComm/build/libs/ under the name "jSerialComm-{VERSION}.jar"


Windows

Ensure that the Visual Studio C++ Compiler is installed on your system. If it is not, you can download a free version (Visual C++ 2019 Community) from here.

Open a command prompt (Run->'cmd') and change to the following project directory: *jSerialComm\src\main\c\Windows*

On Windows, the Visual Studio Compiler must be configured to build either 32- or 64-bit binaries but never both at the same time. Therefore, you will have to build binaries for the two architectures separately. For the following commands, {VC_DIRECTORY} is the installation directory of your Visual Studio C++ Compiler (for example: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC or C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build). Run the following commands in order:

{VC_DIRECTORY}\vcvarsall.bat x64
nmake win64
{VC_DIRECTORY}\vcvarsall.bat x86
nmake win32
cd ..\..\..\..
gradle build

The resulting jSerialComm library can be found in the project directory *jSerialComm\build\libs* under the name "jSerialComm-{VERSION}.jar"