712713715f94fdbcbd33ea283ddaa601aeeebeaf
[feisty_meow.git] / scripts / generator / produce_feisty_meow.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_PATH="$( \cd "$(\dirname "$0")" && /bin/pwd )"
34 #echo build scripts dir initial value: $BUILD_SCRIPTS_PATH
35 BUILD_SCRIPTS_PATH="$(echo $BUILD_SCRIPTS_PATH | tr '\\\\' '/' )"
36 #hmmm: why four backslashes above?  trying two in our unix virtual root code below.
37 #echo build scripts dir after chewing: $BUILD_SCRIPTS_PATH
38
39 # load in feisty meow basic scripts, if not already loaded.
40 if [ -z "$FEISTY_MEOW_SCRIPTS_LOADED" ]; then
41   bash "$BUILD_SCRIPTS_PATH/../core/reconfigure_feisty_meow.sh"
42   source "$BUILD_SCRIPTS_PATH/../core/launch_feisty_meow.sh"
43 fi
44
45 source "$FEISTY_MEOW_SCRIPTS/core/functions.sh"
46
47 # translate to dos format if there's a cygdrive in there; otherwise microsoft's tools
48 # will hose up completely due to unknown paths.
49 export FEISTY_MEOW_APEX="$(unix_to_dos_path $FEISTY_MEOW_APEX)"
50
51 # load in build variables based on our deduced paths.
52 source "$BUILD_SCRIPTS_PATH/build_variables.sh" "$BUILD_SCRIPTS_PATH/build_variables.sh"
53
54 ##############
55
56 # creates the directory for our binaries and gives it a reasonable paths configuration.
57 function prepare_clam_binaries_dir()
58 {
59   # we'll store binaries here from the bootstrap process.
60   if [ ! -d "$CLAM_BINARIES" ]; then
61     echo "creating clam binary dir now in $CLAM_BINARIES"
62     mkdir -p "$CLAM_BINARIES"
63     exit_on_error "creating clam binary directory in $CLAM_BINARIES"
64   fi
65   if [ ! -f "$CLAM_BINARIES/paths.ini" ]; then
66     cp "$PRODUCTION_STORE/paths.ini" "$CLAM_BINARIES"
67     exit_on_error "copying paths.ini to $CLAM_BINARIES"
68     echo "copied paths.ini to clam binary dir."
69   fi
70 }
71
72 # fix the system helper header up by calling cygpath to determine the cygwin
73 # root, if we are on windoze and cygwin.
74 function update_system_helper_header()
75 {
76   # create our main binaries directory if needed.
77   if [ ! -d "$FEISTY_MEOW_BINARIES" ]; then
78     echo "creating feisty meow binary folder now in $FEISTY_MEOW_BINARIES"
79     mkdir -p "$FEISTY_MEOW_BINARIES"
80     exit_on_error "creating feisty meow binary folder in $FEISTY_MEOW_BINARIES"
81   fi
82
83   # copy up the system helper template version into the binaries directory.
84   if [ ! -f "$FEISTY_MEOW_BINARIES/system_helper.h" ]; then
85     cp "$PRODUCTION_STORE/system_helper_template.h" "$FEISTY_MEOW_BINARIES/system_helper.h"
86     exit_on_error "creating system_helper header in $FEISTY_MEOW_BINARIES"
87     echo "copied system_helper header to feisty meow binary dir."
88   fi
89
90   # set the cygwin root path if we're on cygwin.
91   whichable cygpath
92   if [ $? -eq 0 ]; then
93     # found cygpath, so run it now to get the dossy path of the root ('/' folder).
94     found_root="$(cygpath -w -m /)"
95     if [ $? -ne 0 ]; then
96       echo "Failure to find virtual Unix root folder with cygpath."
97       exit 1322
98     fi
99 echo "found root as '$found_root'"
100     # translate any backslashes to forward thinking slashes.    
101     found_root="$(echo $found_root | tr '\\' '/')"
102 echo "processed root is now: '$found_root'"
103     # edit the entry in place to correct the default path.
104     sed -i \
105         -e "s% *#define FEISTY_MEOW_VIRTUAL_UNIX_ROOT \".*$%#define FEISTY_MEOW_VIRTUAL_UNIX_ROOT \"$found_root\"%" \
106         "$FEISTY_MEOW_BINARIES/system_helper.h"
107     exit_on_error "updating system_helper header in $FEISTY_MEOW_BINARIES"
108 echo "system helper file now has:"
109 cat "$FEISTY_MEOW_BINARIES/system_helper.h"
110   fi
111 }
112
113 function make_code {
114   make $* $BE_NOISY ${BUILD_DEFAULTS[@]}
115   if [ $? != 0 ]; then
116     echo "Failed to make on: $*"
117     exit 2323
118   fi
119 }
120
121 # removes pcdos eol from any scripts.  that assumes that the bootstrap script
122 # itself isn't polluted with them.
123 function strip_cr {
124   ctrl_m=$'\015'
125   for i in $*; do
126     tempgrep="$(mktemp "$TEMPORARIES_PILE/tempgrep.XXXXXX")"
127     grep -l "$ctrl_m" "$i" >$tempgrep
128     if [ ! -z "$(cat $tempgrep)" ]; then
129       temp="$(mktemp "$TEMPORARIES_PILE/tempsed.XXXXXX")"
130       sed -e "s/$ctrl_m$//" <$i >$temp
131       mv -f $temp $i
132     fi
133     rm "$tempgrep"
134   done
135 }
136
137 ##############
138
139 # turn off sounds to avoid running the sound player that's not been built yet.
140 unset CLAM_ERROR_SOUND
141 unset CLAM_FINISH_SOUND
142
143 ##############
144
145 echo "Build bootstrap process has started."
146
147 # preconditions for the build process...
148
149 # set a flag for this process so we can omit certain compilations as necessary.
150 export BOOT_STRAPPING=true
151
152 # enable this macro to get a much noisier build.
153 #export BE_NOISY=NOISY=t
154
155 ##############
156
157 # these default flags turn off unnecessary support when we're rebuilding the
158 # minimal toolset needed for a successful build of hoople.
159 declare -a BUILD_DEFAULTS=( "BOOT_STRAPPING=t" "OPTIMIZE=t" "REBUILD=t" "DEBUG=" )
160   # bootstrapping is always turned on for this particular script.
161   # we also always optimize these builds and turn off the debug flag.
162   # rebuild ensures that the new applications are made fresh: "REBUILD=t"
163   #   it can be turned off when the build bootstrapper is being tested.
164   # noisy can be added to spew lots of text: "NOISY=t"
165   #   this can help with compilation issues by showing all the flags.
166
167 # the promote function moves a file from the exe directory into the build's
168 # bin directory.  it performs the copy step and makes the file executable.
169 # the original name should just be the root of the filename without any
170 # extension.
171 # NOTE: this depends on the operating system having been chosen above!
172 if [ "$OPERATING_SYSTEM" = "UNIX" ]; then
173   function promote {
174     prepare_clam_binaries_dir
175
176     if [ ! -f "$INTERMEDIATE_STORE/$1" ]; then
177       echo "Failed to build the application $1--quitting now."
178       exit 1892
179     fi
180     cp "$INTERMEDIATE_STORE/$1" "$CLAM_BINARIES/$1"
181     strip "$CLAM_BINARIES/$1"
182     chmod 755 "$CLAM_BINARIES/$1"
183   }
184 elif [ "$OPERATING_SYSTEM" = "WIN32" ]; then
185   function promote {
186     prepare_clam_binaries_dir
187
188     if [ ! -f "$INTERMEDIATE_STORE/$1.exe" ]; then
189       echo "Failed to build the application $1.exe--quitting now."
190       exit 1892
191     fi
192     cp "$INTERMEDIATE_STORE/$1.exe" "$CLAM_BINARIES"
193     chmod 755 "$CLAM_BINARIES/$1.exe"
194   }
195 else
196   echo "The OPERATING_SYSTEM variable is unset or unknown.  Bailing out."
197   exit 1822
198 fi
199
200 ##############
201
202 # start the actual build process now...
203
204 # load in the feisty meow building environment.
205 source "$BUILD_SCRIPTS_PATH/build_variables.sh" "$BUILD_SCRIPTS_PATH/build_variables.sh"
206
207 # clean out any current contents.
208 bash "$BUILD_SCRIPTS_PATH/whack_build.sh" clean
209
210 # set up our output directories etc.
211 prepare_clam_binaries_dir
212
213 # make this again so no one gets cranky.
214 mkdir -p "$FEISTY_MEOW_LOGS"
215
216 toolset_names=(makedep value_tagger version_stamper vsts_version_fixer write_build_config short_path sleep_ms zap_process playsound create_guid)
217
218 if [ -z "$SAVE_BINARIES" ]; then
219   for i in ${toolset_names[*]}; do
220     whack_name="$CLAM_BINARIES/$i$EXE_ENDING"
221 #echo removing "$whack_name"
222     rm -f "$whack_name"
223   done
224 fi
225
226 # copy the system helper header.
227 update_system_helper_header
228
229 # rebuild the dependency tool.  needed by everything, pretty much, but
230 # since it's from the xfree project, it doesn't need any of our libraries.
231 if [ ! -f "$CLAM_BINARIES/makedep$EXE_ENDING" ]; then
232   pushd "$TOOL_SOURCES/dependency_tool" &>/dev/null
233   make_code pre_compilation NO_DEPS=t OMIT_VERSIONS=t
234   make_code NO_DEPS=t OMIT_VERSIONS=t
235   if [ ! -f "$INTERMEDIATE_STORE/makedep$EXE_ENDING" ]; then
236     echo ""
237     echo ""
238     echo "The build of the makedep tool has failed.  Unknown causes...  Argh."
239     echo ""
240     exit 1820
241   fi
242   # make the tool available for the rest of the build.
243   promote makedep
244   popd &>/dev/null
245 fi
246
247 # rebuild the version tools and other support applications.
248 if [ ! -f "$CLAM_BINARIES/value_tagger$EXE_ENDING" \
249     -o ! -f "$CLAM_BINARIES/version_stamper$EXE_ENDING" \
250     -o ! -f "$CLAM_BINARIES/vsts_version_fixer$EXE_ENDING" \
251     -o ! -f "$CLAM_BINARIES/write_build_config$EXE_ENDING" ]; then
252   pushd "$TOOL_SOURCES/clam_tools" &>/dev/null
253   make_code pre_compilation OMIT_VERSIONS=t
254   make_code OMIT_VERSIONS=t
255
256 #hmmm: really this should check all the expected applications.
257 #      nice to just have an array of the things built by this guy.
258   if [ ! -f "$INTERMEDIATE_STORE/version_stamper$EXE_ENDING" ]; then
259     echo ""
260     echo ""
261     echo "The build of the version_stamper tool has failed.  Unknown causes...  Argh."
262     echo ""
263     exit 1821
264   fi
265
266   promote value_tagger # tool scrambles through headers to standardize outcomes.
267   promote version_stamper  # used for version stamping.
268   promote vsts_version_fixer  # used for version stamping.
269   promote write_build_config  # creates a header of build-specific config info.
270
271   popd &>/dev/null
272 fi
273
274 # build a few other utilities.
275 if [ ! -f "$CLAM_BINARIES/short_path$EXE_ENDING" \
276     -o ! -f "$CLAM_BINARIES/sleep_ms$EXE_ENDING" \
277     -o ! -f "$CLAM_BINARIES/create_guid$EXE_ENDING" \
278     -o ! -f "$CLAM_BINARIES/zap_process$EXE_ENDING" \
279     -o ! -f "$CLAM_BINARIES/playsound$EXE_ENDING" ]; then
280   pushd "$TOOL_SOURCES/simple_utilities" &>/dev/null
281   make_code pre_compilation OMIT_VERSIONS=t
282   make_code OMIT_VERSIONS=t
283
284   promote create_guid  # globally unique ID creator.
285   promote playsound  # sound playback tool.
286   promote short_path  # provides short path names for exes on windows.
287   promote sleep_ms  # sleep tool is used in some scripts.
288   promote zap_process  # kills a process in the task list.
289
290   popd &>/dev/null
291 fi
292
293 echo "The build binaries have been re-created (or were already present)."
294
295 # we won't do the full build if they told us to just do the bootstrap.
296 if [ -z "$JUST_BOOTSTRAP_APPS" ]; then
297   echo Cleaning up the temporary files that were built.
298   bash "$BUILD_SCRIPTS_PATH/whack_build.sh" 
299
300   # recreate our useful junk directories...
301   mkdir -p "$FEISTY_MEOW_GENERATED_STORE"
302   mkdir -p "$TEMPORARIES_PILE"
303   mkdir -p "$FEISTY_MEOW_LOGS"
304
305   # re-copy the system helper header.
306   update_system_helper_header
307
308   echo Now starting a normal build of the repository source code.
309   pushd "$FEISTY_MEOW_APEX" &>/dev/null
310   unset BUILD_DEFAULTS
311   declare -a BUILD_DEFAULTS=( "BOOT_STRAPPING=" "OPTIMIZE=t" "DEBUG=t" "REBUILD=t" )
312   make_code
313
314   popd &>/dev/null
315 fi
316