From: YiyangWu <xgreenlandfor...@gmail.com>

This v3 fixes various problems pointed out by Ulrich. The most important
change, is that check_rw_permission cannot accept wildcards (it is
previously designed to enable wildcard, but I implemented it with bugs,
so I turned to disabling wildcard and loop around devices).

Updated patch is also accessible in
https://github.com/gentoo/gentoo/pull/26784, with QA check by qa-bot.

Changelog compare to v2:
1. Re-wrap description, <80 characters;
2. correct version detection;
3. decorate code block using @CODE;
4. standardize EAPI check copied from multilib-minimal.eclass;
5. correct quotation mark usage (which fixes ROCM_TESTS iteration);
6. use has function to test variable inside array;
7. fix typo;
8. avoid using internal portage variables;
9. disable wildcard in check_rw_permission; iterate devices to check;
10. referece bug #817440 in commit message.

Yiyang Wu (2):
  rocm.eclass: new eclass
  profiles/desc: add amdgpu_targets.desc for USE_EXPAND

 eclass/rocm.eclass                | 278 ++++++++++++++++++++++++++++++
 profiles/base/make.defaults       |   2 +-
 profiles/desc/amdgpu_targets.desc |  15 ++
 3 files changed, 294 insertions(+), 1 deletion(-)
 create mode 100644 eclass/rocm.eclass
 create mode 100644 profiles/desc/amdgpu_targets.desc

Interdiff against v2:
diff --git a/eclass/rocm.eclass b/eclass/rocm.eclass
index 8ca2c051ddce..4b220db0aa81 100644
--- a/eclass/rocm.eclass
+++ b/eclass/rocm.eclass
@@ -9,12 +9,12 @@
 # @SUPPORTED_EAPIS: 7 8
 # @BLURB: Common functions and variables for ROCm packages written in HIP
 # @DESCRIPTION:
-# ROCm packages such as sci-libs/<roc|hip>* can utilize functions in this 
eclass.
-# Currently, it handles the AMDGPU_TARGETS variable via USE_EXPAND, so user can
-# use USE flag to control which GPU architecture to compile, and ensure 
coherence
-# among dependencies. It also specify CXX=hipcc, to let hipcc compile. Another
-# important function is src_test, which checks whether a valid KFD device 
exists
-# for testing, and then execute the test program.
+# ROCm packages such as sci-libs/<roc|hip>* can utilize functions in this
+# eclass.  Currently, it handles the AMDGPU_TARGETS variable via USE_EXPAND, so
+# user can use USE flag to control which GPU architecture to compile, and
+# ensure coherence among dependencies. It also specify CXX=hipcc, to let hipcc
+# compile. Another important function is src_test, which checks whether a valid
+# KFD device exists for testing, and then execute the test program.
 #
 # Most ROCm packages use cmake as build system, so this eclass does not export
 # phase functions which overwrites the phase functions in cmake.eclass. Ebuild
@@ -22,6 +22,7 @@
 #
 # @EXAMPLE:
 # # Example for ROCm packages in https://github.com/ROCmSoftwarePlatform
+# @CODE
 # inherit cmake rocm
 # 
SRC_URI="https://github.com/ROCmSoftwarePlatform/${PN}/archive/rocm-${PV}.tar.gz
 -> ${P}.tar.gz"
 # SLOT="0/$(ver_cut 1-2)"
@@ -46,10 +47,12 @@
 # src_test() {
 #     rocm_src_test
 # }
+# @CODE
 #
 # # Example for packages depend on ROCm libraries -- a package depend on
 # # rocBLAS, and use comma seperated ${HCC_AMDGPU_TARGET} to determine GPU
 # # architecture to compile. Requires ROCm version >5.
+# @CODE
 # ROCM_VERSION=5
 # inherit rocm
 # IUSE="rocm"
@@ -64,20 +67,17 @@
 #     fi
 #     default
 # }
+# @CODE
 
 if [[ ! ${_ROCM_ECLASS} ]]; then
 
-case "${EAPI:-0}" in
-       7|8)
-               ;;
-       *)
-               die "Unsupported EAPI=${EAPI} for ${ECLASS}"
-               ;;
+case ${EAPI} in
+       7|8) ;;
+       *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
 esac
 
 inherit edo
 
-
 # @ECLASS_VARIABLE: ROCM_VERSION
 # @DEFAULT_UNSET
 # @PRE_INHERIT
@@ -96,7 +96,7 @@ inherit edo
 # @ECLASS_VARIABLE: OFFICIAL_AMDGPU_TARGETS
 # @INTERNAL
 # @DESCRIPTION:
-# The list of USE flags corresponding to all officlially supported AMDGPU
+# The list of USE flags corresponding to all officially supported AMDGPU
 # targets in this ROCm version, documented at
 # 
https://docs.amd.com/bundle/ROCm-Installation-Guide-v${PV}/page/Prerequisite_Actions.html.
 # USE flag of these architectures will be default on. Depends on ${PV}.
@@ -106,10 +106,14 @@ inherit edo
 # @DESCRIPTION:
 # Requires at least one AMDGPU target to be compiled.
 # Example use for ROCm libraries:
+# @CODE
 # REQUIRED_USE="${ROCM_REQUIRED_USE}"
+# @CODE
 # Example use for packages that depend on ROCm libraries
+# @CODE
 # IUSE="rocm"
 # REQUIRED_USE="rocm? ( ${ROCM_REQUIRED_USE} )"
