xref: /spdk/scripts/pkgdep/common.sh (revision cc6920a4763d4b9a43aa40583c8397d8f14fa100)
1#!/usr/bin/env bash
2
3install_liburing() {
4	local GIT_REPO_LIBURING=https://github.com/axboe/liburing.git
5	local liburing_dir=/usr/local/src/liburing
6
7	if [[ -d $liburing_dir ]]; then
8		echo "liburing source already present, not cloning"
9	else
10		mkdir -p $liburing_dir
11		git clone "${GIT_REPO_LIBURING}" "$liburing_dir"
12	fi
13	# Use commit we know we can compile against. See #1673 as a reference.
14	git -C "$liburing_dir" checkout liburing-2.0
15	(cd "$liburing_dir" && ./configure --libdir=/usr/lib64 && make install)
16	echo /usr/lib64 > /etc/ld.so.conf.d/spdk-liburing.conf
17	ldconfig
18}
19
20install_shfmt() {
21	# Fetch version that has been tested
22	local shfmt_version=3.1.0
23	local shfmt=shfmt-$shfmt_version
24	local shfmt_dir=${SHFMT_DIR:-/opt/shfmt}
25	local shfmt_dir_out=${SHFMT_DIR_OUT:-/usr/bin}
26	local shfmt_url
27	local os
28	local arch
29
30	if hash "$shfmt" && [[ $("$shfmt" --version) == "v$shfmt_version" ]]; then
31		echo "$shfmt already installed"
32		return 0
33	fi 2> /dev/null
34
35	arch=$(uname -m)
36	os=$(uname -s)
37
38	case "$arch" in
39		x86_64) arch="amd64" ;;
40		aarch64) arch="arm" ;;
41		*)
42			echo "Not supported arch (${arch:-Unknown}), skipping"
43			return 0
44			;;
45	esac
46
47	case "$os" in
48		Linux) shfmt_url=https://github.com/mvdan/sh/releases/download/v$shfmt_version/shfmt_v${shfmt_version}_linux_${arch} ;;
49		FreeBSD) shfmt_url=https://github.com/mvdan/sh/releases/download/v$shfmt_version/shfmt_v${shfmt_version}_freebsd_${arch} ;;
50		*)
51			echo "Not supported OS (${os:-Unknown}), skipping"
52			return 0
53			;;
54	esac
55
56	mkdir -p "$shfmt_dir"
57	mkdir -p "$shfmt_dir_out"
58
59	echo "Fetching ${shfmt_url##*/}"...
60	local err
61	if err=$(curl -f -Lo"$shfmt_dir/$shfmt" "$shfmt_url" 2>&1); then
62		chmod +x "$shfmt_dir/$shfmt"
63		ln -sf "$shfmt_dir/$shfmt" "$shfmt_dir_out"
64	else
65		cat <<- CURL_ERR
66
67			* Fetching $shfmt_url failed, $shfmt will not be available for format check.
68			* Error:
69
70			$err
71
72		CURL_ERR
73		return 0
74	fi
75	echo "$shfmt installed"
76}
77
78install_spdk_bash_completion() {
79	[[ -e /usr/share/bash-completion/bash_completion ]] || return 0
80
81	local compat_dir=/etc/bash_completion.d
82	mkdir -p "$compat_dir"
83
84	if [[ ! -e $compat_dir/spdk ]]; then
85		cp -v "$scriptsdir/bash-completion/spdk" "$compat_dir"
86	fi
87}
88
89install_markdownlint() {
90	local git_repo_mdl="https://github.com/markdownlint/markdownlint.git"
91	local mdl_version="v0.11.0"
92	if [ ! -d /usr/src/markdownlint ]; then
93		sudo -E git clone --branch "$mdl_version" "$git_repo_mdl" "/usr/src/markdownlint"
94		(
95			cd /usr/src/markdownlint
96			if ! hash rake; then
97				sudo -E gem install rake
98			fi
99			if ! hash bundler; then
100				sudo -E gem install bundler
101			fi
102			sudo -E rake install
103		)
104	else
105		echo "Markdown lint tool already in /usr/src/markdownlint. Not installing"
106	fi
107}
108
109if [[ $INSTALL_DEV_TOOLS == true ]]; then
110	install_shfmt
111	install_spdk_bash_completion
112	if [[ $ID != centos ]]; then
113		install_markdownlint
114	else
115		echo "mdl not supported on $ID, disabling"
116	fi
117fi
118
119if [[ $INSTALL_LIBURING == true ]]; then
120	install_liburing
121fi
122