]> git.lyx.org Git - wiki-uploads.git/blob - DevelTools/buildLyx/build-lyx.sh
Import uploads from wiki
[wiki-uploads.git] / DevelTools / buildLyx / build-lyx.sh
1 #!/bin/bash
2
3 S0=`basename $0`
4 D0=`dirname $0`
5
6 if [[ "x$1" == "x" || "x$1" == "x--help" || "x$1" == "x-h" ]]; then
7     cat <<EOF
8 Syntax: $S0 [options]
9 or:     $S0 [options] init
10
11 The first form typically performs tasks to build a version of lyx from scratch.
12
13 The second form is used to create a development directory (\$LYXDEVEL)
14 and check out the CVS module 'lyx-devel' from the CVS server.
15
16 Options:
17   -v                    Be more verbose
18   -n                    Only print commands, do not actually run
19   -f config-file        Name of configuration file to use
20   -l log-file           Set name of log-file
21   --no-rm               Don't remove previous build directory
22   --no-cvs              Don't run cvs update and autogen.sh
23   --no-configure        Don't run configure
24   --no-make             Don't run make
25   -h, --help            Show help
26   --version             Report version
27
28 Example:
29  # Read commands from xforms-devel.cfg
30  $ $0 -f etc/xforms-devel.cfg
31
32 Note that the configuration file is first looked for in the current
33 directory, and then relative to where the script is located.
34
35 For further information, see 
36         http://wiki.lyx.org/pmwiki.php/DevelScripts/BuildLyx
37
38 EOF
39 exit 0;
40 fi
41
42 if [[ "x$1" == "x--version" ]]; then printf "$S0 1.0\n"; exit 0; fi
43
44 # Use: Verbose 1 "Only printed if Verbose >= 1, arg: %s" "an argument"
45 # The first numeric argument can be skipped if the condition is onl that
46 # the verbosity > 0
47 function Verbose() {
48     if [[ ${#1} -eq 1 && $Verbosity -lt $1 ]]; then return; fi;
49     if [[ ${#1} -ne 1 && $Verbosity -eq 0 ]]; then return; fi;
50     if [[ ${#1} -eq 1 ]]; then shift; fi;
51     FMT="$1\n"
52     shift;
53     printf "$FMT" "$@"
54 }
55
56 function Log() {
57     Verbose 0 "# ""$@" | tee --append $LogFile
58 }
59
60 # Use: Error -1 "This argument is wrong: %s" "an argument"
61 function Error() {
62     ERROR_CODE=$1;
63     shift;
64     printf "%s: Error(%d) " "`basename $0`" $ERROR_CODE
65     FMT="$1\n"
66     shift
67     printf "$FMT" "$@"
68     exit $ERROR_CODE
69 }
70
71 # For some reason, 'cd' doesn't work when |tee is used...
72 function Do() {
73     if (($DO)); then msg=""; else msg="## "; fi;
74     if (($Verbosity >= 1)); then Log "%s%s" "$msg" "$*"; fi;
75     fail=0
76     if (($DO)); then
77         if [[ "$1" == "cd" || "$1" == "pushd" || "$1" == "popd" ]]; then
78             printf "%s\n" "$*" >> $LogFile
79             printf "%s\n" "$*"
80             "$@"
81         else
82             "$@" | tee --append $LogFile ;
83             fail=${PIPESTATUS[0]}
84         fi;
85     fi;
86     return $fail
87 }
88
89 ConfigFilesRead=0
90 function ReadConfiguration() {
91     Verbose 2 "Looking for configuration file"
92     for FIL in "$@" ; do
93         Verbose 3 "Checking for %s" "$FIL"
94         if [[ -r $FIL ]]; then
95             Verbose 1 "Reading %s" "$FIL"
96             source $FIL
97             let ++ConfigFilesRead
98         elif [[ -r $D0/$FIL ]]; then
99             Verbose 1 "Reading %s/%s" "$D0" "$FIL"
100             source $D0/$FIL
101             let ++ConfigFilesRead
102         else
103             Verbose 2 "Configuration file %s not found" "$FIL"
104         fi
105     done
106     if (( $ConfigFilesRead > 0 )); then
107         Verbose 2 "Configuration after reading configuration files:"
108         if (( $Verbosity >= 2 )); then ShowConfiguration; fi
109     fi
110 }
111
112 #
113 # Login to CVS server or checkout a cvs module
114 # Syntax: CVS_Command login
115 # or:     CVS_Command checkout <DestDir>
116 # where <DestDir> is the name of the destination directory
117 # (relative to $LYXDEVEL which is created if necessary).
118 # The release tag that's checked out is taken from $LYXTAG
119 # Example: CVS_Command checkout $LYXTAG
120 # will checkout to a directory with the same name as the tag
121 #
122 function CVS_Command() {
123     if [[ "$1" == "login" ]]; then
124         Verbose 0 "The anonymous password is 'lyx'"
125         Do cvs -d $LYX_CVS_SERVER login
126     elif [[ "$1" == "checkout" ]]; then
127         Do mkdir -p $LYXDEVEL;
128         Do pushd $LYXDEVEL
129         Verbose 0 "\n\nGo have coffee or something..."
130         Do cvs -d $LYX_CVS_SERVER co -d $2 -r $LYXTAG $LYX_CVS_MODULE
131         Do popd
132     fi;
133 }
134
135 #
136 # Set default values of variables
137 #
138 # Standard variables
139 Verbosity=0                     # Controls the level of verbosity
140 DO=1                            # Set to zero if nothing should be done
141 LogFile=`pwd`/build-lyx.log
142 #
143 # Script specific variables
144 LYX_CVS_SERVER=":pserver:anoncvs@anoncvs.us.lyx.org:/cvs/lyx"
145 LYX_CVS_MODULE="lyx-devel"
146 MODULE_LYX=lyx/devel/latest-xforms
147 MODULE_GCC=gcc/3.3.2
148 BuildPostfix=""
149 OPTS_CONFIGURE=()
150 DO_RM=1;                        # Remove previous build directory?
151 DO_CVS=1;                       # Do CVS update
152 DO_CONFIGURE=1;                 # Run configure
153 DO_MAKE=1;                      # Run make
154 USE_MODULES=0;                  # Use modules?
155
156 function ShowConfiguration(){
157     if (( $Verbosity >= 2 )); then
158         Verbose "Variables in the script:"
159         Verbose "DO=%s" "$DO"
160         Verbose "buildDir=%s" "$buildDir"
161         Verbose "BuildPostfix=%s" "$BuildPostfix"
162         Verbose "LYXDEVEL=%s" "$LYXDEVEL"
163         Verbose "LYXTAG=%s, LYXFRONTEND=%s" "$LYXTAG" "$LYXFRONTEND"
164     fi;
165 }
166
167 # Initialize modules
168 if (($USE_MODULES)); then
169     . /usr/local/lib/module/default/init/bash
170     module rm lyx gcc           # Remove any lyx and gcc modules
171 fi;
172
173 # Parse arguments
174 savedArgs="$*"
175 for ARG in "$@"; do if [[ "$ARG" == "-v" ]]; then let ++Verbosity; fi; done;
176
177 until [[ $# -eq 0 ]]; do
178     case "$1" in
179         -v)     ;;              # Already parsed for
180         -n)     DO=0 ;;
181         --no-rm)        DO_RM=0 ;;
182         --no-cvs)       DO_CVS=0 ;;
183         --no-make)      DO_MAKE=0 ;;
184         --no-configure) DO_CONFIGURE=0 ;;
185         -f)     ReadConfiguration $2; shift ;;
186         -l)     LogFile=$2; shift ;;
187         -*)     Error -1 "Unknown option: %s" "$1" ;;
188         init)   break ;;
189         *)      Error -2 "Unkown argument: %s" "$1" ;;
190     esac
191     shift;
192 done
193
194 if (( $ConfigFilesRead == 0 )); then
195     Error -2 "No configuration file specified!"
196 fi;
197
198 # Initialize log-file
199 Verbose 0 "Log-file=%s\n" "$LogFile"
200
201 printf "# --------------------------------\n"\
202 "# Log file from executing:\n#  %s$ %s %s\n" \
203 "`pwd`" "$0""$savedArgs" > $LogFile
204 Log "Date: %s\n" "`date`"
205
206 # Verify validity of arguments
207 # if [[ "x$Adr" == "x" ]]; then Error "No address given!"; fi;
208
209 #
210 # -----------------------
211 # Actually do stuff...
212 #
213
214 # Handle initialization
215 if [[ "$1" == "init" ]]; then
216     CVS_Command login
217     CVS_Command checkout $LYXTAG
218     exit 0;
219 fi;
220
221 # Add modules
222 if (($USE_MODULES)); then
223     module add $MODULE_LYX $MODULE_GCC  2>&1 1>>$LogFile
224     if (($?)); then Error -1 "While loading modules"; fi;
225 fi;
226
227 # Create buildDir if necessary
228 if [[ "$buildDir" == "" ]]; then
229     buildDir=$LYXDEVEL/$LYXTAG/build-$LYXFRONTEND
230     if [[ "x$BuildPostfix" != "x" ]]; then
231         buildDir="$buildDir-$BuildPostfix";
232     fi;
233 fi;
234 Log "Build directory=%s" "$buildDir"
235
236
237 GCC_VERSION="`gcc --version`"
238 Log "Using %s" "${GCC_VERSION%%Copyright*}"
239
240 if (($DO_RM)); then
241     Log "Erasing and recreating build directory: %s" "$buildDir"
242     Do rm -rf $buildDir && Do mkdir -p $buildDir
243     if (($?)); then Error -2 "While erasing/creating build directory"; fi
244 else
245     Log "Skipping erasing build directory"
246 fi;
247
248 if (($DO_RM || $DO_CVS)); then
249     Log "Updating source and then running autogen.sh"
250     cd $LYXDEVEL/$LYXTAG && Do cvs -q update -d && Do ./autogen.sh
251     if (($?)); then Error -3 "While cvs update or autogen.sh"; fi
252 else
253     Log "Skipping 'cvs update' and autogen"
254 fi;
255
256 if (($DO_RM || $DO_CVS || $DO_CONFIGURE)); then
257     Log "Running configure"
258     if (($DO)); then cd $buildDir; fi
259     Do ../configure --prefix=$LYXPREFIX ${OPTS_CONFIGURE[*]}
260     if (($?)); then Error -4 "While running configure"; fi
261 else
262     Log "Skipping configure"
263 fi;
264
265 if (($DO_MAKE)); then
266     Log "Running make"
267     Do make -j 5
268     if (($?)); then Error -5 "While running make"; fi
269 else
270     Log "Skipping make"
271 fi;
272
273
274
275
276