Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
895 lines (757 sloc) 21.6 KB
#!/bin/bash
#
# The BSD 3-Clause License. http://www.opensource.org/licenses/BSD-3-Clause
#
# This file is part of mingw-env project.
# Copyright (c) 2011,2012, by niXman (i dotty nixman doggy gmail dotty com)
# All rights reserved.
# Project: mingw-env ( http://sourceforge.net/projects/mingw-env/ )
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the distribution.
# - Neither the name of the 'mingw-env' nor the names of its contributors may
# be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# **************************************************************************
# **************************************************************************
# **************************************************************************
source common/utils.mfun
source common/version.mfun
source common/packaging.mfun
# constants
MINGW_ENV_VERSION="0.1.0"
BUG_URL='http://sourceforge.net/projects/mingw-env/'
PKGVERSION='Built by mingw-env project'
TAB_STOP=3
IS_LINUX_HOST=no
IS_WINDOWS_HOST=no
IS_MACOSX_HOST=no
case $OSTYPE in
linux-gnu) IS_LINUX_HOST=yes ;;
msys) IS_WINDOWS_HOST=yes ;;
darwin*) IS_MACOSX_HOST=yes ;;
*) echo "bad host. terminate."; exit 1 ;;
esac
VIEWVER_NAME=""
[[ $# > 0 && ${@:1} != --install ]] && {
[[ -n $MINGW_ENV ]] && {
. $MINGW_ENV/common/config.sh
func_find_logviewer \
LOGVIEWERS[@] \
VIEWVER_NAME
[[ $? != 0 ]] && {
echo "can not find default logviewer."
}
}
}
# **************************************************************************
# check if mingw-env already installed
function func_mingw_env_installed {
[[ ! -f $HOME/.profile ]] && return 1 # not installed
[[ -n $(cat $HOME/.profile | grep 'MINGW_ENV') ]] && {
[[ -z $MINGW_ENV ]] && {
return 2 # installed. need to restart the shell.
} || {
return 0 # installed and shell restarted.
}
}
return 1 # not installed
}
# **************************************************************************
# prepare mingw-env environment
function func_make_environment {
# $1 - $MINGW_ENV
# $2 - $WORK_DIR
# common variable
ROOT_DIR=$1
COMMON_DIR=$ROOT_DIR/common
METADATA_DIR=$ROOT_DIR/metadata
RULES_DIR=$ROOT_DIR/rules
PATCHES_DIR=$ROOT_DIR/patches
TESTS_DIR=$ROOT_DIR/tests
DATA_DIR=$ROOT_DIR/data
DATA_TOOLS_DIR=$DATA_DIR/tools
DATA_MARKERS_DIR=$DATA_DIR/markers
DATA_STATE_DIR=$DATA_DIR/states
# work dir variable
BUILDS_DIR=$2/builds
LOGS_DIR=$2/logs
SRCS_DIR=$2/srcs
PACKAGES_DIR=$2/packages
# installed packages list
INSTALLED_PACKAGES_FILENAME=$DATA_MARKERS_DIR/installed_packages.list
# tab marker
TAB_LEVEL=0
}
# **************************************************************************
# install mingw-env
function func_mingw_env_install {
# $1 - path for install too
[[ ! -f ./mingw-env ]] && {
echo "run install from the mingw-env root directory."
return 1
}
mkdir -p $1/data || return 1
cp ./mingw-env $1 || return 1
cp -r $COMMON_DIR $1 || return 1
cp -r $METADATA_DIR $1 || return 1
cp -r $RULES_DIR $1 || return 1
cp -r $PATCHES_DIR $1 || return 1
cp -r $TESTS_DIR $1 || return 1
cp -r $DATA_TOOLS_DIR $1/data || return 1
[[ ! -f $HOME/.profile ]] && {
touch $HOME/.profile
}
mv $HOME/.profile $HOME/.profile.back && \
echo "export MINGW_ENV=$1" > $HOME/.profile && \
echo "export PATH=\$MINGW_ENV:\$PATH" >> $HOME/.profile && \
cat $HOME/.profile.back >> $HOME/.profile && \
rm $HOME/.profile.back
[[ $? != 0 ]] && {
rm -fR $1
echo "can't setup MINGW_ENV environment variable."
return 1
}
return 0
}
# uninstall mingw-env
function func_mingw_env_uninstall {
sed /MINGW_ENV/d -i $HOME/.profile
rm -fR $MINGW_ENV
}
# **************************************************************************
# create state of the prefix dir
function func_create_state {
# $1 - state name
[[ -f $DATA_STATE_DIR/$1.marker ]] && {
echo ""
echo "state \"$1\" already exists. terminate."
exit 1
}
touch $DATA_STATE_DIR/$1.marker
}
# restore state of the prefix dir
function func_restore_state {
# $1 - state name
[[ ! -f $DATA_STATE_DIR/$1.marker ]] && {
echo "state \"$1\" does not exist. terminate."
exit 1
}
local _prev=$PWD
cd $PREFIX_DIR
find . -type f -newer $DATA_STATE_DIR/$1.marker | xargs rm -fR > /dev/null 2>&1
find . -type d -newer $DATA_STATE_DIR/$1.marker | xargs rmdir -p > /dev/null 2>&1
cd $_prev
[[ $1 != initial ]] && {
rm -f $DATA_STATE_DIR/$1.marker
}
}
# **************************************************************************
function func_is_valid_package {
# $1 - (meta)package name
RULE_TYPE=""
RULE_NAME=""
. $1
[[ -z $RULE_TYPE || -z $RULE_NAME ]] && {
echo "package \"$1\" is not for using directly. terminate."
exit 1
}
}
# **************************************************************************
# create the error report
function func_create_error_report {
# $1 - (meta)package name
# $2 - report name
echo ""
}
# **************************************************************************
# create the absolute path to relative
function func_absolute_to_relative {
local common_part=$1
local target=$2
local back=""
while [[ "${target#$common_part}" == "${target}" ]]; do
common_part=$(dirname $common_part)
back="../${back}"
done
echo "${back}${target#$common_part/}"
}
# **************************************************************************
# create common dirs
function func_create_common_dirs {
mkdir -p \
$WORK_DIR \
$PREFIX_DIR \
$BUILDS_DIR \
$LOGS_DIR \
$SRCS_DIR \
$PACKAGES_DIR \
$DATA_TOOLS_DIR \
$DATA_MARKERS_DIR \
$DATA_STATE_DIR
}
# **************************************************************************
# **************************************************************************
# **************************************************************************
case $1 in
--install)
[[ $# != 2 ]] && {
echo "usage: mingw-env --install <path>"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 0 ]] && {
# already installed
echo "already installed."; exit 0
} || {
[[ $_function_result == 2 ]] && {
# already installed
echo "already installed. need to restart the shell."
exit 0
}
}
# load config
. common/config.sh
# prepare environment
func_make_environment "$PWD" ""
# installing
func_mingw_env_install $2
[[ $? != 0 ]] && {
echo "error on installig. terminate."
exit 1
}
echo "successfully installed. please restart the shell."
exit 0
;;
--uninstall)
[[ $# != 1 ]] && {
echo "usage: mingw-env --uninstall"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."
exit 1
} || {
[[ $_function_result == 2 ]] && {
# already installed
echo "mingw-env installed. need to restart the shell."
exit 0
}
}
# prepare environment
func_make_environment $MINGW_ENV ""
# uninstall
func_mingw_env_uninstall
[[ $? == 0 ]] && {
echo "successfully uninstalled."
exit 0
} || {
echo "error on uninstallig. terminate."
exit 1
}
;;
--version|-v)
echo "mingw-env-$MINGW_ENV_VERSION"
exit 0
;;
# print list of available packages
--list)
[[ $# > 2 ]] && {
echo "usage: mingw-env --list [substring]"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."; exit 1
} || {
[[ $_function_result == 2 ]] && {
echo "mingw-env installed. need to restart the shell."
exit 0
}
}
# set up environment
func_make_environment $MINGW_ENV $WORK_DIR
# print list of available packages
[[ $# == 1 ]] && {
func_print_packages ""
} || {
func_print_packages ${@:2}
}
exit 0
;;
# print list of installed packages
--installed)
[[ $# != 1 ]] && {
echo "usage: mingw-env --installed"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."
exit 1
} || {
[[ $_function_result == 2 ]] && {
echo "mingw-env installed. need to restart the shell."
exit 0
}
}
# set up environment
func_make_environment $MINGW_ENV $WORK_DIR
# print list of installed packages
func_print_installed_packages
exit 0
;;
# update mingw-env sources from repository
--update)
exit 0
;;
# propose package for include in mingw-env
--propose-package)
exit 0
;;
# create bug-report for specified package
--create-report)
exit 0
;;
# fix current state of the prefix path
--create-state)
[[ $# != 2 ]] && {
echo "usage: mingw-env --create-state <state name>"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."
exit 1
} || {
[[ $_function_result == 2 ]] && {
echo "mingw-env installed. need to restart the shell."
exit 0
}
}
# set up environment
func_make_environment $MINGW_ENV $WORK_DIR
# create
func_create_common_dirs
echo -n "create state \"$2\"... "
func_create_state $2
echo "done."
exit 0
;;
# restore state of the prefix path content.
# if state name is not specified - restore last state.
--restore-state)
[[ $# != 2 ]] && {
echo "usage: mingw-env --restore-state <state name>"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."
exit 1
} || {
[[ $_function_result == 2 ]] && {
echo "mingw-env installed. need to restart the shell."
exit 0
}
}
# set up environment
func_make_environment $MINGW_ENV $WORK_DIR
# restore specified state
echo -n "restore state \"$2\"... "
func_restore_state $2
echo "done."; exit 0
;;
# install
--install)
[[ $# < 2 ]] && {
echo "usage: mingw-env --install <packages list>"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."
exit 1
} || {
[[ $_function_result == 2 ]] && {
echo "mingw-env installed. need to restart the shell."
exit 0
}
}
# check used compiler
[[ $USE_HOST_COMPILER == no && -z $DEFAULT_MINGW_VERSION ]] && {
echo "please set default mingw version in config file (DEFAULT_MINGW_VERSION). terminate."
exit 1
}
[[ $USE_HOST_COMPILER == yes && -n $(echo $@ | grep -oi mingw) ]] && {
echo "you can't build the new host compiler while USE_HOST_COMPILER is set to \"yes\""
exit 1
}
# append the PATH environment
[[ $USE_HOST_COMPILER == no ]] && {
export PATH=$PREFIX_DIR/bin:$PREFIX_DIR/lib:$PATH
}
# set up environment
func_make_environment $MINGW_ENV $WORK_DIR
# create
func_create_common_dirs
# create initial state
[[ ! -f $DATA_STATE_DIR/initial.marker ]] && {
echo -n "first run. create initial state... "
func_create_state initial
echo "done."
}
OIFS=$IFS
IFS=" "
_pkg_list=(${@:2})
IFS=$OIFS;
_inst_list=(`func_set_pkg_list _pkg_list[@]`)
for it in "${_inst_list[@]}"; do
[[ ! -f $it ]] && {
echo "package \"$it\" is not exists. terminate."
exit 1
}
func_processing "$it" "no" "install" "yes" ""
done
;;
# uninstall specified packages
--uninstall)
[[ $# < 2 ]] && {
echo "usage: mingw-env --uninstall <packages list>"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."
exit 1
} || {
[[ $_function_result == 2 ]] && {
echo "mingw-env installed. need to restart the shell."
exit 0
}
}
# check used compiler
[[ $USE_HOST_COMPILER == no && -z $DEFAULT_MINGW_VERSION ]] && {
echo "please set default mingw version in config file (DEFAULT_MINGW_VERSION). terminate."
exit 1
}
[[ $USE_HOST_COMPILER == yes && -n $(echo $@ | grep -oi mingw) ]] && {
echo "you can't build the new host compiler while USE_HOST_COMPILER is set to \"yes\""
exit 1
}
# append the PATH environment
[[ $USE_HOST_COMPILER == no ]] && {
export PATH=$PREFIX_DIR/bin:$PREFIX_DIR/lib:$PATH
}
# set up environment
func_make_environment $MINGW_ENV $WORK_DIR
# create
func_create_common_dirs
# get list of dependent packages
all_dependent=()
args_list=( ${@:2} )
for it in ${args_list[@]}; do
func_get_dependent_for_installed $it args_list[@]
all_dependent=( ${all_dependent[@]} ${deps_list[@]} )
done
all_dependent=( $(printf "%s\n" ${all_dependent[@]} | sort -u) )
((cnt = ${#@}-1))
[[ ${#all_dependent[@]} > $cnt ]] && {
printf "will be uninstalled this packages:\n %s\n[yes/no]: " ${all_dependent[@]}
read ask
[[ $ask != yes && $ask != no ]] && {
echo "bad input. terminate."
exit 1
}
[[ $ask == no ]] && exit 0
}
# process uninstall packages
echo "uninstalling ${#all_dependent[@]} packages... "
for it in ${all_dependent[@]}; do
func_processing "$it" "no" "uninstall" "no" ""
done
exit 0
;;
# reinstall specified packages and it's dependencies
--reinstall)
[[ $# < 2 ]] && {
echo "usage: mingw-env --reinstall <packages list>"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."
exit 1
} || {
[[ $_function_result == 2 ]] && {
echo "mingw-env installed. need to restart the shell."
exit 1
}
}
# set up environment
func_make_environment $MINGW_ENV $WORK_DIR
# create
func_create_common_dirs
# check used compiler
[[ $USE_HOST_COMPILER == no && -z $DEFAULT_MINGW_VERSION ]] && {
echo "please set default mingw version in config file (DEFAULT_MINGW_VERSION)."
exit 1
}
[[ $USE_HOST_COMPILER == yes && -n $(echo $@ | grep -oi mingw) ]] && {
echo "you can't build the new host compiler while USE_HOST_COMPILER is set to \"yes\""
exit 1
}
# process reinstall packages
echo "reinstall specified packages... "
# reinstall
OIFS=$IFS
IFS=" "
_pkg_list=(${@:2})
IFS=$OIFS;
_reinst_list=(`func_set_pkg_list _pkg_list[@]`)
for it in "${_reinst_list[@]}"; do
# check if rule exists
[[ ! -f $it ]] && {
echo "package \"$it\" is not exists. terminate."
exit 1
}
# check if package installed
local _reinst_pkg=`basename ${it} .$RULES_EXT`
[[ -z $(cat $INSTALLED_PACKAGES_FILENAME | grep -o $_reinst_pkg) ]] && {
echo "package \"$_reinst_pkg\" does not installed. terminate."
exit 1
}
func_processing "$it" "no" "uninstall" "yes" ""
func_processing "$it" "no" "install" "yes" ""
done
exit 0
;;
# uninstall all packages
# build directory will not remove
--uninstall-all)
#
;;
# reinstall all packages
--reinstall-all)
#
;;
# clean specified packages
--clean)
[[ $# < 2 ]] && {
echo "usage: mingw-env --clean <packages list>"
exit 1
}
;;
# distclean specified packages
--distclean)
[[ $# < 2 ]] && {
echo "usage: mingw-env --distclean <packages list>"
exit 1
}
exit 0
;;
# download
--download|--download-unpack)
[[ $# < 2 ]] && {
[[ "${@:1:1}" == --download ]] && {
echo "usage: mingw-env --download <packages list>"
exit 1
} || {
echo "usage: mingw-env --download-unpack <packages list>"
exit 1
}
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."
exit 1
} || {
[[ $_function_result == 2 ]] && {
echo "mingw-env installed. need to restart the shell."
exit 0
}
}
# set up environment
func_make_environment $MINGW_ENV $WORK_DIR
# create
func_create_common_dirs
OIFS=$IFS
IFS=" "
_pkg_list=(${@:2})
IFS=$OIFS;
_inst_list=(`func_set_pkg_list _pkg_list[@]`)
for it in "${_inst_list[@]}"; do
[[ ! -f $it ]] && {
echo "package \"$it\" is not exists. terminate."
exit 1
}
func_processing "$it" "no" "install" "yes" "${@:1}"
done
exit 0
;;
# trace
--trace)
[[ $# < 2 ]] && {
echo "usage: mingw-env --trace <packages list>"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."
exit 1
} || {
[[ $_function_result == 2 ]] && {
echo "mingw-env installed. need to restart the shell."
exit 0
}
}
# set up environment
func_make_environment $MINGW_ENV $WORK_DIR
# create
func_create_common_dirs
OIFS=$IFS
IFS=" "
_pkg_list=(${@:2})
IFS=$OIFS;
_inst_list=(`func_set_pkg_list _pkg_list[@]`)
for it in "${_inst_list[@]}"; do
[[ ! -f $RULES_DIR/$it.$RULES_EXT ]] && {
echo "package \"$it\" is not exists. terminate."
exit 1
}
func_processing "$it" "no" "install" "yes" "${@:1}"
done
exit 0
;;
--dependencies)
[[ $# < 2 ]] && {
echo "usage: mingw-env --dependencies <package>"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."
exit 1
} || {
[[ $_function_result == 2 ]] && {
echo "mingw-env installed. need to restart the shell."
exit 0
}
}
# set up environment
func_make_environment $MINGW_ENV $WORK_DIR
# create
func_create_common_dirs
func_get_dependencies "${@:2}"
printf "%s\n" "$( printf "%s\n" ${deps_list[@]} | sort -u )"
exit 0
;;
--dependent)
[[ $# < 2 ]] && {
echo "usage: mingw-env --dependent <package>"
exit 1
}
# check if mingw-env already installed
func_mingw_env_installed
_function_result=$?
[[ $_function_result == 1 ]] && {
echo "mingw-env is not installed. terminate."
exit 1
} || {
[[ $_function_result == 2 ]] && {
echo "mingw-env installed. need to restart the shell."
exit 0
}
}
# set up environment
func_make_environment $MINGW_ENV $WORK_DIR
# create
func_create_common_dirs
func_get_dependent_for_installed "${@:2}"
printf "%s\n" "$( printf "%s\n" ${deps_list[@]} | sort -u )"
exit 0
;;
-h|--help|*)
printf "%s\n" \
"Usage: mingw-env [option]... [packages]" \
" mingw-env --help | display this help and exit" \
"" \
"mingw-env install/uninstall:" \
" --install <path> | install mingw-env in the specified path" \
" --uninstall | uninstall the mingw-env" \
"" \
"mingw-env commands:" \
" --version | show mingw-env version" \
" --list | show list of available packages" \
" --installed | show list of installed packages" \
" --update | update mingw-env from repository" \
" --propose-package <package> | propose a package to mingw-env" \
" --create-report <package> | create bug-report archive for package" \
"" \
"Prefix path controll:" \
" --create-state <state name> | save state of the prefix directory" \
" --restore-state <state name> | restore state of the prefix directory" \
"" \
"Packages controll:" \
" --download <packages> | download packages and it's dependencies" \
" --download-unpack <packages> | download and unpack packages and it's" \
" | dependencies" \
" --trace <package> | trace packages install process" \
" --dependencies <package> | show list of dependencies for package" \
" --dependent <package> | show list of dependent packages" \
" --install <packages> | install specified packages" \
" --uninstall <packages> | uninstall specified packages" \
" --reinstall <packages> | reinstall specified packages" \
" --uninstall-all | uninstall all installed packages" \
" --reinstall-all | reinstall all installed packages" \
" --clean <packages> | clean specified packages" \
" --distclean <packages> | distclean specified packages"
exit 0
;;
esac
# **************************************************************************
# **************************************************************************
# **************************************************************************