|
2 | 2 |
|
3 | 3 | # Usage: ./generateOptions.sh [<wolfssl-prefix-or-source-root>] |
4 | 4 | # |
5 | | -# If the argument is an installed wolfSSL prefix (containing |
6 | | -# include/wolfssl/options.h), options.go is generated and all cgo-bearing |
7 | | -# files are repointed at that prefix. If the argument is a wolfSSL source |
8 | | -# root (containing wolfssl/options.h), only options.go is generated. |
9 | | -# With no argument, defaults to ../wolfssl as a source root and /usr/local. |
| 5 | +# Points the #cgo CFLAGS/LDFLAGS of every cgo-using package at a wolfSSL |
| 6 | +# install. If the argument is an installed wolfSSL prefix (containing |
| 7 | +# include/wolfssl/options.h), the paths point at that prefix. If it is a wolfSSL |
| 8 | +# source root (containing wolfssl/options.h), or no argument is given, they |
| 9 | +# point at /usr/local. |
10 | 10 |
|
11 | 11 | OPTIONS_H="../wolfssl/wolfssl/options.h" |
| 12 | +DEFAULT_PREFIX="/usr/local" |
12 | 13 | PREFIX="" |
13 | 14 |
|
| 15 | +CGO_FILES="aes.go wolftls/conn.go wolfx509/certgen_wolfcrypt.go \ |
| 16 | + examples/client/client-psk.go examples/server/server-psk.go" |
| 17 | + |
| 18 | +SUCCESS=0 |
| 19 | +on_exit() { |
| 20 | + for f in $CGO_FILES; do |
| 21 | + if [ "$SUCCESS" -eq 1 ]; then |
| 22 | + rm -f "$f.bak" |
| 23 | + elif [ -f "$f.bak" ]; then |
| 24 | + mv "$f.bak" "$f" |
| 25 | + fi |
| 26 | + done |
| 27 | + if [ "$SUCCESS" -eq 0 ]; then |
| 28 | + rm -f options.go |
| 29 | + fi |
| 30 | +} |
| 31 | + |
| 32 | +for f in $CGO_FILES; do |
| 33 | + if [ ! -f "$f" ]; then |
| 34 | + echo "Expected cgo-bearing file not found: $f" |
| 35 | + exit 99 |
| 36 | + fi |
| 37 | +done |
| 38 | + |
14 | 39 | if [ -n "$1" ]; then |
15 | | - WOLFSSL_PATH="$1" |
| 40 | + if [ ! -d "$1" ]; then |
| 41 | + echo "Path to wolfSSL is not a directory" |
| 42 | + exit 99 |
| 43 | + fi |
| 44 | + |
| 45 | + # Ensure the path is in absolute format |
| 46 | + WOLFSSL_PATH=$(CDPATH= cd -- "$1" > /dev/null && pwd) |
| 47 | + if [ -z "$WOLFSSL_PATH" ]; then |
| 48 | + echo "Couldn't resolve $1 to an absolute path" |
| 49 | + exit 99 |
| 50 | + fi |
| 51 | + |
16 | 52 | echo "Path to wolfSSL was supplied." |
17 | 53 |
|
18 | 54 | if [ -f "$WOLFSSL_PATH/include/wolfssl/options.h" ]; then |
19 | 55 | OPTIONS_H="$WOLFSSL_PATH/include/wolfssl/options.h" |
20 | 56 | PREFIX="$WOLFSSL_PATH" |
| 57 | + echo "wolfSSL install given, linking/building with $PREFIX" |
21 | 58 | elif [ -f "$WOLFSSL_PATH/wolfssl/options.h" ]; then |
22 | 59 | OPTIONS_H="$WOLFSSL_PATH/wolfssl/options.h" |
23 | 60 | else |
|
32 | 69 | fi |
33 | 70 | fi |
34 | 71 |
|
| 72 | +if [ -z "$PREFIX" ]; then |
| 73 | + PREFIX="$DEFAULT_PREFIX" |
| 74 | + echo "wolfSSL install dir not given, linking/building with default prefix ($DEFAULT_PREFIX)." |
| 75 | +fi |
| 76 | + |
| 77 | +# When on Windows convert the path format |
| 78 | +if command -v cygpath >/dev/null 2>&1; then |
| 79 | + PREFIX=$(cygpath -m "$PREFIX") |
| 80 | +fi |
| 81 | + |
| 82 | +case "$PREFIX" in |
| 83 | + *[!/_.:+[:alnum:]-]*) |
| 84 | + echo "Prefix contains characters a #cgo directive cannot express: $PREFIX" |
| 85 | + echo "Use a path made up of letters, digits and / _ . : + - only." |
| 86 | + exit 99 |
| 87 | + ;; |
| 88 | +esac |
| 89 | + |
| 90 | +trap on_exit EXIT |
| 91 | +trap 'exit 99' INT TERM |
| 92 | + |
35 | 93 | rm -f options.go |
36 | | -echo "package wolfSSL" >> options.go |
37 | | -echo "" >> options.go |
38 | | -echo "// #cgo CFLAGS: -g -Wall -I/usr/include -I/usr/include/wolfssl" >> options.go |
39 | | -echo "// #cgo LDFLAGS: -L/usr/local/lib -lwolfssl -lm" >> options.go |
40 | | -sed 's/^/\/\/ /' "$OPTIONS_H" >> options.go |
41 | | -echo "options.go generated." |
42 | | - |
43 | | -# When the supplied path is an installed wolfSSL prefix, repoint cgo |
44 | | -# directives in every cgo-bearing file at $PREFIX. Skipped for source-tree |
45 | | -# layouts (no <src>/lib to point -L at). |
46 | | -if [ ! -z "$PREFIX" ]; then |
47 | | - # First normalize back to upstream defaults, so re-running this script with a new |
48 | | - # prefix overwrites the old one rather than no-op'ing. |
49 | | - sed -i.bak \ |
50 | | - -e "s|-I[^ ]*/include -I[^ ]*/include/wolfssl|-I/usr/include -I/usr/include/wolfssl|" \ |
51 | | - -e "s|-L[^ ]*/lib -lwolfssl|-L/usr/local/lib -lwolfssl|" \ |
52 | | - options.go aes.go wolfx509/certgen_wolfcrypt.go wolftls/conn.go \ |
53 | | - && rm options.go.bak aes.go.bak wolfx509/certgen_wolfcrypt.go.bak wolftls/conn.go.bak |
54 | | - sed -i.bak \ |
55 | | - -e "s|-I/usr/include -I/usr/include/wolfssl|-I$PREFIX/include -I$PREFIX/include/wolfssl|" \ |
56 | | - -e "s| -I/usr/local/include -I/usr/local/include/wolfssl||" \ |
57 | | - -e "s|-L/usr/local/lib|-L$PREFIX/lib|" \ |
58 | | - options.go aes.go wolfx509/certgen_wolfcrypt.go wolftls/conn.go \ |
59 | | - && rm options.go.bak aes.go.bak wolfx509/certgen_wolfcrypt.go.bak wolftls/conn.go.bak |
60 | | - echo "cgo paths pointed at $PREFIX." |
| 94 | +echo "package wolfSSL" >> options.go |
| 95 | +echo "" >> options.go |
| 96 | +echo "// #cgo CFLAGS: -g -Wall -I$PREFIX/include" >> options.go |
| 97 | +echo "// #cgo LDFLAGS: -L$PREFIX/lib -lwolfssl -lm" >> options.go |
| 98 | +sed 's/^/\/\/ /' "$OPTIONS_H" >> options.go |
| 99 | +if [ $? -ne 0 ]; then |
| 100 | + echo "Failed to generate options.go from $OPTIONS_H." |
| 101 | + exit 99 |
| 102 | +fi |
| 103 | +echo "options.go generated from $OPTIONS_H." |
| 104 | + |
| 105 | +# #cgo directives are package-scoped, so each cgo-using package carries one |
| 106 | +# declaration. Replace the whole directive line to prevent drift. |
| 107 | +sed -i.bak \ |
| 108 | + -e "s|^// #cgo CFLAGS:.*|// #cgo CFLAGS: -g -Wall -I$PREFIX/include|" \ |
| 109 | + -e "s|^// #cgo LDFLAGS:.*|// #cgo LDFLAGS: -L$PREFIX/lib -lwolfssl -lm|" \ |
| 110 | + $CGO_FILES |
| 111 | +if [ $? -ne 0 ]; then |
| 112 | + echo "Failed to update cgo directives." |
| 113 | + exit 99 |
61 | 114 | fi |
62 | 115 |
|
| 116 | +SUCCESS=1 |
| 117 | + |
| 118 | +echo "cgo paths pointed at $PREFIX." |
| 119 | +echo "Success!" |
| 120 | + |
63 | 121 | exit 0 |
0 commit comments