net-libs/nodejs: Call xdg_environment_reset() from xdg-utils.eclass
[gentoo.git] / net-libs / nodejs / nodejs-8.16.1.ebuild
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI=6
5 RESTRICT="test"
6
7 PYTHON_COMPAT=( python2_7 )
8 PYTHON_REQ_USE="threads"
9
10 inherit bash-completion-r1 eutils flag-o-matic pax-utils python-any-r1 toolchain-funcs xdg-utils
11
12 DESCRIPTION="A JavaScript runtime built on Chrome's V8 JavaScript engine"
13 HOMEPAGE="https://nodejs.org/"
14 SRC_URI="https://nodejs.org/dist/v${PV}/node-v${PV}.tar.xz"
15
16 LICENSE="Apache-1.1 Apache-2.0 BSD BSD-2 MIT"
17 SLOT="0"
18 KEYWORDS="~amd64 ~arm ~arm64 ~ppc ~ppc64 ~x86 ~amd64-linux ~x64-macos"
19 IUSE="cpu_flags_x86_sse2 debug doc icu inspector +npm +snapshot +ssl systemtap test"
20 REQUIRED_USE="
21         inspector? ( icu ssl )
22         npm? ( ssl )
23 "
24
25 RDEPEND="
26         >=dev-libs/libuv-1.23.2:=
27         >=net-dns/c-ares-1.10.1
28         >=net-libs/http-parser-2.9.0:=
29         >=net-libs/nghttp2-1.39.2
30         sys-libs/zlib
31         icu? ( >=dev-libs/icu-60.1:= )
32         ssl? ( =dev-libs/openssl-1.0.2*:0=[-bindist] )
33 "
34 DEPEND="
35         ${RDEPEND}
36         ${PYTHON_DEPS}
37         systemtap? ( dev-util/systemtap )
38         test? ( net-misc/curl )
39 "
40 PATCHES=(
41         "${FILESDIR}"/nodejs-10.3.0-global-npm-config.patch
42 )
43 S="${WORKDIR}/node-v${PV}"
44
45 pkg_pretend() {
46         (use x86 && ! use cpu_flags_x86_sse2) && \
47                 die "Your CPU doesn't support the required SSE2 instruction."
48
49         ( [[ ${MERGE_TYPE} != "binary" ]] && ! test-flag-CXX -std=c++11 ) && \
50                 die "Your compiler doesn't support C++11. Use GCC 4.8, Clang 3.3 or newer."
51 }
52
53 src_prepare() {
54         tc-export CC CXX PKG_CONFIG
55         export V=1
56         export BUILDTYPE=Release
57
58         # fix compilation on Darwin
59         # https://code.google.com/p/gyp/issues/detail?id=260
60         sed -i -e "/append('-arch/d" tools/gyp/pylib/gyp/xcode_emulation.py || die
61
62         # make sure we use python2.* while using gyp
63         sed -i -e "s/python/${EPYTHON}/" deps/npm/node_modules/node-gyp/gyp/gyp || die
64         sed -i -e "s/|| 'python2'/|| '${EPYTHON}'/" deps/npm/node_modules/node-gyp/lib/configure.js || die
65
66         # less verbose install output (stating the same as portage, basically)
67         sed -i -e "/print/d" tools/install.py || die
68
69         # proper libdir, hat tip @ryanpcmcquen https://github.com/iojs/io.js/issues/504
70         local LIBDIR=$(get_libdir)
71         sed -i \
72                 -e "s|lib/|${LIBDIR}/|g" \
73                 -e 's|share/doc/node/|share/doc/'"${PF}"'/|g' \
74                 tools/install.py || die
75
76         sed -i -e "s/'lib'/'${LIBDIR}'/" lib/module.js deps/npm/lib/npm.js || die
77
78         # Avoid writing a depfile, not useful
79         sed -i -e "/DEPFLAGS =/d" tools/gyp/pylib/gyp/generator/make.py || die
80
81         sed -i -e "/'-O3'/d" common.gypi deps/v8/gypfiles/toolchain.gypi || die
82
83         # Avoid a test that I've only been able to reproduce from emerge. It doesnt
84         # seem sandbox related either (invoking it from a sandbox works fine).
85         # The issue is that no stdin handle is openened when asked for one.
86         # It doesn't really belong upstream , so it'll just be removed until someone
87         # with more gentoo-knowledge than me (jbergstroem) figures it out.
88         rm test/parallel/test-stdout-close-unref.js || die
89
90         # debug builds. change install path, remove optimisations and override buildtype
91         if use debug; then
92                 sed -i -e "s|out/Release/|out/Debug/|g" tools/install.py || die
93                 BUILDTYPE=Debug
94         fi
95
96         default
97 }
98
99 src_configure() {
100         xdg_environment_reset
101
102         local myconf=( --shared-cares --shared-http-parser --shared-libuv --shared-nghttp2 --shared-zlib )
103         use debug && myconf+=( --debug )
104         use icu && myconf+=( --with-intl=system-icu ) || myconf+=( --with-intl=none )
105         use inspector || myconf+=( --without-inspector )
106         use npm || myconf+=( --without-npm )
107         use snapshot && myconf+=( --with-snapshot )
108         use ssl && myconf+=( --shared-openssl ) || myconf+=( --without-ssl )
109
110         local myarch=""
111         case ${ABI} in
112                 amd64) myarch="x64";;
113                 arm) myarch="arm";;
114                 arm64) myarch="arm64";;
115                 ppc64) myarch="ppc64";;
116                 x32) myarch="x32";;
117                 x86) myarch="ia32";;
118                 *) myarch="${ABI}";;
119         esac
120
121         GYP_DEFINES="linux_use_gold_flags=0
122                 linux_use_bundled_binutils=0
123                 linux_use_bundled_gold=0" \
124         "${PYTHON}" configure \
125                 --prefix="${EPREFIX}"/usr \
126                 --dest-cpu=${myarch} \
127                 $(use_with systemtap dtrace) \
128                 "${myconf[@]}" || die
129 }
130
131 src_compile() {
132         emake -C out mksnapshot
133         pax-mark m "out/${BUILDTYPE}/mksnapshot"
134         emake -C out
135 }
136
137 src_test() {
138         out/${BUILDTYPE}/cctest || die
139         "${PYTHON}" tools/test.py --mode=${BUILDTYPE,,} -J message parallel sequential || die
140 }
141
142 src_install() {
143         local LIBDIR="${ED}/usr/$(get_libdir)"
144         emake install DESTDIR="${D}"
145         pax-mark -m "${ED}"usr/bin/node
146
147         # set up a symlink structure that node-gyp expects..
148         dodir /usr/include/node/deps/{v8,uv}
149         dosym . /usr/include/node/src
150         for var in deps/{uv,v8}/include; do
151                 dosym ../.. /usr/include/node/${var}
152         done
153
154         if use doc; then
155                 # Patch docs to make them offline readable
156                 for i in `grep -rl 'fonts.googleapis.com' "${S}"/out/doc/api/*`; do
157                         sed -i '/fonts.googleapis.com/ d' $i;
158                 done
159                 # Install docs
160                 dodoc -r "${S}"/doc/*
161         fi
162
163         if use npm; then
164                 dodir /etc/npm
165
166                 # Install bash completion for `npm`
167                 # We need to temporarily replace default config path since
168                 # npm otherwise tries to write outside of the sandbox
169                 local npm_config="usr/$(get_libdir)/node_modules/npm/lib/config/core.js"
170                 sed -i -e "s|'/etc'|'${ED}/etc'|g" "${ED}/${npm_config}" || die
171                 local tmp_npm_completion_file="$(emktemp)"
172                 "${ED}/usr/bin/npm" completion > "${tmp_npm_completion_file}"
173                 newbashcomp "${tmp_npm_completion_file}" npm
174                 sed -i -e "s|'${ED}/etc'|'/etc'|g" "${ED}/${npm_config}" || die
175
176                 # Move man pages
177                 doman "${LIBDIR}"/node_modules/npm/man/man{1,5,7}/*
178
179                 # Clean up
180                 rm "${LIBDIR}"/node_modules/npm/{.mailmap,.npmignore,Makefile} || die
181                 rm -rf "${LIBDIR}"/node_modules/npm/{doc,html,man} || die
182
183                 local find_exp="-or -name"
184                 local find_name=()
185                 for match in "AUTHORS*" "CHANGELOG*" "CONTRIBUT*" "README*" \
186                         ".travis.yml" ".eslint*" ".wercker.yml" ".npmignore" \
187                         "*.md" "*.markdown" "*.bat" "*.cmd"; do
188                         find_name+=( ${find_exp} "${match}" )
189                 done
190
191                 # Remove various development and/or inappropriate files and
192                 # useless docs of dependend packages.
193                 find "${LIBDIR}"/node_modules \
194                         \( -type d -name examples \) -or \( -type f \( \
195                                 -iname "LICEN?E*" \
196                                 "${find_name[@]}" \
197                         \) \) -exec rm -rf "{}" \;
198         fi
199 }
200
201 pkg_postinst() {
202         einfo "The global npm config lives in /etc/npm. This deviates slightly"
203         einfo "from upstream which otherwise would have it live in /usr/etc/."
204         einfo ""
205         einfo "Protip: When using node-gyp to install native modules, you can"
206         einfo "avoid having to download extras by doing the following:"
207         einfo "$ node-gyp --nodedir /usr/include/node <command>"
208 }