first check-in of feisty meow codebase. many things broken still due to recent
[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 # switches from a /X/path form to an X:/ form.
57 function msys_to_dos_path() {
58   # we always remove dos slashes in favor of forward slashes.
59   echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/'
60 }
61
62 # switches from an X:/ form to an /X/path form.
63 function dos_to_msys_path() {
64   # we always remove dos slashes in favor of forward slashes.
65   echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/'
66 }
67
68 ###hmmm: move test suite out to the functions file in yeti, where the definitive versions
69 #        of dos to msys etc live.
70 # test suite for above functions.
71 #echo this should go from msys to dos:
72 #  prior='/c/bogart\dingle'
73 #  latter=$(msys_to_dos_path "$prior")
74 #  echo went from $prior to $latter
75 #echo this should go from dos to msys:
76 #  prior='D:\bogart\dingle'
77 #  latter=$(dos_to_msys_path "$prior")
78 #  echo went from $prior to $latter
79
80 ##############
81
82 # perform some calculations to get the right paths from our parameters.
83 if [ ! -z "$PARM_1" ]; then
84   # use the first real parameter since this is probably the 'source' version.
85   export BUILD_SCRIPTS_DIR="$(dirname "$PARM_1")"
86   THIS_TOOL_NAME="$(basename "$PARM_1")"
87 else
88   # use the zeroth parameter, since we know nothing more about our name.
89   export BUILD_SCRIPTS_DIR="$(dirname "$PARM_0")"
90   THIS_TOOL_NAME="$(basename "$PARM_0")"
91 fi
92 BUILD_SCRIPTS_DIR="$(echo $BUILD_SCRIPTS_DIR | tr '\\\\' '/' )"
93 echo buildsc is $BUILD_SCRIPTS_DIR
94 # figure out the other paths based on where we found this script.
95 export BUILDING_HIERARCHY="$(echo "$BUILD_SCRIPTS_DIR" | sed -e 's/\(.*\)\/[^\/]*/\1/')"
96 export CLAM_DIR="$BUILD_SCRIPTS_DIR/../clam"
97 # synonym to make other builds happy.
98 export BUILDER_DIR="$BUILDING_HIERARCHY"
99
100 # guess the current platform.
101 IS_UNIX=$(uname | grep -i linux)
102 if [ -z "$IS_UNIX" ]; then IS_UNIX=$(uname | grep -i unix); fi
103 if [ -z "$IS_UNIX" ]; then IS_UNIX=$(uname | grep -i darwin); fi
104 IS_DOS=$(uname | grep -i ming)
105 if [ -z "$IS_DOS" ]; then IS_DOS=$(uname | grep -i cygwin); fi
106
107 # set some clam parameters for compilation.  if the script can't guess the
108 # right configuration, then you will need to set them in the last 'else'
109 # below.
110 if [ ! -z "$IS_UNIX" ]; then export OPERATING_SYSTEM=UNIX;
111 elif [ ! -z "$IS_DOS" ]; then export OPERATING_SYSTEM=WIN32;
112 else
113   # the system is unknown, so we give up on guessing.
114   export OPERATING_SYSTEM=unknown
115 fi
116 if [ ! -z "$SHELL_DEBUG" ]; then
117   echo "[OS is \"$OPERATING_SYSTEM\"]"
118 fi
119
120 # the impact of always redoing the repository directory below means that we
121 # will always expect the build scripts to be located within the build they're
122 # building.  that's fundamentally broken for some usages.
123 # but just using the existing repository directory is fundamentally broken
124 # too, since then the build variables will never re-adapt to the repository
125 # you want.
126 # maybe just documentation; if you are changing your repository and the build
127 # scripts are in the right place, then you should unset REPOSITORY_DIR.
128 # we at least check that the folder still exists now too, but that's not exact
129 # knowledge that it's still the right directory.
130
131 if [ -z "$REPOSITORY_DIR" -o ! -d "$REPOSITORY_DIR" ]; then
132   # we create the variable repository dir, but we keep the idiotic dos form of
133   # the path, because otherwise lots of bad things happens when passing the
134   # folders around to visual studio commands that don't allow a space after them.
135   if [ -d "$BUILDING_HIERARCHY/source" ]; then
136     # old style repository is same height as building hierarchy.
137     export REPOSITORY_DIR="$BUILDING_HIERARCHY"
138   else
139     # new style repository is a level above the build hierarchy.
140     export REPOSITORY_DIR="$(echo "$BUILDING_HIERARCHY" | sed -e 's/\(.*\)\/[^\/]*/\1/')"
141   fi
142 fi
143
144 if [ "$OPERATING_SYSTEM" = "WIN32" ]; then
145   # make sure repository dir looks right on windoze.
146   export REPOSITORY_DIR="$(msys_to_dos_path "$REPOSITORY_DIR")"
147 fi
148
149 if [ ! -z "$SHELL_DEBUG" ]; then
150   echo "[REPOSITORY_DIR is $REPOSITORY_DIR]"
151 fi
152
153 # new BUILD_TOP variable points at the utter top-most level of any files
154 # in the building hierarchy.
155 if [ -d "$REPOSITORY_DIR/../../build" ]; then
156   export BUILD_TOP="$REPOSITORY_DIR/../.."
157 else
158   # this is actually just a guess.
159   export BUILD_TOP="$REPOSITORY_DIR"
160 fi
161
162 # this variable points at a folder where we store most of the generated products
163 # of the build.  these tend to be the things that will be used for packaging into
164 # different types of products.
165 export PRODUCTION_DIR="$BUILD_TOP/production"
166
167 # we define a log file storage area that can be relied on by the build.
168 export LOGS_DIR="$PRODUCTION_DIR/logs"
169 if [ ! -d "$LOGS_DIR" ]; then
170   mkdir -p "$LOGS_DIR"
171 fi
172
173 # hook clam into the compilation system.
174 function make()
175 {
176   /usr/bin/make -I "$CLAM_DIR" $*
177 }
178
179 ##############
180
181 # debugging area where we say what we think we know.
182
183 #echo scripts: $BUILD_SCRIPTS_DIR
184 #echo build tools hier: $BUILDING_HIERARCHY
185 #echo this tool: $THIS_TOOL_NAME
186 #echo repository: $REPOSITORY_DIR
187 #echo clam: $CLAM_DIR
188 #echo makeflags: $MAKEFLAGS
189
190 ##############
191
192 # test out our computed variables to make sure they look right.
193 pushd / &>/dev/null # jump to the root so relative paths are caught.
194
195 # first the scripts directory; do we find this script there?
196 if [ ! -f "$BUILD_SCRIPTS_DIR/$THIS_TOOL_NAME" ]; then
197   echo "This script must be run using its full pathname.  This enables the script to"
198   echo "locate the proper build folders.  Please try again with the full path, e.g.:"
199   echo "    bash /home/fred/codeplex/scripts/generator/$THIS_TOOL_NAME"
200 #  exit 1
201 fi
202
203 # next the clam directory; is the main variables file present there?
204 if [ ! -f "$CLAM_DIR/variables.def" ]; then
205   echo "The clam directory could not be located under our build tools hierarchy."
206   echo "Please examine the configuration and make sure that this script is in a"
207   echo "directory that resides at the same height as the 'clam' directory."
208 #  exit 1
209 fi
210
211 # now compute some more paths with a bit of "heuristics" for where we can
212 # find the source code.
213 export TOOL_SOURCES="$REPOSITORY_DIR/core/tools"
214 if [ ! -d "$TOOL_SOURCES/dependency_tool" -o ! -d "$TOOL_SOURCES/clam_tools" ]; then
215   if [ ! -d "$TOOL_SOURCES/dependency_tool" -o ! -d "$TOOL_SOURCES/clam_tools" ]; then
216     echo "This script cannot locate the tool source code folder.  This is where the"
217     echo "dependency_tool and clam_tools folders are expected to be."
218 #    exit 1
219   fi
220 fi
221
222 # where we store the binaries used for building the rest of the code base.
223 export BINARY_DIR="$PRODUCTION_DIR/clam_bin"
224   # the final destination for the new binaries which provide the hoople
225   # build with all the apps it needs to get going.
226 export TARGETS_DIR="$PRODUCTION_DIR/binaries"
227   # targets directory is meaningful to clam, which will use it for output.
228 export INTERMEDIATE_EXE_DIR="$TARGETS_DIR"
229   # where we are building the apps before they get promoted.
230
231 export WASTE_DIR="$PRODUCTION_DIR/waste"
232 if [ ! -d "$WASTE_DIR" ]; then
233   mkdir -p "$WASTE_DIR"
234 fi
235 export TEMPORARIES_DIR="$WASTE_DIR/temporaries"
236 if [ ! -d "$TEMPORARIES_DIR" ]; then
237   mkdir -p "$TEMPORARIES_DIR"
238 fi
239
240 # calculate which build ini file to use.
241 export BUILD_PARAMETER_FILE="$PRODUCTION_DIR/feisty_meow_config.ini"
242 if [ ! -f "$BUILD_PARAMETER_FILE" ]; then
243   echo "Cannot find a useful build configuration file."
244 #  exit 1
245 fi
246
247 # pick the executable's file ending based on the platform.
248 if [ "$OPERATING_SYSTEM" == "UNIX" ]; then export EXE_ENDING=;
249 elif [ "$OPERATING_SYSTEM" == "WIN32" ]; then export EXE_ENDING=.exe;
250 else
251   echo "The OPERATING_SYSTEM variable is unset or unknown.  Bailing out."
252 #  exit 1
253 fi
254
255 # load up the helper variables for visual studio on winders.
256 if [ "$OPERATING_SYSTEM" == "WIN32" ]; then
257   source "$BUILD_SCRIPTS_DIR/vis_stu_vars.sh"
258 else
259   export LD_LIBRARY_PATH="$TARGETS_DIR"
260 fi
261
262 popd &>/dev/null # checking is over, jump back to the starting point.
263
264 ############################################################################
265
266 # at this point, all the build related variables should be valid.
267
268 if [ -z "$INCLUDED_FROM_BOOTSTRAP" \
269     -a -z "$PARM_1" ]; then
270   # we are running as a stand-alone script, so we stay resident with our
271   # current set of variables.
272   bash
273 fi
274
275 ############################################################################
276