%bcond_with clang

%global system_zlib 1
%global system_v8 0
%global system_openssl 1
%global system_http_parser 0
%global system_cares 1
%global system_libuv 0

%if "%{_dist_release}" < "vl7"
%global system_openssl 0
%endif

%define _unpackaged_files_terminate_build 1

Name:           nodejs
Version:        16.17.0
Release:        1%{?_dist_release}
Summary:        JavaScript runtime
Summary(ja):    JavaScript ランタイム
Group:          programming,servers
Vendor:         Project Vine
Distribution:   Vine Linux

License:        MIT and ASL 2.0 and ISC and BSD
URL:            https://nodejs.org/
Source0:        https://nodejs.org/dist/v%{version}/node-v%{version}.tar.gz
Source1:        macros.nodejs
Source2:        nodejs.attr
Source3:        nodejs.prov
Source4:        nodejs.req
Source5:        nodejs-symlink-deps
Source6:        nodejs-fixdep

# Exclusive archs must match v8
ExclusiveArch: %{ix86} x86_64 %{arm}

# V8 presently breaks ABI at least every x.y release while never bumping SONAME,
# so we need to be more explicit until spot fixes that
%global v8_ge 6.1.534.48
%global v8_lt 6.2

BuildRequires: libatomic

%if %{with clang}
BuildRequires: clang
BuildRequires: lld
%endif

%if %{system_v8}
BuildRequires: v8-devel >= %{v8_ge}
%endif
%if %{system_http_parser}
BuildRequires: http-parser-devel >= 2.0
%endif
%if %{system_libuv}
BuildRequires: libuv-devel
%endif
%if %{system_cares}
BuildRequires: c-ares-devel
%endif
%if %{system_zlib}
BuildRequires: zlib-devel
%endif

%if %{system_openssl}
# Node.js requires some features from openssl 1.0.1 for SPDY support
BuildRequires: openssl-devel >= 3.0.0
%endif

%if %{system_v8}
Requires: v8%{?isa} >= %{v8_ge}
Requires: v8%{?isa} < %{v8_lt}
%endif

#virtual provides for automatic depedency generation
Provides: nodejs(engine) = %{version}

#npm
Provides: npm = %{version}-%{release}

# Node.js currently has a conflict with the 'node' package in Fedora
# The ham-radio group has agreed to rename their binary for us, but
# in the meantime, we're setting an explicit Conflicts: here
Conflicts: node <= 0.3.2-11

%description
Node.js is a platform built on Chrome's JavaScript runtime
for easily building fast, scalable network applications.
Node.js uses an event-driven, non-blocking I/O model that
makes it lightweight and efficient, perfect for data-intensive
real-time applications that run across distributed devices.
#'


%package devel
Summary: JavaScript runtime - development headers
Summary(ja): JavaScript runtime - development headers
Group: programming
Requires: %{name} == %{version}-%{release}
%if %{system_libuv}
Requires: libuv-devel
%endif
%if %{system_http_parser}
Requires: http-parser-devel
%endif
%if %{system_cares}
Requires: c-ares-devel
%endif
%if %{system_zlib}
Requires: zlib-devel
%endif
%if %{system_openssl}
Requires: openssl-devel
%endif

%description devel
Development headers for the Node.js JavaScript runtime.


%package docs
Summary: Node.js API documentation
Summary(ja): Node.js API documentation
Group: documentation

%description docs
The API documentation for the Node.js JavaScript runtime.


%ifarch x86_64
%debug_package
%endif


%prep
%setup -q -n node-v%{version}

# Make sure nothing gets included from bundled deps:
# We only delete the source and header files, because
# the remaining build scripts are still used.

%if %{system_cares}
find deps/cares -name "*.c" -exec rm -f {} \;
find deps/cares -name "*.h" -exec rm -f {} \;
%endif

%if %{system_zlib}
find deps/zlib -name "*.c" -exec rm -f {} \;
find deps/zlib -name "*.h" -exec rm -f {} \;
%endif

%if %{system_v8}
find deps/v8 -name "*.c" -exec rm -f {} \;
find deps/v8 -name "*.h" -exec rm -f {} \;
%endif

