Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUILD-SYSTEM] Add script to automate Debian package building #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 108 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,109 @@
# V8 Library Packaging

This repository contains tools and scripts for building and packaging the V8 JavaScript engine as a static library for different platforms on AMD64 architecture.

## Linux

### Build Script

The `build.sh` script automates the process of building V8 from the source and creating Debian packages.

#### Requirements

- Debian-based system (tested on Debian Bookworm)
- Internet connection for downloading source code
- Root privileges for dependency installation (can use sudo)

#### Script Usage

```bash
./build.sh [--build-number <num>] [--source-dir <dir>] [--pyenv-root <dir>]
[--output-dir <dir>] [--install-deps] [--setup-pyenv] [--debug]
```

##### Command-Line Options

- `--build-number` : Build number for package versioning (required for V8 build)
- `--source-dir` : Source directory containing libv8-packaging (defaults to script directory)
- `--pyenv-root` : Path for pyenv installation (defaults to /opt/pyenv)
- `--output-dir` : Output directory for deb packages (defaults to $HOME/DEB)
- `--install-deps` : Install dependencies
- `--setup-pyenv` : Setup pyenv and install the required Python version
- `--debug` : Enable verbose debugging output
- `--help` : Show help message

> Note: When --build-number is not specified, the script will only perform the
> environment setup steps (install dependencies and/or setup pyenv) if requested.

#### Usage Examples

##### Preparing the Build Environment

To set up the required dependencies and Python environment:

```bash
sudo ./build.sh --install-deps --setup-pyenv
```

##### Building V8 with a Specific Version Number

```bash
./build.sh --build-number 42
```

##### Setting Custom Output Directory

```bash
./build.sh --build-number 123 --output-dir /path/to/output
```

##### Complete Build Process

```bash
sudo ./build.sh --install-deps --setup-pyenv --build-number 456 --output-dir /custom/output/dir
```

##### Running in Docker

The script can be executed in a Docker container:

```bash
docker run -it -v $(pwd):/usr/src/libv8-packaging/ debian:bookworm bash -c \
"cd /usr/src/libv8-packaging/ && bash"
```

Then inside the container:

```bash
./build.sh --install-deps --setup-pyenv
./build.sh --build-number 42 --output-dir /root/DEB/
```

#### Build Output

The script produces Debian packages (.deb files) placed in the specified output
directory or in `$HOME/DEB` by default.

#### Notes

- The script uses Python 2.7.18 for compatibility with V8 build tools
- V8 version 6.1.298 is built by default

## Windows

### Building on Windows

#### Prerequisites

```
You must install the "Debugging Tools for Windows" feature from the Windows 10 SDK.
https://developer.microsoft.com/windows/downloads/windows-10-sdk
https://developer.microsoft.com/windows/downloads/windows-10-sdk
```

#### Script Usage

For building V8 on Windows, use the `build.cmd` script:

```
build.cmd
```
254 changes: 254 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
#!/usr/bin/env bash

# V8 Library Build Script

### docker run -it -v $(pwd):/usr/src/libv8-packaging/ debian:bookworm bash -c "cd /usr/src/libv8-packaging/ && bash"
### ./build.sh --install-deps --setup-pyenv
### ./build.sh --build-number 42 --output-dir /root/DEB/

### shfmt -w -s -ci -sr -kp -fn build.sh

echo "Preparing environment..."

