xref: /aosp_15_r20/external/pytorch/.ci/docker/common/install_rocm_drm.sh (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1#!/bin/bash
2# Script used only in CD pipeline
3
4###########################
5### prereqs
6###########################
7# Install Python packages depending on the base OS
8ID=$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"')
9case "$ID" in
10  ubuntu)
11    apt-get update -y
12    apt-get install -y libpciaccess-dev pkg-config
13    apt-get clean
14    ;;
15  centos)
16    yum install -y libpciaccess-devel pkgconfig
17    ;;
18  *)
19    echo "Unable to determine OS..."
20    exit 1
21    ;;
22esac
23python3 -m pip install meson ninja
24
25###########################
26### clone repo
27###########################
28GIT_SSL_NO_VERIFY=true git clone https://gitlab.freedesktop.org/mesa/drm.git
29pushd drm
30
31###########################
32### patch
33###########################
34patch -p1 <<'EOF'
35diff --git a/amdgpu/amdgpu_asic_id.c b/amdgpu/amdgpu_asic_id.c
36index a5007ffc..13fa07fc 100644
37--- a/amdgpu/amdgpu_asic_id.c
38+++ b/amdgpu/amdgpu_asic_id.c
39@@ -22,6 +22,13 @@
40  *
41  */
42
43+#define _XOPEN_SOURCE 700
44+#define _LARGEFILE64_SOURCE
45+#define _FILE_OFFSET_BITS 64
46+#include <ftw.h>
47+#include <link.h>
48+#include <limits.h>
49+
50 #include <ctype.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53@@ -34,6 +41,19 @@
54 #include "amdgpu_drm.h"
55 #include "amdgpu_internal.h"
56
57+static char *amdgpuids_path = NULL;
58+static const char* amdgpuids_path_msg = NULL;
59+
60+static int check_for_location_of_amdgpuids(const char *filepath, const struct stat *info, const int typeflag, struct FTW *pathinfo)
61+{
62+	if (typeflag == FTW_F && strstr(filepath, "amdgpu.ids")) {
63+		amdgpuids_path = strdup(filepath);
64+		return 1;
65+	}
66+
67+	return 0;
68+}
69+
70 static int parse_one_line(struct amdgpu_device *dev, const char *line)
71 {
72 	char *buf, *saveptr;
73@@ -113,10 +133,46 @@ void amdgpu_parse_asic_ids(struct amdgpu_device *dev)
74 	int line_num = 1;
75 	int r = 0;
76
77+	// attempt to find typical location for amdgpu.ids file
78 	fp = fopen(AMDGPU_ASIC_ID_TABLE, "r");
79+
80+	// if it doesn't exist, search
81+	if (!fp) {
82+
83+	char self_path[ PATH_MAX ];
84+	ssize_t count;
85+	ssize_t i;
86+
87+	count = readlink( "/proc/self/exe", self_path, PATH_MAX );
88+	if (count > 0) {
89+		self_path[count] = '\0';
90+
91+		// remove '/bin/python' from self_path
92+		for (i=count; i>0; --i) {
93+			if (self_path[i] == '/') break;
94+			self_path[i] = '\0';
95+		}
96+		self_path[i] = '\0';
97+		for (; i>0; --i) {
98+			if (self_path[i] == '/') break;
99+			self_path[i] = '\0';
100+		}
101+		self_path[i] = '\0';
102+
103+		if (1 == nftw(self_path, check_for_location_of_amdgpuids, 5, FTW_PHYS)) {
104+			fp = fopen(amdgpuids_path, "r");
105+			amdgpuids_path_msg = amdgpuids_path;
106+		}
107+	}
108+
109+	}
110+	else {
111+		amdgpuids_path_msg = AMDGPU_ASIC_ID_TABLE;
112+	}
113+
114+	// both hard-coded location and search have failed
115 	if (!fp) {
116-		fprintf(stderr, "%s: %s\n", AMDGPU_ASIC_ID_TABLE,
117-			strerror(errno));
118+		fprintf(stderr, "amdgpu.ids: No such file or directory\n");
119 		return;
120 	}
121
122@@ -132,7 +188,7 @@ void amdgpu_parse_asic_ids(struct amdgpu_device *dev)
123 			continue;
124 		}
125
126-		drmMsg("%s version: %s\n", AMDGPU_ASIC_ID_TABLE, line);
127+		drmMsg("%s version: %s\n", amdgpuids_path_msg, line);
128 		break;
129 	}
130
131@@ -150,7 +206,7 @@ void amdgpu_parse_asic_ids(struct amdgpu_device *dev)
132
133 	if (r == -EINVAL) {
134 		fprintf(stderr, "Invalid format: %s: line %d: %s\n",
135-			AMDGPU_ASIC_ID_TABLE, line_num, line);
136+			amdgpuids_path_msg, line_num, line);
137 	} else if (r && r != -EAGAIN) {
138 		fprintf(stderr, "%s: Cannot parse ASIC IDs: %s\n",
139 			__func__, strerror(-r));
140EOF
141
142###########################
143### build
144###########################
145meson builddir --prefix=/opt/amdgpu
146pushd builddir
147ninja install
148
149popd
150popd
151