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