Skip to content

Commit ac5b094

Browse files
RISC-OS-CommunityRISC-OS-Community
RISC-OS-Community
authored and
RISC-OS-Community
committed
Sync from template@225a66ae16e0898cba4103d231a72b3355158fc4
1 parent 225a66a commit ac5b094

File tree

1 file changed

+177
-71
lines changed

1 file changed

+177
-71
lines changed

.rocog/scripts/ux-src

Lines changed: 177 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
# author: Paolo Fabio Zaino
77
# license: Copyright by Paolo Fabio Zaino, all rights reserved
88
# distributed under CDDL v1.1
9-
#
9+
#
1010
# Long description:
1111
# This script is used to generate a temporary (and not pushable) ux-src directory
1212
# that will be used to reconfigure your source code in a way that is "usable" by
1313
# Linux/BSD based tools for code analysis, SAST etc.
1414
#
1515
# Usage:
1616
# To use this script it's easy:
17-
# ux-src gen
17+
# ux-src gen
1818
# the above will create ux-src in your local directory and will link all your source files
1919
# ina UNIXy way.
2020
# To remove ux-src directory type:
@@ -30,6 +30,9 @@ then
3030
curr_dir="$(pwd)"
3131
fi
3232

33+
# Debug mode:
34+
debug=0
35+
3336
# Display command usage:
3437
function usage()
3538
{
@@ -41,27 +44,164 @@ function usage()
4144
# Link files from directory f_dir to directory ux-src with extension f_ext:
4245
function link_files()
4346
{
44-
f_dir="$1"
45-
f_ext="$2"
46-
if [ -d ${curr_dir}/src/${f_dir} ]
47+
local src_dir="$1"
48+
local dst_dir="$2"
49+
local f_dir="$3"
50+
local f_ext="$4"
51+
if [ -d ${src_dir}/${f_dir} ]
4752
then
48-
for f in ${curr_dir}/src/${f_dir}/*
53+
for f in ${src_dir}/${f_dir}/*
4954
do
5055
if [ ! -f ${f} ]
5156
then
5257
continue
5358
else
54-
fname="$(basename ${f})"
55-
if [ "${env}" == "github" ]
59+
local fname="$(basename ${f})"
60+
if [ "${env}" == "github" ]
61+
then
62+
# GitHub Actions do not support symlinks:
63+
cp ${f} ${dst_dir}/${fname}.${f_ext} > /dev/null 2>&1
64+
else
65+
ln -s ${f} ${dst_dir}/${fname}.${f_ext} > /dev/null 2>&1
66+
fi
67+
fi
68+
done
69+
fi
70+
}
71+
72+
# Check path
73+
function check_path()
74+
{
75+
local last_dir="$1"
76+
if [ "${last_dir}" == "c" ] || [ "${last_dir}" == "C" ] || \
77+
[ "${last_dir}" == "h" ] || [ "${last_dir}" == "H" ] || \
78+
[ "${last_dir}" == "cpp" ] || [ "${last_dir}" == "CPP" ] || \
79+
[ "${last_dir}" == "cxx" ] || [ "${last_dir}" == "CXX" ] || \
80+
[ "${last_dir}" == "cc" ] || [ "${last_dir}" == "CC" ] || \
81+
[ "${last_dir}" == "c++" ] || [ "${last_dir}" == "C++" ] || \
82+
[ "${last_dir}" == "hpp" ] || [ "${last_dir}" == "HPP" ] || \
83+
[ "${last_dir}" == "hxx" ] || [ "${last_dir}" == "HXX" ] || \
84+
[ "${last_dir}" == "s" ] || [ "${last_dir}" == "S" ] || \
85+
[ "${last_dir}" == "Hdr" ] || [ "${last_dir}" == "hdr" ] || \
86+
[ "${last_dir}" == "fth" ] || [ "${last_dir}" == "FTH" ] || \
87+
[ "${last_dir}" == "p" ] || [ "${last_dir}" == "P" ] || \
88+
[ "${last_dir}" == "pl" ] || [ "${last_dir}" == "PL" ] || \
89+
[ "${last_dir}" == "bas" ] || [ "${last_dir}" == "BAS" ];
90+
then
91+
return 1
92+
else
93+
return 0
94+
fi
95+
}
96+
97+
# check for files with the known directory structure and link them in
98+
# the ux-src directory with their appropriate extension:
99+
function find_files_and_link() {
100+
local src_dir="$1"
101+
local dst_dir="$2"
102+
103+
# Link C files (if any):
104+
link_files "${src_dir}" "${dst_dir}" "c" "c"
105+
link_files "${src_dir}" "${dst_dir}" "C" "c"
106+
107+
# Link C++ files (if any):
108+
link_files "${src_dir}" "${dst_dir}" "cpp" "cpp"
109+
link_files "${src_dir}" "${dst_dir}" "CPP" "cpp"
110+
link_files "${src_dir}" "${dst_dir}" "cxx" "cxx"
111+
link_files "${src_dir}" "${dst_dir}" "CXX" "cxx"
112+
link_files "${src_dir}" "${dst_dir}" "cc" "cc"
113+
link_files "${src_dir}" "${dst_dir}" "CC" "cc"
114+
link_files "${src_dir}" "${dst_dir}" "c++" "cpp"
115+
link_files "${src_dir}" "${dst_dir}" "C++" "cpp"
116+
117+
# Link C++ header files (if any):
118+
link_files "${src_dir}" "${dst_dir}" "hpp" "hpp"
119+
link_files "${src_dir}" "${dst_dir}" "HPP" "hpp"
120+
link_files "${src_dir}" "${dst_dir}" "hxx" "hxx"
121+
link_files "${src_dir}" "${dst_dir}" "HXX" "hxx"
122+
123+
# Link C header files (if any):
124+
link_files "${src_dir}" "${dst_dir}" "h" "h"
125+
link_files "${src_dir}" "${dst_dir}" "H" "h"
126+
127+
# Link Assembler files (if any):
128+
link_files "${src_dir}" "${dst_dir}" "s" "s"
129+
link_files "${src_dir}" "${dst_dir}" "S" "s"
130+
link_files "${src_dir}" "${dst_dir}" "Hdr" "s"
131+
132+
# Link Forth files (if any):
133+
link_files "${src_dir}" "${dst_dir}" "fth" "fth"
134+
link_files "${src_dir}" "${dst_dir}" "FTH" "fth"
135+
136+
# Link Pascal and Prolog files (if any):
137+
link_files "${src_dir}" "${dst_dir}" "p" "p"
138+
link_files "${src_dir}" "${dst_dir}" "P" "p"
139+
140+
# Link Perl files (if any):
141+
link_files "${src_dir}" "${dst_dir}" "pl" "pl"
142+
link_files "${src_dir}" "${dst_dir}" "PL" "pl"
143+
144+
# Link BASIC files (if any):
145+
link_files "${src_dir}" "${dst_dir}" "bas" "bas"
146+
link_files "${src_dir}" "${dst_dir}" "BAS" "bas"
147+
148+
# Find and link local files
149+
# (that may also be called Makefile.unix etc.):
150+
local last_dir="$(basename ${src_dir})"
151+
check_path "${last_dir}"
152+
local rval=$?
153+
if [ $rval -eq 0 ];
154+
then
155+
for f in ${src_dir}/*
156+
do
157+
fname="$(basename ${f})"
158+
# Remove ,fd7 etc. from the filename:
159+
fname="$(echo ${fname} | sed 's/,.*//')"
160+
if [ -f ${f} ];
161+
then
162+
if [ "$env" == "github" ]
56163
then
57164
# GitHub Actions do not support symlinks:
58-
cp ${f} ${curr_dir}/ux-src/${fname}.${f_ext}
165+
cp ${f} ${dst_dir}/${fname}
59166
else
60-
ln -s ${f} ${curr_dir}/ux-src/${fname}.${f_ext}
167+
ln -s ${f} ${dst_dir}/${fname}
61168
fi
62169
fi
63170
done
64171
fi
172+
173+
}
174+
175+
# recursively analyse subdirectories tree in ./src and generate all the correspondent directories in ./ux-src and exclude the typical c, h etc. RISC OS directories
176+
function gen_dirs()
177+
{
178+
local dir_to_explore="${1:-$curr_dir/src}" # Use first argument as directory to explore, default to ${curr_dir}/src
179+
for d in "${dir_to_explore}"/*
180+
do
181+
if [ ! -d "${d}" ]
182+
then
183+
local dname2="$(echo ${d} | sed 's/^.*\/src\///')"
184+
find_files_and_link "${d}" "${curr_dir}/ux-src/${dname2}"
185+
else
186+
local dname="$(basename "${d}")"
187+
check_path "${dname}"
188+
local rval=$?
189+
if [ $rval -eq 1 ];
190+
then
191+
local dname2="$(echo ${d} | sed 's/^.*\/src\///')"
192+
find_files_and_link "${d}" "${curr_dir}/ux-src/${dname2}"
193+
else
194+
local dname2="$(echo ${d} | sed 's/^.*\/src\///')"
195+
if [ ! -d "${curr_dir}/ux-src/${dname2}" ]
196+
then
197+
mkdir -p "${curr_dir}/ux-src/${dname2}"
198+
find_files_and_link "${d}" "${curr_dir}/ux-src/${dname2}"
199+
fi
200+
# Recursive call to explore the sub-directory
201+
gen_dirs "${d}"
202+
fi
203+
fi
204+
done
65205
}
66206

67207
# Check command line syntax:
@@ -84,75 +224,36 @@ check_cmd
84224
# Check if we are in a RISC OS source tree:
85225
if [ ! -d ${curr_dir}/src ]
86226
then
87-
echo "Error: you are not in a RISC OS source tree"
227+
echo "Error: you are not in a RISC OS Community source tree"
88228
exit 1
89229
fi
90230

91231
# Run the command:
92232
if [ "$cmd" == "gen" ]
93233
then
94234
echo "Generating ux-src directory in ${curr_dir}"
95-
235+
96236
# Generate ux-src:
97237
mkdir ${curr_dir}/ux-src
98238

99-
# Link C files (if any):
100-
link_files "c" "c"
101-
link_files "C" "c"
102-
103-
# Link C++ files (if any):
104-
link_files "cpp" "cpp"
105-
link_files "CPP" "cpp"
106-
link_files "cxx" "cxx"
107-
link_files "CXX" "cxx"
108-
link_files "cc" "cc"
109-
link_files "CC" "cc"
110-
link_files "c++" "cpp"
111-
link_files "C++" "cpp"
112-
113-
# Link C++ header files (if any):
114-
link_files "hpp" "hpp"
115-
link_files "HPP" "hpp"
116-
link_files "hxx" "hxx"
117-
link_files "HXX" "hxx"
239+
# Generate all the directories in ux-src:
240+
gen_dirs
118241

119-
# Link C header files (if any):
120-
link_files "h" "h"
121-
link_files "H" "h"
122-
123-
# Link Assembler files (if any):
124-
link_files "s" "s"
125-
link_files "S" "s"
126-
link_files "Hdr" "s"
127-
128-
# Link Forth files (if any):
129-
link_files "fth" "fth"
130-
link_files "FTH" "fth"
131-
132-
# Link Pascal and Prolog files (if any):
133-
link_files "p" "p"
134-
link_files "P" "p"
135-
136-
# Link Perl files (if any):
137-
link_files "pl" "pl"
138-
link_files "PL" "pl"
139-
140-
# Link BASIC files (if any):
141-
link_files "bas" "bas"
142-
link_files "BAS" "bas"
143-
144-
# Link Makefiles
145-
for f in ${curr_dir}/src/Makefile*
146-
do
147-
fname="$(basename ${f})"
148-
if [ "$env" == "github" ]
149-
then
150-
# GitHub Actions do not support symlinks:
151-
cp ${f} ${curr_dir}/ux-src/${fname}
152-
else
153-
ln -s ${f} ${curr_dir}/ux-src/${fname}
154-
fi
155-
done
242+
# Link main Makefiles:
243+
if [ -f ${curr_dir}/src/Makefile* ]
244+
then
245+
for f in ${curr_dir}/src/Makefile*
246+
do
247+
fname="$(basename ${f})"
248+
if [ "$env" == "github" ]
249+
then
250+
# GitHub Actions do not support symlinks:
251+
cp ${f} ${curr_dir}/ux-src/${fname}
252+
else
253+
ln -s ${f} ${curr_dir}/ux-src/${fname}
254+
fi
255+
done
256+
fi
156257

157258
# Link the Build Script for Unix:
158259
if [ -f ${curr_dir}/src/MkGCC.sh ]
@@ -166,7 +267,10 @@ then
166267
fi
167268
fi
168269

169-
ls -l ${curr_dir}/ux-src
270+
if [ "$debug" == "1" ]
271+
then
272+
ls -l ${curr_dir}/ux-src
273+
fi
170274
else
171275
# Remove existing ux-src
172276
if [ -d ${curr_dir}/ux-src ]
@@ -178,12 +282,14 @@ else
178282
# GitHub Actions do not support symlinks:
179283
rm -f ${f}
180284
else
181-
unlink ${f}
285+
if [ ! -d ${f} ]
286+
then
287+
unlink ${f}
288+
fi
182289
fi
183290
done
184291
rm -rf ${curr_dir}/ux-src
185292
fi
186-
187293
fi
188294

189295
exit $?

0 commit comments

Comments
 (0)