Skip to content

Commit

Permalink
Fix template subs with arrays (and things not yet arrays)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchcapper committed Jul 28, 2023
1 parent bb838ce commit 5e8ecf4
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 42 deletions.
11 changes: 6 additions & 5 deletions default_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ BUILD_AUX_SCRIPTS_ADDL = ()
INSTALL_FOLDER_NAME=final
PREFER_STATIC_LINKING=1 #note this is only effecting the library searching and may requireother defines, make sure to change the VCPKG triplet var (VCPKG_TRIPLET) to not have static in it as well
INSTALL_FOLDER=[SRC_FOLDER]/[INSTALL_FOLDER_NAME]
CONFIG_CMD_DEFAULT=--config-cache --prefix=[INSTALL_FOLDER]
CONFIG_CMD_GNULIB_ADDL=--enable-threads=windows --disable-silent-rules
CONFIG_CMD_ADDL= "" # --disable-nls
CONFIG_ADL_LIBS= "" # -lSomeLib
FORCE_BUILD_ID=x86_64-MSYS2-mingw32 #there is ming64 but not everything defaults to that triplet support
CONFIG_CMD_DEFAULT=("--config-cache" "--prefix=[INSTALL_FOLDER]" "--build=[FORCE_BUILD_ID]")
CONFIG_CMD_ADDL=()# --disable-nls
CONFIG_CMD_GNULIB_ADDL=("--enable-threads=windows" "--disable-silent-rules")
CONFIG_ADDL_LIBS= "" # -lSomeLib
CONFIG_DEFAULT_WINDOWS_LIBS="-lBcrypt -lWs2_32 -lShell32 -luser32 -lAdvapi32 -lKernel32 -lWinmm"
CONFIG_BYPASS_FILESYSTEM_LIST_FAIL=1
CONFIG_GETHOSTNAME_FIX=1
Expand All @@ -37,7 +38,7 @@ CONFIG_NO_DOCS=1

GIT_SETTINGS_DEFAULT=( "core.autocrlf false" "core.fileMode false" "core.eol lf" "diff.ignoreAllSpace true" "apply.ignoreWhitespace change" "apply.whitespace fix" )
GIT_SETTINGS_ADDL=() #
GIT_IGNORE_DEFAULT=("*.exe" "*.ilk" "*.obj" "*.log" "*.Plo" "*.pdb" "*.idb" "*.zip" "*.tmp" "*.o" ".deps" "*.a" "final" "proj_config.ini" "*~" "*confest*" )
GIT_IGNORE_DEFAULT=("*.exe" "*.ilk" "*.obj" "*.log" "*.Plo" "*.pdb" "*.idb" "*.zip" "*.tmp" "*.o" ".deps" "*.a" "final" "proj_config.ini" "*~" "*confest*" "_configure" )
GIT_IGNORE_ADDL=()

ADD_WIN_ARGV_LIB=1 #should windows expand wildcard command line args, most of hte time we want this, something like find -name "*.c" we don't want it on
Expand Down
75 changes: 55 additions & 20 deletions helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,40 @@ function run_logged_make(){
}
function configure_run(){
setup_build_env;
echo "Running ./configure ${config_cmd}" 1>&2
#gl_cv_host_operating_system="MSYS2" ac_cv_host="x86_64-w64-msys2" ac_cv_build="x86_64-w64-msys2"
ex ./configure "${FULL_CONFIG_CMD_ARR[@]}" > >(tee "${BLD_CONFIG_LOG_CONFIGURE_FILE}");
}
declare -g SETUP_BUILD_ENV_RUN=0

#sometimes we may update a command args from taking a users string to taking an array to fix escape issues, rather than require all scripts update right away we can use this to convert

function var_is_array(){
local VAR_NAME=$1
if [[ ! -v "$VAR_NAME" ]]; then
echo 0
fi
local dec_stmt="$(declare -p $VAR_NAME)"
[[ "${dec_stmt:0:10}" == 'declare -a' ]] && echo 1 || echo 0;
}

