1#!/bin/sh
2#
3#
4
5# error out on failed commands whose return code wasn't explicitly
6# checked
7set -e
8
9usage() {
10cat << EOF
11
12$0 [options]
13
14Builds a non-cygwin version of gerbv or pcb and create a standalone
15windows installer.
16
17Supported options:
18
19  --build-only    - Only run the 'make' part of the process.  This is 
20                    shorthand for all of the --skip-* options except
21                    for --skip-build.
22
23  --debug         - Omits the compiler flag which prevents
24                    a command window from being opened.  This
25                    is useful when trying to use debug printf's.
26                    If listed twice then --enable-debug is passed
27                    down to the configure script which enables 
28                    a good bit of debug output.
29
30  -h|--help       - Show this message and exit.
31
32  --force-autogen - Force running ./autogen.sh.  Normally this is
33                    only done if the configure script is not present.
34
35  --nsis-only     - Only run NSIS to create the installer.  This is
36                    shorthand for all of the following --skip-* options.
37
38  --skip-build    - Skip the "make" step of the process.
39
40  --skip-clean    - Skip the "make clean" step of the process.
41
42  --skip-config   - Skip the "./configure" step of the process.
43
44  --skip-install  - Skip the "make install" step of the process.
45
46  --skip-nsis     - Skip the NSIS step of the process.
47
48For the $0 script to work, you must have the gtk_win32 files
49as well as gdlib installed on your system in very specific
50locations.  Edit $0 to change these.  While you are at it, feel
51free to provide a patch to improve the documentation about 
52those libraries.
53
54On older installs of cygwin, the available gcc will accept -mno-cygwin
55to build an executible that does not link to the cygwin dll.  On newer
56gcc's this option has been removed and you must use either a
57cross compiler that targets mingw or use the native MinGW compiler.
58If you use the native MinGW compiler approach, then 
59the MinGW compilers to be installed in
60c:\MinGW and also in your cygwin environment there needs to be
61a symbolic link:
62
63/mingw -> /cygdrive/c/MinGW
64
65EOF
66}
67
68debug=no
69do_autogen=no
70do_config=yes
71do_build=yes
72do_clean=yes
73do_install=yes
74do_nsis=yes
75config_args=""
76while test $# -ne 0 ; do
77	case $1 in
78		--build-only)
79			do_clean=no
80			do_config=no
81			do_install=no
82			do_nsis=no
83			shift
84			;;
85
86		--debug)
87			if test "X${debug}" = "Xyes" ; then
88				config_args="${config_args} --enable-debug"
89			fi
90			debug=yes
91			shift
92			;;
93
94		-h|--help)
95			usage
96			exit 0
97			;;
98
99		--force-autogen)
100			do_autogen=yes
101			shift
102			;;
103
104		--nsis-only)
105			do_build=no
106			do_clean=no
107			do_config=no
108			do_install=no
109			shift
110			;;
111
112		--skip-build)
113			do_build=no
114			shift
115			;;
116
117		--skip-clean)
118			do_clean=no
119			shift
120			;;
121
122		--skip-config)
123			do_config=no
124			shift
125			;;
126
127		--skip-install)
128			do_install=no
129			shift
130			;;
131
132		--skip-nsis)
133			do_nsis=no
134			shift
135			;;
136
137		-*)
138			echo "ERROR:  Unknown option $1"
139			usage
140			exit 1
141			;;
142
143		*)
144			break
145			;;
146	esac
147done
148
149do_fake_crossbuild=no
150if test -f /mingw/bin/mingw32-gcc.exe ; then
151	do_fake_crossbuild=yes
152fi
153
154echo "do_fake_crossbuild = ${do_fake_crossbuild}"
155
156enable_doc=
157prog_name=unknown
158if test ! -d win32 ; then
159	echo "$0:  ERROR.  This script must be run from the top level of the source tree"
160	exit 1
161fi
162
163if test -f src/gerbv.c ; then
164	prog_name=gerbv
165fi
166
167if test -f src/pcb-menu.res ; then
168	prog_name=pcb
169	config_args="${config_args} --disable-dbus --disable-m4lib-png "
170	if test -f .gitignore -o -f CVS/Root ; then
171		echo "Building from git or CVS so the documentation"
172		echo "build will be disabled (since we are cross building"
173		config_args="${config_args} --disable-doc"
174		enable_doc=";"
175	fi
176fi
177
178if test -f libwcalc/microstrip.c ; then
179	prog_name=wcalc
180	config_args="${config_args} --disable-htdocs --disable-cgi --disable-stdio"
181fi
182
183if test ${prog_name} = unknown ; then
184	cat << EOF
185$0:  ERROR.  Unable to figure out what you are building.
186This may happen if you are trying to execute $0 from a directory
187other than the top of the source tree.
188EOF
189	exit 1
190fi
191
192echo "Building program:  ${prog_name}"
193
194# Run this under cygwin to build gerbv or pcb and create a windows installer for
195# it.  Thanks to Bob Paddock for pointing me to NSIS and answering some
196# beginner windows questions.
197
198# where gtk_win32 is installed
199gtk_win32=c:\\cygwin\\home\\${USER}\\gtk_win32
200gd_win32=c:\\cygwin\\home\\${USER}\\gd_win32
201gd_win32_f=c:/cygwin/home/${USER}/gd_win32
202
203# where only the runtime components are installed
204gtk_win32_runtime=c:\\\\cygwin\\\\home\\\\${USER}\\\\gtk_win32_runtime
205gd_win32_runtime=c:\\\\cygwin\\\\home\\\\${USER}\\\\gd_win32_runtime
206
207# program version
208
209prog_version=`awk '/AC_INIT/ {gsub(/.*,[ \t]*\[/, ""); gsub(/\]\).*/, ""); print}' configure.ac`
210echo "prog_version=${prog_version}"
211
212# location of the NSIS makensis executible (see http://nsis.sourceforge.net)
213makensis="/cygdrive/c/Program Files/NSIS/makensis.exe"
214
215
216# ########################################################################
217#
218# The rest should be ok without editing
219#
220# ########################################################################
221
222
223######################################################################
224#
225# AUTOGEN
226#
227######################################################################
228
229if test ! -f configure -o $do_autogen = yes ; then
230	echo "Bootstrapping autotools"
231	ACLOCAL_FLAGS="-I ${gtk_win32}\\share\\aclocal" ./autogen.sh
232fi
233
234# source directory
235srcdir=`pwd.exe`/win32
236top_srcdir=${srcdir}/..
237
238src_dir=c:\\\\cygwin`echo ${srcdir} | sed 's;/;\\\\\\\\;g'`
239top_src_dir=c:\\\\cygwin`echo ${top_srcdir} | sed 's;/;\\\\\\\\;g'`
240
241
242# where to install the program
243prog_inst=`pwd`/${prog_name}_inst
244
245# DOS version
246prog_inst_dir=c:\\\\cygwin`echo ${prog_inst} | sed 's;/;\\\\\\\\;g'`
247
248PKG_CONFIG_PATH=${gtk_win32}\\lib\\pkgconfig
249export PKG_CONFIG_PATH
250
251PATH=${gtk_win32}\\bin:${gd_win32}:${PATH}
252export PATH
253
254echo "Showing packages known to pkg-config:"
255pkg-config --list-all
256
257
258# do not try to use libpng-config, it seems broken on win32
259if test $prog_name = pcb ; then
260	LIBPNG_CFLAGS="-I${gtk_win32}\\include"
261	LIBPNG_CPPFLAGS="-I${gtk_win32}\\include"
262	LIBPNG_LDFLAGS="-L${gtk_win32}\\lib"
263	LIBPNG_LIBS="-lpng14"
264	LIBGD_CFLAGS="-I${gd_win32}\\include -I${gd_win32_f}/include"
265	LIBGD_CPPFLAGS="-I${gd_win32}\\include"
266	LIBGD_LDFLAGS="-L${gd_win32}\\lib -L${gd_win32_f}/lib"
267	LIBGD_LIBS="-lbgd"
268	# this ugly hack is here because the AC_CHECK_FUNC autoconf
269	# test doesn't include gd.h.  Inside of gd.h, gdImageGif and
270	# friends are declared with
271	#  __declspec(dllimport) void _stdcall
272	# which causes a change in how the function is named in the DLL
273	# which in turn causes the autoconf test to fail.  ugh!  FIXME!
274	export ac_cv_func_gdImageGif=yes
275	export ac_cv_func_gdImageJpeg=yes
276	export ac_cv_func_gdImagePng=yes
277else
278	LIBPNG_CFLAGS=""
279	LIBPNG_LDFLAGS=""
280	LIBPNG_LIBS=""
281fi
282LIBPNG_CONFIG=/usr/bin/true
283export LIBPNG_CONFIG
284
285# add the gcc options to produce a native windows binary that
286# does not need cygwin to run
287if test "x${debug}" = "xno" ; then
288	EXTRA_FLAGS="-mwindows"
289fi
290
291CYGWIN_CFLAGS="-mms-bitfields -mno-cygwin ${EXTRA_FLAGS}"
292export CYGWIN_CFLAGS
293
294CYGWIN_CPPFLAGS="-mms-bitfields -mno-cygwin ${EXTRA_FLAGS}"
295export CYGWIN_CPPFLAGS
296
297
298# older releases of cygwin had a gcc that accepted -mno-cygwin.
299# in more recent releases this flag (which builds and links without
300# the cygwin dll) has been removed.  We either need a normal
301# cross compiler or we can fake it by using the native mingw
302# compiler.
303# see http://www.gnu.org/software/libtool/manual/html_node/Cygwin-to-MinGW-Cross.html
304# in particular, the lt_cv_to_tool_file_cmd bit is needed because
305# libtool will call the mingw ranlib which will not understand
306# any cygwin absolute paths.
307if test ${do_fake_crossbuild} = yes ; then
308	config_args="${config_args} --build=i686-pc-cygwin --host=mingw32"
309	NM=/cygdrive/c/MinGW/bin/nm.exe
310	export NM
311
312	# the ranlib bit here is because putting the mingw tools first
313	# in PATH causes the mingw ranlib to be called.  The problem
314	# with that is tht libtool is passing an absolute cygwin
315	# path at install time to ranlib which can't deal.  The
316	# func_convert... stuff is supposed to help but it didn't.
317	# The libtool folks have a patch for this so at some point
318	# this can go away.
319	RANLIB=/usr/bin/ranlib
320	export RANLIB
321	PATH=/cygdrive/c/MinGW/bin:${PATH}
322	export lt_cv_to_tool_file_cmd=func_convert_file_cygwin_to_w32
323fi
324
325######################################################################
326#
327# CONFIGURE
328#
329######################################################################
330
331# setting WIN32=yes will make sure that the desktop icon
332# gets compiled in
333if test "$do_config" = "yes" ; then
334rm -fr src/.deps
335echo "Configuring for cygwin"
336( ( ( env WIN32=yes \
337	./configure \
338	--prefix=${prog_inst} \
339	--disable-dependency-tracking \
340	--disable-maintainer-mode \
341	--disable-nls \
342	--disable-update-desktop-database \
343	--disable-update-mime-database \
344	${config_args} \
345	CFLAGS="${LIBPNG_CFLAGS} ${LIBGD_CFLAGS}" \
346	CPPFLAGS="${LIBPNG_CPPFLAGS} ${LIBGD_CPPFLAGS}" \
347	LDFLAGS="${LIBPNG_LDFLAGS} ${LIBGD_LDFLAGS}" \
348	LIBS="${LIBPNG_LIBS} ${LIBGD_LIBS}" \
349	WIN32=yes \
350	2>&1 ; echo $? >&4 ) | tee c.log 1>&3) 4>&1 | (read a ; exit $a)) 3>&1
351
352if test $? -ne 0 ; then
353	echo "**** ERROR **** Configure failed. See log in c.log"
354	exit 1
355fi
356
357# If the win32 pkg-config is used, then you end up with spurious CR's
358# in the generated Makefile's and we need to get rid of them.
359
360remove_rc() {
361	f="$1"
362	mv $f $f.bak
363	cat $f.bak | tr '\r' ' ' > $f
364	rm $f.bak
365}
366echo "Removing spurious carriage returns in the Makefiles..."
367for f in `find . -name Makefile -print` ; do
368	remove_rc "$f"
369done
370
371if test -f libtool ; then
372	echo "Removing spurious carriage returns in libtool..."
373	remove_rc libtool
374fi
375
376fi # do_config
377
378######################################################################
379#
380# CLEAN
381#
382######################################################################
383
384if test "$do_clean" = "yes" ; then
385echo "Cleaning"
386( ( ( make clean 2>&1 ; echo $? >&4) | tee clean.log 1>&3) 4>&1 | (read a ; exit $a) ) 3>&1
387if test $? -ne 0 ; then
388	echo "**** ERROR **** Clean failed. See log in clean.log"
389	exit 1
390fi
391fi
392
393######################################################################
394#
395# BUILD
396#
397######################################################################
398
399if test "$do_build" = "yes" ; then
400echo "Building for cygwin"
401( ( ( make 2>&1 ; echo $? >&4) | tee m.log 1>&3) 4>&1 | (read a ; exit $a) ) 3>&1
402if test $? -ne 0 ; then
403	echo "**** ERROR **** Build failed. See log in m.log"
404	exit 1
405fi
406fi
407
408######################################################################
409#
410# INSTALL
411#
412######################################################################
413
414if test "$do_install" = "yes" ; then
415echo "Installing for cygwin"
416# first clean out the installation directory to make sure
417# we don't have old junk lying around.
418if test -d ${prog_inst} ; then
419	rm -fr ${prog_inst}
420fi
421( ( ( make install 2>&1 ; echo $? >&4) | tee -a m.log 1>&3) 4>&1 | (read a ; exit $a) ) 3>&1
422if test $? -ne 0 ; then
423	echo "**** ERROR **** Build failed. See log in m.log"
424	exit 1
425fi
426fi
427
428######################################################################
429#
430# NSIS INSTALLER CREATION
431#
432######################################################################
433
434if test "$do_nsis" = "yes" ; then
435echo "Creating NSIS script"
436echo "srcdir = ${srcdir}"
437echo "src_dir = ${src_dir}"
438echo "top_srcdir = ${top_srcdir}"
439echo "top_src_dir = ${top_src_dir}"
440
441sed \
442	-e "s|@enable_doc@|${enable_doc}|g" \
443	-e "s;@prog_version@;${prog_version};g" \
444	-e "s;@prog_prefix@;${prog_inst_dir};g" \
445	-e "s;@prog_srcdir@;${top_src_dir};g" \
446	-e "s;@gd_win32_runtime@;${gd_win32_runtime};g" \
447	-e "s;@gtk_win32_runtime@;${gtk_win32_runtime};g" \
448	${srcdir}/${prog_name}.nsi.in > ${srcdir}/${prog_name}.nsi
449
450echo "Creating windows installer"
451"${makensis}" ${src_dir}/${prog_name}.nsi
452
453
454echo "Windows installer left in ${srcdir}:"
455ls -l ${srcdir}/*.exe
456
457
458bat=run_install.bat
459
460cat << EOF
461
462Creating DOS batch file wrapper for the installer.
463If you have just built this under cygwin on Vista, 
464you will need to either run the installer from
465the Vista start menu, Windows explorer or directly from
466the cygwin shell with
467
468./${bat}
469
470EOF
471
472cat > ${bat} << EOF
473
474.\win32\\${prog_name}inst-${prog_version}.exe
475
476EOF
477chmod 755 ${bat}
478else
479	echo "Skipping NSIS step per user request"
480fi
481
482
483