Skip to content

Commit c987fb7

Browse files
authored
LibHttpClient.Linux: refactor dependency install to optional step and eliminate forced sudo calls (#963)
LibHttpClient's linux build had `sudo` authorized calls sprinkled throughout its build by default to install dependencies. Generally, this is an anti-pattern. It means that to ever build libHttpClient for linux you need to have sudo privileges even if all required dependencies are installed. sudo should be isolated to and reserved for operations which require elevations. General builds should not require elevation. Callers can continue to retain the old behavior by passing `--install-dependencies` to the old build script. I've also eliminated some unnecessary sudo usage from the openssl build. it was primarily there to give write access to files in the global /usr/local folder. Instead of installing openssl build results there, install them to a local temp folder instead and the need for sudo goes away.
1 parent 0fa5f24 commit c987fb7

5 files changed

Lines changed: 160 additions & 44 deletions

File tree

Build/libHttpClient.Linux/curl_Linux.bash

100644100755
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
set -e
4+
35
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
46
CONFIGURATION="Release"
57

@@ -31,14 +33,25 @@ else
3133
echo "No previously-built library present at $SCRIPT_DIR/../../Out/x64/$CONFIGURATION/libcurl.Linux/libcurl.a - performing build"
3234
fi
3335

36+
OPENSSL_INSTALL_DIR="$SCRIPT_DIR/../../Int/x64/$CONFIGURATION/openssl.Linux/"
37+
38+
CONFIGURE_ARGS=(
39+
--disable-shared
40+
--with-zlib
41+
--disable-dependency-tracking
42+
--with-openssl=$OPENSSL_INSTALL_DIR
43+
--enable-symbol-hiding
44+
--without-brotli
45+
)
3446
if [ "$CONFIGURATION" = "Debug" ]; then
35-
# make libcrypto and libssl
36-
./configure --disable-shared --with-zlib --disable-dependency-tracking -with-openssl=/usr/local/ssl --enable-symbol-hiding --enable-debug --without-brotli
47+
CONFIGURE_ARGS+=(--enable-debug)
3748
else
38-
# make libcrypto and libssl
39-
./configure --disable-shared --with-zlib --disable-dependency-tracking -with-openssl=/usr/local/ssl --enable-symbol-hiding --disable-debug --without-brotli
49+
CONFIGURE_ARGS+=(--disable-debug)
4050
fi
4151

52+
# make libcrypto and libssl
53+
./configure "${CONFIGURE_ARGS[@]}"
54+
4255
MAKE_PARALLELISM="-j$(nproc)" # run Make in parallel to speed up the build process
4356
make $MAKE_PARALLELISM
4457

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6+
7+
POSITIONAL_ARGS=()
8+
DO_INSTALL=true
9+
while [[ $# -gt 0 ]]; do
10+
case $1 in
11+
--check)
12+
DO_INSTALL=false
13+
shift # past value
14+
;;
15+
-*|--*)
16+
echo "Unknown option $1"
17+
exit 1
18+
;;
19+
*)
20+
POSITIONAL_ARGS+=("$1") # save positional arg
21+
shift # past argument
22+
;;
23+
esac
24+
done
25+
26+
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
27+
28+
build_dependencies=(clang make autoconf automake libtool)
29+
library_dependencies=(zlib1g zlib1g-dev)
30+
31+
if [ "$DO_INSTALL" = false ]; then
32+
33+
dependencies_missing=false
34+
for dep in "${build_dependencies[@]}" "${library_dependencies[@]}"; do
35+
if ! dpkg -s "$dep" &> /dev/null; then
36+
echo "Missing dependency: $dep"
37+
dependencies_missing=true
38+
else
39+
echo "Dependency installed: $dep"
40+
fi
41+
done
42+
43+
if [ "$dependencies_missing" = true ]; then
44+
exit 1
45+
fi
46+
47+
echo "All dependencies are installed"
48+
exit 0
49+
fi
50+
51+
echo "Installing dependencies..."
52+
echo "Build dependencies: ${build_dependencies[*]}"
53+
echo "Library dependencies: ${library_dependencies[*]}"
54+
55+
sudo hwclock --hctosys
56+
sudo apt-get update
57+
sudo apt-get -y install "${build_dependencies[@]}" "${library_dependencies[@]}"

Build/libHttpClient.Linux/libHttpClient_Linux.bash

100644100755
Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#!/bin/bash
2+
3+
set -e
4+
25
log () {
36
echo "***** $1 *****"
47
}
@@ -15,6 +18,8 @@ BUILD_STATIC=false
1518
BUILD_UNREAL_ENGINE_4=false
1619
C_COMPILER="clang"
1720
CXX_COMPILER="clang++"
21+
INSTALL_DEPENDENCIES=false
22+
REQUIRE_VERIFIED_DEPENDENCIES=true
1823

