c6bb8faaa914e81fdd9130168ac2db32e015674a
[feisty_meow.git] / scripts / generator / bootstrap_build.sh
1 ##############
2 #  Name   : initial setup script for HOOPLE
3 #  Author : Chris Koeritz
4 #  Purpose:
5 #    This script can bootstrap the HOOPLE libraries from a state where none   #
6 #  of the required binaries are built yet.  It will build the tools that the  #
7 #  CLAM system and HOOPLE need to get a build done.  Then the script builds   #
8 #  the whole source tree as a test of the code's overall health.              #
9 ##############
10 # Copyright (c) 2004-$now By Author.  This program is free software; you can  #
11 # redistribute it and/or modify it under the terms of the GNU General Public  #
12 # License as published by the Free Software Foundation; either version 2 of   #
13 # the License or (at your option) any later version.  This is online at:      #
14 #     http://www.fsf.org/copyleft/gpl.html                                    #
15 # Please send any updates to: fred@gruntose.com                               #
16 ##############
17
18 # prerequisites for this script:
19 #
20 # (1) the script should be run with a full path, so that it can decide where
21 #     it lives with minimal fuss.
22 # (2) on windows, the unix tools bin directory should already be in the path
23 #     so that tools like dirname are already available.  use msys or cygwin
24 #     at your discretion and your own risk.
25
26 # make sure we know how to find our bash bins.
27 export PATH=/bin:$PATH
28
29 # signals that we're doing a fresh build to the variables script.
30 export INCLUDED_FROM_BOOTSTRAP=true
31
32 # pull in our build variables using the path to this script.
33 export BUILD_SCRIPTS_DIR="$( \cd "$(\dirname "$0")" && \pwd )"
34 #echo build scripts dir initial value: $BUILD_SCRIPTS_DIR
35 BUILD_SCRIPTS_DIR="$(echo $BUILD_SCRIPTS_DIR | tr '\\\\' '/' )"
36 #echo build scripts dir after chewing: $BUILD_SCRIPTS_DIR
37
38 # load in feisty meow basic scripts, if not already loaded.
39 if [ -z "$FEISTY_MEOW_SCRIPTS_LOADED" ]; then
40   bash "$BUILD_SCRIPTS_DIR/../core/bootstrap_shells.sh"
41   source "$BUILD_SCRIPTS_DIR/../core/launch_feisty_meow.sh"
42 fi
43
44 # load in build variables based on our deduced paths.
45 source "$BUILD_SCRIPTS_DIR/build_variables.sh" "$BUILD_SCRIPTS_DIR/build_variables.sh"
46
47 ##############
48
49 # creates the directory for our binaries and gives it a reasonable paths configuration.
50 function prepare_binaries_dir()
51 {
52   # we'll store binaries here from the bootstrap process.
53   if [ ! -d "$BINARY_DIR" ]; then
54     echo "creating binary dir now in $BINARY_DIR"
55     mkdir "$BINARY_DIR"
56   fi
57   if [ ! -f "$BINARY_DIR/paths.ini" ]; then
58     echo "copied paths.ini to binary dir."
59     cp "$PRODUCTION_DIR/paths.ini" "$BINARY_DIR"
60   fi
61 }
62
63 ##############
64
65 # turn off sounds to avoid running the sound player that's not been built yet.
66 unset CLAM_ERROR_SOUND
67 unset CLAM_FINISH_SOUND
68
69 ##############
70
71 echo "Build bootstrap process has started."
72
73 # preconditions for the build process...
74
75 # set up our output directories etc.
76 prepare_binaries_dir
77
78 # set a flag for this process so we can omit certain compilations as necessary.
79 export BOOT_STRAPPING=true
80
81 # enable this macro to get a much noisier build.
82 #export BE_NOISY=NOISY=t
83
84 ##############
85
86 # these default flags turn off unnecessary support when we're rebuilding the
87 # minimal toolset needed for a successful build of hoople.
88 declare -a BUILD_DEFAULTS=( "BOOT_STRAPPING=t" "OPTIMIZE=t" "REBUILD=t" "DEBUG=" )
89   # bootstrapping is always turned on for this particular script.
90   # we also always optimize these builds and turn off the debug flag.
91   # rebuild ensures that the new apps are made fresh: "REBUILD=t"
92   #   it can be turned off when the build bootstrapper is being tested.
93   # noisy can be added to spew lots of text: "NOISY=t"
94   #   this can help with compilation issues by showing all the flags.
95
96 function make_code {
97   make $* $BE_NOISY ${BUILD_DEFAULTS[@]}
98   if [ $? != 0 ]; then
99     echo "Failed to make on: $*"
100     exit 2323
101   fi
102 }
103
104 # removes pcdos eol from any scripts.  that assumes that the bootstrap script
105 # itself isn't polluted with them.
106 function strip_cr {
107   ctrl_m=$'\015'
108   for i in $*; do
109     tempgrep="$(mktemp "$TEMPORARIES_DIR/tempgrep.XXXXXX")"
110     grep -l "$ctrl_m" "$i" >$tempgrep
111     if [ ! -z "$(cat $tempgrep)" ]; then
112       temp="$(mktemp "$TEMPORARIES_DIR/tempsed.XXXXXX")"
113       sed -e "s/$ctrl_m$//" <$i >$temp
114       mv -f $temp $i
115     fi
116     rm "$tempgrep"
117   done
118 }
119
120 # the promote function moves a file from the exe directory into the build's
121 # bin directory.  it performs the copy step and makes the file executable.
122 # the original name should just be the root of the filename without any
123 # extension.
124 # NOTE: this depends on the operating system having been chosen above!
125 if [ "$OPERATING_SYSTEM" = "UNIX" ]; then
126   function promote {
127     prepare_binaries_dir
128
129     if [ ! -f "$INTERMEDIATE_EXE_DIR/$1" ]; then
130       echo "Failed to build the application $1--quitting now."
131       exit 1892
132     fi
133     cp "$INTERMEDIATE_EXE_DIR/$1" "$BINARY_DIR/$1"
134     strip "$BINARY_DIR/$1"
135     chmod 755 "$BINARY_DIR/$1"
136   }
137 elif [ "$OPERATING_SYSTEM" = "WIN32" ]; then
138   function promote {
139     prepare_binaries_dir
140
141     if [ ! -f "$INTERMEDIATE_EXE_DIR/$1.exe" ]; then
142       echo "Failed to build the application $1.exe--quitting now."
143       exit 1892
144     fi
145     cp "$INTERMEDIATE_EXE_DIR/$1.exe" "$BINARY_DIR"
146     chmod 755 "$BINARY_DIR/$1.exe"
147   }
148 else
149   echo "The OPERATING_SYSTEM variable is unset or unknown.  Bailing out."
150   exit 1822
151 fi
152
153 ##############
154
155 # start the actual build process now...
156
157 # recreate our useful waste directories and other things...
158 source "$BUILD_SCRIPTS_DIR/build_variables.sh" "$BUILD_SCRIPTS_DIR/build_variables.sh"
159
160 # clean out any current contents.
161 bash "$BUILD_SCRIPTS_DIR/whack_build.sh" clean
162
163 # make this again so no one gets cranky.
164 mkdir -p "$LOGS_DIR"
165
166 toolset_names=(makedep value_tagger version_stamper vsts_version_fixer write_build_config short_path sleep_ms zap_process playsound create_guid)
167
168 if [ -z "$SAVE_BINARIES" ]; then
169   for i in ${toolset_names[*]}; do
170     whack_name="$BINARY_DIR/$i$EXE_ENDING"
171 #echo removing "$whack_name"
172     rm -f "$whack_name"
173   done
174 fi
175
176 # make the clam shell scripts executable.
177 chmod 755 "$CLAM_DIR"/*.sh
178 chmod 755 "$CLAM_DIR"/cpp/*.sh
179 #chmod 755 "$CLAM_DIR"/csharp/*.sh
180
181 # rebuild the dependency tool.  needed by everything, pretty much, but
182 # since it's from the xfree project, it doesn't need any of our libraries.
183 if [ ! -f "$BINARY_DIR/makedep$EXE_ENDING" ]; then
184   pushd "$TOOL_SOURCES/dependency_tool" &>/dev/null
185   make_code pre_compilation NO_DEPS=t OMIT_VERSIONS=t
186   make_code NO_DEPS=t OMIT_VERSIONS=t
187   if [ ! -f "$INTERMEDIATE_EXE_DIR/makedep$EXE_ENDING" ]; then
188     echo ""
189     echo ""
190     echo "The build of the makedep tool has failed.  Unknown causes...  Argh."
191     echo ""
192     exit 1820
193   fi
194   # make the tool available for the rest of the build.
195   promote makedep
196   popd &>/dev/null
197 fi
198
199 # rebuild the version tools and other support apps.
200 if [ ! -f "$BINARY_DIR/value_tagger$EXE_ENDING" \
201     -o ! -f "$BINARY_DIR/version_stamper$EXE_ENDING" \
202     -o ! -f "$BINARY_DIR/vsts_version_fixer$EXE_ENDING" \
203     -o ! -f "$BINARY_DIR/write_build_config$EXE_ENDING" ]; then
204   pushd "$TOOL_SOURCES/clam_tools" &>/dev/null
205   make_code pre_compilation OMIT_VERSIONS=t
206   make_code OMIT_VERSIONS=t
207
208 #hmmm: really this should check all the expected apps.
209 #      nice to just have an array of the things built by this guy.
210   if [ ! -f "$INTERMEDIATE_EXE_DIR/version_stamper$EXE_ENDING" ]; then
211     echo ""
212     echo ""
213     echo "The build of the version_stamper tool has failed.  Unknown causes...  Argh."
214     echo ""
215     exit 1821
216   fi
217
218   promote value_tagger # tool scrambles through headers to standardize outcomes.
219   promote version_stamper  # used for version stamping.
220   promote vsts_version_fixer  # used for version stamping.
221   promote write_build_config # creates a header of build-specific config info.
222
223   popd &>/dev/null
224 fi
225
226 # build a few other utilities.
227 if [ ! -f "$BINARY_DIR/short_path$EXE_ENDING" \
228     -o ! -f "$BINARY_DIR/sleep_ms$EXE_ENDING" \
229     -o ! -f "$BINARY_DIR/create_guid$EXE_ENDING" \
230     -o ! -f "$BINARY_DIR/zap_process$EXE_ENDING" \
231     -o ! -f "$BINARY_DIR/playsound$EXE_ENDING" ]; then
232   pushd "$TOOL_SOURCES/simple_utilities" &>/dev/null
233   make_code pre_compilation OMIT_VERSIONS=t
234   make_code OMIT_VERSIONS=t
235
236   promote create_guid  # globally unique ID creator.
237   promote playsound  # sound playback tool.
238   promote short_path  # provides short path names for exes on windows.
239   promote sleep_ms  # sleep tool is used in some scripts.
240   promote zap_process  # kills a process in the task list.
241
242   popd &>/dev/null
243 fi
244
245 echo "The build binaries have been re-created (or were already present)."
246
247 # we won't do the full build if they told us to just do the bootstrap.
248 if [ -z "$JUST_BOOTSTRAP_APPS" ]; then
249   echo Cleaning up the temporary files that were built.
250   bash "$BUILD_SCRIPTS_DIR/whack_build.sh" clean
251
252   # recreate our useful junk directories...
253   mkdir -p "$WASTE_DIR"
254   mkdir -p "$TEMPORARIES_DIR"
255   mkdir -p "$LOGS_DIR"
256
257   echo Now starting a normal build of the repository source code.
258   pushd "$FEISTY_MEOW_DIR" &>/dev/null
259   unset BUILD_DEFAULTS
260   declare -a BUILD_DEFAULTS=( "BOOT_STRAPPING=" "OPTIMIZE=t" "DEBUG=t" "REBUILD=t" )
261   make_code
262
263   popd &>/dev/null
264 fi
265