1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
#!/usr/bin/env bash
#
# Auxiliary functions for the KOW Framework main build system
#
# @author Marcelo C. de Freitas
set -e
######################
# Environment Checks #
######################
# Check if a given command is in path;
# usage:
# check_in_path COMMAND_NAME
check_in_path(){
echo -n "Looking for $1 ... "
hash $1 2>&- || { echo "[false]"; echo >&2 "I need $1 in path but I can't find it... aborting"; exit 1; } && echo "[ok]"
}
# Check if a project file is available
# usage:
# check_project projectname
check_project(){
proj=$1;
echo -n "Looking for project $proj ... "
${GPRBUILD} -ws -P$proj 2>&- || { echo "[false]"; echo "${GPRBUILD} can't find $proj in ADA_PROJECT_PATH"; exit -1;} && echo "[ok]";
}
# Copy updating a regular file or directory
# usage:
# cpu origin destination
cpu(){
origin="$1"
fname=$(basename "$1")
destination="$2/$fname"
if [[ -f "$origin" ]]
then
if [[ "$origin" -nt "$destination" ]]
then
cp "$origin" "$destination"
else
echo "Skipping \"$origin\"" >> configure.log
fi
else
echo "Can't copy (not a regular file): \"$origin\"" >&2
exit 1;
fi
}
############################
# Configuration Management #
############################
# reset the configuration file
init_configuration(){
echo -n "" > .configuration
}
# Set a configuration key/value:
# usage:
# setconfiguration key value
set_configuration() {
echo export $1=\"$2\" >> .configuration
}
# Printout the configuration file
# usage:
# cat_configuration
cat_configuration(){
cat .configuration
}
# load the configuration file:
# usage:
# load_configuration
load_configuration(){
if [[ -f .configuration ]]
then
source .configuration
else
echo "Please run configure first" >&2;
exit 1;
fi
}
###################
# Data Processing #
###################
# iterate for printing a list of declarations, used internally by print_enum_declaration and print_for_declaration
# usage:
# iterate_enum_list "the list of values" echo_function_name
#
# The list of values should respect the format:
# number name
# number name
# number name
# number name
#
# and eatch line is trimmed before calling the given function
iterate_enum_list(){
local is_first=1;
echo "$1" | while read a
do
if [[ "$a" = "" ]]
then
echo -n
#skip empty lines
else
if [[ $is_first -eq 1 ]]
then
is_first=0;
else
echo ',';
fi;
echo -n " ";
$2 $a
fi
done
echo;
}
_enum_declaration(){
echo -n $2;
}
# For each entry print as expected for the enum declaration type
echo_enum_declaration(){
iterate_enum_list "$1" _enum_declaration
}
_for_declaration(){
echo -n "$2 => $1";
}
echo_for_declaration(){
iterate_enum_list "$1" _for_declaration
}
# Will set a enum value using the same list as the one used in iterate_enum_list
# in the given file (edit the given file).
# usage:
# set_enum_values FILE LIST_OF_VALUES PREFIX
#
# This will replace:
# %${PREFIX}_DECLARATION% with the result of echo_enum_declaration
# %${PREFIX}_FOR% with the result of echo_for_declaration
#
set_enum_values(){
local outfile="$1"
local values="$2"
local prefix="$3"
declaration_values=`echo_enum_declaration "$values"`
for_values=`echo_for_declaration "$values"`
replace_in_file "$outfile" "%${prefix}_DECLARATION%" "$declaration_values"
replace_in_file "$outfile" "%${prefix}_FOR%" "$for_values"
echo "[ok]"
}
# Simple str_replace in a file
# usage
# replace_in_file FILENAME FROM TO
replace_in_file(){
filename="$1"
from="$2"
to=$(echo "$3" | sed -e 's/$/\\&/' | sed -e 's/\//\\&/g' );
sed -i -e "s/$from/$to /" "$filename"
}
################################
# Gnatprep def file management #
################################
# see the documentation for configuration files; basically the same thing
init_gnatprep() {
echo -n "" > gnatprep.def
}
set_gnatprep(){
echo $1:=\"$2\" >> gnatprep.def
}
# transform a list of parameters in a format both sed and gprbuild will understand...
# the output of this function is meant to be used by replace_in_file, which processes / and new lines
sedfy_gpr_list(){
is_first=1
for option in $1
do
if [[ $is_first -eq 1 ]]
then
is_first=0;
else
echo -n ","
fi
#option=`echo $i | sed 's/\//\\\&/g'`
echo -n \\\"$option\\\"
done
}
############
# Building #
############
build_libraries(){
if [[ "$enable_static" = "true" ]]
then
build_library static
fi
if [[ "$enable_relocatable" = "true" ]]
then
build_library relocatable
fi
}
build_library(){
kind=$1;
echo "Building $kind library";
export LIBRARY_KIND=$kind
$GPRBUILD -P$work_path/lib/gnat/$project.gpr -j$processors --create-missing-dirs $gprbuild_params
}
#############
# File list #
#############
gen_filelist(){
list="$PWD/files.list"
cd "$work_path" && find * > "$list"
}
cat_filelist(){
if [[ -f files.list ]]
then
cat files.list
else
echo "Please remember to build $project first" >&2;
fi;
}
# iterate over the list of files...
# usage:
# iterate_filelist thecommandtobexecuted
iterate_filelist(){
cat_filelist | while read a; do $1 "$a";done
}
# Reverse iterate in the sense of listing first files than directories..
reverse_iterate_filelist(){
cat_filelist | sort -nr | while read a; do $1 "$a";done
}
###########
# Install #
###########
install_item(){
if [[ -d "$work_path"/"$1" ]]
then
install_directory "$1";
else
install_file "$1";
fi
}
install_directory(){
install -d "$prefix/$1";
}
install_file(){
install "$work_path/$1" "$prefix/$1"
}
#############
# Uninstall #
#############
uninstall_item(){
if [[ -d "$work_path"/"$1" ]]
then
uninstall_directory "$1";
else
uninstall_file "$1";
fi
}
uninstall_directory(){
rmdir "$prefix/$1" || echo skipping "$prefix/$1"
}
uninstall_file(){
rm "$prefix/$1"
}
|