1924
while [[ $# -gt 0 ]]; do
2025
case $1 in
@@ -35,8 +40,16 @@ while [[ $# -gt 0 ]]; do
3540
BUILD_UNREAL_ENGINE_4=true
3641
shift
3742
;;
43+
--install-dependencies)
44+
INSTALL_DEPENDENCIES=true
45+
shift
46+
;;
3847
-sg|--skipaptget)
39-
DO_APTGET=false
48+
# NOOP. allow user to specify old --skipaptget args before that became the default
49+
shift
50+
;;
51+
-sd|--skip-dependency-check)
52+
REQUIRE_VERIFIED_DEPENDENCIES=false
4053
shift
4154
;;
4255
-st|--static)
@@ -56,16 +69,31 @@ done
5669

5770
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
5871

59-
if [ "$DO_APTGET" != "false" ]; then
60-
sudo hwclock --hctosys
61-
sudo apt-get update
62-
sudo apt-get install clang
63-
sudo apt-get install make
64-
sudo apt-get install autoconf
65-
sudo apt-get install automake
66-
sudo apt-get install libtool
67-
sudo apt-get install zlib1g zlib1g-dev
72+
set +e # temporarily disable exit-on-error to provide more graceful handling of dependency installation failures
73+
if [ "$INSTALL_DEPENDENCIES" = "true" ]; then
74+
bash "$SCRIPT_DIR"/install_dependencies.bash
75+
if [ $? -ne 0 ]; then
76+
echo ""
77+
echo "Failed to install dependencies."
78+
exit 1
79+
fi
80+
else
81+
bash "$SCRIPT_DIR"/install_dependencies.bash --check
82+
if [ $? -ne 0 ]; then
83+
if [ "$REQUIRE_VERIFIED_DEPENDENCIES" = true ]; then
84+
echo ""
85+
echo "Some dependencies are missing."
86+
echo "Please run with --install-dependencies to install them or run $SCRIPT_DIR/install_dependencies.bash directly"
87+
exit 1
88+
else
89+
echo ""
90+
echo "Some dependencies are missing."
91+
echo "--skip-dependency-check specified, ignoring and continuing."
92+
echo ""
93+
fi
94+
fi
6895
fi
96+
set -e # re-enable exit-on-error after dependency installation check
6997

7098
log "CONFIGURATION = ${CONFIGURATION}"
7199
log "BUILD SSL = ${BUILD_SSL}"
@@ -97,10 +125,10 @@ fi
97125
MAKE_PARALLELISM="-j$(nproc)" # run Make in parallel to speed up the build process
98126
if [ "$BUILD_STATIC" = false ]; then
99127
# make libHttpClient shared
100-
sudo cmake -S "$SCRIPT_DIR" -B "$SCRIPT_DIR"/../../Int/CMake/libHttpClient.Linux -D CMAKE_BUILD_TYPE=$CONFIGURATION -D CMAKE_C_COMPILER=$C_COMPILER -D CMAKE_CXX_COMPILER=$CXX_COMPILER -D BUILD_SHARED_LIBS=ON
101-
sudo make $MAKE_PARALLELISM -C "$SCRIPT_DIR"/../../Int/CMake/libHttpClient.Linux
128+
cmake -S "$SCRIPT_DIR" -B "$SCRIPT_DIR"/../../Int/CMake/libHttpClient.Linux -D CMAKE_BUILD_TYPE=$CONFIGURATION -D CMAKE_C_COMPILER=$C_COMPILER -D CMAKE_CXX_COMPILER=$CXX_COMPILER -D BUILD_SHARED_LIBS=ON
129+
make $MAKE_PARALLELISM -C "$SCRIPT_DIR"/../../Int/CMake/libHttpClient.Linux
102130
else
103131
# make libHttpClient static
104-
sudo cmake -S "$SCRIPT_DIR" -B "$SCRIPT_DIR"/../../Int/CMake/libHttpClient.Linux -D CMAKE_BUILD_TYPE=$CONFIGURATION -D CMAKE_C_COMPILER=$C_COMPILER -D CMAKE_CXX_COMPILER=$CXX_COMPILER -D BUILD_SHARED_LIBS=OFF
105-
sudo make $MAKE_PARALLELISM -C "$SCRIPT_DIR"/../../Int/CMake/libHttpClient.Linux
132+
cmake -S "$SCRIPT_DIR" -B "$SCRIPT_DIR"/../../Int/CMake/libHttpClient.Linux -D CMAKE_BUILD_TYPE=$CONFIGURATION -D CMAKE_C_COMPILER=$C_COMPILER -D CMAKE_CXX_COMPILER=$CXX_COMPILER -D BUILD_SHARED_LIBS=OFF
133+
make $MAKE_PARALLELISM -C "$SCRIPT_DIR"/../../Int/CMake/libHttpClient.Linux
106134
fi

Build/libHttpClient.Linux/openssl_Linux.bash

