File nodejs.spec of Package nodejs14

%define realname node
%define realver  14.16.0
%define srcext   tar.xz

# See: deps/npm/package.json
%define npm_ver  6.14.11

%define node_gen %(VER='%{realver}'; echo ${VER%%%%.*})
%define npm_gen  %(VER='%{npm_ver}'; echo ${VER%%%%.*})

%if 0%{?rhel} == 8
%define python3_pkg platform-python
%define python3     %{_libexecdir}/platform-python
%else
%define python3_pkg python3
%define python3     %{_bindir}/python3
%endif

%if 0%{?suse_version}
%bcond_without system_cares
%else
%bcond_with system_cares
%endif

%bcond_with system_openssl
%bcond_with system_uv
%bcond_with system_brotli
%bcond_with system_icu

# turn off the generation of debuginfo rpm  (RH9) ??
%global debug_package %{nil}

%if 0%{?suse_version} < 1500
%define cxx11abi -D_GLIBCXX_USE_CXX11_ABI=0
%endif

# Common info
Name:          nodejs%{node_gen}
Version:       %{realver}
%if 0%{?rhel} == 7
%define        epoch 1
Epoch:         %{epoch}
%endif
Release:       %{?extraver:0.}1%{?dist}
License:       MIT
Group:         Development/Languages/NodeJS
URL:           https://nodejs.org
Summary:       JavaScript runtime built on Chrome's V8 JavaScript engine

# Install-time parameters
%if 0%{?suse_version}
Requires:      update-alternatives
%else
Requires:      chkconfig
%endif
Provides:      nodejs = %{?epoch:%{epoch}:}%{version}-%{release}
Obsoletes:     nodejs < %{?epoch:%{epoch}:}%{version}-%{release}
Provides:      nodejs(engine) = %{?epoch:%{epoch}:}%{version}-%{release}
Provides:      nodejs(abi) = %{node_gen}.0
# openSUSE/SLES specific
Provides:      nodejs-common = %{?epoch:%{epoch}:}%{version}-%{release}
Obsoletes:     nodejs-common < %{?epoch:%{epoch}:}%{version}-%{release}

# Build-time parameters
BuildRequires: xz
BuildRequires: coreutils
BuildRequires: %{python3_pkg}%{?suse_version:-base} %{python3_pkg}-setuptools
BuildRequires: pkg-config
BuildRequires: gcc-c++
### BuildRequires: http-parser-devel
%if %{with system_uv}
BuildRequires: libuv-devel
%endif
BuildRequires: nghttp2-devel >= 1.34.0
%if %{with system_openssl}
BuildRequires: openssl-devel
%endif
BuildRequires: zlib-devel
%if %{with system_brotli}
BuildRequires: %{?suse_version:lib}brotli-devel
%endif
%if %{with system_cares}
BuildRequires: c-ares-devel
%endif
%if %{with system_icu}
BuildRequires: pkgconfig(icu-i18n) >= 57.0
%endif
%if 0%{?suse_version}
BuildRequires: fdupes
%endif
BuildRoot:     %{_tmppath}/%{name}-root
Source:        https://nodejs.org/dist/v%{realver}/node-v%{realver}.%{srcext}
# Script to set MANPATH
Source11:      npm-manpath.sh

%description
As an asynchronous event-driven JavaScript runtime, Node.js is designed to build
scalable network applications.

Node.js is similar in design to, and influenced by, systems like Ruby's Event
Machine and Python's Twisted. Node.js takes the event model a bit further.
It presents an event loop as a runtime construct instead of as a library.

%package devel
Group:         Development/Languages/NodeJS
Summary:       Node.js development stuff
Requires:      %{name} = %{?epoch:%{epoch}:}%{version}-%{release}
%if 0%{?suse_version}
Requires:      update-alternatives
%else
Requires:      chkconfig
%endif
Provides:      nodejs-devel = %{?epoch:%{epoch}:}%{version}-%{release}
BuildArch:     noarch

%description devel
Node.js development headers and libraries.

%package -n npm
Version:       %{npm_ver}
License:       Artistic-2.0
Group:         Development/Languages/NodeJS
Summary:       Node.js package manager
BuildArch:     noarch
Requires:      nodejs(engine)
Provides:      nodejs-npm = %{npm_ver}-%{release}
Provides:      npm(npm) = %{npm_ver}-%{release}
Provides:      npm%{node_gen} = %{?epoch:%{epoch}:}%{realver}-%{release}
Obsoletes:     npm%{node_gen} < %{?epoch:%{epoch}:}%{realver}-%{release}
# openSUSE/SLES specific
Conflicts:     nodejs-common < %{?epoch:%{epoch}:}%{realver}-%{release}

