install_json_c.sh

Overview

install_json_c.sh is a Bash shell script designed to automate the process of downloading, building, and installing the json-c C library from its GitHub repository. It manages the environment setup, error handling, and installation path configuration to ensure a reliable and repeatable installation of the library into a specific project directory structure.


Script Breakdown and Functionality

1. Shell Environment Setup

#!/usr/bin/env bash
set -e
if [[ "${BASHOPTS}" != *extdebug* ]]; then
    set -e
fi

2. Error Reporting Function: err_report

err_report() {
    cd ${source}
    echo "ERROR: $0:$*"
    exit 8
}

3. Error Trap Setup

if [[ "${BASHOPTS}" != *extdebug* ]]; then
    trap 'err_report $LINENO' ERR
fi

4. Directory and Build Setup

cwd=$(cd "$(dirname $(dirname "${BASH_SOURCE[0]}"))" &> /dev/null && pwd)
BUILD_DIR="${cwd}/build"

mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"

5. Cloning and Building json-c

git clone https://github.com/json-c/json-c.git
cd json-c

cmake -DCMAKE_INSTALL_PREFIX=../../libs/ -DCMAKE_BUILD_TYPE=release
make all install

6. Finalization

cd "${cwd}"

[[ $SHLVL -eq 2 ]] && echo OK

cd "${pwd}"

Important Implementation Details


Interaction with Other Parts of the System


Usage Example

To use this script, navigate to the appropriate project directory and run:

./install_json_c.sh

Upon successful execution, the json-c library will be cloned, built, and installed into the libs/ directory relative to the project root.


Mermaid Diagram: Script Workflow

flowchart TD
Start --> Check_BashOpts
Check_BashOpts --> Setup_Error_Trap
Setup_Error_Trap --> Set_Directories
Set_Directories --> Create_Build_Dir
Create_Build_Dir --> Clone_Repo
Clone_Repo --> Enter_Repo
Enter_Repo --> Run_CMake
Run_CMake --> Make_Install
Make_Install --> Return_To_Root
Return_To_Root --> Check_SHLVL
Check_SHLVL --> End
subgraph ErrorHandling
Check_BashOpts
Setup_Error_Trap
end
subgraph BuildSetup
Set_Directories
Create_Build_Dir
end
subgraph BuildAndInstall
Clone_Repo
Enter_Repo
Run_CMake
Make_Install
end
subgraph Finalization
Return_To_Root
Check_SHLVL
end

This documentation provides a detailed understanding of the install_json_c.sh script, enabling effective use and modification in the context of its project.