nodejs-23.6.0.ebuild 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. # Copyright 1999-2024 Gentoo Authors
  2. # Distributed under the terms of the GNU General Public License v2
  3. EAPI=8
  4. CONFIG_CHECK="~ADVISE_SYSCALLS"
  5. PYTHON_COMPAT=( python3_{11..13} )
  6. PYTHON_REQ_USE="threads(+)"
  7. MULTIPLEXER_VER="11"
  8. inherit bash-completion-r1 check-reqs flag-o-matic linux-info ninja-utils pax-utils python-any-r1 toolchain-funcs xdg-utils
  9. DESCRIPTION="A JavaScript runtime built on Chrome's V8 JavaScript engine"
  10. HOMEPAGE="https://nodejs.org/"
  11. LICENSE="Apache-1.1 Apache-2.0 BSD BSD-2 MIT"
  12. SLOT_MAJOR="$(ver_cut 1 ${PV})"
  13. SLOT="${SLOT_MAJOR}/$(ver_cut 1-2 ${PV})"
  14. if [[ ${PV} == *9999 ]]; then
  15. inherit git-r3
  16. EGIT_REPO_URI="https://github.com/nodejs/node"
  17. SLOT="23/0"
  18. else
  19. SRC_URI="https://nodejs.org/dist/v${PV}/node-v${PV}.tar.xz"
  20. KEYWORDS="~amd64 ~arm ~arm64 ~loong ~ppc64 ~riscv ~x86 ~amd64-linux ~x64-macos"
  21. S="${WORKDIR}/node-v${PV}"
  22. fi
  23. IUSE="+corepack cpu_flags_x86_sse2 debug doc +icu inspector +jit \
  24. lto lld man mold +npm pax-kernel -pointer-compression +snapshot +ssl \
  25. system-icu +system-ssl test"
  26. REQUIRED_USE="
  27. ^^ ( mold lld )
  28. corepack
  29. inspector? ( icu ssl )
  30. npm? ( corepack )
  31. system-icu? ( icu )
  32. system-ssl? ( ssl )
  33. x86? ( cpu_flags_x86_sse2 )
  34. "
  35. RESTRICT="
  36. !test? ( test )
  37. mirror
  38. "
  39. RDEPEND="
  40. !net-libs/nodejs:0
  41. >=app-arch/brotli-1.1.0
  42. >=app-eselect/eselect-nodejs-20250106
  43. dev-db/sqlite:3
  44. >=dev-libs/libuv-1.49.1:=
  45. >=dev-libs/simdjson-3.9.4:=
  46. >=net-dns/c-ares-1.34.3
  47. >=net-libs/nghttp2-1.62.1:=
  48. >=sys-libs/zlib-1.3
  49. corepack? ( sys-apps/yarn )
  50. system-icu? (
  51. >=dev-libs/icu-76.1:=
  52. )
  53. system-ssl? (
  54. >=net-libs/ngtcp2-1.3.0:=
  55. >=dev-libs/openssl-3.0.15:0=
  56. )
  57. "
  58. BDEPEND="${PYTHON_DEPS}
  59. app-alternatives/ninja
  60. sys-apps/coreutils
  61. virtual/pkgconfig
  62. test? ( net-misc/curl )
  63. pax-kernel? ( sys-apps/elfix )
  64. mold? ( sys-devel/mold )
  65. lld? ( llvm-core/lld )
  66. "
  67. DEPEND="${RDEPEND}"
  68. # These are measured on a loong machine with -ggdb on, and only checked
  69. # if debugging flags are present in CFLAGS.
  70. #
  71. # The final link consumed a little more than 7GiB alone, so 8GiB is the lower
  72. # limit for memory usage. Disk usage was 19.1GiB for the build directory and
  73. # 1.2GiB for the installed image, so we leave some room for architectures with
  74. # fatter binaries and set the disk requirement to 22GiB.
  75. CHECKREQS_MEMORY="8G"
  76. CHECKREQS_DISK_BUILD="22G"
  77. PATCHES=(
  78. "${FILESDIR}/${PN}-12.22.5-shared_c-ares_nameser_h.patch"
  79. "${FILESDIR}/${PN}-22.2.0-global-npm-config.patch"
  80. "${FILESDIR}/${PN}-22.2.0-lto-update.patch"
  81. "${FILESDIR}/${PN}-20.1.0-support-clang-pgo.patch"
  82. "${FILESDIR}/${PN}-19.3.0-v8-oflags.patch"
  83. )
  84. pkg_pretend() {
  85. if [[ ${MERGE_TYPE} != "binary" ]]; then
  86. if is-flagq "-g*" && ! is-flagq "-g*0" ; then
  87. einfo "Checking for sufficient disk space and memory to build ${PN} with debugging CFLAGS"
  88. check-reqs_pkg_pretend
  89. fi
  90. fi
  91. ( use x86 && ! use cpu_flags_x86_sse2 ) && \
  92. die "Your CPU doesn't support the required SSE2 instruction."
  93. }
  94. pkg_setup() {
  95. python-any-r1_pkg_setup
  96. linux-info_pkg_setup
  97. }
  98. is_flagq_last() {
  99. local flag="${1}"
  100. local olast=$(echo "${CFLAGS}" | grep -o -E -e "-O(0|g|1|z|s|2|3|4|fast)" | tr " " "\n" | tail -n 1)
  101. einfo "CFLAGS:\t${CFLAGS}"
  102. einfo "olast:\t${olast}"
  103. [[ "${flag}" == "${olast}" ]] && return 0
  104. return 1
  105. }
  106. src_prepare() {
  107. tc-export AR CC CXX PKG_CONFIG
  108. export V=1
  109. export CONFIGURATION="Release"
  110. # Fix compilation on Darwin
  111. # https://code.google.com/p/gyp/issues/detail?id=260
  112. sed -i -e "/append('-arch/d" tools/gyp/pylib/gyp/xcode_emulation.py || die
  113. # Proper libdir, hat tip @ryanpcmcquen https://github.com/iojs/io.js/issues/504
  114. local LIBDIR=$(get_libdir)
  115. sed -i -e "s|lib/|${LIBDIR}/|g" tools/install.py || die
  116. sed -i -e "s/'lib'/'${LIBDIR}'/" deps/npm/lib/npm.js || die
  117. # Avoid writing a depfile, not useful
  118. sed -i -e "/DEPFLAGS =/d" tools/gyp/pylib/gyp/generator/make.py || die
  119. # We need to disable mprotect on two files when it builds Bug 694100.
  120. use pax-kernel && PATCHES+=( "${FILESDIR}"/${PN}-20.6.0-paxmarking.patch )
  121. # https://github.com/nodejs/node/issues/51339
  122. use pointer-compression && PATCHES+=( "${FILESDIR}"/${PN}-23.0.0-fix-v8-external-code-space.patch )
  123. default
  124. local FP=(
  125. $(grep -l -r -e "-O3" $(find deps/openssl -name "*.gn*" -o -name "*gyp*"))
  126. common.gypi
  127. deps/llhttp/common.gypi
  128. deps/uv/common.gypi
  129. node.gypi
  130. )
  131. # -O3 removal breaks _FORITIFY_SOURCE
  132. local a1="-O3"
  133. local r1="-O2"
  134. local a2="-O2"
  135. local r2="-O3"
  136. local oflag="-O3"
  137. if is_flagq_last '-O0'; then
  138. oflag="-O0"
  139. elif is_flagq_last '-Og'; then
  140. oflag="-Og"
  141. elif is_flagq_last '-O1'; then
  142. oflag="-O1"
  143. elif is_flagq_last '-O2'; then
  144. oflag="-O2"
  145. elif is_flagq_last '-O3'; then
  146. oflag="-O3 "
  147. elif is_flagq_last '-O4'; then
  148. oflag="-O4 "
  149. elif is_flagq_last '-Ofast'; then
  150. oflag="-Ofast"
  151. elif is_flagq_last '-Os'; then
  152. oflag="-Os"
  153. elif is_flagq_last '-Oz'; then
  154. oflag="-Oz"
  155. fi
  156. sed -i -e "s|- O3|${oflag}|g" ${FP[@]} || die
  157. a1="${oflag}"
  158. a2="${oflag}"
  159. sed -i \
  160. -e "s|__OFLAGS_A1__|${a1}|g" \
  161. -e "s|__OFLAGS_R1__|${r1}|g" \
  162. -e "s|__OFLAGS_A2__|${a2}|g" \
  163. -e "s|__OFLAGS_R2__|${r2}|g" \
  164. tools/v8_gypfiles/toolchain.gypi \
  165. || die
  166. # debug builds. change install path, remove optimisations and override buildtype
  167. if use debug; then
  168. sed -i -e "s|out/Release/|out/Debug/|g" tools/install.py || die
  169. CONFIGURATION="Debug"
  170. fi
  171. }
  172. src_configure() {
  173. export ENINJA_BUILD_DIR="out/"$(usex debug "Debug" "Release")
  174. xdg_environment_reset
  175. # LTO compiler flags are handled by configure.py itself
  176. filter-lto
  177. # GCC with -ftree-vectorize miscompiles node's exception handling code
  178. # causing it to fail to catch exceptions sometimes
  179. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116057
  180. tc-is-gcc && append-cxxflags -fno-tree-vectorize
  181. # https://bugs.gentoo.org/931514
  182. use arm64 && append-flags $(test-flags-CXX -mbranch-protection=none)
  183. # nodejs unconditionally links to libatomic #869992
  184. # specifically it requires __atomic_is_lock_free which
  185. # is not yet implemented by sys-libs/compiler-rt (see
  186. # https://reviews.llvm.org/D85044?id=287068), therefore
  187. # we depend on gcc and force using libgcc as the support lib
  188. tc-is-clang && append-ldflags "--rtlib=libgcc --unwindlib=libgcc"
  189. local myconf=(
  190. --ninja
  191. # ada is not packaged yet
  192. # https://github.com/ada-url/ada
  193. # --shared-ada
  194. --shared-brotli
  195. --shared-cares
  196. --shared-libuv
  197. --shared-nghttp2
  198. --shared-ngtcp2
  199. --shared-simdjson
  200. # sindutf is not packaged yet
  201. # https://github.com/simdutf/simdutf
  202. # --shared-simdutf
  203. --shared-sqlite
  204. --shared-zlib
  205. )
  206. use debug && myconf+=( --debug )
  207. use lto && myconf+=( --enable-lto )
  208. use mold && myconf+=( --with-moldlto )
  209. use lld && myconf+=( --with-thinlto )
  210. if use system-icu; then
  211. myconf+=( --with-intl=system-icu )
  212. elif use icu; then
  213. myconf+=( --with-intl=full-icu )
  214. else
  215. myconf+=( --with-intl=none )
  216. fi
  217. use corepack || myconf+=( --without-corepack )
  218. use inspector || myconf+=( --without-inspector )
  219. use npm || myconf+=( --without-npm )
  220. use snapshot || myconf+=( --without-node-snapshot )
  221. if use ssl; then
  222. use system-ssl && myconf+=( --shared-openssl --openssl-use-def-ca-store )
  223. else
  224. myconf+=( --without-ssl )
  225. fi
  226. use jit || myconf+=( --v8-lite-mode )
  227. if use jit && use debug && has_version "dev-debug/gdb" ; then
  228. myconf+=( --gdb )
  229. fi
  230. use pointer-compression && myconf+=( --experimental-enable-pointer-compression )
  231. if use kernel_linux && linux_chkconfig_present "TRANSPARENT_HUGEPAGE" ; then
  232. myconf+=( --v8-enable-hugepage )
  233. fi
  234. if ! use pointer-compression ; then
  235. ewarn "Disabing USE=pointer-compression will disable the V8 Sandbox."
  236. fi
  237. local myarch
  238. myarch="${ABI/amd64/x64}"
  239. myarch="${myarch/x86/ia32}"
  240. [[ "${ARCH}:${ABI}" =~ "loong:lp64" ]] && myarch="loong64"
  241. [[ "${ARCH}:${ABI}" =~ "riscv:lp64" ]] && myarch="riscv64"
  242. GYP_DEFINES="linux_use_gold_flags=0
  243. linux_use_bundled_binutils=0
  244. linux_use_bundled_gold=0" \
  245. "${EPYTHON}" configure.py \
  246. --prefix="${EPREFIX}/usr" \
  247. --dest-cpu="${myarch}" \
  248. ${myconf[@]} || die
  249. # Prevent double build on install.
  250. sed -i -e "s|^install: all|install: |g" "Makefile" || die
  251. }
  252. src_compile() {
  253. export NINJA_ARGS=" $(get_NINJAOPTS)"
  254. emake -Onone
  255. }
  256. src_install() {
  257. local LIBDIR="${ED}/usr/$(get_libdir)"
  258. default
  259. local REL_D_BASE="usr/$(get_libdir)"
  260. local D_BASE="/${REL_D_BASE}"
  261. local ED_BASE="${ED}/${REL_D_BASE}"
  262. ${EPYTHON} tools/install.py install \
  263. --dest-dir "${D}" \
  264. --prefix "${EPREFIX}/usr" \
  265. || die
  266. mv "${ED}/usr/bin/node" "${ED}/usr/bin/node${SLOT_MAJOR}" || die
  267. dosym "node${SLOT_MAJOR}" "/usr/bin/node"
  268. pax-mark -m "${ED}/usr/bin/node${SLOT_MAJOR}"
  269. # set up a symlink structure that node-gyp expects..
  270. local D_INCLUDE_BASE="/usr/include/node${SLOT_MAJOR}"
  271. dodir "${D_INCLUDE_BASE}/deps/"{"v8","uv"}
  272. dosym "." "${D_INCLUDE_BASE}/src"
  273. local var
  274. for var in deps/{uv,v8}/include; do
  275. dosym "../.." "${D_INCLUDE_BASE}/${var}"
  276. done
  277. # Avoid merge conflict
  278. mv "${ED}/usr/include/node/"* "${ED}${D_INCLUDE_BASE}" || die
  279. rm -rf "${ED}/usr/include/node" || die
  280. if use doc; then
  281. docinto html
  282. dodoc -r "${S}/doc/"*
  283. fi
  284. if ! use man ; then
  285. rm -rf "${ED}//usr/share/man/man1/node.1"* || die
  286. fi
  287. # Use tarball instead.
  288. rm -rf "${ED}/usr/$(get_libdir)/node_modules/npm"
  289. rm -rf "${ED}/usr/bin/npm"
  290. rm -rf "${ED}/usr/bin/npx"
  291. mv \
  292. "${ED}/usr/share/doc/node" \
  293. "${ED}/usr/share/doc/${PF}" \
  294. || die
  295. # Let eselect-nodejs handle switching corepack
  296. dodir "/usr/$(get_libdir)/corepack"
  297. mv \
  298. "${ED}/usr/$(get_libdir)/node_modules/corepack" \
  299. "${ED}/usr/$(get_libdir)/corepack/node${SLOT_MAJOR}" \
  300. || die
  301. rm -rf "${ED}/usr/bin/corepack"
  302. if use npm; then
  303. keepdir /etc/npm
  304. echo "NPM_CONFIG_GLOBALCONFIG=${EPREFIX}/etc/npm/npmrc" > "${T}"/50npm
  305. #echo "PATH=${EPREFIX}/usr/$(get_libdir)/corepack/node${SLOT_MAJOR}" > "${T}"/50npm
  306. doenvd "${T}"/50npm
  307. dosym "/usr/$(get_libdir)/corepack/node${SLOT_MAJOR}/shims/npm" "/usr/bin/npm"
  308. sed -e "/^dirname /s|\"$(.+)\"|\"$(readlink -q \$0)\"|g" -i "${ED}/usr/$(get_libdir)/corepack/node${SLOT_MAJOR}/shims/npm" || die
  309. # Install bash completion for `npm`
  310. local tmp_npm_completion_file="$(TMPDIR="${T}" mktemp -t npm.XXXXXXXXXX)"
  311. "${ED}/usr/$(get_libdir)/corepack/node${SLOT_MAJOR}/shims/npm" completion > "${tmp_npm_completion_file}"
  312. newbashcomp "${tmp_npm_completion_file}" npm
  313. # Move man pages
  314. use man && doman "${LIBDIR}"/node_modules/npm/man/man{1,5,7}/*
  315. # Clean up
  316. rm -f "${LIBDIR}"/node_modules/npm/{.mailmap,.npmignore,Makefile}
  317. rm -rf "${LIBDIR}"/node_modules/npm/{doc,html,man}
  318. local find_exp="-or -name"
  319. local find_name=()
  320. for match in "AUTHORS*" "CHANGELOG*" "CONTRIBUT*" "README*" \
  321. ".travis.yml" ".eslint*" ".wercker.yml" ".npmignore" \
  322. "*.md" "*.markdown" "*.bat" "*.cmd"; do
  323. find_name+=( ${find_exp} "${match}" )
  324. done
  325. # Remove various development and/or inappropriate files and
  326. # useless docs of dependend packages.
  327. find "${LIBDIR}"/node_modules \
  328. \( -type d -name examples \) -or \( -type f \( \
  329. -iname "LICEN?E*" \
  330. "${find_name[@]}" \
  331. \) \) -exec rm -rf "{}" \;
  332. fi
  333. use doc && ( mv "${ED}"/usr/share/doc/node "${ED}"/usr/share/doc/${PF} || die )
  334. cp --remove-destination "${FILESDIR}/node-multiplexer-v${MULTIPLEXER_VER}" "${ED}/usr/bin/node" || die
  335. sed -e "s|__EPREFIX__|${EPREFIX}|g" -i "${ED}/usr/bin/node" || die
  336. fperms 0755 "/usr/bin/node" || die
  337. fowners -R root:root "/usr/bin/node" || die
  338. }
  339. src_test() {
  340. local drop_tests=(
  341. test/parallel/test-dns.js
  342. test/parallel/test-dns-resolveany-bad-ancount.js
  343. test/parallel/test-dns-setserver-when-querying.js
  344. test/parallel/test-fs-read-stream.js
  345. test/parallel/test-fs-utimes-y2K38.js
  346. test/parallel/test-fs-watch-recursive-add-file.js
  347. test/parallel/test-process-euid-egid.js
  348. test/parallel/test-process-get-builtin.mjs
  349. test/parallel/test-process-initgroups.js
  350. test/parallel/test-process-setgroups.js
  351. test/parallel/test-process-uid-gid.js
  352. test/parallel/test-release-npm.js
  353. test/parallel/test-socket-write-after-fin-error.js
  354. test/parallel/test-strace-openat-openssl.js
  355. test/sequential/test-util-debug.js
  356. )
  357. [[ "$(nice)" -gt 10 ]] && drop_tests+=( "test/parallel/test-os.js" )
  358. use inspector ||
  359. drop_tests+=(
  360. test/parallel/test-inspector-emit-protocol-event.js
  361. test/parallel/test-inspector-network-domain.js
  362. test/sequential/test-watch-mode.mjs
  363. )
  364. rm -f "${drop_tests[@]}" || die "disabling tests failed"
  365. if has usersandbox ${FEATURES}; then
  366. rm -f "${S}/test/parallel/test-fs-mkdir.js"
  367. ewarn
  368. ewarn "You are emerging ${PN} with 'usersandbox' enabled. Excluding tests known"
  369. ewarn "to fail in this mode. For full test coverage, emerge =${CATEGORY}/${PF}"
  370. ewarn "with 'FEATURES=-usersandbox'."
  371. ewarn
  372. fi
  373. "out/${CONFIGURATION}/cctest" || die
  374. "${EPYTHON}" \
  375. "tools/test.py" \
  376. --mode="${CONFIGURATION,,}" \
  377. --flaky-tests="dontcare" \
  378. -J \
  379. message \
  380. parallel \
  381. sequential \
  382. || die
  383. }
  384. pkg_postinst() {
  385. if use npm; then
  386. ewarn "remember to run: source /etc/profile if you plan to use nodejs"
  387. ewarn "in your current shell"
  388. fi
  389. if has_version ">net-libs/nodejs-${PV}" ; then
  390. einfo "Found higher slots, manually change the headers with \`eselect nodejs\`."
  391. else
  392. eselect nodejs set "node${SLOT_MAJOR}"
  393. fi
  394. grep -q -F "NODE_VERSION" "${EROOT}/usr/bin/node" || die "Wrapper did not copy."
  395. einfo
  396. einfo "When compiling with nodejs multislot, you to switch via"
  397. einfo "\`eselect nodejs\` in order to compile against the headers matching the"
  398. einfo "corresponding SLOT. This means that you cannot compile with different"
  399. einfo "SLOTS simultaneously."
  400. einfo
  401. }