Skip to content

Commit a64770b

Browse files
authored
Merge pull request #53 from sebastian-carpenter/build-changes
harden generateOptions.sh script
2 parents f10db1a + 7b24e5c commit a64770b

8 files changed

Lines changed: 136 additions & 51 deletions

File tree

.github/workflows/build.yml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,28 @@ jobs:
7575
go get golang.org/x/term
7676
go mod tidy
7777
78-
- name: Generate options.go (full config)
79-
run: ./generateOptions.sh /usr/local
78+
- name: Smoke test generateOptions.sh
79+
run: |
80+
set -e
81+
CGO_FILES="aes.go wolftls/conn.go wolfx509/certgen_wolfcrypt.go \
82+
examples/client/client-psk.go examples/server/server-psk.go"
83+
84+
# the path used must be under fake-prefix
85+
mkdir -p /tmp/fake-prefix/include/wolfssl
86+
cp /usr/local/include/wolfssl/options.h /tmp/fake-prefix/include/wolfssl/
87+
./generateOptions.sh /tmp/fake-prefix
88+
for f in options.go $CGO_FILES; do
89+
grep -q -- '-I/tmp/fake-prefix/include' "$f" || { echo "$f CFLAGS not repointed"; exit 1; }
90+
if grep -q '^// #cgo LDFLAGS:' "$f"; then
91+
grep -q -- '-L/tmp/fake-prefix/lib -lwolfssl -lm' "$f" || { echo "$f LDFLAGS not repointed"; exit 1; }
92+
fi
93+
done
94+
95+
# cleanup should function properly
96+
test -z "$(find . -name '*.bak')" || { echo "leftover .bak files"; exit 1; }
97+
98+
# generate with the actual install dir
99+
./generateOptions.sh /usr/local
80100
81101
- name: Build go-wolfssl library
82102
run: go build .

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# wolfSSL Golang Wrapper
22

3-
This repository contains a very light wrapper around wolfSSL for GO, a server/client example, and some example wolfCrypt applications.
3+
This repository contains a very light wrapper around wolfSSL for GO, a server/client example, and some example wolfCrypt applications.
44

55
## Usage
66

77
To use the wolfSSL go module, first build and install wolfSSL as shown below.
88

99
```
1010
git clone https://github.com/wolfSSL/wolfssl
11+
cd wolfssl
1112
./autogen.sh
1213
./configure
1314
make
1415
sudo make install
15-
```
16+
```
1617

1718
If you plan to use the `wolftls` subpackage and need concurrent `Read` and
1819
`Write` on a single `Conn` to run in parallel, add `--enable-writedup` to the
@@ -30,32 +31,40 @@ If you have a different path to your wolfSSL directory, run the script with the
3031
./generateOptions.sh ../files/wolfSSL
3132
```
3233

