build 6.7 KB
Newer Older
1 2
#!/bin/bash

3
pushd $(dirname "$0") > /dev/null
4 5 6 7 8 9 10 11 12 13 14 15
D=$(pwd -P)
popd > /dev/null

BASED="$D/.build"
HOMED="$D/.home"

SOURCED="$HOMED/source"
INSTALLD="$HOMED/.local"

GTK_OSX_BASE_URL="https://git.gnome.org/browse/gtk-osx/plain/"
JHBUILD_SOURCED="$SOURCED/jhbuild"

16 17
ME="$0"

18 19 20 21 22 23 24
if [ -z "$GEDIT_SDK" ]; then
	export GEDIT_SDK=10.7
fi

export PATH="$INSTALLD/bin:$PATH"

function do_exit() {
25
	printf "$@"
26
	echo ""
27 28 29 30 31 32 33 34 35 36 37
	exit 1
}

if [ ! which git &>/dev/null ]; then
	do_exit "You need to have git installed to build gedit for OS X"
fi

if [ ! xcodebuild -version &>/dev/null ]; then
	do_exit "You need to have Xcode installed to build gedit for OS X"
fi

38 39 40 41 42 43 44
xcodebase=$(xcode-select -p)
sdkdir="$xcodebase/Platforms/MacOSX.platform/Developer/SDKs/MacOSX$GEDIT_SDK.sdk"

if [ ! -d "$sdkdir" ]; then
	do_exit "SDK base directory for $GEDIT_SDK not found. Please make sure to install the SDK at $sdkdir"
fi

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
function checkout_jhbuild() {
	#JHBUILD_REVISION=$(curl -ks "$GTK_OSX_BASE_URL/jhbuild-revision")

	#if test x"$JHBUILD_REVISION" = x; then
	#	do_exit "Could not find jhbuild revision to use."
	#fi

	mkdir -p $(dirname "$JHBUILD_SOURCED")

	git clone git://git.gnome.org/jhbuild "$JHBUILD_SOURCED" || do_exit "Failed to clone jhbuild."
	#git --work-tree "$JHBUILD_SOURCED" checkout -b stable $JHBUILD_REVISION || do_exit "Failed to checkout stable jhbuild revision."
}

function build_jhbuild() {
	echo "Building jhbuild..."
	(cd "$JHBUILD_SOURCED" && ./autogen.sh --prefix="$INSTALLD" && make -f Makefile.plain DISABLE_GETTEXT=1 install) >/dev/null || do_exit "Jhbuild installation failed";

	rm -f "$INSTALLD/bin/python2"

	cat << PYTHON2 > "$INSTALLD/bin/python2"
#!/bin/bash

exec /usr/bin/python "\$@"
PYTHON2
	chmod +x "$INSTALLD/bin/python2"
}

function setup_jhbuildrc() {
	echo "Installing jhbuild configuration..."

	mkdir -p "$HOMED"
    curl -ks "$GTK_OSX_BASE_URL/jhbuildrc-gtk-osx" | \
    	sed -e 's/^.*PYTHONPATH.*$//g' -e 's/^.*PYTHON_SITE_PACKAGES.*$//g' > "$HOMED/.jhbuildrc" || do_exit "Failed to get jhbuildrc."

    rm -f "$HOMED/.jhbuildrc-gedit"
    ln -s "$D/config/jhbuildrc-gedit" "$HOMED/.jhbuildrc-gedit" || exit 1
}

83 84 85 86
function init_help_short() {
	echo "Initialize the in-tree build environment (installs jhbuild)"
}

87 88 89 90 91 92 93 94 95
function cmd_init() {
	mkdir -p "$SOURCED"

	if [ ! -d "$JHBUILD_SOURCED" ]; then
		checkout_jhbuild
	fi

	build_jhbuild
	setup_jhbuildrc
96 97

	rm -f "$BASED/$GEDIT_SDK/source/gedit"
98
	mkdir -p "$BASED/$GEDIT_SDK/source"
99
	ln -s $(cd "$D/../../" && pwd) "$BASED/$GEDIT_SDK/source/gedit"
100 101
}

102 103 104 105
function bootstrap_help_short() {
	echo "Run jhbuild bootstrap"
}

106 107 108 109 110 111 112 113 114 115 116 117 118
function cmd_bootstrap() {
	# Built python once
	cmd_jh bootstrap -q python || exit 1

	# Built python twice! There is a bug where python somehow manages to link
	# its main binary to the system framework library, which then doesn't work...
	# Building python again seems to resolve the issue...
	cmd_jh bootstrap -q -f -t python python || exit 1

	# Bootstrap all the rest
	cmd_jh bootstrap -q || exit 1
}

119 120 121 122 123 124 125 126
function jh_help_usage() {
	echo "jhbuild-command ..."
}

function jh_help_short() {
	echo "Run jhbuild commands"
}

