1 # Copyright 2019-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: llvm.org.eclass
6 # Michał Górny <mgorny@gentoo.org>
8 # Michał Górny <mgorny@gentoo.org>
9 # @BLURB: Common bits for fetching & unpacking llvm.org projects
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.
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.
26 # LLVM_COMPONENTS=( lld )
27 # LLVM_TEST_COMPONENTS=( llvm/utils/lit )
28 # llvm.org_set_globals
35 die "Unsupported EAPI=${EAPI} for ${ECLASS}"
40 # == internal control bits ==
42 # @ECLASS-VARIABLE: _LLVM_MASTER_MAJOR
45 # The major version of current LLVM trunk. Used to determine
46 # the correct branch to use.
49 # @ECLASS-VARIABLE: _LLVM_SOURCE_TYPE
52 # Source type to use: 'git' or 'tar'.
53 if [[ -z ${_LLVM_SOURCE_TYPE+1} ]]; then
54 if [[ ${PV} == *.9999 ]]; then
61 [[ ${_LLVM_SOURCE_TYPE} == git ]] && inherit git-r3
63 [[ ${PV} == ${_LLVM_MASTER_MAJOR}.* && ${_LLVM_SOURCE_TYPE} == tar ]] &&
64 die "${ECLASS}: Release ebuild for master branch?!"
67 # == control variables ==
69 # @ECLASS-VARIABLE: LLVM_COMPONENTS
72 # List of components needed unconditionally. Specified as bash array
73 # with paths relative to llvm-project git. Automatically translated
74 # for tarball releases.
76 # The first path specified is used to construct default S.
78 # @ECLASS-VARIABLE: LLVM_TEST_COMPONENTS
81 # List of additional components needed for tests.
84 # == global scope logic ==
86 # @FUNCTION: _llvm.org_get_archives
87 # @USAGE: <components>
90 # Set 'archives' array to list of unique archive filenames
91 # for components passed as parameters.
92 _llvm.org_get_archives() {
102 local a=${cn}-${PV}.src.tar.xz
103 has "${a}" "${archives[@]}" || archives+=( "${a}" )
107 # @FUNCTION: llvm.org_set_globals
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.'
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.'
121 if [[ ${_LLVM_SOURCE_TYPE} == git ]]; then
122 EGIT_REPO_URI="https://github.com/llvm/llvm-project.git"
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
130 https://github.com/llvm/llvm-project/archive/llvmorg-${PV/_/-}.tar.gz"
133 _llvm.org_get_archives "${LLVM_COMPONENTS[@]}"
134 for a in "${archives[@]}"; do
136 https://releases.llvm.org/${PV}/${a}"
140 die "Invalid _LLVM_SOURCE_TYPE: ${LLVM_SOURCE_TYPE}"
143 S=${WORKDIR}/${LLVM_COMPONENTS[0]}
145 if [[ -n ${LLVM_TEST_COMPONENTS+1} ]]; then
147 RESTRICT+=" !test? ( test )"
149 if [[ ${_LLVM_SOURCE_TYPE} == tar ]]; then
150 if ver_test -ge 9.0.1_rc1; then
151 # everything already fetched
154 # split 9.0.0 release and older
158 _llvm.org_get_archives "${LLVM_TEST_COMPONENTS[@]}"
159 for a in "${archives[@]}"; do
161 https://releases.llvm.org/${PV}/${a}"
170 _LLVM_ORG_SET_GLOBALS_CALLED=1
174 # == phase functions ==
176 EXPORT_FUNCTIONS src_unpack
178 # @FUNCTION: llvm.org_src_unpack
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"
186 local components=( "${LLVM_COMPONENTS[@]}" )
187 if [[ ${LLVM_TEST_COMPONENTS+1} ]] && use test; then
188 components+=( "${LLVM_TEST_COMPONENTS[@]}" )
191 if [[ ${_LLVM_SOURCE_TYPE} == git ]]; then
193 git-r3_checkout '' . '' "${components[@]}"
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
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#*/}
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