%bcond_with test

%define build_compat32 %{?_with_compat32:1}%{!?_with_compat32:0}

%define ver_suffix %(echo "%{version}" | cut -d . -f 1)

%define _unpackaged_files_terminate_build 1

# Documentation install path
%global llvmdocdir() %{_docdir}/%1-%{version}

%define _unpackaged_files_terminate_build 1

Summary:        A C language family front-end for LLVM
Summary(ja):    LLVM の C 言語フロントエンド
Name:           clang
Version:        18.1.5
Release:        1%{?_dist_release}
Group:          programming
Vendor:         Project Vine
Distribution:   Vine Linux

License:        NCSA
URL:            https://llvm.org/
Source0:        https://github.com/llvm/llvm-project/releases/download/llvmorg-%{version}/clang-%{version}.src.tar.xz
Source1:        https://github.com/llvm/llvm-project/releases/download/llvmorg-%{version}/clang-tools-extra-%{version}.src.tar.xz

# multilib fixes
Source11:   clang-config.h

# Patches for clang
Patch1:     0001-PATCH-clang-Make-funwind-tables-the-default-on-all-a.patch
Patch2:     0003-PATCH-clang-Don-t-install-static-libraries.patch

# Workaround a bug in ORC on ppc64le.
# More info is available here: https://reviews.llvm.org/D159115#4641826
Patch5:     0001-Workaround-a-bug-in-ORC-on-ppc64le.patch

# RHEL specific patches
# Avoid unwanted dependency on python-myst-parser
Patch101:  0009-disable-myst-parser.patch

%if %{without compat_build}
# Patches for clang-tools-extra
# See https://reviews.llvm.org/D120301
Patch201:   0001-clang-tools-extra-Make-test-dependency-on-LLVMHello-.patch
%endif


BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root

BuildRequires:  cmake
BuildRequires:  ninja
BuildRequires:  llvm-devel = %{version}
BuildRequires:  llvm-static = %{version}
BuildRequires:  libatomic
BuildRequires:  libxml2-devel
BuildRequires:  perl
BuildRequires:  ncurses-devel
BuildRequires:  python3
BuildRequires:  python3-devel

Requires:       llvm = %{version}-%{release}
# clang requires gcc, clang++ requires libstdc++-devel
Requires:       libstdc++-devel

# LLVM is not supported on PPC64
# http://llvm.org/bugs/show_bug.cgi?id=3729
ExcludeArch:    ppc64

%description
clang: noun
    1. A loud, resonant, metallic sound.
    2. The strident call of a crane or goose.
    3. C-language family front-end toolkit.

The goal of the Clang project is to create a new C, C++, Objective C
and Objective C++ front-end for the LLVM compiler. Its tools are built
as libraries and designed to be loosely-coupled and extensible.


%Package libs
Summary:        Runtime library for clang
Summary(ja):    Clang のランタイムライブラリ
Group:          system

%description libs
Runtime library for clang.

%description libs -l ja
Clang のランタイムライブラリです．


%package devel
Summary:        Header files for clang
Summary(ja):    Clang のヘッダファイル
Group:          programming
Requires:       clang = %{version}-%{release}

%description devel
This package contains header files for the Clang compiler.

%description devel -l ja
Clang コンパイラのヘッダファイル集です．


%package analyzer
Summary:        A source code analysis framework
Summary(ja):    ソースコード分析フレームワーク
License:        NCSA
Group:          programming
Requires:       clang = %{version}-%{release}
# not picked up automatically since files are currently not instaled
# in standard Python hierarchies yet
Requires:       python3

%description analyzer
The Clang Static Analyzer consists of both a source code analysis
framework and a standalone tool that finds bugs in C and Objective-C
programs. The standalone tool is invoked from the command-line, and is
intended to run in tandem with a build of a project or code base.


%package -n python3-clang
Summary: Python3 bindings for clang
Group:          programming
Requires: %{name}-libs%{?_isa} = %{version}-%{release}
Requires: python3

%description -n python3-clang
Python3 bindings for clang


%package tools-extra
Summary:	Extra tools for clang
Group:          programming
Requires:	%{name}-libs%{?_isa} = %{version}-%{release}

%description tools-extra
A set of extra tools built using Clang's tooling API.


%debug_package


%prep
%setup -T -q -b 1 -n clang-tools-extra-%{version}.src
%autopatch -m200 -p2

# failing test case
rm test/clang-tidy/checkers/altera/struct-pack-align.cpp