handle_error()
{
echo "Error occurred at line $1"

if [ -n "$BUILD_DIR" ] && [[ $BUILD_DIR == /tmp/* ]]; then
echo "Cleaning up temporary build directory: $BUILD_DIR"
rm -rf "$BUILD_DIR"
fi

exit 1
}

set -e
set -o pipefail

trap 'handle_error $LINENO' ERR

if [ "$(id -u)" -ne 0 ]; then
echo "WARNING: This script may require root privileges to install packages and modify system files."
echo "Consider running with sudo if you encounter permission errors."
echo ""
fi

usage()
{
echo "Usage: $0 [--build-number <num>] [--source-dir <dir>] [--pyenv-root <dir>] [--output-dir <dir>] [--install-deps] [--setup-pyenv] [--debug]"
echo " --build-number : Build number for package versioning (required for V8 build)"
echo " --source-dir : Source directory containing libv8-packaging (defaults to script directory)"
echo " --pyenv-root : Path for pyenv installation (defaults to /opt/pyenv)"
echo " --output-dir : Output directory for deb packages (defaults to $HOME/DEB)"
echo " --install-deps : Install dependencies"
echo " --setup-pyenv : Setup pyenv and install the required Python version"
echo " --debug : Enable verbose debugging output"
echo " --help : Show this help message"
echo ""
echo "Note: When --build-number is not specified, the script will only perform the"
echo " environment setup steps (install dependencies and/or setup pyenv) if requested."
exit 1
}

create_temp_build_dir()
{
BUILD_DIR=$(mktemp -d /tmp/v8-build.XXXXXX)
if [ $? -ne 0 ]; then
echo "Failed to create temporary build directory"
exit 1
fi

echo "Created temporary build directory: $BUILD_DIR"
trap 'rm -rf "$BUILD_DIR"' EXIT
}

install_dependencies()
{
echo "Installing dependencies..."
apt-get update
apt-get -y install \
build-essential \
cmake \
devscripts \
docbook-xsl \
libbz2-dev \
libffi-dev \
libglib2.0-dev \
liblzma-dev \
libncurses5-dev \
libncursesw5-dev \
libreadline-dev \
libsqlite3-dev \
libssl-dev \
libtinfo5 \
llvm \
lsb-release \
ninja-build \
pkg-config \
tk-dev \
zlib1g-dev

echo "Dependencies installed successfully."
}

setup_pyenv()
{
echo "Setting up pyenv..."
if [ ! -d "$PYENV_ROOT" ]; then
git clone --depth 1 --branch "$PYENV_VERSION_TAG" https://github.com/pyenv/pyenv.git "$PYENV_ROOT"
sed -i 's|PATH="/|PATH="$PYENV_ROOT/bin/:/|g' /etc/profile
"$PYENV_ROOT/bin/pyenv" init - | tee -a /etc/profile
if [ -n "$BUILD_DIR" ]; then
echo "export PATH=\"$BUILD_DIR/depot_tools:\${PATH}\"" | tee -a /etc/profile
fi
fi

echo "Installing Python $PYTHON_VERSION..."
pyenv install -s $PYTHON_VERSION
pyenv global $PYTHON_VERSION

echo "Pyenv setup completed successfully."
}

build_v8()
{
echo "Starting V8 build process..."

create_temp_build_dir

cd "$SOURCE_DIR"

cp "$SOURCE_DIR/stub-gclient-spec" "$BUILD_DIR/.gclient"
cp -av "$SOURCE_DIR/debian/" "$BUILD_DIR/"
cd "$BUILD_DIR"

echo "Setting up depot_tools and syncing v8..."
if [ ! -d "$BUILD_DIR/depot_tools" ]; then
git clone --depth=1 https://chromium.googlesource.com/chromium/tools/depot_tools.git "$BUILD_DIR/depot_tools"
fi

export PATH="$BUILD_DIR/depot_tools:$PATH"

echo "Sync to specific v8 version..."
gclient sync --verbose -r $V8_GIT_VERSION

echo "Building v8..."
cd v8
gn gen out.gn --args="is_debug=true symbol_level=2 blink_symbol_level=1 v8_symbol_level=1 v8_static_library=true is_component_build=false v8_enable_i18n_support=false v8_use_external_startup_data=false"
gn args out.gn --list | tee out.gn/gn_args.txt
ninja -v d8 -C out.gn

echo "Updating debian packaging..."
cd "$BUILD_DIR"
sed -i -e "s/GIT_VERSION/$V8_GIT_VERSION/g" "$BUILD_DIR/debian/v8-6.1_static.pc"
sed -i -e "s/GIT_VERSION/$V8_GIT_VERSION/g" "$BUILD_DIR/debian/changelog"
sed -i -e "s/DATE/$(env LC_ALL=en_US.utf8 date '+%a, %d %b %Y %H:%M:%S %z')/g" "$BUILD_DIR/debian/changelog"
sed -i -e "s/DISTRO/$(lsb_release -sc | tr -d '\n')/g" "$BUILD_DIR/debian/changelog"
sed -i -e "s/BUILD_NUMBER/$BUILD_NUMBER/g" "$BUILD_DIR/debian/changelog"

echo "Building debian package..."
debuild -b -us -uc

echo "Moving output files..."
mkdir -p "$OUTPUT_DIR"
mv -v "$BUILD_DIR"/../*.deb "$OUTPUT_DIR"/

echo "Build completed successfully!"
echo "Output packages are available in $OUTPUT_DIR"
}

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_DIR="$SCRIPT_DIR"
BUILD_NUMBER=""
PYENV_ROOT="/opt/pyenv"
OUTPUT_DIR="$HOME/DEB"
INSTALL_DEPS=false
SETUP_PYENV=false
DEBUG=false

while [[ $# -gt 0 ]]; do
case "$1" in
--source-dir)
SOURCE_DIR="$2"
shift 2
;;
--build-number)
BUILD_NUMBER="$2"
shift 2
;;
--pyenv-root)
PYENV_ROOT="$2"
shift 2
;;
--output-dir)
OUTPUT_DIR="$2"
shift 2
;;
--install-deps)
INSTALL_DEPS=true
shift
;;
--setup-pyenv)
SETUP_PYENV=true
shift
;;
--debug)
DEBUG=true
shift
;;
--help)
usage
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done

SOURCE_DIR=$(realpath "$SOURCE_DIR")
echo "Using source directory: $SOURCE_DIR"

if [ "$DEBUG" = true ]; then
echo "Debug mode enabled - verbose output will be shown"
set -x
fi

export DEBIAN_FRONTEND=noninteractive

if [ "$INSTALL_DEPS" = true ]; then
install_dependencies
fi

if ! git config --global --get-all safe.directory | grep -q '\*'; then
git config --global --add safe.directory '*'
fi

export BUILD_NUMBER="$BUILD_NUMBER"
export CODENAME=$(lsb_release -sc)
export PYENV_VERSION_TAG=v2.4.0
export PYTHON_VERSION=2.7.18
export V8_GIT_VERSION=6.1.298
export PYENV_ROOT="$PYENV_ROOT"
export BUILD_DIR="$BUILD_DIR"
export PATH="$PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH"

if [ "$SETUP_PYENV" = true ]; then
setup_pyenv
fi

if [ -n "$BUILD_NUMBER" ]; then
build_v8
else
if [ "$INSTALL_DEPS" = false ] && [ "$SETUP_PYENV" = false ]; then
echo "No operations requested. Please specify at least one action."
echo "Use --install-deps to install dependencies or --setup-pyenv to set up Python environment."
echo "Add --build-number <num> to build V8 library."
usage
else
echo "Environment setup completed. Skipping V8 build as no build number was specified."
echo "To build V8, run again with --build-number <num>"
fi
fi

echo "Script execution completed."