#This function was designed to convert variables we used to keep as strings (say command args) now store as arrays for better quoting support. Ironically this function is able to break strings up slightly better than the default bash word splitting when passed to a function, so things that previously wouldn't work in a string will work here
function make_array_if_str(){
local VAR_NAME=$1
if [[ ! -v "$VAR_NAME" ]]; then
return;
fi
if [[ "$(var_is_array $VAR_NAME)" != "1" ]]; then
if [[ "${!VAR_NAME}" != "" ]]; then
eval "array=(${!VAR_NAME})"
else
declare -a array=()
fi
declare -g $VAR_NAME=""
declare -g -a $VAR_NAME
declare -n ref_var="$VAR_NAME"
ref_var=("${array[@]}")
fi
}
function setup_build_env(){
ADL_LIB_FLAGS=""
ADL_C_FLAGS=""
Expand Down Expand Up @@ -205,11 +235,10 @@ function setup_build_env(){
if [[ $BLD_CONFIG_ADD_WIN_ARGV_LIB -eq 1 ]]; then
ADL_LIB_FLAGS+=" ${XLINKER_CMD} setargv.obj"
fi

config_cmd=$"$BLD_CONFIG_CONFIG_CMD_DEFAULT $BLD_CONFIG_CONFIG_CMD_ADDL"
if [[ $BLD_CONFIG_GNU_LIBS_USED -eq 1 ]]; then
config_cmd=$"$config_cmd $CONFIG_CMD_GNULIB_ADDL"

declare -g -a FULL_CONFIG_CMD_ARR=("${BLD_CONFIG_CONFIG_CMD_DEFAULT[@]}" "${BLD_CONFIG_CONFIG_CMD_ADDL[@]}")
if [[ $BLD_CONFIG_GNU_LIBS_USED -eq 1 ]]; then
FULL_CONFIG_CMD_ARR=("${FULL_CONFIG_CMD_ARR[@]}" "${CONFIG_CMD_GNULIB_ADDL[@]}")
fi
MSVC_DESIRED_LIB="msvcrt"
if [[ $BLD_CONFIG_BUILD_MSVC_RUNTIME_INFO_ADD_TO_C_AND_LDFLAGS -eq 1 ]]; then
Expand Down Expand Up @@ -276,7 +305,7 @@ function setup_build_env(){
AR="${WIN_SCRIPT_FOLDER}/wraps/lib.bat"
STATIC_ADD="-showIncludes ${STATIC_ADD}"
fi
export CXX="${CL}" AR="$AR" CC="${CL}" CYGPATH_W="echo" LDFLAGS="$ADL_LIB_FLAGS ${LDFLAGS}" CFLAGS="${STATIC_ADD} ${ADL_C_FLAGS} ${CFLAGS}" LIBS="${BLD_CONFIG_CONFIG_DEFAULT_WINDOWS_LIBS} ${BLD_CONFIG_CONFIG_ADL_LIBS}" LD="${LINK_PATH}";
export CXX="${CL}" AR="$AR" CC="${CL}" CYGPATH_W="echo" LDFLAGS="$ADL_LIB_FLAGS ${LDFLAGS}" CFLAGS="${STATIC_ADD} ${ADL_C_FLAGS} ${CFLAGS}" LIBS="${BLD_CONFIG_CONFIG_DEFAULT_WINDOWS_LIBS} ${BLD_CONFIG_CONFIG_ADDL_LIBS}" LD="${LINK_PATH}";
export -p > "$BLD_CONFIG_LOG_CONFIG_ENV_FILE";
}

Expand Down Expand Up @@ -304,13 +333,25 @@ function add_items_to_gitignore(){

function configure_fixes(){
cd $BLD_CONFIG_SRC_FOLDER
if [[ $BLD_CONFIG_CONFIG_BYPASS_FILESYSTEM_LIST_FAIL -eq 1 ]]; then
sed -i -E "s/^.+read list of mounted file systems.+$/echo 'Skipping FS Test fail'/" configure
if [[ -e "build-aux/config.guess" ]]; then #while for the most part we can just use configure --host overrides for this, incase somewthing else calls the guess we have this
echo -e "#!/bin/sh\necho $BLD_CONFIG_FORCE_BUILD_ID" > build-aux/config.guess
fi
if [[ $BLD_CONFIG_CONFIG_GETHOSTNAME_FIX -eq 1 ]]; then
#so while we will have GETHOSTNAME due to winsock it still wants to use the gnulib module which is fine, but with this set to comment outtrue it only redefined gethostname to use the rpl_gethostname but doesn't include the lib
sed -i -E "s/GL_COND_OBJ_GETHOSTNAME_TRUE='#'/GL_COND_OBJ_GETHOSTNAME_TRUE=' '/g" configure
if [[ -e configure ]]; then
cp configure _configure
if [[ $BLD_CONFIG_CONFIG_BYPASS_FILESYSTEM_LIST_FAIL -eq 1 ]]; then
sed -i -E "s/^.+read list of mounted file systems.+$/echo 'Skipping FS Test fail'/" _configure
fi
if [[ $BLD_CONFIG_CONFIG_GETHOSTNAME_FIX -eq 1 ]]; then
#so while we will have GETHOSTNAME due to winsock it still wants to use the gnulib module which is fine, but with this set to comment outtrue it only redefined gethostname to use the rpl_gethostname but doesn't include the lib
sed -i -E "s/GL_COND_OBJ_GETHOSTNAME_TRUE='#'/GL_COND_OBJ_GETHOSTNAME_TRUE=' '/g" _configure
fi
#some things incorrectly harvest strings up to the \n but literally leave a CR bare in the configure file this screws you if you try to edit it at all as many editors will change a bare CR to a CL adding a new line
sed -i -E 's#\r##g' _configure
if ! cmp -s "configure" "_configure"; then
mv _configure configure
fi
fi



}
Expand Down Expand Up @@ -338,6 +379,9 @@ function git_settings_to_env(){
export GIT_CONFIG_COUNT
}
function startcommon(){
# need to do these early before everything else


SetupIgnores;
DoTemplateSubs;
unset TMP
Expand Down Expand Up @@ -367,17 +411,8 @@ function startcommon(){
fi
trace_init;

#echo DEFAULT GIT SETTINGS ${BLD_CONFIG_GIT_SETTINGS_DEFAULT[@]} and other array: "${BLD_CONFIG_GIT_SETTINGS_ADDL[@]}"

#echo GIT SSETTINGSS IS: ${GIT_SETTINGS[@]}

git_settings_to_env;

#if [ -d "$BLD_CONFIG_SRC_FOLDER" ]; then
#cd $BLD_CONFIG_SRC_FOLDER
#else
#cd $BLD_CONFIG_BASE_FOLDER
#fi
if [[ -n "$BLD_CONFIG_CMAKE_STYLE" ]]; then
cmake_init;
fi
Expand Down
2 changes: 1 addition & 1 deletion helpers_bashtrace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function trace_final(){
function ex(){
local PARAMS=("${@:2}")
EX_CUR_CMD="$1 ${PARAMS[@]@Q}"
echo "EX Running $EX_CUR_CMD"
echo "EX Running $EX_CUR_CMD" >&2
echo "$EX_CUR_CMD" >&3
"$@"
EX_CUR_CMD=""
Expand Down
61 changes: 45 additions & 16 deletions helpers_ini.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,18 @@ ini_read(){
fi
}


function _DoTemplateSub(){
local VAR_NAME="$1"
declare -n cur_val="$VAR_NAME"
for sub_value in "${TEMPLATE_SUB_ORDER[@]}"; do
declare -n replace_with="BLD_CONFIG_$sub_value"
cur_val=${cur_val//\[$sub_value\]/$replace_with}
done
}
DoTemplateSubs(){
INI_OUT=""
TEMPLATE_SUB_ORDER+=("STATIC_RELEASE_TRIPLET_AUTO" "CMAKE_BUILD_TARGET_AUTO")
local VAR_ARRAY_START_REGEX="^[ ]*\("
TEMPLATE_SUB_ORDER+=("STATIC_RELEASE_TRIPLET_AUTO" "CMAKE_BUILD_TARGET_AUTO")
BLD_CONFIG_STATIC_RELEASE_TRIPLET_AUTO=""
BLD_CONFIG_CMAKE_BUILD_TARGET_AUTO="Release"
BLD_CONFIG_CMAKE_BUILD_TYPE_AUTO="MinSizeRel"
Expand All @@ -145,27 +153,48 @@ DoTemplateSubs(){
BLD_CONFIG_STATIC_RELEASE_TRIPLET_AUTO+="-release"
fi

for value in "${TEMPLATE_SUB_ORDER[@]}"
do
declare -n cur_val="BLD_CONFIG_$value"
for sub_value in "${TEMPLATE_SUB_ORDER[@]}"
do
declare -n replace_with="BLD_CONFIG_$sub_value"
cur_val=${cur_val//\[$sub_value\]/$replace_with}
done
if [[ ${OPTION_TYPES["$value"]} -eq 1 ]]; then
eval "declare -g -a BLD_CONFIG_$value=$cur_val"
for value in "${TEMPLATE_SUB_ORDER[@]}"; do
local var_name="BLD_CONFIG_$value"
# if [[ (var_is_array "$var_name") ]]; then
# local var_val=
# for i in ${!array[@]}; do
# _DoTemplateSub

# else
# _DoTemplateSub "$var_name"
# fi
declare -n cur_val="$var_name"
local IS_ARRAY="$(var_is_array $var_name)";
if [[ $IS_ARRAY != "0" || $cur_val == *"["* ]]; then
for sub_value in "${TEMPLATE_SUB_ORDER[@]}"; do
declare -n replace_with="BLD_CONFIG_$sub_value"
if [[ $IS_ARRAY == "0" ]]; then
cur_val=${cur_val//\[$sub_value\]/$replace_with}
else
cur_val=("${cur_val[@]//\[$sub_value\]/$replace_with}")
fi
done
fi
if [[ ${OPTION_TYPES["$value"]} -eq 1 && $IS_ARRAY == "0" ]]; then #if it is supposed to be an array but isn't quite yet

if [[ "$cur_val" =~ $VAR_ARRAY_START_REGEX ]]; then #if its a string starting with a parens assuming we can eval to an array
eval "declare -g -a BLD_CONFIG_$value=$cur_val"
else #its not an array so transform to an array
if [[ "$var_name" == "BLD_CONFIG_CONFIG_CMD_DEFAULT" || "$var_name" == "BLD_CONFIG_CONFIG_CMD_ADDL" ]]; then
make_array_if_str "$var_name"
else
echo "Likely an error the variable: $var_name is supposed to be an array but it seeems to be a strin its value is: $cur_val"
exit 1
fi
fi
fi
done
for value in "${TEMPLATE_SUB_ORDER[@]}"
do
for value in "${TEMPLATE_SUB_ORDER[@]}"; do
if [[ $BLD_CONFIG_PRINT_VARS_AT_START -eq 1 ]]; then
echo $"${value}=>>${cur_val}|"
fi

if [[ $SKIP_STEP == "export_config" ]]; then
#TMP_VAR_NAME="BLD_CONFIG_${value}"
#VAL_TO=$"${!TMP_VAR_NAME}"
VAL_TO=`declare -p "BLD_CONFIG_${value}" | sed -E 's/declare \-\- //' | sed -E 's/^BLD_CONFIG_//'`
if [[ "$VAL_TO" =~ "declare -a" ]]; then
VAL_TO=`echo "$VAL_TO" | sed -E 's/\[[0-9]{1,2}\]=//g' | sed -E 's/declare \-a //' | sed -E 's/^BLD_CONFIG_//'`
Expand Down

0 comments on commit 5e8ecf4

Please sign in to comment.