%if %{system_http_parser}
find deps/http_parser -name "*.c" -exec rm -f {} \;
find deps/http_parser -name "*.h" -exec rm -f {} \;
%endif

%if %{system_openssl}
find deps/openssl -name "*.c" -exec rm -f {} \;
find deps/openssl -name "*.h" -exec rm -f {} \;
%endif

%if %{system_libuv}
find deps/uv -name "*.c" -exec rm -f {} \;
find deps/uv -name "*.h" -exec rm -f {} \;
%endif


%build
# build with debugging symbols and add defines from libuv (#892601)
%ifarch x86_64
%global debug_flag -g
%global libatomic_flag ""
%else
%global debug_flag -g0
%global libatomic_flag -latomic
%endif

export CFLAGS='%{optflags} %{debug_flag} -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DZLIB_CONST -fno-delete-null-pointer-checks'
export CXXFLAGS='%{optflags} %{debug_flag} -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DZLIB_CONST -fno-delete-null-pointer-checks'
export LDFLAGS="%{build_ldflags}"

%if %{with clang}
export CC=clang
export CXX=clang++
export LD=clang

export CFLAGS="$CFLAGS -Wno-ignored-optimization-argument"
export CXXFLAGS="$CXXFLAGS -Wno-ignored-optimization-argument"
export LDFLAGS="$LDFLAGS %{libatomic_flag} -fuse-ld=lld"
%endif

%set_build_flags

./configure \
	--prefix=%{_prefix} \
%if %{system_v8}
	--shared-v8 \
%endif
%if %{system_openssl}
	--shared-openssl \
%endif
%if %{system_zlib}
	--shared-zlib \
%endif
%if %{system_cares}
	--shared-cares \
%endif
%if %{system_libuv}
	--shared-libuv \
%endif
%if %{system_http_parser}
	--shared-http-parser \
%endif
	--without-dtrace \
	--openssl-use-def-ca-store

# Setting BUILDTYPE=Debug builds both release and debug binaries
make BUILDTYPE=Release %{?_smp_mflags}


%install
rm -rf %{buildroot}

./tools/install.py install %{buildroot} %{_prefix}

# and remove dtrace file again
rm -rf %{buildroot}/%{_prefix}/lib/dtrace

# remove systemtap files
rm -rf %{buildroot}/%{_datadir}/systemtap

# Set the binary permissions properly
chmod 0755 %{buildroot}/%{_bindir}/node

# Install the debug binary and set its permissions
#install -Dpm0755 out/Debug/node %{buildroot}/%{_bindir}/node_g

# own the sitelib directory
mkdir -p %{buildroot}%{_prefix}/lib/node_modules

# install rpm magic
install -Dpm0644 %{SOURCE1} %{buildroot}%{_sysconfdir}/rpm/macros.nodejs
install -Dpm0644 %{SOURCE2} %{buildroot}%{_rpmconfigdir}/fileattrs/nodejs.attr
install -pm0755 %{SOURCE3} %{buildroot}%{_rpmconfigdir}/nodejs.prov
install -pm0755 %{SOURCE4} %{buildroot}%{_rpmconfigdir}/nodejs.req
install -pm0755 %{SOURCE5} %{buildroot}%{_rpmconfigdir}/nodejs-symlink-deps
install -pm0755 %{SOURCE6} %{buildroot}%{_rpmconfigdir}/nodejs-fixdep

