xref: /unit/auto/modules/go (revision 2062:ede07f84045b)
1
2# Copyright (C) Max Romanov
3# Copyright (C) NGINX, Inc.
4
5
6shift
7
8NXT_GO=go
9
10for nxt_option; do
11
12    case "$nxt_option" in
13        -*=*) value=`echo "$nxt_option" | sed -e 's/[-_a-zA-Z0-9]*=//'`     ;;
14           *) value="" ;;
15    esac
16
17    case "$nxt_option" in
18        --go=*)       NXT_GO="$value"                                        ;;
19        --go-path=*)  NXT_GO_PATH="$value"                                   ;;
20
21        --help)
22            cat << END
23
24    --go=NAME        set go executable
25    --go-path=PATH   set GOPATH variable to install package
26
27END
28            exit 0
29        ;;
30
31        *)
32            echo
33            echo $0: error: invalid Go option \"$nxt_option\"
34            echo
35            exit 1
36        ;;
37
38    esac
39
40done
41
42
43if [ ! -f $NXT_AUTOCONF_DATA ]; then
44   echo
45   echo Please run common $0 before configuring module \"$nxt_module\".
46   echo
47   exit 1
48fi
49
50. $NXT_AUTOCONF_DATA
51
52
53$echo "configuring Go package"
54$echo "configuring Go package ..." >> $NXT_AUTOCONF_ERR
55
56$echo -n "checking for Go ..."
57$echo "checking for Go ..." >> $NXT_AUTOCONF_ERR
58
59
60if /bin/sh -c "${NXT_GO} version" >> $NXT_AUTOCONF_ERR 2>&1; then
61    $echo " found"
62
63    NXT_GO_VERSION="`${NXT_GO} version`"
64    $echo " + ${NXT_GO_VERSION}"
65
66else
67    $echo
68    $echo $0: error: no Go found.
69    $echo
70    exit 1;
71fi
72
73
74NXT_GO_PATH=${NXT_GO_PATH=`${NXT_GO} env GOPATH`}
75NXT_GO_PATH=${NXT_GO_PATH:-${PWD}/${NXT_BUILD_DIR}/${NXT_GO}}
76
77NXT_GO_PKG=unit.nginx.org/go
78
79$echo " + Go package path: \"${NXT_GO_PATH}\""
80
81if grep ^$NXT_GO: $NXT_MAKEFILE 2>&1 > /dev/null; then
82    $echo
83    $echo $0: error: duplicate \"$NXT_GO\" package configured.
84    $echo
85    exit 1;
86fi
87
88NXT_GO_LDFLAGS=
89
90for o in ${CFLAGS} ${NXT_CC_OPT}; do
91    case "$o" in
92      -fsanitize* | -L* | -l*) NXT_GO_LDFLAGS="${NXT_GO_LDFLAGS} $o" ;;
93    esac
94done
95
96cat << END >> $NXT_MAKEFILE
97
98.PHONY: ${NXT_GO}
99.PHONY: ${NXT_GO}-install
100.PHONY: ${NXT_GO}-install-src
101.PHONY: ${NXT_GO}-uninstall
102
103GOPATH =	$NXT_GO_PATH
104GOOS =		`${NXT_GO} env GOOS`
105GOARCH =	`${NXT_GO} env GOARCH`
106
107NXT_GO_DST =	${NXT_GO_PATH%%:*}
108
109install: ${NXT_GO}-install
110
111${NXT_GO}:
112
113${NXT_GO}-install: ${NXT_GO}-install-src ${NXT_GO}-install-env
114	cd \$(DESTDIR)\$(NXT_GO_DST)/src/${NXT_GO_PKG} && \
115    GOPATH=\$(DESTDIR)\$(GOPATH) ${NXT_GO} build ${NXT_GO_PKG}
116
117${NXT_GO}-install-src:
118	install -d \$(DESTDIR)\$(NXT_GO_DST)/src/${NXT_GO_PKG}
119	install -p -m644 ./go/* \$(DESTDIR)\$(NXT_GO_DST)/src/${NXT_GO_PKG}/
120
121${NXT_GO}-install-env: \$(DESTDIR)\$(NXT_GO_DST)/src/${NXT_GO_PKG}/env.go \
122    ${NXT_VERSION_H} ${NXT_BUILD_DIR}/${NXT_LIB_UNIT_STATIC}
123
124\$(DESTDIR)\$(NXT_GO_DST)/src/${NXT_GO_PKG}/env.go:
125	install -d \$(DESTDIR)\$(NXT_GO_DST)/src/${NXT_GO_PKG}
126	$echo "package unit" > \$@
127	$echo "/*" >> \$@
128	$echo "#cgo CFLAGS: ${CFLAGS} ${NXT_CC_OPT}" >> \$@
129	$echo "#cgo CPPFLAGS: -I${PWD}/src -I${PWD}/${NXT_BUILD_DIR}" >> \$@
130	$echo "#cgo LDFLAGS: -L${PWD}/${NXT_BUILD_DIR} ${NXT_GO_LDFLAGS} ${NXT_LD_OPT}" >> \$@
131	$echo "*/" >> \$@
132	$echo 'import "C"' >> \$@
133
134
135uninstall: ${NXT_GO}-uninstall
136
137${NXT_GO}-uninstall:
138	rm -rf \$(DESTDIR)\$(NXT_GO_DST)/src/${NXT_GO_PKG}
139	rm -rf \$(DESTDIR)\$(NXT_GO_DST)/pkg/\$(GOOS)_\$(GOARCH)/${NXT_GO_PKG}
140
141END
142