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