+# @CODE
 
 # @ECLASS_VARIABLE: ROCM_USEDEP
 # @OUTPUT_VARIABLE
@@ -130,7 +134,7 @@ inherit edo
 # OFFICIAL_AMDGPU_TARGETS, ROCM_REQUIRED_USE, and ROCM_USEDEP
 _rocm_set_globals() {
        case ${ROCM_VERSION:-${PV}} in
-               4*)
+               4.*)
                        ALL_AMDGPU_TARGETS=(
                                gfx803 gfx900 gfx906 gfx908
                                gfx1010 gfx1011 gfx1012 gfx1030
@@ -139,7 +143,7 @@ _rocm_set_globals() {
                                gfx906 gfx908
                        )
                        ;;
-               5*)
+               5.*)
                        ALL_AMDGPU_TARGETS=(
                                gfx803 gfx900 gfx906 gfx908 gfx90a
                                gfx1010 gfx1011 gfx1012 gfx1030 gfx1031
@@ -154,8 +158,8 @@ _rocm_set_globals() {
        esac
 
        ROCM_REQUIRED_USE+=" || ("
-       for gpu_target in ${ALL_AMDGPU_TARGETS[@]}; do
-               if [[ " ${OFFICIAL_AMDGPU_TARGETS[*]} " =~ " ${gpu_target} " 
]]; then
+       for gpu_target in "${ALL_AMDGPU_TARGETS[@]}"; do
+               if has ${gpu_target} "${OFFICIAL_AMDGPU_TARGETS[*]}"; then
                        IUSE+=" ${gpu_target/#/+amdgpu_targets_}"
                else
                        IUSE+=" ${gpu_target/#/amdgpu_targets_}"
@@ -180,7 +184,7 @@ unset -f _rocm_set_globals
 # https://llvm.org/docs/AMDGPUUsage.html#target-features
 get_amdgpu_flags() {
        local AMDGPU_TARGET_FLAGS
-       for gpu_target in ${ALL_AMDGPU_TARGETS[@]}; do
+       for gpu_target in "${ALL_AMDGPU_TARGETS[@]}"; do
                local target_feature=
                if use amdgpu_targets_${gpu_target}; then
                        case ${gpu_target} in
@@ -205,8 +209,8 @@ get_amdgpu_flags() {
 # check read and write permissions on specific files.
 # allow using wildcard, for example check_rw_permission /dev/dri/render*
 check_rw_permission() {
-       [[ -r "$1" ]] && [[ -w "$1" ]] || die \
-               "${PORTAGE_USERNAME} do not have read or write permissions on 
$1! \n Make sure ${PORTAGE_USERNAME} is in render group and check the 
permissions."
+       [[ -r $1 ]] && [[ -w $1 ]] || die \
+               "Portage do not have read or write permissions on $1! \n Make 
sure both are in render group and check the permissions."
 }
 
 # == phase functions ==
@@ -232,23 +236,22 @@ rocm_src_configure() {
 # @DESCRIPTION:
 # Test whether valid GPU device is present. If so, find how to, and execute 
test.
 # ROCm packages can have to test mechanism:
-# 1. cmake_src_test. Set MAKEOPTS="-j1" to make sure only one test on GPU at a 
time;
+# 1. cmake_src_test. MAKEOPTS="-j1" ensures only one test on GPU at a time;
 # 2. one single gtest binary called "${PN,,}"-test;
 # 3. Some package like rocFFT have alternative test like rocfft-selftest;
 # 4. Custome testing binaries like dev-libs/rccl. Use ${ROCM_TESTS} to specify.
 rocm_src_test() {
-       addwrite /dev/kfd
-       addwrite /dev/dri/
-
-       # check permissions on /dev/kfd and /dev/dri/render*
-       check_rw_permission /dev/kfd
-       check_rw_permission /dev/dri/render*
+       # grant and check permissions on /dev/kfd and /dev/dri/render*
+       for device in /dev/kfd /dev/dri/render*; do
+               addwrite ${device}
+               check_rw_permission ${device}
+       done
 
        : 
${LD_LIBRARY_PATH:="${BUILD_DIR}/clients:${BUILD_DIR}/src:${BUILD_DIR}/library:${BUILD_DIR}/library/src:${BUILD_DIR}/library/src/device"}
        export LD_LIBRARY_PATH
        if grep -q 'build test:' "${BUILD_DIR}"/build.ninja; then
                MAKEOPTS="-j1" cmake_src_test
-       elif [[ -d "${BUILD_DIR}"/clients/staging ]]; then
+       elif [[ -d ${BUILD_DIR}/clients/staging ]]; then
                cd "${BUILD_DIR}/clients/staging" || die "Test directory not 
found!"
                for test_program in "${PN,,}-"*test; do
                        if [[ -x ${test_program} ]]; then
@@ -257,8 +260,8 @@ rocm_src_test() {
                                die "The test program ${test_program} does not 
exist or cannot be excuted!"
                        fi
                done
-       elif [[ ! -z "${ROCM_TESTS}" ]]; then
-               for test_program in "${ROCM_TESTS}"; do
+       elif [[ -n ${ROCM_TESTS} ]]; then
+               for test_program in ${ROCM_TESTS}; do
                        cd "${BUILD_DIR}" || die
                        if [[ -x ${test_program} ]]; then
                        edob ./${test_program}
-- 
2.34.1


Reply via email to