File clang.spec of Package clang

%define realname clang
%define realver  17.0.1
%define srcext   tar.xz

%define gen     %(awk -F. '{print $1}' <<< %{realver})
%define mainver %(awk -F. '{printf "%i.%i", $1, $2}' <<< %{realver})

%bcond_with    doc
%bcond_without ninja

%if 0%{?suse_version}
%define pkg_name_lib   libclang%{gen}
%else
%define pkg_name_lib   clang-libs
%endif

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

# Common info
Name:          %{realname}
Version:       %{realver}
Release:       wiz%{?extraver:0.}1%{?dist}
License:       BSD-3-Clause
Group:         Development/Languages/C and C++
URL:           http://clang.llvm.org/
Summary:       C language family frontend for LLVM

# Install-time parameters
Provides:      c_compiler c++_compiler
Provides:      llvm-clang = %{version}-%{release}
# Perl-written tools deps
Requires:      perl%{?suse_version:-base}

# Build-time parameters
BuildRequires: xz pkg-config cmake >= 3.13.4
BuildRequires: gcc-c++
BuildRequires: python3
%if %{with ninja}
BuildRequires: ninja
%endif
BuildRequires: llvm-devel = %{version}
BuildRequires: libffi-devel
BuildRequires: libxml2-devel >= 2.5.3
# Documentation
%if %{with doc}
BuildRequires: doxygen graphviz graphviz-gd
BuildRequires: fontconfig ghostscript-fonts%{?suse_version:-std}
BuildRequires: python3-Sphinx
%if 0%{?suse_version}
BuildRequires: fdupes
%endif
%endif
BuildRoot:     %{_tmppath}/%{name}-root
Source:        https://github.com/llvm/llvm-project/releases/download/llvmorg-%{realver}/%{realname}-%{realver}%{?extraver}.src.%{srcext}
# CMake modules
Source15:      https://github.com/llvm/llvm-project/raw/llvmorg-%{realver}/cmake/Modules/CMakePolicy.cmake
Source16:      https://github.com/llvm/llvm-project/raw/llvmorg-%{realver}/cmake/Modules/GetClangResourceDir.cmake

%description
The Clang project provides a language front-end and tooling infrastructure for
languages in the C language family (C, C++, Objective C/C++, OpenCL, CUDA, and
RenderScript) for the LLVM project.

%package -n %{pkg_name_lib}
Group:         System/Libraries
Summary:       Shared libraries from C language family frontend for LLVM
Provides:      clang-libs = %{version}-%{release}
Provides:      libclang = %{version}-%{release}
Provides:      llvm-clang-libs = %{version}-%{release}
%if 0%{?rhel} && %{__isa_bits} == 64
Provides:      lib%{realname}.so.%{gen}()(64bit)
Provides:      lib%{realname}.so.%{gen}(LLVM_%{gen})(64bit)
%endif

%description -n %{pkg_name_lib}
Shared libraries from C language family frontend for LLVM.

%package devel
Group:         Development/Languages/C and C++
Summary:       Development files for %{name}
Requires:      %{name} = %{version}-%{release}
Requires:      %{pkg_name_lib} = %{version}-%{release}
Requires:      llvm-devel

%description devel
Development files for %{name}

%package devel-static
Group:         Development/Languages/C and C++
Summary:       clang static libraries
Requires:      %{name} = %{version}-%{release}
Requires:      %{pkg_name_lib} = %{version}-%{release}
Requires:      llvm-devel
Requires:      clang-devel = %{version}-%{release}

%description devel-static
clang static libraries

# Preparation step (unpackung and patching if necessary)
%prep
%setup -q -n %{realname}-%{realver}%{?extraver}.src
%{__install} -m0644 %{S:15} cmake/modules/
%{__install} -m0644 %{S:16} cmake/modules/
# Don't use upper level CMake modules
%{__sed} -ri \
 -e '/^\s*set\(LLVM_COMMON_CMAKE_UTILS/ s|\.\./||' \
 -e 's|/Modules|/modules|' \
 CMakeLists.txt
# Set correct libexec dir for scan-build
LIBEXEC='%{_libexecdir}'
LIBEXEC_LIB="${LIBEXEC##*/}"
%{__sed} -i \
 -e "s|DESTINATION libexec|DESTINATION $LIBEXEC_LIB|g" \
 tools/scan-build/CMakeLists.txt tools/scan-build-py/CMakeLists.txt
%{__sed} -ri \
 -e '/^my \$Cmd(CXX)?/ s|\$AbsRealBin/../libexec|%{_libexecdir}|' \
 tools/scan-build/bin/scan-build

