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