Skip to content

Commit

Permalink
Use generic system for installing php extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiang committed Dec 2, 2014
1 parent 798bb76 commit ad455b9
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 240 deletions.
28 changes: 28 additions & 0 deletions bin/php-build
Expand Up @@ -104,6 +104,8 @@ CONFIGURE_OPTIONS=$(cat "$PHP_BUILD_ROOT/share/php-build/default_configure_optio
# Patches to be applied at the source
PATCH_FILES=""

[ -z "$PHPBUILD_INSTALL_EXTENSION" ] && PHPBUILD_INSTALL_EXTENSION=""

if [ -n "$PHP_BUILD_CONFIGURE_OPTS" ]; then
CONFIGURE_OPTIONS="$CONFIGURE_OPTIONS $PHP_BUILD_CONFIGURE_OPTS"
fi
Expand Down Expand Up @@ -428,6 +430,25 @@ function apply_patches {
fi
}

# Install extensions
function install_extensions {
## handle extensions that should be installed by defined environment variable
## variable must be in the format: extension_name=version extension_name=version
for extension_def in $PHPBUILD_INSTALL_EXTENSION; do
local extension=$(echo $extension_def | cut -d"=" -f1)
local version=$(echo $extension_def | cut -d"=" -f2)
local first_char=$(echo $version | cut -c1 )

# if first character of version is an "@" it's meant to be a revision
if [ $first_char = "@" ]; then
local version=$(echo $version | cut -c"2-")
install_extension_source $extension "$version"
else
install_extension $extension $version
fi
done
}

# Definition commands
# -------------------

Expand Down Expand Up @@ -780,6 +801,10 @@ exec 4<> "$LOG_PATH"

[ "$ENABLE_PEAR" = "1" ] && log Warning "Installing Pear alongside of Pyrus is still experimental."

# Load extension plugin
source "$PHP_BUILD_ROOT/share/php-build/extension/extension.sh"
log Info "Loaded extension plugin"

# Load all definition plugins. Plugins register functions
# for use whithin definitions. See the xdebug and pyrus plugins for
# examples.
Expand All @@ -805,6 +830,9 @@ if [ "$ENABLE_PEAR" = "1" ]; then
pear_setup
fi

# Installed extensions defined by environment variable
install_extensions 2>&4

# Unbind the error handler.
trap - ERR
trap - EXIT
Expand Down
8 changes: 8 additions & 0 deletions share/php-build/extension/definition
@@ -0,0 +1,8 @@
"name","url-dist","url_source","source_cwd","configure_args","extension_type","after_install"
"apc","http://pecl.php.net/get/APC-$version.tgz","git@git.php.net:/pecl/caching/apc.git",,"--enable-apc","extension",
"apcu","http://pecl.php.net/get/apcu-$version.tgz","https://github.com/krakjoe/apcu.git",,,"extension",
"imagick","http://pecl.php.net/get/imagick-$version.tgz","https://github.com/mkoppanen/imagick.git",,,"extension",
"uprofiler",,"https://github.com/FriendsOfPHP/uprofiler.git","extension",,"extension","uprofiler_after_install"
"xcache","http://xcache.lighttpd.net/pub/Releases/$version/xcache-$version.tar.gz",,,"--enable-xcache","extension",
"xdebug","http://xdebug.org/files/xdebug-$version.tgz","git://github.com/xdebug/xdebug.git",,"--enable-xdebug","zend_extension","xdebug_after_install"
"xhprof","http://pecl.php.net/get/xhprof-$version.tgz","git://github.com/facebook/xhprof.git",,,"extension","xhprof_after_install"
156 changes: 156 additions & 0 deletions share/php-build/extension/extension.sh
@@ -0,0 +1,156 @@
#!/usr/bin/env bash
#
# Plugin for php-build to install PHP extensions
#

function install_extension {
local extension="$1"
local version="$2"
local type="$3"
local database=$PHP_BUILD_ROOT/share/php-build/extension/definition

# set type to "dist" per default
[[ -z "$type" ]] && type="dist"

local extension_line=$(grep "\"$extension\"" $database)

if [ -n "$extension_line" ]; then
IFS=, read name url_dist url_source source_cwd configure_args \
extension_type after_install <<< "$(echo "$extension_line" | sed 's/\"//g')"

# install from distribution package
if [ $type = "dist" ]; then
log "$extension" "Installing version $version"

if [ -z "$version" ]; then
echo "No version given for extension \"$extension\"" >&3
return 1
fi

_download_extension $name $version $url_dist "$configure_args" \
$extension_type "$after_install"
else
log "$extension" "Installing from source"

_checkout_extension $name "$version" $url_source "$source_cwd" \
"$configure_args" $extension_type "$after_install"
fi
else
echo "No configuration found fo extension \"$extension\", skipping" >&3
fi
}

function install_extension_source {
local extension="$1"
local revision="$2"

install_extension $extension "$revision" "source"
}

function _download_extension {
local name=$1
local version=$2
local url=$3
local configure_args=$4
local extension_type=$5
local after_install=$6
local package_url=$(eval echo $url)
local package_name="$name-$version"

# We cache the tarballs for APC versions in `packages/`.
if [ ! -f "$TMP/packages/$name-$version.tgz" ]; then
http get "$package_url" > "$TMP/packages/$package_name.tgz"
fi

# Each tarball gets extracted to `source/apcu-$version`.
if [ -d "$TMP/source/$package_name" ]; then
rm -rf "$TMP/source/$package_name"
fi

tar -xzf "$TMP/packages/$package_name.tgz" -C "$TMP/source"

[[ -f "$TMP/source/package.xml" ]] && rm "$TMP/source/package.xml"
[[ -f "$TMP/source/package2.xml" ]] && rm "$TMP/source/package2.xml"

_build_extension "$TMP/source/$package_name" $name "" "$configure_args" \
$extension_type "$after_install"
}

function _checkout_extension {
local name=$1
local version="$2"
local url_source="$3"
local source_cwd="$4"
local configure_args="$5"
local extension_type="$6"
local after_install="$7"
local source_dir="$TMP/source/$name-master"

if [ -d "$source_dir" ] && [ -d "$source_dir/.git" ]; then
log "$name" "Updating $name from Git Master"
cd "$source_dir"
git pull origin master > /dev/null
cd "$cwd"
else
log "$name" "Fetching from Git Master"
git clone "$url_source" "$source_dir" > /dev/null
fi

if [ -n "$version" ]; then
log "$name" "Checkout specified revision: $version"
cd "$source_dir"
git reset --hard $revision
cd "$cwd"
fi

_build_extension "$source_dir" $name "$source_cwd" "$configure_args" \
$extension_type "$after_install"
}

function _build_extension {
local source_dir="$1"
local name=$2
local source_cwd="$3"
local configure_args=$4
local extension_type=$5
local after_install=$6
local cwd=$(pwd)

log "$name" "Compiling $name in $source_dir"

cd "$source_dir/$source_cwd"

{
$PREFIX/bin/phpize > /dev/null
"$(pwd)/configure" --with-php-config=$PREFIX/bin/php-config \
$configure_args > /dev/null

make > /dev/null
make install > /dev/null
} >&4 2>&1

local extension_home="$PREFIX/share/$name"

[ ! -d "$extension_home" ] && mkdir -p "$extension_home"

local extension_ini="$PREFIX/etc/conf.d/$name.ini"

if [ ! -f "$extension_ini" ]; then
log "$name" "Installing $name configuration in $extension_ini"

echo "$extension_type=\"$name.so\"" > $extension_ini
fi

if [ -n "$after_install" ]; then
# Zend extensions are not looked up in PHP's extension dir, so
# we need to find the absolute path for the extension_dir.
local extension_dir=$("$PREFIX/bin/php-config" --extension-dir)

$after_install "$source_dir" "$extension_ini" "$extension_type" "$extension_dir" "$extension_home"
fi

log "$name" "Cleaning up."
make clean > /dev/null

cd "$cwd" > /dev/null
}
69 changes: 4 additions & 65 deletions share/php-build/plugins.d/apc.sh
@@ -1,69 +1,8 @@
#!/usr/bin/env bash
#
# This shell scriplet is meant to be included by other shell scripts
#!/usr/bin/env bash
#
# This shell scriplet is meant to be included by other shell scripts
# to set up some variables and a few helper shell functions.

function install_apc {
local version=$1
local package_url="http://pecl.php.net/get/APC-$version.tgz"

if [ -z "$version" ]; then
echo "install_APC: No Version given." >&3
return 1
fi

log "APC" "Downloading $package_url"

# We cache the tarballs for APC versions in `packages/`.
if [ ! -f "$TMP/packages/APC-$version.tgz" ]; then
wget -qP "$TMP/packages" "$package_url"
fi

# Each tarball gets extracted to `source/APC-$version`.
if [ -d "$TMP/source/APC-$version" ]; then
rm -rf "$TMP/source/APC-$version"
fi

tar -xzf "$TMP/packages/APC-$version.tgz" -C "$TMP/source"

[[ -f "$TMP/source/package.xml" ]] && rm "$TMP/source/package.xml"
[[ -f "$TMP/source/package2.xml" ]] && rm "$TMP/source/package2.xml"

_build_apc "$TMP/source/APC-$version"
}

function _build_apc {
local source_dir="$1"
local cwd=$(pwd)

log "APC" "Compiling in $source_dir"

cd "$source_dir"

{
$PREFIX/bin/phpize > /dev/null
"$(pwd)/configure" --enable-apc \
--with-php-config=$PREFIX/bin/php-config > /dev/null

make > /dev/null
make install > /dev/null
} >&4 2>&1

local apc_home="$PREFIX/share/apc"

[ ! -d "$apc_home" ] && mkdir -p "$apc_home"

local apc_ini="$PREFIX/etc/conf.d/apc.ini"

if [ ! -f "$apc_ini" ]; then
log "APC" "Installing APC configuration in $apc_ini"

echo "extension=\"apc.so\"" > $apc_ini

fi

log "APC" "Cleaning up."
make clean > /dev/null

cd "$cwd" > /dev/null
install_extension "apc" "$1"
}
12 changes: 12 additions & 0 deletions share/php-build/plugins.d/uprofiler.sh
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

function uprofiler_after_install {
local source_dir=$1
local ini_file=$2
local extension_home=$5

cp -r "$source_dir/uprofiler_html" "$extension_home"
cp -r "$source_dir/uprofiler_lib" "$extension_home"

echo "uprofiler.output_dir=\"/var/tmp/uprofiler\"" >> $ini_file
}

0 comments on commit ad455b9

Please sign in to comment.