OSDN Git Service

GAST - Gcc Automatically Save Temps
[gast/master.git] / gast_rpmbuild
1 #!/bin/bash
2 #   gast_rpmbuild : wrapper script for rpmbuild command to set environment using gast
3 #
4 #   Copyright (C) 2009 Tadashi Koike
5 #
6 #   This program is free software; you can redistribute it and/or modify
7 #   it under the terms of the GNU General Public License as published by
8 #   the Free Software Foundation; either version 2 of the License, or
9 #   (at your option) any later version.
10 #
11 #   This program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU General Public License for more details.
15 #
16 #   You should have received a copy of the GNU General Public License
17 #   along with this program; if not, write to the Free Software
18 #   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
19 #   02110-1301, USA.
20 #
21 #----------------------------------------------------------------------
22 # (I am weak in English. Please modify English comment more suitable. :T.K)
23
24 VERSION=0.1.0
25 ORIGINAL_NAME="gast_rpmbuild"
26 SCRIPT_NAME=${0##*/}
27 SCRIPT_PATH=${0%/*}
28 CWD=`pwd`
29
30 # When this script is executed as 'gast_rpmbuild', this script creates symbolic link
31 # file name as 'rpmbuild' and exit.
32 if [ "${SCRIPT_NAME}" = "${ORIGINAL_NAME}" ]; then
33     for arg in "$@"
34     do
35         case "${arg}" in
36         '--version'|'-v')
37             echo "${ORIGINAL_NAME} ${VERSION}"
38             exit 0
39             ;;
40         '--help'|'-h')
41             cat <<EOL
42 usage : ${ORIGINAL_NAME} [-v|--version] [-h|--help]
43
44   -h, --help              display this help and exit
45   -v, --version           display version information and exit
46
47   When this script is executed as '${ORIGINAL_NAME}' with no option, this script 
48   creates symbolic link toward itself which name is 'rpmbuild'.
49
50   When this script is executed as 'rpmbuild', this script behaves just as
51   rpmbuild command with accepting additional '--save-temps-dir' option and
52   with setting GAST_* environment variables.
53 EOL
54             exit 0
55             ;;
56         *)
57             echo "usage : ${ORIGINAL_NAME} [--version|-v] [--help|-h]"
58             exit 1
59             ;;
60         esac
61     done
62
63     cd "${SCRIPT_PATH}"
64     for cmd in rpmbuild
65     do
66         if [ -e "${cmd}" ]; then
67             file "${cmd}" 2>/dev/null | egrep 'symbolic link' >/dev/null 2>&1
68             if [ "$?" -ne 0 ]; then
69                 echo "${ORIGINAL_NAME} : '${cmd}' exists as normal file or directory. Could not create symbolic link." >&2
70                 continue
71             fi
72         fi
73         ln -sf "${ORIGINAL_NAME}" "${cmd}"
74         if [ "$?" -eq 0 ]; then
75             echo "${ORIGINAL_NAME} : Create symbolic link ${cmd}" >&2
76         else
77             echo "${ORIGINAL_NAME} : Couldn't create symbolic link ${cmd}" >&2
78         fi
79     done
80     exit 0
81 fi
82
83 # get a full-path name of original rpmbuild/rpm2cpio command.
84 RPMBUILD_BIN=`which rpmbuild 2>/dev/null`
85 if [ -z "${RPMBUILD_BIN}" ]; then
86     echo "${ORIGINAL_NAME} : Couldn't find a path to rpmbuild command. exit" >&2
87     exit 1
88 fi
89
90 # get a directory path (full-path) of directory this script
91 case "${SCRIPT_PATH}" in
92     '.')        SCRIPT_PATH="${CWD}" ;;
93     \./*)       SCRIPT_PATH="${CWD}/${SCRIPT_PATH#*/}" ;;
94     '~')        SCRIPT_PATH="${HOME}" ;;
95     \~/*)       SCRIPT_PATH="${HOME}/${SCRIPT_PATH#*/}" ;;
96     \/*)                :       ;;
97     *)          SCRIPT_PATH="${CWD}/${SCRIPT_PATH}" ;;
98 esac
99
100 # Set this script's directory to PATH environment variable.
101 # (to be found a gcc wrapper script (=gast) in advance of original compiler binary).
102 PATH="${SCRIPT_PATH}:${PATH}"
103 export PATH
104
105 # Set environment variable to get compiler's temporalry file
106 export GAST_ENABLE=
107
108 # Analyse command options. Get directory name to move temporary file.
109 next_is_savedir=
110 idx=0
111 # new_args_a : argument list for rpmbuild {-bb|-bc}
112 # new_args_b : argument list for rpmbuild {--recompile|--rebuild}
113 # srpm_list  : list of srpm file(s)
114 for arg in "$@"
115 do
116     if [ "${arg}" = "--save-temps-dir" ]; then
117         next_is_savedir="yes"
118     elif [ -n "${next_is_savedir}" ]; then
119         next_is_savedir=
120         case "${arg}" in
121             \-*)        GAST_SAVE_DIR=""
122                         echo "${ORIGINAL_NAME} : Error in --save-temps-dir option. ignore this option." >&2
123                         new_args[$idx]="${arg}"
124                         let idx+=1
125                         ;;
126             '')         GAST_SAVE_DIR="" ;;
127             '.')        GAST_SAVE_DIR="${CWD}" ;;
128             \./*)       GAST_SAVE_DIR="${CWD}/${arg#*/}" ;;
129             '~')        GAST_SAVE_DIR="${HOME}" ;;
130             \~/*)       GAST_SAVE_DIR="${HOME}/${arg#*/}" ;;
131             \/*)        GAST_SAVE_DIR="${arg}" ;;
132             *)          GAST_SAVE_DIR="${CWD}/${arg}" ;;
133         esac
134
135         if [ -n "${GAST_SAVE_DIR}" ]; then
136             GAST_SAVE_DIR="${GAST_SAVE_DIR%/}"
137             export GAST_SAVE_DIR
138         else
139             unset GAST_SAVE_DIR
140         fi
141     else
142         new_args[$idx]="${arg}"
143         let idx+=1
144     fi
145 done
146 exec "${RPMBUILD_BIN}" "${new_args[@]}"