summaryrefslogtreecommitdiff
path: root/setup/openvpn/build.zsh
blob: cc5d17bba2fd4a1997b58115f0e974d7776805ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env zsh
#
# Copyright (C) 2012 Denis Roio <jaromil@dyne.org>
#
# This source  code is free  software; you can redistribute  it and/or
# modify it under the terms of  the GNU Public License as published by
# the Free  Software Foundation; either  version 3 of the  License, or
# (at your option) any later version.
#
# This source code is distributed in  the hope that it will be useful,
# but  WITHOUT ANY  WARRANTY;  without even  the  implied warranty  of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


REPO="http://files.dyne.org/leap/openvpn/sources"
TOPSRC=`pwd`
QUIET=0
DEBUG=0


autoload colors; colors
# standard output message routines
# it's always useful to wrap them, in case we change behaviour later
notice() { if [[ $QUIET == 0 ]]; then print "$fg_bold[green][*]$fg_no_bold[default] $1" >&2; fi }
error()  { if [[ $QUIET == 0 ]]; then print "$fg[red][!]$fg[default] $1" >&2; fi }
func()   { if [[ $DEBUG == 1 ]]; then print "$fg[blue][D]$fg[default] $1" >&2; fi }
act()    {
    if [[ $QUIET == 0 ]]; then
        if [ "$1" = "-n" ]; then
            print -n "$fg_bold[white] . $fg_no_bold[default] $2" >&2;
        else
            print "$fg_bold[white] . $fg_no_bold[default] $1" >&2;
        fi
    fi
}

{ test "$1" = "clean" } && {
	notice "Cleaning up all build in ${TOPSRC}"
	for src in `cat Sources | awk '
/^#/ {next}
/^./ { print $1 }'`; do
		{ test "$src" != "" } && { rm -rf "${src}" }
	done
	act "Done."
	return 0
}

notice "OpenVPN build in ${TOPSRC}"

prepare_sources() {
    notice "Preparing sources"
    # look for a file names "Sources", download and decompress entries
    # format of file: name version compression (complete filename when merged)
    { test -r Sources } || {
        error "Sources not found, nothing to build here"
        return 1
    }
    for src in `cat Sources | awk '
/^#/ {next}
/^./ { print $1 ";" $2 ";" $3 }'`; do
        name="${src[(ws:;:)1]}"
        ver="${src[(ws:;:)2]}"
        arch="${src[(ws:;:)3]}"
        file="${name}${ver}${arch}"
        func "preparing source for ${name}${ver}"
        # download the file
        { test -r ${file} } || {
            act "downloading ${file}"
            wget ${REPO}/${file}
        }
        # decompress the file
        { test -r ${name} } || {
            act "decompressing ${name}"
            case $arch in
                ## BARE SOURCE
                .tar.gz)  tar xfz ${file}; mv ${name}${ver} ${name} ;;
                .tar.bz2) tar xfj ${file}; mv ${name}${ver} ${name} ;;
                *) error "compression not supported: $arch"
            esac
        }
        act "${name} source ready"
    done
}

prepare_sources

# tap windows
{ test -r tap-windows } || { git clone https://github.com/OpenVPN/tap-windows.git }

notice "Sources ready, now compiling..."
LOG="`pwd`/build.log"; touch ${LOG}
act "logs saved in build.log"

{ test -r lzo/src/liblzo2.la } || { pushd lzo
	act "building LZO lib"
	./configure --host=i586-mingw32msvc >> ${LOG}
	make >> ${LOG}; popd }
# openssl
{ test -r openssl/libssl.a } || {
	act "building OpenSSL lib"
    pushd openssl
    ./Configure --cross-compile-prefix=i586-mingw32msvc- mingw >> ${LOG}
    make ${LOG}; popd }

# openvpn
{ test -r openvpn } || { git clone https://github.com/OpenVPN/openvpn.git } 
pushd openvpn
act "building latest OpenVPN"
{ test -r configure } || {
 sed -i -e 's/-municode//' src/openvpn/Makefile.am
 autoreconf -i >> ${LOG}
}
CFLAGS="-I/usr/i586-mingw32msvc/include/ddk -D_WIN32_WINNT=0x0501" \
LZO_LIBS="${TOPSRC}/lzo/src/liblzo2.la" \
LZO_CFLAGS="-I${TOPSRC}/lzo/include" \
TAP_CFLAGS="-I${TOPSRC}/tap-windows/src" \
OPENSSL_SSL_CFLAGS="-I${TOPSRC}/openssl/include" \
OPENSSL_CRYPTO_CFLAGS="-I${TOPSRC}/openssl/crypto" \
OPENSSL_SSL_LIBS="${TOPSRC}/openssl/libssl.a" \
OPENSSL_CRYPTO_LIBS="${TOPSRC}/openssl/libcrypto.a" \
./configure --host=i586-mingw32msvc >> ${LOG}
make >> ${LOG}
popd
notice "Done."
act "If OpenVPN build reports a final error on linkage, it might be due to a libtool bug"
act "(something like undefined reference to `_WinMain@16')"
act "You need to go inside openvpn/src/openvpn and issue the last compile line manually"
act "adding an flat '-shared' at the end of it, then do 'cp .libs/openvpn.exe .'"
act "Happy hacking."