#install documentation
mkdir -p %{buildroot}%{_defaultdocdir}/%{name}-docs-%{version}/html
cp -pr doc/* %{buildroot}%{_defaultdocdir}/%{name}-docs-%{version}/html
rm -f %{_defaultdocdir}/%{name}-docs-%{version}/html/nodejs.1

# move files for debug
mkdir -p debug
cp -pr %{buildroot}%{_docdir}/node/* debug/
rm -rf %{buildroot}%{_docdir}/node

#install development headers
#FIXME: we probably don't really need *.h but node-gyp downloads the whole
#freaking source tree so I can't be sure ATM
mkdir -p %{buildroot}%{_includedir}/node
cp -p src/*.h %{buildroot}%{_includedir}/node

#node-gyp needs common.gypi too
mkdir -p %{buildroot}%{_datadir}/node
cp -p common.gypi %{buildroot}%{_datadir}/node


%files
%license LICENSE
%doc CHANGELOG.md README.md AUTHORS
%{_bindir}/corepack
%{_bindir}/node
%{_bindir}/npm
%{_bindir}/npx
%{_mandir}/man1/node.*
%dir %{_prefix}/lib/node_modules
%{_prefix}/lib/node_modules/corepack
%{_prefix}/lib/node_modules/npm

%files devel
%doc debug
#%{_bindir}/node_g
%{_includedir}/node
%{_datadir}/node
%{_sysconfdir}/rpm/macros.nodejs
%{_rpmconfigdir}/fileattrs/nodejs.attr
%{_rpmconfigdir}/nodejs*

%files docs
%doc %{_docdir}/%{name}-docs-%{version}
%license LICENSE


%changelog
* Tue Aug 16 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.17.0-1
- updated to 16.17.0.

* Fri Jul 08 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.16.0-1
- updated to 16.16.0.

* Thu Jun 02 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.15.1-1
- updated to 16.15.1.

* Wed May 04 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.15.0-1
- updated to 16.15.0.

* Fri Mar 18 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.14.2-1
- updated to 16.14.2.

* Wed Feb 09 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.14.0-1
- updated to 16.14.0.

* Fri Jan 14 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.13.2-1
- updated to 16.13.2.

* Thu Dec 16 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.13.1-1
- updated to 16.13.1.

* Mon Nov 22 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.13.0-1
- updated to 16.13.0.

* Wed Oct 27 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.12.0-1
- updated to 16.12.0.

* Wed Oct 20 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.11.1-1
- updated to 16.11.1.

* Thu Sep 30 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.10.0-1
- updated to 16.10.0.
- built with openssl-3.0.0.

* Thu Aug 26 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 14.17.5-1
- updated to 14.17.5.

* Fri Jul 16 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 14.17.3-1
- updated to 14.17.3.

* Wed Jun 09 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 14.17.0-1
- updated to 14.17.0.
- built with gcc.

* Wed Mar 10 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 14.16.0-1
- updated to 14.16.0.

* Wed Jan 13 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 14.15.4-1
- updated to 14.15.4.

* Mon Dec 14 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 14.15.1-1
- updated to 14.15.1.

* Thu Oct 01 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 12.18.4-1
- updated to 12.18.4.

* Fri Jul 17 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 12.18.2-1
- updated to 12.18.2.

* Fri Jun 12 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 12.18.0-1
- updated to 12.18.0.

* Thu Feb 13 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 12.16.0-1
- updated to 12.16.0.

* Mon Dec 23 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 12.14.0-1
- updated to 12.14.0.

* Sat Sep 28 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 10.16.3-1
- updated to 10.16.3.
- dropped Patch0 and 1: fixed in upstream.
- built with clang / lld.

* Tue Dec 26 2017 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 8.9.3-1
- updated to 8.9.3.
- built with bundled v8.
- built with system c-ares.
- added Patch0 and 1.

* Fri Sep 02 2016 Toshiaki Ara <ara_t@384.jp> 0.10.26-2
- rebuild with gcc-5.4.0

* Mon Mar 24 2014 Daisuke SUZUKI <daisuke@linux.or.jp> 0.10.26-1
- update to 0.10.26

* Thu May 02 2013 Daisuke SUZUKI <daisuke@linux.or.jp> 0.10.5-1
- update to 0.10.5
- fix deps

* Fri Mar 15 2013 Daisuke SUZUKI <daisuke@linux.or.jp> 0.10.0-2
- build with internal openssl on Vine Linux 6

* Wed Mar 13 2013 Daisuke SUZUKI <daisuke@linux.or.jp> 0.10.0-1
- update to 0.10.0

* Mon Mar 11 2013 Daisuke SUZUKI <daisuke@linux.or.jp> 0.9.5-1
- initial build for Vine Linux
- include npm

* Thu Feb 14 2013 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 0.9.5-11
- Rebuilt for https://fedoraproject.org/wiki/Fedora_19_Mass_Rebuild

* Tue Jan 22 2013 T.C. Hollingsworth <tchollingsworth@gmail.com> - 0.9.5-10
- minor bugfixes to RPM magic
  - nodejs-symlink-deps: don't create an empty node_modules dir when a module
    has no dependencies
  - nodes-fixdep: support adding deps when none exist
- Add the full set of headers usually bundled with node as deps to nodejs-devel.
  This way `npm install` for native modules that assume the stuff bundled with
  node exists will usually "just work".
-move RPM magic to nodejs-devel as requested by FPC

* Sat Jan 12 2013 T.C. Hollingsworth <tchollingsworth@gmail.com> - 0.9.5-9
- fix brown paper bag bug in requires generation script

* Thu Jan 10 2013 Stephen Gallagher <sgallagh@redhat.com> - 0.9.5-8
- Build debug binary and install it in the nodejs-devel subpackage

* Thu Jan 10 2013 T.C. Hollingsworth <tchollingsworth@gmail.com> - 0.9.5-7
- don't use make install since it rebuilds everything

* Thu Jan 10 2013 T.C. Hollingsworth <tchollingsworth@gmail.com> - 0.9.5-6
- add %%{?isa}, epoch to v8 deps

* Wed Jan 09 2013 T.C. Hollingsworth <tchollingsworth@gmail.com> - 0.9.5-5
- add defines to match libuv (#892601)
- make v8 dependency explicit (and thus more accurate)
- add -g to $C(XX)FLAGS instead of patching configure to add it
- don't write pointless 'npm(foo) > 0' deps

* Sat Jan 05 2013 T.C. Hollingsworth <tchollingsworth@gmail.com> - 0.9.5-4
- install development headers
- add nodejs_sitearch macro

* Wed Jan 02 2013 T.C. Hollingsworth <tchollingsworth@gmail.com> - 0.9.5-3
- make nodejs-symlink-deps actually work

* Tue Jan 01 2013 T.C. Hollingsworth <tchollingsworth@gmail.com> - 0.9.5-2
- provide nodejs-devel so modules can BuildRequire it (and be consistent
  with other interpreted languages in the distro)

* Tue Jan 01 2013 T.C. Hollingsworth <tchollingsworth@gmail.com> - 0.9.5-1
- new upstream release 0.9.5
- provide nodejs-devel for the moment
- fix minor bugs in RPM magic
- add nodejs_fixdep macro so packagers can easily adjust dependencies in
  package.json files

* Wed Dec 26 2012 T.C. Hollingsworth <tchollingsworth@gmail.com> - 0.9.4-1
- new upstream release 0.9.4
- system library patches are now upstream
- respect optflags
- include documentation in subpackage
- add RPM dependency generation and related magic
- guard libuv depedency so it always gets bumped when nodejs does
- add -devel subpackage with enough to make node-gyp happy

* Wed Dec 19 2012 Dan Horák <dan[at]danny.cz> - 0.9.3-8
- set exclusive arch list to match v8

* Tue Dec 18 2012 Stephen Gallagher <sgallagh@redhat.com> - 0.9.3-7
- Add remaining changes from code review
- Remove unnecessary BuildRequires on findutils
- Remove %%clean section

* Fri Dec 14 2012 Stephen Gallagher <sgallagh@redhat.com> - 0.9.3-6
- Fixes from code review
- Fix executable permissions
- Correct the License field
- Build debuginfo properly

* Thu Dec 13 2012 Stephen Gallagher <sgallagh@redhat.com> - 0.9.3-5
- Return back to using the standard binary name
- Temporarily adding a conflict against the ham radio node package until they
  complete an agreed rename of their binary.

* Wed Nov 28 2012 Stephen Gallagher <sgallagh@redhat.com> - 0.9.3-4
- Rename binary and manpage to nodejs

* Mon Nov 19 2012 Stephen Gallagher <sgallagh@redhat.com> - 0.9.3-3
- Update to latest upstream development release 0.9.3
- Include upstreamed patches to unbundle dependent libraries

* Tue Oct 23 2012 Adrian Alves <alvesadrian@fedoraproject.org>  0.8.12-1
- Fixes and Patches suggested by Matthias Runge

* Mon Apr 09 2012 Adrian Alves <alvesadrian@fedoraproject.org> 0.6.5
- First build.