100644100755
Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
set -e
4+
35
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
46
OPENSSL_SRC="$SCRIPT_DIR/../../External/openssl"
57
CONFIGURATION="Release"
@@ -22,48 +24,59 @@ while [[ $# -gt 0 ]]; do
2224
esac
2325
done
2426

25-
sudo hwclock --hctosys
26-
sudo rm -rf /usr/local/ssl
27-
sudo mkdir /usr/local/ssl
28-
sudo mkdir /usr/local/ssl/lib
29-
sudo mkdir /usr/local/ssl/include
30-
sudo mkdir /usr/local/ssl/include/openssl
27+
if [ -f "$SCRIPT_DIR/../../Out/x64/$CONFIGURATION/libcrypto.Linux/libcrypto.a" ]; then
28+
echo "Previously-built library present at $SCRIPT_DIR/../../Out/x64/$CONFIGURATION/libcrypto.Linux/libcrypto.a - skipping build"
29+
exit 0
30+
else
31+
echo "No previously-built library present at $SCRIPT_DIR/../../Out/x64/$CONFIGURATION/libcrypto.Linux/libcrypto.a - performing build"
32+
fi
33+
34+
OPENSSL_INSTALL_DIR="$SCRIPT_DIR/../../Int/x64/$CONFIGURATION/openssl.Linux/"
3135

32-
if [ ! -d /usr/local/ssl ] ; then
33-
echo "Directory /usr/local/ssl does not exist"
36+
rm -rf "$OPENSSL_INSTALL_DIR"
37+
mkdir -p "$OPENSSL_INSTALL_DIR"
38+
mkdir -p "$OPENSSL_INSTALL_DIR/lib"
39+
mkdir -p "$OPENSSL_INSTALL_DIR/include"
40+
mkdir -p "$OPENSSL_INSTALL_DIR/include/openssl"
41+
42+
if [ ! -d "$OPENSSL_INSTALL_DIR" ] ; then
43+
echo "Directory $OPENSSL_INSTALL_DIR does not exist"
3444
exit 1
3545
fi
36-
if [ ! -d /usr/local/ssl/lib ] ; then
37-
echo "Directory /usr/local/ssl/lib does not exist"
46+
if [ ! -d "$OPENSSL_INSTALL_DIR/lib" ] ; then
47+
echo "Directory $OPENSSL_INSTALL_DIR/lib does not exist"
3848
exit 1
3949
fi
40-
if [ ! -d /usr/local/ssl/include/openssl ] ; then
41-
echo "Directory /usr/local/ssl/include/openssl does not exist"
50+
if [ ! -d "$OPENSSL_INSTALL_DIR/include/openssl" ] ; then
51+
echo "Directory $OPENSSL_INSTALL_DIR/include/openssl does not exist"
4252
exit 1
4353
fi
4454

45-
if [ -f "$SCRIPT_DIR/../../Out/x64/$CONFIGURATION/libcrypto.Linux/libcrypto.a" ]; then
46-
echo "Previously-built library present at $SCRIPT_DIR/../../Out/x64/$CONFIGURATION/libcrypto.Linux/libcrypto.a - skipping build"
47-
exit 0
48-
else
49-
echo "No previously-built library present at $SCRIPT_DIR/../../Out/x64/$CONFIGURATION/libcrypto.Linux/libcrypto.a - performing build"
50-
fi
51-
5255
pushd $OPENSSL_SRC
53-
make clean
56+
if [ -f Makefile ]; then
57+
echo "Cleaning previous OpenSSL build"
58+
make clean
59+
fi
5460
sed -i -e 's/\r$//' Configure
5561

62+
CONFIGURE_ARGS=(
63+
--prefix=$OPENSSL_INSTALL_DIR
64+
--openssldir=$OPENSSL_INSTALL_DIR
65+
linux-x86_64-clang
66+
no-shared
67+
no-hw
68+
no-tests
69+
)
5670
if [ "$CONFIGURATION" = "Debug" ]; then
57-
# make libcrypto and libssl
58-
./Configure --prefix=/usr/local/ssl --openssldir=/usr/local/ssl linux-x86_64-clang no-shared no-hw no-tests -d
59-
else
60-
# make libcrypto and libssl
61-
./Configure --prefix=/usr/local/ssl --openssldir=/usr/local/ssl linux-x86_64-clang no-shared no-hw no-tests
71+
CONFIGURE_ARGS+=(-d)
6272
fi
6373

74+
# make libcrypto and libssl
75+
./Configure "${CONFIGURE_ARGS[@]}"
76+
6477
MAKE_PARALLELISM="-j$(nproc)" # run Make in parallel to speed up the build process
6578
make $MAKE_PARALLELISM CFLAGS="-fvisibility=hidden" CXXFLAGS="-fvisibility=hidden"
66-
sudo make install
79+
make install
6780
# copies binaries to final directory
6881
mkdir -p "$SCRIPT_DIR"/../../Out/x64/"$CONFIGURATION"/libcrypto.Linux
6982
cp -R "$PWD"/libcrypto.a "$SCRIPT_DIR"/../../Out/x64/"$CONFIGURATION"/libcrypto.Linux

Utilities/Pipelines/Tasks/linux-build.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ parameters:
55
steps:
66
- template: checkout.yml
77

8+
- task: Bash@3
9+
displayName: 'Ensure libHttpClient dependencies installed'
10+
inputs:
11+
filePath: './Build/libHttpClient.Linux/install_dependencies.bash'
12+
813
- task: Bash@3
914
displayName: 'Build and install OpenSSL'
1015
inputs:

0 commit comments

Comments
 (0)