%description -n npm
npm is the package manager for the Node JavaScript platform. It puts modules in
place so that node can find them, and manages dependency conflicts intelligently.

It is extremely configurable to support a wide variety of use cases. Most
commonly, it is used to publish, discover, install, and develop node programs.

# Preparation step (unpackung and patching if necessary)
%prep
%setup -q -n %{realname}-v%{realver}%{?extraver}
# Change node_modules directory
%{__sed} -i 's|lib/node_modules|share/node_modules|' tools/install.py

%build
%if 0%{?rhel} && 0%{?rhel} <= 6
alias realpath='readlink -e'
%endif
_CFLAGS='%{optflags} %{?gcc_lto}'
_CXXFLAGS='%{optflags} %{?gcc_lto} %{?cxx11abi}'
_LDFLAGS='-Wl,--as-needed -Wl,--strip-all -Wl,--no-undefined -Wl,-z,now -Wl,-z,relro %{?gcc_lto}'
export CFLAGS="$_CFLAGS"
export CXXFLAGS="$_CXXFLAGS"
export LDFLAGS="$_LDFLAGS"
%{python3} configure.py \
 --prefix=%{_prefix} \
 --enable-lto \
 --openssl-use-def-ca-store \
 --verbose \
 \
%if %{with system_uv}
 --shared-libuv \
%endif
 --shared-nghttp2 \
%if %{with system_openssl}
 --shared-openssl \
%endif
 --shared-zlib \
%if %{with system_brotli}
 --shared-brotli \
%endif
%if %{with system_cares}
 --shared-cares \
%endif
%if %{with system_icu}
 --with-intl=system-icu \
%endif
 --download=none
%{__make} %{?_smp_mflags} \
 CFLAGS="$_CFLAGS" \
 CXXFLAGS="$_CXXFLAGS" \
 LDFLAGS="$_LDFLAGS"

%install
%{__make} install DESTDIR=%{buildroot}
# Prepare alternatives symlinks
%{__mv} %{buildroot}%{_bindir}/node              %{buildroot}%{_bindir}/node%{node_gen}
%{__mv} %{buildroot}%{_mandir}/man1/node.1       %{buildroot}%{_mandir}/man1/node%{node_gen}.1
%{__mv} %{buildroot}%{_includedir}/node          %{buildroot}%{_includedir}/node%{node_gen}
%{__mv} %{buildroot}%{_datadir}/doc/node         %{buildroot}%{_datadir}/node%{node_gen}
%{__mv} %{buildroot}%{_datadir}/node_modules/npm %{buildroot}%{_datadir}/node_modules/npm%{?npm_gen}
%{__ln_s} %{_datadir}/node_modules/npm%{?npm_gen}/bin/npm-cli.js %{buildroot}%{_bindir}/npm%{?npm_gen}
%{__ln_s} %{_datadir}/node_modules/npm%{?npm_gen}/bin/npx-cli.js %{buildroot}%{_bindir}/npx%{?npm_gen}
%{__install} -d %{buildroot}%{_sysconfdir}/alternatives
touch %{buildroot}%{_sysconfdir}/alternatives/node
touch %{buildroot}%{_sysconfdir}/alternatives/node.1.gz
touch %{buildroot}%{_sysconfdir}/alternatives/node-includes
touch %{buildroot}%{_sysconfdir}/alternatives/npm
touch %{buildroot}%{_sysconfdir}/alternatives/npx
touch %{buildroot}%{_sysconfdir}/alternatives/npm-mod
%{__ln_s} %{_sysconfdir}/alternatives/node          %{buildroot}%{_bindir}/node
%{__ln_s} %{_sysconfdir}/alternatives/node.1.gz     %{buildroot}%{_mandir}/man1/node.1.gz
%{__ln_s} %{_sysconfdir}/alternatives/node-includes %{buildroot}%{_includedir}/node
%{__ln_s} -fn %{_sysconfdir}/alternatives/npm           %{buildroot}%{_bindir}/npm
%{__ln_s} -fn %{_sysconfdir}/alternatives/npx           %{buildroot}%{_bindir}/npx
%{__ln_s} %{_sysconfdir}/alternatives/npm-mod       %{buildroot}%{_datadir}/node_modules/npm
# Install script setting MANPATH for npm manpages
%{__install} -d %{buildroot}%{_sysconfdir}/profile.d
%{__install} -m0644 %{S:11} %{buildroot}%{_sysconfdir}/profile.d/npm-manpath.sh
(
  cd %{buildroot}%{_datadir}/node_modules/npm%{?npm_gen}/
  # Fix shebang
  find . -type f -executable \
  | while read -r f; do
    %{__sed} -i "$f" \
      -e '1 s|^#!.* node$|#!%{_bindir}/node|' \
      -e '1 s|^#!.* python$|#!%{_bindir}/python|' \
      -e '1 s|^#!.* bash$|#!/bin/bash|' \
      -e '1 s|^#!.* sh$|#!/bin/sh|'
  done
  # Remove non-needed files
  %{__rm} -f \
    node_modules/copy-concurrently/README.md~ \
    node_modules/form-data/README.md.bak \
    node_modules/move-concurrently/README.md~
)
# Config directory
%{__install} -d -m0755 %{buildroot}%{_sysconfdir}/nodejs
# Fix shebang
find %{buildroot}%{_datadir}/node_modules/npm%{?npm_gen}/node_modules/node-gyp/gyp/ -name \*.py \
  | while read FILE; do
      %{__sed} -ri '1 s|#!.+$|#!%{python3}|' $FILE
    done