%build
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$(pwd -P)/build/%{_lib}
_CFLAGS="%{optflags}"
_LDFLAGS="-Wl,--as-needed -Wl,--strip-all -Wl,--no-undefined -Wl,-z,defs -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro"
cmake -S . -B build \
%if %{with ninja}
 -G Ninja \
%endif
 -DCMAKE_INSTALL_PREFIX=%{_prefix} \
 -DCMAKE_INSTALL_LIBEXECDIR=%{_libexecdir} \
 -DCMAKE_BUILD_TYPE=Release \
 -DCMAKE_CXX_FLAGS_RELEASE="$_CFLAGS" \
 -DCMAKE_C_FLAGS_RELEASE="$_CFLAGS" \
 -DCMAKE_EXE_LINKER_FLAGS_RELEASE="$_LDFLAGS" \
 -DCMAKE_MODULE_LINKER_FLAGS_RELEASE="$_LDFLAGS" \
 -DCMAKE_SHARED_LINKER_FLAGS_RELEASE="$_LDFLAGS" \
 -DCMAKE_SKIP_RPATH=YES \
 -DCMAKE_VERBOSE_MAKEFILE=YES \
 \
 -DBUILD_SHARED_LIBS=YES \
 \
 -DCLANG_BUILT_STANDALONE=YES \
 \
 -DCLANG_INCLUDE_TESTS=NO \
 -DLLVM_INCLUDE_TESTS=NO \
%if %{with doc}
 -DCLANG_INCLUDE_DOCS=YES \
 -DLLVM_ENABLE_SPHINX=YES \
%else
 -DCLANG_INCLUDE_DOCS=NO \
%endif
 \
 -DLLVM_ENABLE_LTO=YES
%if %{with ninja}
ninja -v -C build %{?_smp_mflags}
%else
%{__make} -C build %{?_smp_mflags}
%endif

%install
%if %{with ninja}
DESTDIR=%{buildroot} ninja -v -C build install
%else
%{__make} -C build install DESTDIR=%{buildroot}
%endif
%__ln_s -fn %{gen} %{buildroot}%{_libdir}/clang/%{version}
%if %{with doc}
# Handle file duplicates
%if 0%{?suse_version}
%fdupes %{buildroot}%{_defaultdocdir}
%endif
%endif
# Explicitly set python interpreter
%{__sed} -ri '1 s|#!.+python$|#!%{_bindir}/python3|' \
  %{buildroot}%{_bindir}/analyze-build \
  %{buildroot}%{_bindir}/git-clang-format \
  %{buildroot}%{_bindir}/hmaptool \
  %{buildroot}%{_bindir}/intercept-build \
  %{buildroot}%{_bindir}/scan-build-py \
  %{buildroot}%{_bindir}/scan-view \
  %{buildroot}%{_datadir}/clang/clang-format-diff.py \
  %{buildroot}%{_datadir}/scan-view/startfile.py \
  %{buildroot}%{_libexecdir}/analyze-c++ \
  %{buildroot}%{_libexecdir}/intercept-c++ \
  %{buildroot}%{_libexecdir}/analyze-cc \
  %{buildroot}%{_libexecdir}/intercept-cc
%{__sed} -ri '1 s|#!.+perl$|#!%{_bindir}/perl|' \
  %{buildroot}%{_libexecdir}/c++-analyzer \
  %{buildroot}%{_libexecdir}/ccc-analyzer \
  %{buildroot}%{_bindir}/scan-build

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

%files
%defattr(-,root,root)
%license LICENSE.TXT
%doc NOTES.txt README.txt
%{_bindir}/*
%{_libexecdir}/*
%{_libdir}/libear/
%{_libdir}/libscanbuild/
%{_datadir}/clang/
%{_datadir}/scan-build/
%{_datadir}/scan-view/
%if %{with doc}
%doc %attr(0644,root,root) %{_mandir}/man1/*
%else
%doc %attr(0644,root,root) %{_mandir}/man1/scan-build.1*
%endif

# Clang shared libraries
%files -n %{pkg_name_lib}
%defattr(-,root,root)
%{_libdir}/libclang*.so.%{gen}*

# Development stuff
%files devel
%defattr(-,root,root)
%{_libdir}/libclang*.so
%{_includedir}/clang/
%{_includedir}/clang-c/
%{_libdir}/clang/
%dir %{_libdir}/cmake/clang
%dir %{_libdir}/cmake
%{_libdir}/cmake/clang/*.cmake

# Static libraries
%files devel-static
%defattr(-,root,root)
### %{_libdir}/libclang*.a

%post   -n %{pkg_name_lib} -p /sbin/ldconfig
%postun -n %{pkg_name_lib} -p /sbin/ldconfig

%changelog
openSUSE Build Service is sponsored by