1#!/usr/bin/env bash
2#
3# This file is part of the Phalcon Framework.
4#
5# (c) Phalcon Team <team@phalcon.io>
6#
7# For the full copyright and license information, please view the LICENSE.txt
8# file that was distributed with this source code.
9#
10#  Available params:
11#  --arch
12#  --phpize
13#  --php-config
14#
15#  Example:
16#  ./install --phpize /usr/bin/phpize7.3 --php-config /usr/bin/php-config7.3 --arch 32bits
17
18# Check best compilation flags for GCC
19# By default we compile to be as compatible as possible with all processors.
20# If you would like instruct the compiler to generate optimized machine code that matches the processor where it is currently running on you can set your own compile flags by exporting CFLAGS before the build.
21#
22# Example:
23# export CFLAGS="-march=native -O2 -fomit-frame-pointer"
24#
25# This will generate the best possible code for that chipset but will likely break the compiled object on older chipsets.
26
27export CC="gcc"
28if [ -z "$CFLAGS" ]
29then
30  export CFLAGS="-mtune=native -O2 -fomit-frame-pointer"
31fi
32export CPPFLAGS="-DPHALCON_RELEASE"
33
34# Set defaults
35ARCH=
36PHPIZE_BIN=$(command -v phpize 2>/dev/null)
37PHPCONFIG_BIN=$(command -v php-config 2>/dev/null)
38
39# Translate long options to short
40for arg in "$@"; do
41  shift
42  case "$arg" in
43    "--arch") set -- "$@" "-a" ;;
44    "--phpize") set -- "$@" "-i" ;;
45    "--php-config") set -- "$@" "-c" ;;
46    *) set -- "$@" "$arg"
47  esac
48done
49
50# Options switcher
51while getopts a:i:c: opts; do
52   case ${opts} in
53      a) ARCH=${OPTARG} ;;
54      i) PHPIZE_BIN=${OPTARG} ;;
55      c) PHPCONFIG_BIN=${OPTARG} ;;
56   esac
57done
58
59PHP_FULL_VERSION=`${PHPCONFIG_BIN} --version`
60
61if [ $? != 0 ]; then
62	echo "php-config is not installed"
63	exit 1
64fi
65
66if [ "${PHP_FULL_VERSION:0:1}" == "5" ]; then
67	echo "php 5 is no longer supported"
68	exit 1
69fi
70
71PHP_VERSION="php7"
72
73# Detect possible flags
74echo "int main() {}" > t.c
75gcc ${CFLAGS} t.c -o t 2> t.t
76if [ $? != 0 ]; then
77	chmod +x gcccpuopt
78	BFLAGS=`./gcccpuopt`
79	export CFLAGS="-O2 -fomit-frame-pointer $BFLAGS"
80	gcc ${CFLAGS} t.c -o t 2> t.t
81	if [ $? != 0 ]; then
82		export CFLAGS="-O2"
83	fi
84fi
85
86# Activate some gcc specific optimizations for gcc >= 4
87if [ $(gcc -dumpversion | cut -f1 -d.) -ge 4 ]; then
88	gcc ${CFLAGS}-fvisibility=hidden t.c -o t 2> t.t && export CFLAGS="$CFLAGS -fvisibility=hidden"
89fi
90
91# gcc $CFLAGS -flto t.c -o t 2> t.t && { export CFLAGS="$CFLAGS -flto"; export LDFLAGS="$LDFLAGS $CFLAGS"; }
92rm -f t.t t.c t
93
94# Check processor architecture
95if [ -z ${ARCH} ]; then
96	DIR="32bits"
97	gcc gccarch.c -o gccarch
98	if [ -f gccarch ]; then
99		P64BITS=`./gccarch`
100		if [ "$P64BITS" == "1" ]; then
101			DIR="64bits"
102		fi
103	fi
104else
105	DIR=${ARCH}
106fi
107
108# Move to specified architecture
109cd "$PHP_VERSION/$DIR"
110
111# Clean current compilation
112if [ -f Makefile ]; then
113	make clean
114	${PHPIZE_BIN} --clean
115fi
116
117# Perform the compilation
118${PHPIZE_BIN}
119
120# For some reason the libtool script being generated by autogen contains lines referring
121# to "$echo message" instead of "echo message".
122export echo=echo
123
124# Detect Gentoo Linux
125if [ -f /etc/gentoo-release ]; then
126    LIBTOOLIZE_BIN=$(command -v libtoolize 2>/dev/null)
127    aclocal && ${LIBTOOLIZE_BIN} --force && autoheader && autoconf
128fi
129
130# Detect macOS
131if [ "$(uname -s 2>/dev/null)" = "Darwin" ]; then
132    LIBTOOLIZE_BIN=$(command -v glibtoolize 2>/dev/null)
133    aclocal && ${LIBTOOLIZE_BIN} --force && autoheader && autoconf
134fi
135
136./configure --silent --with-php-config=${PHPCONFIG_BIN} --enable-phalcon
137
138make -s -j"$(getconf _NPROCESSORS_ONLN)"
139make -s install
140
141echo -e "\nThanks for compiling Phalcon!\nBuild succeed: Please restart your web server to complete the installation\n"
142