xref: /unit/auto/modules/nodejs (revision 1830:aa3396945980)
1
2# Copyright (C) NGINX, Inc.
3
4
5shift
6
7for nxt_option; do
8
9    case "$nxt_option" in
10        -*=*) value=`echo "$nxt_option" | sed -e 's/[-_a-zA-Z0-9]*=//'`     ;;
11           *) value=""                                                      ;;
12    esac
13
14    case "$nxt_option" in
15        --node=*)       NXT_NODE="$value"                                   ;;
16        --npm=*)        NXT_NPM="$value"                                    ;;
17        --node-gyp=*)   NXT_NODE_GYP="$value"                               ;;
18        --local=*)      NXT_NODE_LOCAL="$value"                             ;;
19
20        --help)
21            cat << END
22
23    --node=FILE        set node executable
24    --npm=FILE         set npm executable
25    --node-gyp=FILE    set node-gyp executable
26    --local=DIRECTORY  set directory path for local installation
27
28END
29            exit 0
30        ;;
31
32        *)
33            echo
34            echo $0: error: invalid Node option \"$nxt_option\"
35            echo
36            exit 1
37        ;;
38
39    esac
40
41done
42
43
44if [ ! -f $NXT_AUTOCONF_DATA ]; then
45   echo
46   echo Please run common $0 before configuring module \"$nxt_module\".
47   echo
48   exit 1
49fi
50
51. $NXT_AUTOCONF_DATA
52
53
54NXT_NODE=${NXT_NODE=node}
55NXT_NPM=${NXT_NPM=npm}
56NXT_NODE_GYP=${NXT_NODE_GYP=node-gyp}
57NXT_NODE_LOCAL=${NXT_NODE_LOCAL=}
58
59
60$echo "configuring nodejs module"
61$echo "configuring nodejs module..." >> $NXT_AUTOCONF_ERR
62
63$echo -n "checking for node ..."
64$echo "checking for node ..." >> $NXT_AUTOCONF_ERR
65
66if /bin/sh -c "${NXT_NODE} -v" >> $NXT_AUTOCONF_ERR 2>&1; then
67    $echo " found"
68
69    NXT_NODE_VERSION="`${NXT_NODE} -v`"
70    $echo " + node version ${NXT_NODE_VERSION}"
71
72else
73    $echo " not found"
74    $echo
75    $echo $0: error: no Node found.
76    $echo
77    exit 1;
78fi
79
80
81$echo -n "checking for npm ..."
82$echo "checking for npm ..." >> $NXT_AUTOCONF_ERR
83
84if /bin/sh -c "${NXT_NPM} -v" >> $NXT_AUTOCONF_ERR 2>&1; then
85    $echo " found"
86
87    NXT_NPM_VERSION="`${NXT_NPM} -v`"
88    $echo " + npm version ${NXT_NPM_VERSION}"
89
90else
91    $echo " not found"
92    $echo
93    $echo $0: error: no npm found.
94    $echo
95    exit 1;
96fi
97
98
99$echo -n "checking for node-gyp ..."
100$echo "checking for node-gyp ..." >> $NXT_AUTOCONF_ERR
101
102if /bin/sh -c "${NXT_NODE_GYP} -v" >> $NXT_AUTOCONF_ERR 2>&1; then
103    $echo " found"
104
105    NXT_NODE_GYP_VERSION="`${NXT_NODE_GYP} -v`"
106    $echo " + node-gyp version ${NXT_NODE_GYP_VERSION}"
107
108else
109    $echo " not found"
110    $echo
111    $echo $0: error: no node-gyp found.
112    $echo
113    exit 1;
114fi
115
116if grep ^$NXT_NODE: $NXT_MAKEFILE 2>&1 > /dev/null; then
117    $echo
118    $echo $0: error: duplicate \"$NXT_NODE\" package configured.
119    $echo
120    exit 1;
121fi
122
123
124NXT_NODE_TMP=${NXT_BUILD_DIR}/src/${NXT_NODE}/unit-http
125NXT_NODE_TMP_G=${NXT_BUILD_DIR}/src/${NXT_NODE}/unit-http-g
126NXT_NODE_TARBALL=${NXT_BUILD_DIR}/${NXT_NODE}-unit-http.tar.gz
127NXT_NODE_TARBALL_G=${NXT_BUILD_DIR}/${NXT_NODE}-unit-http-g.tar.gz
128NXT_NODE_VERSION_FILE=${NXT_BUILD_DIR}/src/${NXT_NODE}/version.h
129NXT_NODE_PACKAGE_FILE=${NXT_BUILD_DIR}/src/${NXT_NODE}/package.json
130NXT_NODE_EXPORTS="export UNIT_SRC_PATH=${PWD}/src \
131    && export UNIT_BUILD_PATH=${PWD}/${NXT_BUILD_DIR} \
132    && export UNIT_LIB_STATIC_PATH=${PWD}/${NXT_BUILD_DIR}/libunit.a"
133
134if [ -n "$NXT_NODE_LOCAL" ]; then
135    NXT_NODE_INSTALL=local-install
136else
137    NXT_NODE_INSTALL=install
138fi
139
140cat << END >> $NXT_MAKEFILE
141
142.PHONY: ${NXT_NODE}
143.PHONY: ${NXT_NODE}-copy
144.PHONY: ${NXT_NODE}-copy-g
145.PHONY: ${NXT_NODE}-install
146.PHONY: ${NXT_NODE}-uninstall
147.PHONY: ${NXT_NODE}-local-check
148.PHONY: ${NXT_NODE}-local-install
149.PHONY: ${NXT_NODE}-build
150.PHONY: ${NXT_NODE}-publish
151
152${NXT_NODE}: ${NXT_NODE}-copy $NXT_BUILD_DIR/$NXT_LIB_UNIT_STATIC
153	${NXT_NODE_EXPORTS} && \\
154	cd ${NXT_NODE_TMP} && ${NXT_NODE_GYP} configure build clean
155
156${NXT_NODE}-copy: ${NXT_NODE_VERSION_FILE} ${NXT_NODE_PACKAGE_FILE}
157	mkdir -p ${NXT_NODE_TMP}
158	cp -rp src/nodejs/unit-http/* ${NXT_NODE_TMP}/
159	cp -p ${NXT_NODE_VERSION_FILE} ${NXT_NODE_PACKAGE_FILE} ${NXT_NODE_TMP}/
160
161${NXT_NODE}-copy-g: ${NXT_NODE_VERSION_FILE} ${NXT_NODE_PACKAGE_FILE}
162	mkdir -p ${NXT_NODE_TMP_G}
163	cp -rp src/nodejs/unit-http/* ${NXT_NODE_TMP_G}/
164	cp -p ${NXT_NODE_VERSION_FILE} ${NXT_NODE_PACKAGE_FILE} ${NXT_NODE_TMP_G}/
165	mv ${NXT_NODE_TMP_G}/binding_pub.gyp ${NXT_NODE_TMP_G}/binding.gyp
166
167${NXT_NODE_VERSION_FILE}: ${NXT_VERSION_H}
168	mkdir -p ${NXT_BUILD_DIR}/src/${NXT_NODE}
169	$echo '#define NXT_NODE_VERNUM \$(NXT_VERNUM)' > $NXT_NODE_VERSION_FILE
170
171${NXT_NODE_PACKAGE_FILE}: ${NXT_VERSION_H} src/nodejs/unit-http/package.json
172	mkdir -p ${NXT_BUILD_DIR}/src/${NXT_NODE}
173	sed -e 's/"version"\s*:.*/"version": "\$(NXT_VERSION)",/' \
174		src/nodejs/unit-http/package.json > ${NXT_NODE_PACKAGE_FILE}
175
176${NXT_NODE_TARBALL}: ${NXT_NODE}-copy
177	tar -zcvf ${NXT_NODE_TARBALL} -C ${NXT_NODE_TMP} .
178
179${NXT_NODE_TARBALL_G}: ${NXT_NODE}-copy-g
180	tar -zcvf ${NXT_NODE_TARBALL_G} -C ${NXT_NODE_TMP_G} .
181
182
183install: ${NXT_NODE}-$NXT_NODE_INSTALL
184
185${NXT_NODE}-install: ${NXT_NODE_TARBALL_G} libunit-install
186	${NXT_NPM} install -g --unsafe-perm ${PWD}/${NXT_NODE_TARBALL_G}
187
188${NXT_NODE}-uninstall:
189	${NXT_NPM} uninstall -g unit-http
190
191${NXT_NODE}-local-check:
192	@test -n "\$(DESTDIR)$NXT_NODE_LOCAL" \\
193	|| (echo; \\
194	    echo "error: to make ${NXT_NODE}-local-install you need either"; \\
195	    echo "    to configure --local option"; \\
196	    echo "    or to set DESTDIR environment variable."; \\
197	    echo; \\
198	    exit 1)
199
200${NXT_NODE}-local-install: ${NXT_NODE_TARBALL} ${NXT_NODE}-local-check \
201    $NXT_BUILD_DIR/$NXT_LIB_UNIT_STATIC
202	${NXT_NODE_EXPORTS} && \\
203	mkdir -p \$(DESTDIR)${NXT_NODE_LOCAL} && \\
204	cd \$(DESTDIR)${NXT_NODE_LOCAL} && \\
205	${NXT_NPM} install ${PWD}/${NXT_NODE_TARBALL}
206
207
208${NXT_NODE}-build: ${NXT_NODE}
209
210${NXT_NODE}-publish: ${NXT_NODE}
211	cd ${NXT_NODE_TMP} && ${NXT_NPM} publish
212
213END
214