127 128 129 130
function cmd_jh() {
	# Setup our jhbuild environment
	export GEDIT_OSX_SOURCE_BASE="$D"
	export GEDIT_OSX_BUILD_BASE="$BASED"
131
	export __GEDIT_OSX_OLD_HOME="$HOME"
132 133 134
	export HOME="$HOMED"
	export JHB=gedit

135 136 137 138 139 140
	# We set this because without it, GNU gettext intltool or whatever the cause
	# may be, will set DATADIRNAME=lib during configure because gettext is not part
	# of libc on OS X. The result is that translations would be installed in lib/
	# instead of share/ and would not get picked up.
	export DATADIRNAME=share

141 142 143
	"$INSTALLD/bin/jhbuild" -f "$HOMED/.jhbuildrc" "$@"
}

144 145
function dbg_help_usage() {
	echo "build command ..."
146 147
}

148 149
function dbg_help_short() {
	echo "Run remaining build commands in a debug environment"
150 151
}

152
function cmd_dbg() {
153
	export GEDIT_OSX_DEBUG=1
154
	_process "$@"
155 156
}

157 158 159 160
function shell_help_short() {
	echo "Start the jhbuild shell (shorthand for jh shell)"
}

161 162 163 164
function cmd_shell() {
	cmd_jh shell
}

165 166 167 168 169 170 171 172
function make_help_usage() {
	echo "module-source-directory"
}

function make_help_short() {
	echo "Run jhbuild make at the provided source directory"
}

173 174 175 176
function cmd_make() {
	cd "$BASED/$GEDIT_SDK/source/$1" && cmd_jh make
}

177 178 179 180 181 182 183 184
function run_help_usage() {
	echo "program ..."
}

function run_help_short() {
	echo "Run the provided program in the jhbuild environment (shorthand for jh run)"
}

185 186 187 188
function cmd_run() {
	cmd_jh run "$@"
}

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
function env_help_short() {
	echo "Obtain certain environment paths"
}

function cmd_env() {
	vars=(source inst bin home local-bin)

	case "$1" in
	source)
		echo "$BASED/$GEDIT_SDK/source"
		;;
	inst)
		echo "$BASED/$GEDIT_SDK/inst"
		;;
	bin)
		echo "$BASED/$GEDIT_SDK/inst/bin"
		;;
	home)
		echo "$HOMED"
		;;
	local-bin)
		echo "$HOMED/.local/bin"
		;;
	"")
		for v in "${vars[@]}"; do
			u=$(echo -n "$v" | tr '-' '_')
			echo -n "$u="
			cmd_env "$v"
		done
		;;
	*)
		varnames=$(printf ", \033[1m%s\033[0m" "${vars[@]}")
		varnames=${varnames:2}

		do_exit "Unknown environment variable \033[1m$1\033[0m, available variables are: $varnames"
		;;
	esac
}

228 229 230 231
function all_help_short() {
	echo "Runs the init, bootstrap and build commands"
}

232 233 234 235 236 237 238 239 240 241 242 243
function cmd_all() {
	if [ ! -d "$JHBUILD_SOURCED" ]; then
		cmd_init || exit 1
	fi

	if [ ! -f "$BASED/$GEDIT_SDK/inst/bin/python" ]; then
		cmd_bootstrap || exit 1
	fi

	cmd_jh build -q
}

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
function help_help_short() {
	echo "Shows this help message"
}

function cmd_help() {
	if [ -z "$1" ]; then
		echo "Usage: $ME [command]"
		echo ""
		echo "Available commands:"
		echo ""

		for cmd in "${commands[@]}"; do
			printf "  \033[1m$cmd\x1B[0m "

			l=${#cmd}
			let d="$commandsmaxlen - $l + 1"

			printf "%${d}s" ""
			echo -n "- "

			if [[ $(type -t "${cmd}_help_short") = "function" ]]; then
				"${cmd}_help_short"
			else
				echo ""
			fi
		done

		echo ""
	else
		cmd="cmd_$1"

		if [[ $(type -t "$cmd") != "function" ]]; then
276
			do_exit "Invalid command \033[1m$1\033[0m, available commands are: $cmds"
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
		fi

		printf "Usage: $ME \033[1m$1\033[0m "

		if [[ $(type -t "$1_help_usage") = "function" ]]; then
			"$1_help_usage"
		else
			echo ""
		fi

		echo ""

		if [[ $(type -t "$1_help_long") != "function" ]]; then
			"$1_help_short"
		else
			"$1_help_long"
		fi
	fi
}

297
commands=()
298
commandsmaxlen=0
299 300 301 302 303 304

while read line
do
	cmd=${line#declare -f }

	if [[ "$cmd" = cmd_* ]]; then
305 306 307 308 309 310 311 312 313
		cname=${cmd#cmd_}

		commands+=($cname)

		l=${#cname}

		if [[ $l > $commandsmaxlen ]]; then
			commandsmaxlen=$l
		fi
314 315 316
	fi
done < <(declare -F)

317
cmds=$(printf ", \033[1m%s\033[0m" "${commands[@]}")
318 319
cmds=${cmds:2}

320 321 322 323 324
function _process() {
	if [ -z "$1" ]; then
		cmd_help
		exit 0
	fi
325

326
	cmd="cmd_$1"
327

328 329 330 331 332 333 334
	if [[ $(type -t "$cmd") != "function" ]]; then
		do_exit "Invalid command $1, available commands are: $cmds"
	fi

	shift 1
	"$cmd" "$@"
}
335

336
_process "$@"