trying to script around the nuisance called cygwin.
[feisty_meow.git] / scripts / generator / build_variables.sh
1 ##############
2 #
3 #  Name   : build variable calculator
4 #  Author : Chris Koeritz
5 #
6 #  Purpose:
7 #
8 #    This script sets up all the variables needed by the HOOPLE system for
9 #  building the source code.  It can either be run as a bash script directly
10 #  like so:
11 #
12 #      bash ~/feisty_meow/scripts/generator/build_variables.sh
13 #
14 #  which will establish a new shell containing all the variables, or you can
15 #  'source' the script like so:
16 #
17 #      build_vars=~/feisty_meow/scripts/generator/build_variables.sh
18 #      source $build_vars $build_vars
19 #
20 #  to set all of the variables in your current shell.  The full path is
21 #  necessary in these commands to allow the script to easily find itself.
22 #  The 'source' version needs to be fed the actual path to the script
23 #  because bash 'source' commands cause the first parameter (called $0) to
24 #  be set to just the path to bash itself.
25 #
26 ##############
27 # Copyright (c) 2004-$now By Author.  This program is free software; you can
28 # redistribute it and/or modify it under the terms of the GNU General Public
29 # License as published by the Free Software Foundation; either version 2 of
30 # the License or (at your option) any later version.  This is online at:
31 #     http://www.fsf.org/copyleft/gpl.html
32 # Please send any updates to: fred@gruntose.com
33 ##############
34
35 # prerequisites for this script:
36 #
37 # (1) the script should be run with a full path, so that it can decide where
38 #     it lives with minimal fuss.
39 # (2) on windows, the msys bin directory should already be in the path so that
40 #     tools like dirname are already available.
41
42 # here is where we compute the locations for the build's pieces, based on
43 # where this script is located.  we currently assume that the build scripts
44 # like this one are at the same height in the hierarchy as the clam scripts
45 # that are used in the bootstrapping process.
46
47 # get the most important bits first; the directory this script lives in and
48 # the script's name.
49 PARM_0="$0"
50 PARM_1="$1"
51
52 ##############
53
54 # helpful build function zone.
55
56 source $FEISTY_MEOW_SCRIPTS/core/functions.sh
57   # load msys_to_dos_path and dos_to_msys_path.
58
59 #### switches from a /X/path form to an X:/ form.
60 ###function msys_to_dos_path() {
61 ###  # we always remove dos slashes in favor of forward slashes.
62 ###  echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/'
63 ###}
64
65 #### switches from an X:/ form to an /X/path form.
66 ###function dos_to_msys_path() {
67 ###  # we always remove dos slashes in favor of forward slashes.
68 ###  echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/'
69 ###}
70
71 ##############
72
73 # perform some calculations to get the right paths from our parameters.
74 if [ ! -z "$PARM_1" ]; then
75   # use the first real parameter since this is probably the 'source' version.
76   export BUILD_SCRIPTS_DIR="$(dirname "$PARM_1")"
77   THIS_TOOL_NAME="$(basename "$PARM_1")"
78 #echo sourced version buildscriptsdir is $BUILD_SCRIPTS_DIR
79 else
80   # use the zeroth parameter, since we know nothing more about our name.
81   export BUILD_SCRIPTS_DIR="$(dirname "$PARM_0")"
82   THIS_TOOL_NAME="$(basename "$PARM_0")"
83 #echo bashed version buildscriptsdir is $BUILD_SCRIPTS_DIR
84 fi
85 BUILD_SCRIPTS_DIR="$(cd $(echo $BUILD_SCRIPTS_DIR | tr '\\\\' '/' ); \pwd)"
86 echo ">> buildvars buildscriptsdir is $BUILD_SCRIPTS_DIR"
87
88 # figure out the other paths based on where we found this script.
89 export BUILDING_HIERARCHY="$(echo "$BUILD_SCRIPTS_DIR" | sed -e 's/\(.*\)\/[^\/]*/\1/')"
90 export CLAM_DIR="$(cd $BUILD_SCRIPTS_DIR/../clam ; \pwd)"
91 echo ">> buildvars clamdir is $CLAM_DIR"
92 # synonym to make other builds happy.
93 export BUILDER_DIR="$BUILDING_HIERARCHY"
94
95 # guess the current platform.
96 IS_UNIX=$(uname | grep -i linux)
97 if [ -z "$IS_UNIX" ]; then IS_UNIX=$(uname | grep -i unix); fi
98 if [ -z "$IS_UNIX" ]; then IS_UNIX=$(uname | grep -i darwin); fi
99 IS_DOS=$(uname | grep -i ming)
100 if [ -z "$IS_DOS" ]; then IS_DOS=$(uname | grep -i cygwin); fi
101
102 # set some clam parameters for compilation.  if the script can't guess the
103 # right configuration, then you will need to set them in the last 'else'
104 # below.
105 if [ ! -z "$IS_UNIX" ]; then export OPERATING_SYSTEM=UNIX;
106 elif [ ! -z "$IS_DOS" ]; then export OPERATING_SYSTEM=WIN32;
107 else
108   # the system is unknown, so we give up on guessing.
109   export OPERATING_SYSTEM=unknown
110 fi
111 if [ ! -z "$SHELL_DEBUG" ]; then
112   echo "[OS is \"$OPERATING_SYSTEM\"]"
113 fi
114
115 #hmmm: all this stuff is highly questionable value now.
116 # we create the variable FEISTY_MEOW_DIR, but we keep the dos form of
117 # the path, because otherwise lots of bad things happens when passing the
118 # folders around to visual studio commands that don't allow a space after them.
119 if [ -d "$BUILDING_HIERARCHY/source" ]; then
120   # old style repository is same height as building hierarchy.
121   export FEISTY_MEOW_DIR="$BUILDING_HIERARCHY"
122 else
123   # new style repository is a level above the build hierarchy.
124   export FEISTY_MEOW_DIR="$(echo "$BUILDING_HIERARCHY" | sed -e 's/\(.*\)\/[^\/]*/\1/')"
125 fi
126
127 if [ "$OPERATING_SYSTEM" = "WIN32" ]; then
128   # make sure repository dir looks right on windoze.
129   export FEISTY_MEOW_DIR="$(msys_to_dos_path "$FEISTY_MEOW_DIR")"
130 fi
131
132 if [ ! -z "$SHELL_DEBUG" ]; then
133   echo "[FEISTY_MEOW_DIR is $FEISTY_MEOW_DIR]"
134 fi
135
136 # new BUILD_TOP variable points at the utter top-most level of any files
137 # in the building hierarchy.
138 export BUILD_TOP="$FEISTY_MEOW_DIR"
139
140 # this variable points at a folder where we store most of the generated products
141 # of the build.  these tend to be the things that will be used for packaging into
142 # different types of products.
143 export PRODUCTION_DIR="$BUILD_TOP/production"
144
145 # we define a log file storage area that can be relied on by the build.
146 export LOGS_DIR="$PRODUCTION_DIR/logs"
147 if [ ! -d "$LOGS_DIR" ]; then
148   mkdir -p "$LOGS_DIR"
149 fi
150
151 # hook clam into the compilation system.
152 function make()
153 {
154   /usr/bin/make -I "$CLAM_DIR" $*
155 }
156
157 ##############
158
159 # debugging area where we say what we think we know.
160
161 #echo scripts: $BUILD_SCRIPTS_DIR
162 #echo build tools hier: $BUILDING_HIERARCHY
163 #echo this tool: $THIS_TOOL_NAME
164 #echo repository: $FEISTY_MEOW_DIR
165 #echo clam: $CLAM_DIR
166 #echo makeflags: $MAKEFLAGS
167
168 ##############
169
170 # test out our computed variables to make sure they look right.
171 pushd / &>/dev/null # jump to the root so relative paths are caught.
172
173 # flag for whether any checks have failed.
174 got_bad=
175
176 # first the scripts directory; do we find this script there?
177 if [ ! -f "$BUILD_SCRIPTS_DIR/$THIS_TOOL_NAME" ]; then
178   echo "This script cannot locate the proper build folders.  The crucial path"
179   echo "variable seems to be '$BUILD_SCRIPTS_DIR', which"
180   echo "does not seem to contain '$THIS_TOOL_NAME' (this"
181   echo "script's apparent name)."
182   got_bad=1
183 fi
184
185 # next the clam directory; is the main variables file present there?
186 if [ -z "$got_bad" -a ! -f "$CLAM_DIR/variables.def" ]; then
187   echo "The clam directory could not be located under our build tools hierarchy."
188   echo "Please examine the configuration and make sure that this script is in a"
189   echo "directory that resides at the same height as the 'clam' directory."
190   got_bad=1
191 fi
192
193 # now compute some more paths with a bit of "heuristics" for where we can
194 # find the source code.
195 export TOOL_SOURCES="$FEISTY_MEOW_DIR/nucleus/tools"
196 if [ -z "$got_bad" -a ! -d "$TOOL_SOURCES/dependency_tool" -o ! -d "$TOOL_SOURCES/clam_tools" ]; then
197   echo "This script cannot locate the tool source code folder.  This is where the"
198   echo "dependency_tool and clam_tools folders are expected to be."
199   got_bad=1
200 fi
201
202 ############################
203   
204 # we only run the rest of the script if we know we didn't have some kind of
205 # bad thing happen earlier.
206 if [ -z "$got_bad" ]; then
207
208   # where we store the binaries used for building the rest of the code base.
209   export BINARY_DIR="$PRODUCTION_DIR/clam_bin"
210     # the final destination for the new binaries which provide the hoople
211     # build with all the apps it needs to get going.
212   export TARGETS_DIR="$PRODUCTION_DIR/binaries"
213     # targets directory is meaningful to clam, which will use it for output.
214   export INTERMEDIATE_EXE_DIR="$TARGETS_DIR"
215     # where we are building the apps before they get promoted.
216
217   export WASTE_DIR="$PRODUCTION_DIR/waste"
218   if [ ! -d "$WASTE_DIR" ]; then
219     mkdir -p "$WASTE_DIR"
220   fi
221   export TEMPORARIES_DIR="$WASTE_DIR/temporaries"
222   if [ ! -d "$TEMPORARIES_DIR" ]; then
223     mkdir -p "$TEMPORARIES_DIR"
224   fi
225   
226   # calculate which build ini file to use.
227   export BUILD_PARAMETER_FILE="$PRODUCTION_DIR/feisty_meow_config.ini"
228   if [ ! -f "$BUILD_PARAMETER_FILE" ]; then
229     echo "Cannot find a useful build configuration file."
230   fi
231   
232   # pick the executable's file ending based on the platform.
233   if [ "$OPERATING_SYSTEM" == "UNIX" ]; then export EXE_ENDING=;
234   elif [ "$OPERATING_SYSTEM" == "WIN32" ]; then export EXE_ENDING=.exe;
235   else
236     echo "The OPERATING_SYSTEM variable is unset or unknown.  Bailing out."
237   fi
238   
239   # we should have established our internal variables now, so let's try
240   # using them.
241   export PATH=$BINARY_DIR:$PATH
242   
243   # load up the helper variables for visual studio on winders.
244   if [ "$OPERATING_SYSTEM" == "WIN32" ]; then
245     source "$BUILD_SCRIPTS_DIR/vis_stu_vars.sh"
246   else
247     export LD_LIBRARY_PATH="$TARGETS_DIR"
248   fi
249   
250   popd &>/dev/null # checking is over, jump back to the starting point.
251   
252   ############################
253   
254   # at this point, all the build related variables should be valid.
255   
256   if [ -z "$INCLUDED_FROM_BOOTSTRAP" \
257       -a -z "$PARM_1" ]; then
258     # we are running as a stand-alone script, so we stay resident with our
259     # current set of variables.
260     bash
261   fi
262
263 fi
264