33-
If wolfSSL is installed (i.e. `make install`'d to a custom `--prefix=...` path), pass the install prefix
34-
instead. The script regenerates `options.go` AND can repoint the `#cgo`
35-
`CFLAGS` / `LDFLAGS` directives in every cgo-bearing file to that
36-
prefix in one step:
34+
If wolfSSL is installed (i.e. `make install`'d to a custom `--prefix=...` path),
35+
pass the install prefix instead:
3736
```
3837
./generateOptions.sh /usr/local # system install
3938
./generateOptions.sh /opt/wolfssl-fips # custom prefix
4039
```
4140

41+
Every invocation regenerates `options.go` and rewrites the `#cgo` `CFLAGS` /
42+
`LDFLAGS` directives in every cgo-bearing file, so the whole tree agrees on one
43+
wolfSSL. An install prefix points them at that prefix; a source root or no
44+
argument points them at `/usr/local`.
45+
46+
The prefix may contain only letters, digits and `/ _ . : + -`, since a `#cgo`
47+
directive cannot express a path containing spaces or shell metacharacters.
48+
Anything else is rejected. On any failure the script exits 99 and leaves the
49+
tree as it found it.
50+
4251
To install the wrapper module, run these commands:
4352
```
44-
go get -u github.com/wolfssl/go-wolfssl
53+
go get -u github.com/wolfssl/go-wolfssl
4554
go mod edit -replace github.com/wolfssl/go-wolfssl=<path to your go-wolfssl directory>
4655
```
4756

4857
## Running the TLS Server/Client example
4958

50-
The example `.go` files are located in the `client` and `server` directories.
59+
The example `.go` files are located in the `client` and `server` directories.
5160

52-
To build the server, run :
61+
To build the server, run:
5362
```
5463
cd examples/server
5564
go build server.go
5665
```
5766

58-
To build the client, run :
67+
To build the client, run:
5968
```
6069
cd examples/client
6170
go build client.go
@@ -65,8 +74,6 @@ go build client.go
6574

6675
See [examples/README.md](examples/README.md) for details on building/running the other examples.
6776

68-
**NOTE**: If you have wolfSSL installed in a non-standard location, edit the `CFLAGS` and `LDFLAGS` specifications in the `*.go` source files to correspond to your custom installation path.
69-
7077
## Support
7178

7279
For inquiries, suggestions and feedback please contact support@wolfssl.com.

aes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
package wolfSSL
2222

23-
// #cgo CFLAGS: -g -Wall -I/usr/include -I/usr/include/wolfssl -I/usr/local/include -I/usr/local/include/wolfssl
24-
// #cgo LDFLAGS: -L/usr/local/lib -lwolfssl
23+
// #cgo CFLAGS: -g -Wall -I/usr/local/include
24+
// #cgo LDFLAGS: -L/usr/local/lib -lwolfssl -lm
2525
// #include <wolfssl/options.h>
2626
// #include <wolfssl/wolfcrypt/aes.h>
2727
// #include <wolfssl/wolfcrypt/pwdbased.h>

examples/client/client-psk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
package main
2222

23-
//#cgo CFLAGS: -g -Wall -I/usr/include
23+
// #cgo CFLAGS: -g -Wall -I/usr/local/include
2424
//#include <string.h>
2525
//#include <wolfssl/options.h>
2626
//#include <wolfssl/ssl.h>

examples/server/server-psk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
package main
2222

23-
//#cgo CFLAGS: -g -Wall -I/usr/include
23+
// #cgo CFLAGS: -g -Wall -I/usr/local/include
2424
//#include <string.h>
2525
//#include <wolfssl/options.h>
2626
//#include <wolfssl/ssl.h>

generateOptions.sh

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,59 @@
22

33
# Usage: ./generateOptions.sh [<wolfssl-prefix-or-source-root>]
44
#
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.
1010

1111
OPTIONS_H="../wolfssl/wolfssl/options.h"
12+
DEFAULT_PREFIX="/usr/local"
1213
PREFIX=""
1314

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+
1439
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+
1652
echo "Path to wolfSSL was supplied."
1753

1854
if [ -f "$WOLFSSL_PATH/include/wolfssl/options.h" ]; then
1955
OPTIONS_H="$WOLFSSL_PATH/include/wolfssl/options.h"
2056
PREFIX="$WOLFSSL_PATH"
57+
echo "wolfSSL install given, linking/building with $PREFIX"
2158
elif [ -f "$WOLFSSL_PATH/wolfssl/options.h" ]; then
2259
OPTIONS_H="$WOLFSSL_PATH/wolfssl/options.h"
2360
else
@@ -32,32 +69,53 @@ else
3269
fi
3370
fi
3471

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+
3593
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
61114
fi
62115

116+
SUCCESS=1
117+
118+
echo "cgo paths pointed at $PREFIX."
119+
echo "Success!"
120+
63121
exit 0

wolftls/conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
package wolftls
2222

23-
// #cgo CFLAGS: -g -Wall -I/usr/include -I/usr/include/wolfssl -I/usr/local/include -I/usr/local/include/wolfssl
23+
// #cgo CFLAGS: -g -Wall -I/usr/local/include
2424
// #cgo LDFLAGS: -L/usr/local/lib -lwolfssl -lm
2525
import "C"
2626

wolfx509/certgen_wolfcrypt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
package wolfx509
2222

23-
// #cgo CFLAGS: -g -Wall -I/usr/include -I/usr/include/wolfssl -I/usr/local/include -I/usr/local/include/wolfssl
23+
// #cgo CFLAGS: -g -Wall -I/usr/local/include
2424
// #cgo LDFLAGS: -L/usr/local/lib -lwolfssl -lm
2525
// #include <stdlib.h>
2626
// #include <string.h>

0 commit comments

Comments
 (0)