%global pkgvers 0
%global scdate0 20230825
%global schash0 f7740b6a523ba29cfd56e922ce08d86c4bee301b
%global branch0 master
%global source0 https://github.com/chipsalliance/Surelog.git

%global sshort0 %{expand:%%{lua:print(('%{schash0}'):sub(1,8))}}

%global ext_antlr 0

Name:           surelog
Version:        %(curl -s https://raw.githubusercontent.com/chipsalliance/surelog/%{schash0}/CMakeLists.txt | grep "SURELOG VERSION" | awk '{print $3}' | sed 's|[^[:digit:].]||g')
Release:        %{scdate0}.%{pkgvers}.git%{sshort0}%{?dist}
Summary:        SystemVerilog 2017 Pre-processor, Parser, Elaborator, UHDM Compiler
License:        Apache

URL:            https://github.com/chipsalliance/Surelog

BuildRequires:  gcc-c++ git cmake pkgconfig swig python3 python3-devel
BuildRequires:  libuuid-devel uhdm-devel java-11-openjdk-headless gperftools-devel
BuildRequires:  gtest-devel capnproto capnproto-devel gmock-devel gtest-devel
%if 0%{?ext_antlr}
BuildRequires:  antlr4-cpp-runtime-devel
%endif

%global __cmake_in_source_build 1

%description
SystemVerilog 2017 Pre-processor, Parser, Elaborator, UHDM Compiler.
Provides IEEE Design/TB C/C++ VPI and Python AST API.

%package devel
Summary: Development library of %{name}
Requires: %{name} = %{version}-%{release}

%description devel
Development %{name} library


%prep
%setup -T -c -n %{name}
find %{_builddir} -name SPECPARTS -exec rm -rf {} +
git clone --depth 1 -n -b %{branch0} %{source0} .
git fetch --depth 1 origin %{schash0}
git reset --hard %{schash0}
%if ! 0%{?ext_antlr}
git submodule update --init --depth 1 third_party/antlr4
%endif
git log --format=fuller
# missing stds
sed -i -e '1i #include <iomanip>' src/roundtrip.cpp
sed -i -e '1i #include <iomanip>' src/ErrorReporting/Report.cpp
sed -i -e '1i #include <iomanip>' src/Common/PlatformFileSystem.cpp
sed -i -e '1i #include <cstdint>' include/Surelog/ErrorReporting/Waiver.h
sed -i -e '1i #include <cstdint>' src/Utils/StringUtils.cpp
# shared
sed -i -e 's|surelog STATIC|surelog SHARED|g' CMakeLists.txt
# ext libs
sed -i -e '/(third_party\/UHDM/d' CMakeLists.txt
sed -i -e '/(third_party\/googletest/d' CMakeLists.txt
sed -i -e '/GenerateSerializer uhdm/d' CMakeLists.txt
sed -i -e '/uhdm PRIVATE/d' CMakeLists.txt
sed -i -e 's|TARGETS capnp kj|TARGETS |' CMakeLists.txt
sed -i -e 's|TARGETS uhdm|TARGETS |' CMakeLists.txt
sed -i -e 's|>=2.0.7|>=2.0.0|' CMakeLists.txt
# ext antlr4
%if 0%{?ext_antlr}
sed -i -e '/GenerateParser antlr4_static/d' CMakeLists.txt
sed -i -e 's|surelog PUBLIC antlr4_static|surelog PUBLIC antlr4-runtime|' CMakeLists.txt
sed -i -e 's|TARGETS surelog antlr4_static|TARGETS surelog |' CMakeLists.txt
%endif

# fix
sed -i '1i #include <iomanip>' src/Common/FileSystem.cpp
sed -i '1i #include <cstring>' src/SourceCompile/SV3_1aPpTreeShapeListener.cpp
# sover
sed -i 's|surelog PROPERTIES|surelog PROPERTIES SOVERSION 1 VERSION 1.0|' CMakeLists.txt
%if 0%{?rhel}
# old c++17
sed -i 's|\[\[maybe_unused\]\]||g' include/Surelog/Design/ClockingBlock.h
sed -i 's|\[\[maybe_unused\]\]||g' include/Surelog/DesignCompile/CompileFileContent.h
%endif
# install path
sed -i 's|DESTINATION cmake|DESTINATION %{_libdir}/cmake|'  CMakeLists.txt
sed -i 's|${CMAKE_INSTALL_LIBDIR}/surelog|${CMAKE_INSTALL_LIBDIR}/|'  CMakeLists.txt
sed -i 's|DESTINATION lib/|DESTINATION ${CMAKE_INSTALL_PREFIX}/%{_lib}/|' CMakeLists.txt
sed -i 's|${CMAKE_INSTALL_PREFIX}/lib/surelog|${CMAKE_INSTALL_PREFIX}/%{_lib}/surelog|' CMakeLists.txt


%build
mkdir -p build
pushd build
%global optflags $(echo "%{optflags} -fPIC -I/usr/include/uhdm")
export LD_LIBRARY_PATH=%{_builddir}/%{name}/build/bin
%cmake .. -Wno-dev \
       -DCMAKE_SKIP_RPATH=ON \
       -DCMAKE_SKIP_BUILD_RPATH=ON \
       -DCMAKE_VERBOSE_MAKEFILE=OFF \
       -DCMAKE_INSTALL_BINDIR=%{_bindir} \
       -DCMAKE_BUILD_TYPE=RelWithDebInfo \
       -DCMAKE_INSTALL_LIBDIR=%{_libdir} \
       -DCMAKE_INSTALL_INCLUDEDIR=%{_includedir} \
       -DSURELOG_USE_HOST_UHDM=ON \
       -DUHDM_LIBRARY=%{_libdir}/libuhdm.so \
%if 0%{?ext_antlr}
       -DSURELOG_USE_HOST_ANTLR=ON \
%endif
%if 0%{?rhel}
       -DSURELOG_BUILD_TESTS=OFF \
       -DSURELOG_USE_HOST_GTEST=OFF \
%else
       -DSURELOG_USE_HOST_GTEST=ON \
%endif
       -DSURELOG_WITH_PYTHON=ON
make %{?_smp_mflags}
popd


%install
pushd build
make install DESTDIR=%{buildroot}
popd
# fix path
mv -f %{buildroot}/%{_libdir}/python %{buildroot}/%{_libdir}/%{name}
# clean spurious
find %{buildroot} -name '*.a' -exec rm -rf {} \;


%files
%doc README.md
%license LICENSE
%{_bindir}/*
%{_libdir}/*.so.*
%{_libdir}/%{name}/*
%{_datadir}/*

%files devel
%{_includedir}/*
%{_libdir}/*.so
%{_libdir}/cmake/*
%{_libdir}/pkgconfig/*


%changelog
* Sat Mar 12 2022 Balint Cristian <cristian.balint@gmail.com>
- github update releases