Project

General

Profile

Patching NCURSES Using Static Linking

Introduction

In Linux/UNIX, the FWD native library code depends upon a terminal functions library called NCURSES. That library requires some slight modifications to work properly with FWD. The process described in this page explains how to patch NCURSES to be compatible with FWD.

Previously the NCURSES calls were used via dynamic linkage at runtime. Using dynamic runtime linkage requires that the system version of NCURSES is patched. This is intrusive because it can affect other applications on the same system. It also meant that we have to keep track of the current system wide version of this library and re-patch it whenever it changes, which can happen at any moment due to software updates being applied. To always be patched before building/running FWD application, we would have to hook the software updates process and apply the patching. This is not easy and not convenient.

With the new approach described here we can forget about this issue and have full control over NCURSES that is used just for a specific FWD build. The solution is to statically link with functions we need in NCURSES library. This embeds the patched NCURSES library inside libp2j.so so that the correct version of the functions will always be available.

In this approach the NCURSES package is building from sources on the local system and the produced libraries can be stored within the same account as the developer has for building FWD. This avoids requiring administrative rights (e.g. sudo or root access).

For details on the dynamic linking approach see Patching NCURSES.

Building Process

Prerequisites

Ensure that dpkg-dev package is installed on the system. The dpkg-dev package provides the development tools (including dpkg-source) required to unpack and build source packages.

sudo apt-get install dpkg-dev

Enable Source Package Downloads

It is important that apt has access to the source code repositories.

Run cat /etc/apt/sources.list | grep deb-src to see the status of the source repositories. Check if all of the entries are commented out (with a # character at the beginning of the line. If you see something like this:

# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates multiverse
# deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted
# deb-src http://security.ubuntu.com/ubuntu xenial-security universe
# deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse

Then you should remove the comment character, leaving behind the rest of the line.

Before changing the file, you can back it up using this command:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.orig

And here is a command to make all the replacements for you:

sudo sed --in-place 's/# deb-src/deb-src/' /etc/apt/sources.list

After this, ensure the repositories are loaded using this command:

sudo apt-get update

Scripted Patching

To automatically integrate the patched NCURSES it is convenient to use single script (written for Ubuntu 22.04 LTS): setup_ncurses6x.sh. Just put it into a new directory for patched libraries and execute.

The possible options:

Usage: ./setup_ncurses6x.sh [-p<path>] [-sd]
Where:
p     = Use specific path instead of current directory
s     = Set system-wide environment instead of just current user
d     = Dry run. Just show commands.

Otherwise the manual setup process is described in a sections below.

Manual Patching Process

This assumes the directory ~/projects/ncurses_static exists on local system.

Get the NCURSES Source Code
cd ~/projects/ncurses_static
apt-get source ncurses
Patch the NCURSES Source Code

The recent versions of Ubuntu have the 6.X revision of NCURSES. Assuming the obtained version is 6.1:

cd ncurses-6.1
wget https://proj.goldencode.com/downloads/ncurses/ncurses_curses_h_in_v6.1_20200708.patch
wget https://proj.goldencode.com/downloads/ncurses/ncurses_lib_getch_c_v6.1_20200708.patch
patch include/curses.h.in ncurses_curses_h_in_v6.1_20200708.patch
patch ncurses/base/lib_getch.c ncurses_lib_getch_c_v6.1_20200708.patch
Build the Patched NCURSES
./configure --with-termlib CFLAGS='-fPIC -O2' --with-abi-version=6
make

The -fPIC option is mandatory. Otherwise the FWD will not be statically linked with object code produced.

After building completed the link to the ncurses.h should be created to make the FWD new build:

cd include
ln -s curses.h ncurses.h
cd ..

Configure the System to Use a Custom NCURSES

Before FWD can be successfully built, it is required to configure the location to search for the static libraries to the gcc linker. This can be done by defining the bash shell variable NCURSES_FWD_STATIC and exporting it for all subsequent commands.

To have this always available in your shell, add the following commands to the .bashrc session config file:

export NCURSES_FWD_STATIC="custom/path/to/patched/root/directory" 

The custom/path/to/patched/root/directory should be adjusted with current location of the patched NCURSES root directory. In our example it is: ~/projects/ncurses_static/ncurses-6.1

Build FWD

Then open new terminal window and run FWD build as usual.

As an alternative to defining NCURSES_FWD_STATIC in .bashrc, you can just use an "inline configuration" approach:

export NCURSES_FWD_STATIC="custom/path/to/patched/root/directory"; <command_to_build_fwd>

setup_ncurses6x.sh Magnifier - Ubuntu NCURSES 6.x patching script (static linking). (2.01 KB) Greg Shah, 06/07/2023 08:59 AM

setup_ncurses6x.sh Magnifier - Ubuntu and RedHat NCURSES 6.x patching script (static linking) (3.93 KB) Greg Shah, 06/08/2023 04:59 AM

setup_ncurses6x.sh Magnifier - Fixed bug in environmental setup (static linking) (4.02 KB) Roger Borrello, 12/12/2023 05:41 PM