Skip to content

Commit

Permalink
Replace AWK with Bash scripts (dotnet#46313)
Browse files Browse the repository at this point in the history
* Replace AWK with Bash scripts

* Replace awk usage with Bash or POSIX tr

* Simplify genmoduleindex.sh
  • Loading branch information
am11 authored Dec 23, 2020
1 parent 7a02953 commit 8f46367
Show file tree
Hide file tree
Showing 31 changed files with 281 additions and 310 deletions.
24 changes: 12 additions & 12 deletions eng/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ actInt=($(comm -12 <(printf '%s\n' "${actions[@]/#/-}" | sort) <(printf '%s\n' "
firstArgumentChecked=0

while [[ $# > 0 ]]; do
opt="$(echo "${1/#--/-}" | awk '{print tolower($0)}')"
opt="$(echo "${1/#--/-}" | tr "[:upper:]" "[:lower:]")"

if [[ $firstArgumentChecked -eq 0 && $opt =~ ^[a-zA-Z.+]+$ ]]; then
if [ $opt == "help" ]; then
Expand All @@ -188,7 +188,7 @@ while [[ $# > 0 ]]; do
showSubsetHelp
exit 0
else
passedSubset="$(echo "$2" | awk '{print tolower($0)}')"
passedSubset="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
if [ $passedSubset == "help" ]; then
showSubsetHelp
exit 0
Expand All @@ -203,7 +203,7 @@ while [[ $# > 0 ]]; do
echo "No architecture supplied. See help (--help) for supported architectures." 1>&2
exit 1
fi
passedArch="$(echo "$2" | awk '{print tolower($0)}')"
passedArch="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
case "$passedArch" in
x64|x86|arm|armel|arm64|wasm)
arch=$passedArch
Expand All @@ -222,7 +222,7 @@ while [[ $# > 0 ]]; do
echo "No configuration supplied. See help (--help) for supported configurations." 1>&2
exit 1
fi
passedConfig="$(echo "$2" | awk '{print tolower($0)}')"
passedConfig="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
case "$passedConfig" in
debug|release|checked)
val="$(tr '[:lower:]' '[:upper:]' <<< ${passedConfig:0:1})${passedConfig:1}"
Expand All @@ -242,7 +242,7 @@ while [[ $# > 0 ]]; do
echo "No framework supplied. See help (--help) for supported frameworks." 1>&2
exit 1
fi
val="$(echo "$2" | awk '{print tolower($0)}')"
val="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
arguments="$arguments /p:BuildTargetFramework=$val"
shift 2
;;
Expand All @@ -252,7 +252,7 @@ while [[ $# > 0 ]]; do
echo "No target operating system supplied. See help (--help) for supported target operating systems." 1>&2
exit 1
fi
passedOS="$(echo "$2" | awk '{print tolower($0)}')"
passedOS="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
case "$passedOS" in
windows)
os="windows" ;;
Expand Down Expand Up @@ -313,7 +313,7 @@ while [[ $# > 0 ]]; do
echo "No runtime configuration supplied. See help (--help) for supported runtime configurations." 1>&2
exit 1
fi
passedRuntimeConf="$(echo "$2" | awk '{print tolower($0)}')"
passedRuntimeConf="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
case "$passedRuntimeConf" in
debug|release|checked)
val="$(tr '[:lower:]' '[:upper:]' <<< ${passedRuntimeConf:0:1})${passedRuntimeConf:1}"
Expand All @@ -333,7 +333,7 @@ while [[ $# > 0 ]]; do
echo "No runtime flavor supplied. See help (--help) for supported runtime flavors." 1>&2
exit 1
fi
passedRuntimeFlav="$(echo "$2" | awk '{print tolower($0)}')"
passedRuntimeFlav="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
case "$passedRuntimeFlav" in
coreclr|mono)
val="$(tr '[:lower:]' '[:upper:]' <<< ${passedRuntimeFlav:0:1})${passedRuntimeFlav:1}"
Expand All @@ -353,7 +353,7 @@ while [[ $# > 0 ]]; do
echo "No libraries configuration supplied. See help (--help) for supported libraries configurations." 1>&2
exit 1
fi
passedLibConf="$(echo "$2" | awk '{print tolower($0)}')"
passedLibConf="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
case "$passedLibConf" in
debug|release)
val="$(tr '[:lower:]' '[:upper:]' <<< ${passedLibConf:0:1})${passedLibConf:1}"
Expand Down Expand Up @@ -398,7 +398,7 @@ while [[ $# > 0 ]]; do
echo "No value for portablebuild is supplied. See help (--help) for supported values." 1>&2
exit 1
fi
passedPortable="$(echo "$2" | awk '{print tolower($0)}')"
passedPortable="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
if [ "$passedPortable" = false ]; then
portableBuild=0
arguments="$arguments /p:PortableBuild=false"
Expand All @@ -411,7 +411,7 @@ while [[ $# > 0 ]]; do
echo "No value for keepNativeSymbols is supplied. See help (--help) for supported values." 1>&2
exit 1
fi
passedKeepNativeSymbols="$(echo "$2" | awk '{print tolower($0)}')"
passedKeepNativeSymbols="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
if [ "$passedKeepNativeSymbols" = true ]; then
arguments="$arguments /p:KeepNativeSymbols=true"
fi
Expand All @@ -424,7 +424,7 @@ while [[ $# > 0 ]]; do
arguments="$arguments /p:Ninja=true"
shift 1
else
ninja="$(echo "$2" | awk '{print tolower($0)}')"
ninja="$(echo "$2" | tr "[:upper:]" "[:lower:]")"
if [ "$ninja" = true ]; then
arguments="$arguments /p:Ninja=true"
shift 2
Expand Down
2 changes: 1 addition & 1 deletion eng/docker/build-docker-sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ configuration="Release"
privateaspnetcore=0

while [[ $# > 0 ]]; do
opt="$(echo "${1/#--/-}" | awk '{print tolower($0)}')"
opt="$(echo "${1/#--/-}" | tr "[:upper:]" "[:lower:]")"
case "$opt" in
-imagename|-t)
imagename=$2
Expand Down
5 changes: 3 additions & 2 deletions eng/native/build-commons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ __msbuildonunsupportedplatform=0
# processors available to a single process.
platform="$(uname)"
if [[ "$platform" == "FreeBSD" ]]; then
__NumProc=$(sysctl hw.ncpu | awk '{ print $2+1 }')
output=("$(sysctl hw.ncpu)")
__NumProc="$((output[1] + 1))"
elif [[ "$platform" == "NetBSD" || "$platform" == "SunOS" ]]; then
__NumProc=$(($(getconf NPROCESSORS_ONLN)+1))
elif [[ "$platform" == "Darwin" ]]; then
Expand All @@ -276,7 +277,7 @@ while :; do
break
fi

lowerI="$(echo "$1" | awk '{print tolower($0)}')"
lowerI="$(echo "$1" | tr "[:upper:]" "[:lower:]")"
case "$lowerI" in
-\?|-h|--help)
usage
Expand Down
20 changes: 10 additions & 10 deletions eng/native/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function(set_exports_linker_option exports_filename)
endfunction()

# compile_asm(TARGET target ASM_FILES file1 [file2 ...] OUTPUT_OBJECTS [variableName])
# CMake does not support the ARM or ARM64 assemblers on Windows when using the
# CMake does not support the ARM or ARM64 assemblers on Windows when using the
# MSBuild generator. When the MSBuild generator is in use, we manually compile the assembly files
# using this function.
function(compile_asm)
Expand Down Expand Up @@ -212,15 +212,15 @@ function(generate_exports_file)
list(REMOVE_AT INPUT_LIST -1)

if(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)
set(AWK_SCRIPT generateexportedsymbols.awk)
set(SCRIPT_NAME generateexportedsymbols.sh)
else()
set(AWK_SCRIPT generateversionscript.awk)
set(SCRIPT_NAME generateversionscript.sh)
endif()

add_custom_command(
OUTPUT ${outputFilename}
COMMAND ${AWK} -f ${CLR_ENG_NATIVE_DIR}/${AWK_SCRIPT} ${INPUT_LIST} >${outputFilename}
DEPENDS ${INPUT_LIST} ${CLR_ENG_NATIVE_DIR}/${AWK_SCRIPT}
COMMAND ${CLR_ENG_NATIVE_DIR}/${SCRIPT_NAME} ${INPUT_LIST} >${outputFilename}
DEPENDS ${INPUT_LIST} ${CLR_ENG_NATIVE_DIR}/${SCRIPT_NAME}
COMMENT "Generating exports file ${outputFilename}"
)
set_source_files_properties(${outputFilename}
Expand All @@ -230,18 +230,18 @@ endfunction()
function(generate_exports_file_prefix inputFilename outputFilename prefix)

if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(AWK_SCRIPT generateexportedsymbols.awk)
set(SCRIPT_NAME generateexportedsymbols.sh)
else()
set(AWK_SCRIPT generateversionscript.awk)
set(SCRIPT_NAME generateversionscript.sh)
if (NOT ${prefix} STREQUAL "")
set(AWK_VARS ${AWK_VARS} -v prefix=${prefix})
set(EXTRA_ARGS ${prefix})
endif()
endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)

add_custom_command(
OUTPUT ${outputFilename}
COMMAND ${AWK} -f ${CLR_ENG_NATIVE_DIR}/${AWK_SCRIPT} ${AWK_VARS} ${inputFilename} >${outputFilename}
DEPENDS ${inputFilename} ${CLR_ENG_NATIVE_DIR}/${AWK_SCRIPT}
COMMAND ${CLR_ENG_NATIVE_DIR}/${SCRIPT_NAME} ${inputFilename} ${EXTRA_ARGS} >${outputFilename}
DEPENDS ${inputFilename} ${CLR_ENG_NATIVE_DIR}/${SCRIPT_NAME}
COMMENT "Generating exports file ${outputFilename}"
)
set_source_files_properties(${outputFilename}
Expand Down
2 changes: 1 addition & 1 deletion eng/native/gen-buildsys.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ generator="Unix Makefiles"
__UnprocessedCMakeArgs=""

for i in "${@:7}"; do
upperI="$(echo "$i" | awk '{print toupper($0)}')"
upperI="$(echo "$i" | tr "[:lower:]" "[:upper:]")"
case "$upperI" in
# Possible build types are DEBUG, CHECKED, RELEASE, RELWITHDEBINFO.
DEBUG | CHECKED | RELEASE | RELWITHDEBINFO)
Expand Down
12 changes: 0 additions & 12 deletions eng/native/generateexportedsymbols.awk

This file was deleted.

25 changes: 25 additions & 0 deletions eng/native/generateexportedsymbols.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

saved=("$@")
while test $# -gt 0; do
case "$1" in
--help|-h)
printf "Usage:\ngenerateexportedsymbols.sh <filename>\n"
exit 1;;
esac
shift
done
set -- "${saved[@]}"

while read -r line; do
# Skip empty lines and comment lines starting with semicolon
if [[ "$line" =~ ^\;.*$|^[[:space:]]*$ ]]; then
continue
fi

# Remove the CR character in case the sources are mapped from
# a Windows share and contain CRLF line endings
line="${line//$'\r'/}"

printf "_%s\n" "${line//#/}"
done < "$1"
28 changes: 0 additions & 28 deletions eng/native/generateversionscript.awk

This file was deleted.

36 changes: 36 additions & 0 deletions eng/native/generateversionscript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash

saved=("$@")
while test $# -gt 0; do
case "$1" in
--help|-h)
printf "Usage:\ngenerateversionscript.sh <filename> <prefix>\n"
exit 1;;
esac
shift
done
set -- "${saved[@]}"

prefix="$2"

printf "V1.0 {\n global:\n"

while read -r line; do
# Skip empty lines and comment lines starting with semicolon
if [[ "$line" =~ ^\;.*$|^[[:space:]]*$ ]]; then
continue
fi

# Remove the CR character in case the sources are mapped from
# a Windows share and contain CRLF line endings
line="${line//$'\r'/}"

# Only prefix the entries that start with "#"
if [[ "$line" =~ ^#.*$ ]]; then
printf " %s%s;\n" "$prefix" "${line//#/}"
else
printf " %s;\n" "$line"
fi
done < "$1"

printf " local: *;\n};\n"
39 changes: 25 additions & 14 deletions eng/native/genmoduleindex.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,32 @@ if [[ "$#" -lt 2 ]]; then
exit 1
fi

OSName=$(uname -s)
function printIdAsBinary() {
id="$1"

case "$OSName" in
# Print length in bytes
bytesLength="${#id}"
printf "0x%02x, " "$((bytesLength/2))"

# Print each pair of hex digits with 0x prefix followed by a comma
while [[ "$id" ]]; do
printf '0x%s, ' "${id:0:2}"
id=${id:2}
done
}

case "$(uname -s)" in
Darwin)
# Extract the build id and prefix it with its length in bytes
dwarfdump -u $1 |
awk '/UUID:/ { gsub(/\-/,"", $2); printf("%02x", length($2)/2); print $2}' |
# Convert each byte of the id to 0x prefixed constant followed by comma
sed -E s/\(\.\.\)/0x\\1,\ /g > $2
;;
cmd="dwarfdump -u $1"
pattern='^UUID: ([0-9A-Fa-f\-]+)';;
*)
# Extract the build id and prefix it with its length in bytes
readelf -n $1 |
awk '/Build ID:/ { printf("%02x", length($3)/2); print $3 }' |
# Convert each byte of the id to 0x prefixed constant followed by comma
sed -E s/\(\.\.\)/0x\\1,\ /g > $2
;;
cmd="readelf -n $1"
pattern='^[[:space:]]*Build ID: ([0-9A-Fa-f\-]+)';;
esac

while read -r line; do
if [[ "$line" =~ $pattern ]]; then
printIdAsBinary "${BASH_REMATCH[1]//-/}"
break
fi
done < <(eval "$cmd") > "$2"
4 changes: 2 additions & 2 deletions eng/pipelines/evaluate-changed-paths.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ azure_variable=''
diff_target=''

while [[ $# > 0 ]]; do
opt="$(echo "${1/#--/-}" | awk '{print tolower($0)}')"
opt="$(echo "${1/#--/-}" | tr "[:upper:]" "[:lower:]")"
case "$opt" in
-help|-h)
usage
Expand Down Expand Up @@ -135,7 +135,7 @@ probePaths() {
local exclude_path_string=""
local include_path_string=""
local found_applying_changes=false

if [[ ${#exclude_paths[@]} -gt 0 ]]; then
echo ""
echo "******* Probing $_subset exclude paths *******";
Expand Down
3 changes: 2 additions & 1 deletion eng/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ then
else
platform="$(uname)"
if [ "$platform" = "FreeBSD" ]; then
maxProcesses=$(sysctl hw.ncpu | awk '{ print $2+1 }')
output=("$(sysctl hw.ncpu)")
maxProcesses="$((output[1] + 1))"
if [ "$platform" = "NetBSD" ] || [ "$platform" = "SunOS" ] ; then
maxProcesses=$(($(getconf NPROCESSORS_ONLN)+1))
else
Expand Down
Loading

0 comments on commit 8f46367

Please sign in to comment.