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