nodejs-23.1.0.ebuild 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 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-20230521
  43. dev-db/sqlite:3
  44. >=dev-libs/libuv-1.48.0:=
  45. >=dev-libs/simdjson-3.9.4:=
  46. >=net-dns/c-ares-1.29.0
  47. >=net-libs/nghttp2-1.62.1:=
  48. >=net-libs/ngtcp2-1.6.0:=
  49. >=sys-libs/zlib-1.3
  50. corepack? ( !sys-apps/yarn )
  51. system-icu? ( >=dev-libs/icu-73.0:= )
  52. system-ssl? (
  53. >=net-libs/ngtcp2-1.3.0:=
  54. >=dev-libs/openssl-3.0.13:0=
  55. )
  56. !system-ssl? ( >=net-libs/ngtcp2-1.3.0:=[-gnutls] )
  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? ( sys-devel/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 BUILDTYPE="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. # Less verbose install output (stating the same as portage, basically)
  114. #sed -i -e "/print/d" tools/install.py || die
  115. # Proper libdir, hat tip @ryanpcmcquen https://github.com/iojs/io.js/issues/504
  116. local LIBDIR=$(get_libdir)
  117. sed -i -e "s|lib/|${LIBDIR}/|g" tools/install.py || die
  118. sed -i -e "s/'lib'/'${LIBDIR}'/" deps/npm/lib/npm.js || die
  119. # Avoid writing a depfile, not useful
  120. sed -i -e "/DEPFLAGS =/d" tools/gyp/pylib/gyp/generator/make.py || die
  121. # We need to disable mprotect on two files when it builds Bug 694100.
  122. use pax-kernel && PATCHES+=( "${FILESDIR}"/${PN}-20.6.0-paxmarking.patch )
  123. # https://github.com/nodejs/node/issues/51339
  124. use pointer-compression && PATCHES+=( "${FILESDIR}"/${PN}-23.0.0-fix-v8-external-code-space.patch )
  125. default
  126. local FP=(
  127. $(grep -l -r -e "-O3" $(find deps/openssl -name "*.gn*" -o -name "*gyp*"))
  128. common.gypi
  129. deps/llhttp/common.gypi
  130. deps/uv/common.gypi
  131. node.gypi
  132. )
  133. # -O3 removal breaks _FORITIFY_SOURCE
  134. local a1="-O3"
  135. local r1="-O2"
  136. local a2="-O2"
  137. local r2="-O3"
  138. local oflag="-O3"
  139. if is_flagq_last '-O0'; then
  140. oflag="-O0"
  141. elif is_flagq_last '-Og'; then
  142. oflag="-Og"
  143. elif is_flagq_last '-O1'; then
  144. oflag="-O1"
  145. elif is_flagq_last '-O2'; then
  146. oflag="-O2"
  147. elif is_flagq_last '-O3'; then
  148. oflag="-O3 "
  149. elif is_flagq_last '-O4'; then
  150. oflag="-O4 "
  151. elif is_flagq_last '-Ofast'; then
  152. oflag="-Ofast"
  153. elif is_flagq_last '-Os'; then
  154. oflag="-Os"
  155. elif is_flagq_last '-Oz'; then
  156. oflag="-Oz"
  157. fi
  158. sed -i -e "s|- O3|${oflag}|g" ${FP[@]} || die
  159. a1="${oflag}"
  160. a2="${oflag}"
  161. sed -i \
  162. -e "s|__OFLAGS_A1__|${a1}|g" \
  163. -e "s|__OFLAGS_R1__|${r1}|g" \
  164. -e "s|__OFLAGS_A2__|${a2}|g" \
  165. -e "s|__OFLAGS_R2__|${r2}|g" \
  166. tools/v8_gypfiles/toolchain.gypi \
  167. || die
  168. # debug builds. change install path, remove optimisations and override buildtype
  169. if use debug; then
  170. sed -i -e "s|out/Release/|out/Debug/|g" tools/install.py || die
  171. BUILDTYPE="Debug"
  172. fi
  173. }
  174. src_configure() {
  175. export ENINJA_BUILD_DIR="out/"$(usex debug "Debug" "Release")
  176. xdg_environment_reset
  177. # LTO compiler flags are handled by configure.py itself
  178. filter-lto
  179. # nodejs unconditionally links to libatomic #869992
  180. # specifically it requires __atomic_is_lock_free which
  181. # is not yet implemented by sys-libs/compiler-rt (see
  182. # https://reviews.llvm.org/D85044?id=287068), therefore
  183. # we depend on gcc and force using libgcc as the support lib
  184. tc-is-clang && append-ldflags "--rtlib=libgcc --unwindlib=libgcc"
  185. local myconf=(
  186. --ninja
  187. # ada is not packaged yet
  188. # https://github.com/ada-url/ada
  189. # --shared-ada
  190. --shared-brotli
  191. --shared-cares
  192. --shared-libuv
  193. --shared-nghttp2
  194. --shared-ngtcp2
  195. --shared-simdjson
  196. # sindutf is not packaged yet
  197. # https://github.com/simdutf/simdutf
  198. # --shared-simdutf
  199. --shared-sqlite
  200. --shared-zlib
  201. )
  202. use debug && myconf+=( --debug )
  203. use lto && myconf+=( --enable-lto )
  204. use mold && myconf+=( --with-moldlto )
  205. use lld && myconf+=( --with-thinlto )
  206. if use system-icu; then
  207. myconf+=( --with-intl=system-icu )
  208. elif use icu; then
  209. myconf+=( --with-intl=full-icu )
  210. else
  211. myconf+=( --with-intl=none )
  212. fi
  213. use corepack || myconf+=( --without-corepack )
  214. use inspector || myconf+=( --without-inspector )
  215. use npm || myconf+=( --without-npm )
  216. use snapshot || myconf+=( --without-node-snapshot )
  217. if use ssl; then
  218. use system-ssl && myconf+=( --shared-openssl --openssl-use-def-ca-store )
  219. else
  220. myconf+=( --without-ssl )
  221. fi
  222. use jit || myconf+=( --v8-lite-mode )
  223. if use jit && use debug && has_version "dev-debug/gdb" ; then
  224. myconf+=( --gdb )
  225. fi
  226. use pointer-compression && myconf+=( --experimental-enable-pointer-compression )
  227. if use kernel_linux && linux_chkconfig_present "TRANSPARENT_HUGEPAGE" ; then
  228. myconf+=( --v8-enable-hugepage )
  229. fi
  230. if ! use pointer-compression ; then
  231. ewarn "Disabing USE=pointer-compression will disable the V8 Sandbox."
  232. fi
  233. local myarch
  234. myarch="${ABI/amd64/x64}"
  235. myarch="${myarch/x86/ia32}"
  236. [[ "${ARCH}:${ABI}" =~ "loong:lp64" ]] && myarch="loong64"
  237. [[ "${ARCH}:${ABI}" =~ "riscv:lp64" ]] && myarch="riscv64"
  238. GYP_DEFINES="linux_use_gold_flags=0
  239. linux_use_bundled_binutils=0
  240. linux_use_bundled_gold=0" \
  241. "${EPYTHON}" configure.py \
  242. --prefix="${EPREFIX}/usr" \
  243. --dest-cpu="${myarch}" \
  244. ${myconf[@]} || die
  245. # Prevent double build on install.
  246. sed -i -e "s|^install: all|install: |g" "Makefile" || die
  247. }
  248. src_compile() {
  249. emake
  250. }
  251. src_install() {
  252. local LIBDIR="${ED}/usr/$(get_libdir)"
  253. default
  254. local REL_D_BASE="usr/$(get_libdir)"
  255. local D_BASE="/${REL_D_BASE}"
  256. local ED_BASE="${ED}/${REL_D_BASE}"
  257. ${EPYTHON} tools/install.py install \
  258. --dest-dir "${D}" \
  259. --prefix "${EPREFIX}/usr" \
  260. || die
  261. mv "${ED}/usr/bin/node" "${ED}/usr/bin/node${SLOT_MAJOR}" || die
  262. dosym "node${SLOT_MAJOR}" "/usr/bin/node"
  263. pax-mark -m "${ED}/usr/bin/node${SLOT_MAJOR}"
  264. # set up a symlink structure that node-gyp expects..
  265. local D_INCLUDE_BASE="/usr/include/node${SLOT_MAJOR}"
  266. dodir "${D_INCLUDE_BASE}/deps/"{"v8","uv"}
  267. dosym "." "${D_INCLUDE_BASE}/src"
  268. local var
  269. for var in deps/{uv,v8}/include; do
  270. dosym "../.." "${D_INCLUDE_BASE}/${var}"
  271. done
  272. # Avoid merge conflict
  273. mv "${ED}/usr/include/node/"* "${ED}${D_INCLUDE_BASE}" || die
  274. rm -rf "${ED}/usr/include/node" || die
  275. if use doc; then
  276. docinto html
  277. dodoc -r "${S}/doc/"*
  278. fi
  279. if ! use man ; then
  280. rm -rf "${ED}//usr/share/man/man1/node.1"* || die
  281. fi
  282. # Use tarball instead.
  283. rm -rf "${ED}/usr/$(get_libdir)/node_modules/npm"
  284. rm -rf "${ED}/usr/bin/npm"
  285. rm -rf "${ED}/usr/bin/npx"
  286. mv \
  287. "${ED}/usr/share/doc/node" \
  288. "${ED}/usr/share/doc/${PF}" \
  289. || die
  290. # Let eselect-nodejs handle switching corepack
  291. dodir "/usr/$(get_libdir)/corepack"
  292. mv \
  293. "${ED}/usr/$(get_libdir)/node_modules/corepack" \
  294. "${ED}/usr/$(get_libdir)/corepack/node${SLOT_MAJOR}" \
  295. || die
  296. rm -rf "${ED}/usr/bin/corepack"
  297. if use npm; then
  298. keepdir /etc/npm
  299. echo "NPM_CONFIG_GLOBALCONFIG=${EPREFIX}/etc/npm/npmrc" > "${T}"/50npm
  300. doenvd "${T}"/50npm
  301. # Install bash completion for `npm`
  302. local tmp_npm_completion_file="$(TMPDIR="${T}" mktemp -t npm.XXXXXXXXXX)"
  303. "${ED}/usr/bin/npm" completion > "${tmp_npm_completion_file}"
  304. newbashcomp "${tmp_npm_completion_file}" npm
  305. # Clean up
  306. rm -f "${LIBDIR}"/node_modules/npm/{.mailmap,.npmignore,Makefile}
  307. rm -rf "${LIBDIR}"/node_modules/npm/{doc,html,man}
  308. local find_exp="-or -name"
  309. local find_name=()
  310. for match in "AUTHORS*" "CHANGELOG*" "CONTRIBUT*" "README*" \
  311. ".travis.yml" ".eslint*" ".wercker.yml" ".npmignore" \
  312. "*.md" "*.markdown" "*.bat" "*.cmd"; do
  313. find_name+=( ${find_exp} "${match}" )
  314. done
  315. # Remove various development and/or inappropriate files and
  316. # useless docs of dependend packages.
  317. find "${LIBDIR}"/node_modules \
  318. \( -type d -name examples \) -or \( -type f \( \
  319. -iname "LICEN?E*" \
  320. "${find_name[@]}" \
  321. \) \) -exec rm -rf "{}" \;
  322. fi
  323. cp --remove-destination "${FILESDIR}/node-multiplexer-v${MULTIPLEXER_VER}" "${ED}/usr/bin/node" || die
  324. sed -e "s|__EPREFIX__|${EPREFIX}|g" -i "${ED}/usr/bin/node" || die
  325. fperms 0755 "/usr/bin/node" || die
  326. fowners -R root:root "/usr/bin/node" || die
  327. }
  328. src_test() {
  329. if has usersandbox ${FEATURES}; then
  330. rm -f "${S}/test/parallel/test-fs-mkdir.js"
  331. ewarn
  332. ewarn "You are emerging ${PN} with 'usersandbox' enabled. Excluding tests known"
  333. ewarn "to fail in this mode. For full test coverage, emerge =${CATEGORY}/${PF}"
  334. ewarn "with 'FEATURES=-usersandbox'."
  335. ewarn
  336. fi
  337. "out/${CONFIGURATION}/cctest" || die
  338. "${EPYTHON}" \
  339. "tools/test.py" \
  340. --mode="${CONFIGURATION,,}" \
  341. --flaky-tests="dontcare" \
  342. -J \
  343. message \
  344. parallel \
  345. sequential \
  346. || die
  347. }
  348. pkg_postinst() {
  349. if use npm; then
  350. ewarn "remember to run: source /etc/profile if you plan to use nodejs"
  351. ewarn " in your current shell"
  352. fi
  353. if has_version ">net-libs/nodejs-${PV}" ; then
  354. einfo "Found higher slots, manually change the headers with \`eselect nodejs\`."
  355. else
  356. eselect nodejs set "node${SLOT_MAJOR}"
  357. fi
  358. grep -q -F "NODE_VERSION" "${EROOT}/usr/bin/node" || die "Wrapper did not copy."
  359. einfo
  360. einfo "When compiling with nodejs multislot, you to switch via"
  361. einfo "\`eselect nodejs\` in order to compile against the headers matching the"
  362. einfo "corresponding SLOT. This means that you cannot compile with different"
  363. einfo "SLOTS simultaneously."
  364. einfo
  365. }