llvm.eclass: Add initial tests
[gentoo.git] / eclass / llvm.org.eclass
1 # Copyright 2019-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: llvm.org.eclass
5 # @MAINTAINER:
6 # Michał Górny <mgorny@gentoo.org>
7 # @AUTHOR:
8 # Michał Górny <mgorny@gentoo.org>
9 # @BLURB: Common bits for fetching & unpacking llvm.org projects
10 # @DESCRIPTION:
11 # The llvm.org eclass provides common code to fetch and unpack parts
12 # of the llvm.org project tree.  It takes care of handling both git
13 # checkouts and source tarballs, making it possible to unify the code
14 # of live and release ebuilds and effectively reduce the work needed
15 # to package new releases/RCs/branches.
16 #
17 # In order to use this eclass, the ebuild needs to declare
18 # LLVM_COMPONENTS and then call llvm.org_set_globals.  If tests require
19 # additional components, they need to be listed in LLVM_TEST_COMPONENTS.
20 # The eclass exports an implementation of src_unpack() phase.
21 #
22 # Example:
23 # @CODE
24 # inherit llvm.org
25 #
26 # LLVM_COMPONENTS=( lld )
27 # LLVM_TEST_COMPONENTS=( llvm/utils/lit )
28 # llvm.org_set_globals
29 # @CODE
30
31 case "${EAPI:-0}" in
32         7)
33                 ;;
34         *)
35                 die "Unsupported EAPI=${EAPI} for ${ECLASS}"
36                 ;;
37 esac
38
39
40 # == internal control bits ==
41
42 # @ECLASS-VARIABLE: _LLVM_MASTER_MAJOR
43 # @INTERNAL
44 # @DESCRIPTION:
45 # The major version of current LLVM trunk.  Used to determine
46 # the correct branch to use.
47 _LLVM_MASTER_MAJOR=11
48
49 # @ECLASS-VARIABLE: _LLVM_SOURCE_TYPE
50 # @INTERNAL
51 # @DESCRIPTION:
52 # Source type to use: 'git' or 'tar'.
53 if [[ -z ${_LLVM_SOURCE_TYPE+1} ]]; then
54         if [[ ${PV} == *.9999 ]]; then
55                 _LLVM_SOURCE_TYPE=git
56         else
57                 _LLVM_SOURCE_TYPE=tar
58         fi
59 fi
60
61 [[ ${_LLVM_SOURCE_TYPE} == git ]] && inherit git-r3
62
63 [[ ${PV} == ${_LLVM_MASTER_MAJOR}.* && ${_LLVM_SOURCE_TYPE} == tar ]] &&
64         die "${ECLASS}: Release ebuild for master branch?!"
65
66
67 # == control variables ==
68
69 # @ECLASS-VARIABLE: LLVM_COMPONENTS
70 # @REQUIRED
71 # @DESCRIPTION:
72 # List of components needed unconditionally.  Specified as bash array
73 # with paths relative to llvm-project git.  Automatically translated
74 # for tarball releases.
75 #
76 # The first path specified is used to construct default S.
77
78 # @ECLASS-VARIABLE: LLVM_TEST_COMPONENTS
79 # @DEFAULT_UNSET
80 # @DESCRIPTION:
81 # List of additional components needed for tests.
82
83
84 # == global scope logic ==
85
86 # @FUNCTION: _llvm.org_get_archives
87 # @USAGE: <components>
88 # @INTERNAL
89 # @DESCRIPTION:
90 # Set 'archives' array to list of unique archive filenames
91 # for components passed as parameters.
92 _llvm.org_get_archives() {
93         local c
94         archives=()
95
96         for c; do
97                 local cn=${c%%/*}
98                 case ${cn} in
99                         clang) cn=cfe;;
100                 esac
101
102                 local a=${cn}-${PV}.src.tar.xz
103                 has "${a}" "${archives[@]}" || archives+=( "${a}" )
104         done
105 }
106
107 # @FUNCTION: llvm.org_set_globals
108 # @DESCRIPTION:
109 # Set global variables.  This must be called after setting LLVM_*
110 # variables used by the eclass.
111 llvm.org_set_globals() {
112         if [[ $(declare -p LLVM_COMPONENTS) != "declare -a"* ]]; then
113                 die 'LLVM_COMPONENTS must be an array.'
114         fi
115         if declare -p LLVM_TEST_COMPONENTS &>/dev/null; then
116                 if [[ $(declare -p LLVM_TEST_COMPONENTS) != "declare -a"* ]]; then
117                         die 'LLVM_TEST_COMPONENTS must be an array.'
118                 fi
119         fi
120
121         if [[ ${_LLVM_SOURCE_TYPE} == git ]]; then
122                 EGIT_REPO_URI="https://github.com/llvm/llvm-project.git"
123
124                 [[ ${PV} != ${_LLVM_MASTER_MAJOR}.* ]] &&
125                         EGIT_BRANCH="release/${PV%%.*}.x"
126         elif [[ ${_LLVM_SOURCE_TYPE} == tar ]]; then
127                 if ver_test -ge 9.0.1_rc1; then
128                         # 9.0.1 RCs as GitHub archive
129                         SRC_URI+="
130                                 https://github.com/llvm/llvm-project/archive/llvmorg-${PV/_/-}.tar.gz"
131                 else
132                         local a archives=()
133                         _llvm.org_get_archives "${LLVM_COMPONENTS[@]}"
134                         for a in "${archives[@]}"; do
135                                 SRC_URI+="
136                                         https://releases.llvm.org/${PV}/${a}"
137                         done
138                 fi
139         else
140                 die "Invalid _LLVM_SOURCE_TYPE: ${LLVM_SOURCE_TYPE}"
141         fi
142
143         S=${WORKDIR}/${LLVM_COMPONENTS[0]}
144
145         if [[ -n ${LLVM_TEST_COMPONENTS+1} ]]; then
146                 IUSE+=" test"
147                 RESTRICT+=" !test? ( test )"
148
149                 if [[ ${_LLVM_SOURCE_TYPE} == tar ]]; then
150                         if ver_test -ge 9.0.1_rc1; then
151                                 # everything already fetched
152                                 :
153                         else
154                                 # split 9.0.0 release and older
155                                 SRC_URI+="
156                                         test? ("
157
158                                 _llvm.org_get_archives "${LLVM_TEST_COMPONENTS[@]}"
159                                 for a in "${archives[@]}"; do
160                                         SRC_URI+="
161                                                 https://releases.llvm.org/${PV}/${a}"
162                                 done
163
164                                 SRC_URI+="
165                                         )"
166                         fi
167                 fi
168         fi
169
170         _LLVM_ORG_SET_GLOBALS_CALLED=1
171 }
172
173
174 # == phase functions ==
175
176 EXPORT_FUNCTIONS src_unpack
177
178 # @FUNCTION: llvm.org_src_unpack
179 # @DESCRIPTION:
180 # Unpack or checkout requested LLVM components.
181 llvm.org_src_unpack() {
182         if [[ ! ${_LLVM_ORG_SET_GLOBALS_CALLED} ]]; then
183                 die "llvm.org_set_globals must be called in global scope"
184         fi
185
186         local components=( "${LLVM_COMPONENTS[@]}" )
187         if [[ ${LLVM_TEST_COMPONENTS+1} ]] && use test; then
188                 components+=( "${LLVM_TEST_COMPONENTS[@]}" )
189         fi
190
191         if [[ ${_LLVM_SOURCE_TYPE} == git ]]; then
192                 git-r3_fetch
193                 git-r3_checkout '' . '' "${components[@]}"
194         else
195                 if ver_test -ge 9.0.1_rc1; then
196                         local archive=llvmorg-${PV/_/-}.tar.gz
197                         ebegin "Unpacking from ${archive}"
198                         tar -x -z -o --strip-components 1 \
199                                 -f "${DISTDIR}/${archive}" \
200                                 "${components[@]/#/llvm-project-${archive%.tar*}/}" || die
201                         eend ${?}
202                 else
203                         local c archives
204                         # TODO: optimize this
205                         for c in "${components[@]}"; do
206                                 local top_dir=${c%%/*}
207                                 _llvm.org_get_archives "${c}"
208                                 local sub_path=${archives[0]%.tar.xz}
209                                 [[ ${c} == */* ]] && sub_path+=/${c#*/}
210
211                                 ebegin "Unpacking ${sub_path} from ${archives[0]}"
212                                 mkdir -p "${top_dir}" || die
213                                 tar -C "${top_dir}" -x -J -o --strip-components 1 \
214                                         -f "${DISTDIR}/${archives[0]}" "${sub_path}" || die
215                                 eend ${?}
216                         done
217                 fi
218         fi
219 }