pathfix%{python3_version}.py -i %{__python3} -pn \
	clang-tidy/tool/ \
	clang-include-fixer/find-all-symbols/tool/run-find-all-symbols.py

%setup -q -n clang-%{version}.src
%autopatch -M200 -p2

# failing test case
rm test/CodeGen/profile-filter.c

pathfix%{python3_version}.py -i %{__python3} -pn \
	tools/clang-format/ \
	tools/clang-format/git-clang-format \
	utils/hmaptool/hmaptool \
	tools/scan-view/bin/scan-view \
	tools/scan-view/share/Reporter.py \
	tools/scan-view/share/startfile.py \
	tools/scan-build-py/bin/* \
	tools/scan-build-py/libexec/*

sed -i -e 's/"i686-linux-gnu",/"i686-linux-gnu","i686-vine-linux","i686-vine-linux-gnu",/' lib/Driver/ToolChains/Gnu.cpp
sed -i -e 's/"x86_64-linux-gnu",/"x86_64-linux-gnu","x86_64-vine-linux","x86_64-vine-linux-gnu",/' lib/Driver/ToolChains/Gnu.cpp

#sed -i -e 's/"ld"/"ld.gold"/' include/clang/Driver/ToolChain.h


%build
# lto builds with gcc 11 fail while running the lit tests.
%define _lto_cflags %{nil}

%ifarch s390 s390x %{arm} %ix86
# Decrease debuginfo verbosity to reduce memory consumption during final library linking
%global optflags %(echo %{optflags} | sed 's/-g /-g1 /')
%endif

%cmake \
    -GNinja \
    -DCLANG_DEFAULT_PIE_ON_LINUX=OFF \
    -DLLVM_PARALLEL_LINK_JOBS=1 \
    -DLLVM_LINK_LLVM_DYLIB:BOOL=ON \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DPYTHON_EXECUTABLE=%{__python3} \
    -DCMAKE_SKIP_RPATH:BOOL=ON \
%ifarch %ix86
    -DCMAKE_C_FLAGS_RELWITHDEBINFO="%{optflags} -DNDEBUG" \
    -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="%{optflags} -DNDEBUG" \
%endif
    -DLLVM_CMAKE_DIR=%{_libdir}/cmake/llvm \
%if 0%{?__isa_bits} == 64
    -DLLVM_LIBDIR_SUFFIX=64 \
%else
    -DLLVM_LIBDIR_SUFFIX= \
%endif
    -DCLANG_INCLUDE_TESTS:BOOL=OFF \
    -DLLVM_BUILD_UTILS:BOOL=ON \
    -DLLVM_EXTERNAL_CLANG_TOOLS_EXTRA_SOURCE_DIR=../clang-tools-extra-%{version}.src \
    -DLLVM_MAIN_SRC_DIR=%{_datadir}/llvm/src \
    \
    -DLLVM_TABLEGEN_EXE:FILEPATH=%{_bindir}/llvm-tblgen \
    -DLLVM_COMMON_CMAKE_UTILS=%{_datadir}/llvm/cmake \
    -DCLANG_ENABLE_ARCMT:BOOL=ON \
    -DCLANG_ENABLE_STATIC_ANALYZER:BOOL=ON \
    -DCLANG_INCLUDE_DOCS:BOOL=ON \
    -DCLANG_PLUGIN_SUPPORT:BOOL=OFF \
    -DLLVM_ENABLE_PLUGINS:BOOL=OFF \
    -DENABLE_LINKER_BUILD_ID:BOOL=ON \
    -DLLVM_ENABLE_EH=ON \
    -DLLVM_ENABLE_RTTI=ON \
    -DLLVM_BUILD_DOCS=ON \
    -DLLVM_ENABLE_SPHINX=OFF \
    -DLLVM_INCLUDE_TESTS:BOOL=OFF \
    -DCLANG_LINK_CLANG_DYLIB=ON \
    \
    -DCLANG_LINK_CLANG_DYLIB=ON \
    -DCLANG_BUILD_EXAMPLES:BOOL=OFF \
    -DBUILD_SHARED_LIBS=OFF \
    -DCLANG_REPOSITORY_STRING="Vine Linux %{version}-%{release}" \
    -DCLANG_DEFAULT_UNWINDLIB=libgcc

%cmake_build


%install
rm -rf %{buildroot}

%cmake_install

# remove static libraries
rm -fv %{buildroot}%{_libdir}/libclang*.a

# Add clang++-{version} symlink
ln -s clang++ %{buildroot}%{_bindir}/clang++-%{ver_suffix}

# install clang python bindings
mkdir -p %{buildroot}%{python3_sitelib}/clang/
install -p -m644 bindings/python/clang/* %{buildroot}%{python3_sitelib}/clang/

# install scanbuild-py to python sitelib.
mv %{buildroot}%{_libdir}/{libear,libscanbuild} %{buildroot}%{python3_sitelib}

mv -v %{buildroot}%{_includedir}/clang/Config/config{,-%{__isa_bits}}.h
install -m 0644 %{SOURCE11} %{buildroot}%{_includedir}/clang/Config/config.h

# TODO: Package html docs
rm -Rvf %{buildroot}%{_docdir}/clang/html
rm -Rvf %{buildroot}%{_datadir}/clang/clang-doc-default-stylesheet.css
rm -Rvf %{buildroot}%{_datadir}/clang/index.js

# remove git integration
rm -vf %{buildroot}%{_bindir}/git-clang-format

# remove editor integrations (bbedit, sublime, emacs, vim)
rm -vf %{buildroot}%{_datadir}/clang/clang-format-bbedit.applescript
rm -vf %{buildroot}%{_datadir}/clang/clang-format-sublime.py*
rm -vf %{buildroot}%{_datadir}/clang/clang-format.el
rm -vf %{buildroot}%{_datadir}/clang/clang-format.py*
# remove diff reformatter
rm -vf %{buildroot}%{_datadir}/clang/clang-format-diff.py*

rm -f %{buildroot}%{_datadir}/clang/bash-autocomplete.sh
rm -f %{buildroot}%{_datadir}/clang/clang-rename.el
rm -f %{buildroot}%{_datadir}/clang/clang-rename.py

rm -f %{buildroot}%{_datadir}/clang/clang-include-fixer.el
rm -f %{buildroot}%{_datadir}/clang/clang-include-fixer.py

# Remove clang-tidy headers.  We don't ship the libraries for these.
rm -Rvf %{buildroot}%{_includedir}/clang-tidy/


%if %{with test}
%check
%ctest check-clang
%endif


%clean
rm -rf %{buildroot}


%files
%defattr(-,root,root,-)
#doc %{llvmdocdir clang}/
%{_bindir}/clang
%{_bindir}/clang++
%{_bindir}/clang-%{ver_suffix}
%{_bindir}/clang++-%{ver_suffix}
%{_bindir}/clang-cl
%{_bindir}/clang-cpp

%files libs
%defattr(-,root,root,-)
%{_libdir}/clang
%{_libdir}/libclang.so.*
%{_libdir}/libclang-cpp.so.*

%files devel
%defattr(-,root,root,-)
#doc %{llvmdocdir clang-devel}/
%{_libdir}/libclang.so
%{_libdir}/libclang-cpp.so
%{_includedir}/clang
%{_includedir}/clang-c
%dir %{_datadir}/clang/
%{_libdir}/cmake/*

%files analyzer
%defattr(-,root,root,-)
%{_bindir}/analyze-build
%{_bindir}/intercept-build
%{_bindir}/scan-build
%{_bindir}/scan-build-py
%{_bindir}/scan-view
%{_libexecdir}/ccc-analyzer
%{_libexecdir}/c++-analyzer
%{_libexecdir}/analyze-c++
%{_libexecdir}/analyze-cc
%{_libexecdir}/intercept-c++
%{_libexecdir}/intercept-cc
%{_datadir}/scan-view
%{_datadir}/scan-build
%{_mandir}/man1/scan-build.1.*
%{python3_sitelib}/libear
%{python3_sitelib}/libscanbuild

%files -n python3-clang
%{python3_sitelib}/clang/

%files tools-extra
%{_bindir}/amdgpu-arch
%{_bindir}/c-index-test
%{_bindir}/clang*
%exclude %{_bindir}/clang
%exclude %{_bindir}/clang++
%exclude %{_bindir}/clang-%{ver_suffix}
%exclude %{_bindir}/clang++-%{ver_suffix}
%exclude %{_bindir}/clang-cl
%exclude %{_bindir}/clang-cpp
%{_bindir}/diagtool
%{_bindir}/find-all-symbols
%{_bindir}/hmaptool
%{_bindir}/modularize
%{_bindir}/nvptx-arch
%{_bindir}/pp-trace
%{_bindir}/run-clang-tidy
%{_datadir}/clang/clang-tidy-diff.py*
%{_datadir}/clang/run-find-all-symbols.py*


%changelog
* Sat May 04 2024 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 18.1.5-1
- new upstream release.

* Sat Oct 07 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 17.0.2-1
- new upstream release.

* Sat Sep 30 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 17.0.1-1
- new upstream release.

* Sun May 28 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 16.0.4-1
- new upstream release.

* Fri Dec 16 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 15.0.6-1
- new upstream release.

* Wed Oct 05 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 15.0.2-1
- new upstream release.

* Wed Sep 07 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 15.0.0-1
- new upstream release.

* Wed Jun 15 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 14.0.5-1
- new upstream release.
- dropped patch5.
- updated Patch1.
- imported Patch5-202 from rawhide.

* Thu Feb 03 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 13.0.1-1
- new upstream release.

* Fri Oct 22 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 13.0.0-1
- new upstream release.
- refreshed patches.

* Wed Aug 11 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 12.0.1-1
- new upstream release.
- refreshed patches.

* Sat Apr 17 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 12.0.0-1
- devided clang from llvm.
- new upstream release.

* Fri Oct 16 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 11.0.0-2
- enabled to build utils.

* Thu Oct 15 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 11.0.0-1
- new upstream release.

* Mon Aug 17 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 10.0.1-1
- new upstream release.

* Thu Apr 16 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 10.0.0-2
- rebuilt with libffi-3.3.

* Fri Mar 27 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 10.0.0-1
- new upstream release.

* Sun Dec 22 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 9.0.1-1
- new upstream release.

* Sun Sep 22 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 9.0.0-2
- dropped all patches.
- switched build-system to ninja.
- switched python to python3.

* Fri Sep 20 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 9.0.0-1
- new upstream release.
- added OpenMP.

* Sun Dec 16 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 7.0.1-1
- fixed %%files.

* Wed Dec 05 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 7.0.0-1
- new upstream release.
- dropped Patch0, 4, 5, 100 and 200.
- imported Patch3, 7, 12, 15, 100, 101, 102 and 400 from rawhide.
- renamed a subpackage "llvm-libs".
- added a subpackage "lld".

* Tue Jan 02 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 5.0.1-2
- added subpackages "llvm-static", "python-lldb" and "python-clang".

* Mon Jan 01 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 5.0.1-1
- new upstream release.
- dropped Patch1 and 2: fixed in upstream.
- imported Patch0, 3-5, 100 and 200 from rawhide.
- disabled Patch1002: no longer needed?

* Fri Aug  5 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.8.1-1
- new upstream release.
- switched to cmake.
- disable ocaml binding as default.
- updated Patch1000 and 1002.
- disabled Patch1000 as default.
- cleanup patches.

* Thu Jun 30 2016 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.6.2-2
- rebuild with gcc-5.4.0

* Sat Sep  5 2015 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 3.6.2-1
- new upstream release
- added BR: ocaml-ctypes

* Thu Jun  4 2015 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 3.6.1-1
- new upstream release
- dropt Patch 1, 201 and 1001
- updated Patch 1000

* Tue Mar 24 2015 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 3.5.1-1
- updated to 3.5.1
- added Patch1, 2, 100, 101, 200, 201 and 202 from Fedora 
- added clang-libs, lldb, lldb-devel and compat32-llvm-libs subpackage
- obsoleted clang-doc
- built with ocaml 4.02.1

* Sat Nov 29 2014 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.5.0-2
- fix <BTS:2832>
  - fix configure option "--with-c-include-dirs"
  - update Patch1000: clang-3.5.0-driver-ld.gold.patch
  - update Patch1001: clang-3.5.0-driver-lib64.patch
  - update Patch1002: clang-3.5.0-driver-vine.patch

* Tue Oct 28 2014 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.5.0-1
- update to 3.5.0
- remove Patch11 (clang-hardfloat-hack.patch)

* Sun Jul 06 2014 Yoji TOYODA <bsyamato@sea.plala.or.jp> 3.3-2
- rebuild with libffi-3.0.13

* Mon Oct 28 2013 Daisuke SUZUKI <daisuke@linux.or.jp> 3.3-1
- update to 3.3

* Mon Dec 03 2012 Daisuke SUZUKI <daisuke@linux.or.jp> 3.1-2
- ld.gold (patch1000,1001)
- add /%{_lib} to ld search path
- add support *-vine-linux gcc (patch1002)

* Fri Nov 30 2012 Daisuke SUZUKI <daisuke@linux.or.jp> 3.1-1
- update to 3.1

* Tue Jan 24 2012 NAKAMURA Kenta <kenta@vinelinux.org> - 3.0-1
- updated to 3.0 release

* Sun Jan 15 2012 NAKAMURA Kenta <kenta@vinelinux.org> - 2.9-3
- added patch 2-4 to support -O4 link-time optimization

* Fri Sep  9 2011 MATSUBAYASHI Kohji <shaolin@vinelinux.org> - 2.9-2
- disable -fno-var-tracking-assignments on ppc
  (seems like gcc-4.4.5 still doesn't support this)

* Thu Aug 25 2011 Daisuke SUZUKI <daisuke@linux.or.jp> 2.9-1
- new upstream release
- add BR: libffi-devel
- add R: libffi-devel to -devel

* Sun May 23 2010 Daisuke SUZUKI <daisuke@linux.or.jp> 2.7-1
- initial build for Vine Linux

* Sun May  2 2010 Michel Salim <salimma@fedoraproject.org> - 2.7-1
- Update to final 2.7 release

* Sun Mar 28 2010 Michel Salim <salimma@fedoraproject.org> - 2.7-0.1.pre1
- Update to first 2.7 pre-release

* Fri Sep 18 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.6.pre2
- Update to 2.6 pre-release2
- -devel subpackage now virtually provides -static

* Wed Sep  9 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.5.pre1
- Disable var tracking assignments on PPC

* Wed Sep  9 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.4.pre1
- Don't adjust clang include dir; files there are noarch (bz#521893)
- Enable clang unit tests
- clang and clang-analyzer renamed; no longer depend on llvm at runtime

* Mon Sep  7 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.3.pre1
- Package Clang's static analyzer tools

* Mon Sep  7 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.2.pre1
- PIC is now enabled by default; explicitly disable on %%{ix86}

* Mon Sep  7 2009 Michel Salim <salimma@fedoraproject.org> - 2.6-0.1.pre1
- First 2.6 prerelease
- Enable Clang front-end
- Enable debuginfo generation

* Sat Sep  5 2009 Michel Salim <salimma@fedoraproject.org> - 2.5-6
- Disable assertions (needed by OpenGTL, bz#521261)
- Align spec file with upstream build instructions
- Enable unit tests

* Sat Aug 22 2009 Michel Salim <salimma@fedoraproject.org> - 2.5-5
- Only disable PIC on %%ix86; ppc actually needs it

* Sat Aug 22 2009 Michel Salim <salimma@fedoraproject.org> - 2.5-4
- Disable use of position-independent code on 32-bit platforms
  (buggy in LLVM <= 2.5)

* Sat Jul 25 2009 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 2.5-3
- Rebuilt for https://fedoraproject.org/wiki/Fedora_12_Mass_Rebuild

* Wed Mar  4 2009 Michel Salim <salimma@fedoraproject.org> - 2.5-2
- Remove build scripts; they require the build directory to work

* Wed Mar  4 2009 Michel Salim <salimma@fedoraproject.org> - 2.5-1
- Update to 2.5
- Package build scripts (bug #457881)

* Tue Dec  2 2008 Michel Salim <salimma@fedoraproject.org> - 2.4-2
- Patched build process for the OCaml binding

* Tue Dec  2 2008 Michel Salim <salimma@fedoraproject.org> - 2.4-1
- Update to 2.4
- Package Ocaml binding

* Wed Jun 18 2008 Bryan O'Sullivan <bos@serpentine.com> - 2.3-2
- Add dependency on groff

* Wed Jun 18 2008 Bryan O'Sullivan <bos@serpentine.com> - 2.3-1
- LLVM 2.3

* Thu May 29 2008 Tom "spot" Callaway <tcallawa@redhat.com> - 2.2-4
- fix license tags

* Wed Mar  5 2008 Bryan O'Sullivan <bos@serpentine.com> - 2.2-3
- Fix compilation problems with gcc 4.3

* Mon Feb 18 2008 Fedora Release Engineering <rel-eng@fedoraproject.org> - 2.2-2
- Autorebuild for GCC 4.3

* Sun Jan 20 2008 Bryan O'Sullivan <bos@serpentine.com> - 2.1-2
- Fix review comments

* Sun Jan 20 2008 Bryan O'Sullivan <bos@serpentine.com> - 2.1-1
- Initial version