%{__sed} -ri '1 s|#!.+$|#!%{python3}|' %{buildroot}%{_datadir}/node_modules/npm%{?npm_gen}/node_modules/node-gyp/gyp/samples/samples
# Handle file duplicates
%if 0%{?suse_version}
%fdupes %{buildroot}%{_datadir}
%endif

%clean
[ "%{buildroot}" != "/" ] && %{__rm} -rf %{buildroot}

%files
%defattr(-,root,root)
%doc AUTHORS CHANGELOG.md GOVERNANCE.md LICENSE README.md SECURITY.md
%{_bindir}/node%{node_gen}
%{_bindir}/node
%{_mandir}/man1/node.1.gz
%{_datadir}/node%{node_gen}/
%dir %{_sysconfdir}/nodejs
%dir %{_datadir}/node_modules
%doc %{_mandir}/man1/node%{node_gen}.1.gz
%ghost %{_sysconfdir}/alternatives/node
%ghost %{_sysconfdir}/alternatives/node.1.gz
%exclude %{_datadir}/systemtap/tapset/node.stp

%files devel
%defattr(-,root,root)
%doc AUTHORS CHANGELOG.md GOVERNANCE.md LICENSE README.md SECURITY.md
%doc CODE_OF_CONDUCT.md CONTRIBUTING.md doc/api doc/guides
%{_includedir}/node%{node_gen}
%{_includedir}/node
%ghost %{_sysconfdir}/alternatives/node-includes

%files -n npm
%defattr(-,root,root)
%doc deps/npm/AUTHORS deps/npm/CHANGELOG.md deps/npm/LICENSE deps/npm/README.md
%config %{_sysconfdir}/profile.d/npm-manpath.sh
%{_bindir}/npm
%{_bindir}/npx
%{_datadir}/node_modules/npm
%{_bindir}/npm%{?npm_gen}
%{_bindir}/npx%{?npm_gen}
%{_datadir}/node_modules/npm%{?npm_gen}/
%ghost %{_sysconfdir}/alternatives/npm
%ghost %{_sysconfdir}/alternatives/npx
%ghost %{_sysconfdir}/alternatives/npm-mod

%post
update-alternatives \
    --install %{_bindir}/node           node         %{_bindir}/node%{node_gen} %{node_gen} \
    --slave   %{_mandir}/man1/node.1.gz node.1.gz    %{_mandir}/man1/node%{node_gen}.1.gz

%postun
if [ $1 -eq 0 ] ; then
    update-alternatives --remove node %{_bindir}/node%{node_gen}
fi

%post devel
update-alternatives \
    --install %{_includedir}/node node-includes %{_includedir}/node%{node_gen} %{node_gen}

%postun devel
if [ $1 -eq 0 ] ; then
    update-alternatives --remove node-includes %{_includedir}/node%{node_gen}
fi

%post -n npm
update-alternatives \
    --install %{_bindir}/npm               npm     %{_bindir}/npm%{?npm_gen} %{npm_gen} \
    --slave   %{_bindir}/npx               npx     %{_bindir}/npx%{?npm_gen} \
    --slave   %{_datadir}/node_modules/npm npm-mod %{_datadir}/node_modules/npm%{?npm_gen}

%postun -n npm
if [ $1 -eq 0 ] ; then
    update-alternatives --remove npm %{_datadir}/node_modules/npm%{?npm_gen}
fi

%changelog
openSUSE Build Service is sponsored by