download_and_compile.sh: changes to the generated scripts

The first goal of this commit was to ensure that the LD_LIBRARY_PATH
setting performed in some of the generated scripts uses absolute paths,
otherwise if a process (such as ctest) changes the current directory
before starting an executable that needs our LD_LIBRARY_PATH setting, we
can have problems (thanks to Patrick Callahan for the report).

Some refactoring of the automated script generation plus a few other
fixes concerning these scripts were done along the way (use here
documents for better readability; don't use 'echo' with options as this
is non-portable; etc.).
This commit is contained in:
Florent Rougon 2022-11-30 17:40:32 +01:00
parent 1e65f0e144
commit 7469b39cad

View File

@ -63,7 +63,7 @@ _elementIn(){
# From <https://stackoverflow.com/questions/1527049/how-can-i-join-elements-of-an-array-in-bash>
function _joinBy(){
local d="$1"; shift
echo -n "$1"; shift
printf "%s" "$1"; shift
printf "%s" "${@/#/$d}"
}
@ -1511,28 +1511,29 @@ if _elementIn "FGFS" "${WHATTOBUILD[@]}" || \
fi
cd "$CBD"
paths="../../$SIMGEAR_INSTALL_DIR/lib:../../$OSG_INSTALL_DIR/lib:../../$OPENRTI_INSTALL_DIR/lib:../../$PLIB_INSTALL_DIR/lib"
gdb="gdb"
common=""
common="${common}#!/bin/sh\n"
common="${common}cd \"\$(dirname \"\$0\")\"\n"
common="${common}cd '$SUB_INSTALL_DIR/$FGFS_INSTALL_DIR/bin'\n"
paths="\$d/$SIMGEAR_INSTALL_DIR/lib:\$d/$OSG_INSTALL_DIR/lib:\$d/$OPENRTI_INSTALL_DIR/lib:\$d/$PLIB_INSTALL_DIR/lib"
common="#! /bin/sh"$'\n\n'
if [[ "$(uname)" == 'OpenBSD' ]]; then
# Force use of our zlib.
paths="$paths:../../$ZLIB_INSTALL_DIR/lib"
paths="$paths:\$d/$ZLIB_INSTALL_DIR/lib"
# OpenBSD's base gdb is too old; `pkg_add egdb` gives one that we can use.
gdb="egdb"
common="${common}ulimit -d 4194304\n"
common="${common}ulimit -d 4194304"$'\n'
fi
set_ld_library_path="export LD_LIBRARY_PATH='$paths'\"\${LD_LIBRARY_PATH:+:}\${LD_LIBRARY_PATH}\""
common="${common}${set_ld_library_path}\n"
set_ld_library_path="\
d=\$(realpath \"\$(dirname \"\$0\")/$SUB_INSTALL_DIR\")
export LD_LIBRARY_PATH=$paths\${LD_LIBRARY_PATH:+:}\${LD_LIBRARY_PATH}"
common="\
${common}${set_ld_library_path}
cd \"\$d/$FGFS_INSTALL_DIR/bin\""$'\n'
SCRIPT=run_fgfs.sh
echo -en "$common" > $SCRIPT
echo "exec ./fgfs --fg-root=\"\$PWD/../fgdata\" \"\$@\"" >> $SCRIPT
cat >"$SCRIPT" <<EndOfScriptText
$common
exec ./fgfs --fg-root="\$d/$FGFS_INSTALL_DIR/fgdata" "\$@"
EndOfScriptText
chmod 755 $SCRIPT
SCRIPT=run_fgfs_debug.sh
@ -1572,7 +1573,7 @@ eval set -- "\$TEMP"
while true; do
case "\$1" in
-c|--core-file) CORE_FILE="\$2"; shift 2 ;;
-c|--core-file) CORE_FILE=\$(realpath "\$2"); shift 2 ;;
--help) echo "\$USAGE"; exit 0 ;;
--) shift; break ;;
*) echo "\$PROGNAME: unexpected option '\$1'; please report a bug." >&2
@ -1580,16 +1581,16 @@ while true; do
esac
done
cd "\$(dirname "\$0")/$SUB_INSTALL_DIR/$FGFS_INSTALL_DIR/bin"
${set_ld_library_path}
cd "\$d/$FGFS_INSTALL_DIR/bin"
if [ -z "\$CORE_FILE" ]; then
gdb --directory="$INSTALL_DIR_SIMGEAR" \\
--directory="$INSTALL_DIR_FGFS" \\
--args ./fgfs --fg-root="$INSTALL_DIR_FGFS/fgdata" "\$@"
gdb --directory="$CBD/simgear" \\
--directory="$CBD/flightgear" \\
--args ./fgfs --fg-root="\$d/$FGFS_INSTALL_DIR/fgdata" "\$@"
else
gdb --directory="$INSTALL_DIR_SIMGEAR" \\
--directory="$INSTALL_DIR_FGFS" \\
gdb --directory="$CBD/simgear" \\
--directory="$CBD/flightgear" \\
--core="\$CORE_FILE" ./fgfs
fi
EndOfScriptText
@ -1598,8 +1599,7 @@ EndOfScriptText
# Useful for debugging library problems.
SCRIPT=run_ldd.sh
cat >"$SCRIPT" <<EndOfScriptText
#!/bin/sh
$common
usage() {
echo "Usage: \$0 LDD_ARGUMENT..."
echo "Run 'ldd' with the same LD_LIBRARY_PATH setup as done inside run_fgfs.sh."
@ -1613,10 +1613,6 @@ if [ \$# -eq 0 ] || [ "\$1" = "--help" ]; then
exit 1
fi
cd "\$(dirname "\$0")"
cd '$SUB_INSTALL_DIR/$FGFS_INSTALL_DIR/bin'
export LD_LIBRARY_PATH='../../$SIMGEAR_INSTALL_DIR/lib:../../$OSG_INSTALL_DIR/lib:../../$OPENRTI_INSTALL_DIR/lib:../../$PLIB_INSTALL_DIR/lib'"\${LD_LIBRARY_PATH:+:}\${LD_LIBRARY_PATH}"
exec ldd "\$@"
EndOfScriptText
chmod 755 "$SCRIPT"
@ -1626,18 +1622,17 @@ EndOfScriptText
cat >"$SCRIPT" <<EndOfScriptText
#! /bin/sh
d="\$(dirname "\$0")/$SUB_INSTALL_DIR"
export LD_LIBRARY_PATH="\$d/simgear/lib:\$d/openscenegraph/lib:\$d/openrti/lib:\$d/plib/lib\${LD_LIBRARY_PATH:+:}\${LD_LIBRARY_PATH}"
${set_ld_library_path}
exec "\$@"
EndOfScriptText
chmod 755 "$SCRIPT"
SCRIPT=run_fgcom.sh
echo "#!/bin/sh" > $SCRIPT
echo "cd \"\$(dirname \"\$0\")\"" >> $SCRIPT
echo "cd '$SUB_INSTALL_DIR/$FGFS_INSTALL_DIR/bin'" >> $SCRIPT
echo "exec ./fgcom \"\$@\"" >> $SCRIPT
chmod 755 $SCRIPT
cat >"$SCRIPT" <<EndOfScriptText
$common
exec ./fgcom "\$@"
EndOfScriptText
chmod 755 "$SCRIPT"
fi
#######################################################
@ -1672,12 +1667,14 @@ if _elementIn "FGRUN" "${WHATTOBUILD[@]}"; then
cd "$CBD"
SCRIPT=run_fgrun.sh
echo "#!/bin/sh" > $SCRIPT
echo "cd \"\$(dirname \"\$0\")\"" >> $SCRIPT
echo "cd '$SUB_INSTALL_DIR/$FGRUN_INSTALL_DIR/bin'" >> $SCRIPT
echo "export LD_LIBRARY_PATH='../../$SIMGEAR_INSTALL_DIR/lib:../../$OSG_INSTALL_DIR/lib:../../$OPENRTI_INSTALL_DIR/lib:../../$PLIB_INSTALL_DIR/lib'\"\${LD_LIBRARY_PATH:+:}\${LD_LIBRARY_PATH}\"" \
>> $SCRIPT
echo "./fgrun --fg-exe=\"\$PWD\"/../../'$FGFS_INSTALL_DIR/bin/fgfs' --fg-root=\"\$PWD\"/../../'$FGFS_INSTALL_DIR/fgdata' \"\$@\"" >> $SCRIPT
cat >"$SCRIPT" <<EndOfScriptText
#!/bin/sh
${set_ld_library_path}
cd "\$d/$FGRUN_INSTALL_DIR/bin"
exec ./fgrun --fg-exe="\$d/$FGFS_INSTALL_DIR/bin/fgfs" \
--fg-root="\$d/$FGFS_INSTALL_DIR/fgdata" "\$@"
EndOfScriptText
chmod 755 $SCRIPT
fi
@ -1703,11 +1700,11 @@ if _elementIn "FGO" "${WHATTOBUILD[@]}"; then
cd "$CBD"
SCRIPT=run_fgo.sh
echo "#!/bin/sh" > $SCRIPT
echo "cd \"\$(dirname \"\$0\")\"" >> $SCRIPT
echo "cd '$SUB_INSTALL_DIR'" >> $SCRIPT
echo "cd '$FGO_INSTALL_DIR'" >> $SCRIPT
echo "python ./fgo" >> $SCRIPT
cat >"$SCRIPT" <<EndOfScriptText
#!/bin/sh
cd "\$(dirname "\$0")/$SUB_INSTALL_DIR/$FGO_INSTALL_DIR"
exec python2 ./fgo
EndOfScriptText
chmod 755 $SCRIPT
fi
@ -1759,10 +1756,11 @@ if _elementIn "FGX" "${WHATTOBUILD[@]}"; then
cd "$CBD"
SCRIPT=run_fgx.sh
echo "#!/bin/sh" > $SCRIPT
echo "cd \"\$(dirname \"\$0\")\"" >> $SCRIPT
echo "cd '$FGX_INSTALL_DIR'" >> $SCRIPT
echo "./fgx \"\$@\"" >> $SCRIPT
cat >"$SCRIPT" <<EndOfScriptText
#!/bin/sh
cd "\$(dirname "\$0")/$SUB_INSTALL_DIR/$FGX_INSTALL_DIR"
exec ./fgx "\$@"
EndOfScriptText
chmod 755 $SCRIPT
fi
@ -1785,10 +1783,11 @@ if _elementIn "ATCPIE" "${WHATTOBUILD[@]}"; then
cd "$CBD"
SCRIPT=run_atcpie.sh
echo "#!/bin/sh" > $SCRIPT
echo "cd \"\$(dirname \"\$0\")\"" >> $SCRIPT
echo "cd '$SUB_INSTALL_DIR/$ATCPIE_INSTALL_DIR'" >> $SCRIPT
echo "./ATC-pie.py \"\$@\"" >> $SCRIPT
cat >"$SCRIPT" <<EndOfScriptText
#!/bin/sh
cd "\$(dirname "\$0")/$SUB_INSTALL_DIR/$ATCPIE_INSTALL_DIR"
exec ./ATC-pie.py "\$@"
EndOfScriptText
chmod 755 $SCRIPT
fi
@ -1811,10 +1810,11 @@ if _elementIn "OPENRADAR" "${WHATTOBUILD[@]}"; then
fi
SCRIPT=run_openradar.sh
echo "#!/bin/sh" > $SCRIPT
echo "cd \"\$(dirname \"\$0\")\"" >> $SCRIPT
echo "cd install/OpenRadar" >> $SCRIPT
echo "java -jar OpenRadar.jar" >> $SCRIPT
cat >"$SCRIPT" <<EndOfScriptText
#!/bin/sh
cd "\$(dirname "\$0")/$SUB_INSTALL_DIR/OpenRadar"
exec java -jar OpenRadar.jar
EndOfScriptText
chmod 755 $SCRIPT
fi
@ -1849,26 +1849,29 @@ if _elementIn "TERRAGEAR" "${WHATTOBUILD[@]}"; then
_make terragear
cd "$CBD"
echo "#!/bin/sh" > run_tg-construct.sh
echo "cd \"\$(dirname \"\$0\")\"" >> run_tg-construct.sh
echo "cd install/terragear/bin" >> run_tg-construct.sh
echo "export LD_LIBRARY_PATH='$INSTALL_DIR_SIMGEAR/lib'\"\${LD_LIBRARY_PATH:+:}\${LD_LIBRARY_PATH}\"" \
>> run_tg-construct.sh
echo "./tg-construct \"\$@\"" >> run_tg-construct.sh
SCRIPT=run_tg-construct.sh
cat >"$SCRIPT" <<EndOfScriptText
#!/bin/sh
${set_ld_library_path}
cd "\$d/terragear/bin"
exec ./tg-construct "\$@"
EndOfScriptText
echo "#!/bin/sh" > run_ogr-decode.sh
echo "cd \"\$(dirname \"\$0\")\"" >> run_ogr-decode.sh
echo "cd install/terragear/bin" >> run_ogr-decode.sh
echo "export LD_LIBRARY_PATH='$INSTALL_DIR_SIMGEAR/lib'\"\${LD_LIBRARY_PATH:+:}\${LD_LIBRARY_PATH}\"" \
>> run_ogr-decode.sh
echo "./ogr-decode \"\$@\"" >> run_ogr-decode.sh
SCRIPT=run_ogr-decode.sh
cat >"$SCRIPT" <<EndOfScriptText
#!/bin/sh
${set_ld_library_path}
cd "\$d/terragear/bin"
exec ./ogr-decode "\$@"
EndOfScriptText
echo "#!/bin/sh" > run_genapts850.sh
echo "cd \"\$(dirname \"\$0\")\"" >> run_genapts850.sh
echo "cd install/terragear/bin" >> run_genapts850.sh
echo "export LD_LIBRARY_PATH='$INSTALL_DIR_SIMGEAR/lib'\"\${LD_LIBRARY_PATH:+:}\${LD_LIBRARY_PATH}\"" \
>> run_genapts850.sh
echo "./genapts850 \"\$@\"" >> run_genapts850.sh
SCRIPT=run_genapts850.sh
cat >"$SCRIPT" <<EndOfScriptText
#!/bin/sh
${set_ld_library_path}
cd "\$d/terragear/bin"
exec ./genapts850 "\$@"
EndOfScriptText
chmod 755 run_tg-construct.sh run_ogr-decode.sh run_genapts850.sh
fi
@ -1918,10 +1921,9 @@ if _elementIn "TERRAGEARGUI" "${WHATTOBUILD[@]}"; then
_log "Creating $SCRIPT"
cat >"$SCRIPT" <<EndOfScriptText
#! /bin/sh
cd "\$(dirname "\$0")"
cd '$SUB_INSTALL_DIR/$TGGUI_INSTALL_DIR/bin'
export LD_LIBRARY_PATH='$INSTALL_DIR_SIMGEAR/lib'"\${LD_LIBRARY_PATH:+:}\${LD_LIBRARY_PATH}"
./TerraGUI "\$@"
${set_ld_library_path}
cd "\$d/$TGGUI_INSTALL_DIR/bin"
exec ./TerraGUI "\$@"
EndOfScriptText
chmod 755 "$SCRIPT"
fi