1#!/bin/bash
2#
3# NAME:
4#         libacpica.sh - ACPICA release script library
5#
6#
7
8if [ -z $SCRIPT ]; then
9	echo "SCRIPT is empty, please invoking libacpica.sh from release scripts."
10	exit 1
11fi
12
13CURDIR=`pwd`
14TOOLDIR=$SCRIPT/bin
15ACPISRC=$TOOLDIR/acpisrc
16ACPI_TYPES=
17
18linux_dirs()
19{
20	local dirs
21
22	dirs="\
23		drivers/acpi/acpica \
24		include/acpi \
25		include/acpi/platform \
26		tools/power/acpi/common \
27		tools/power/acpi/os_specific/service_layers \
28		tools/power/acpi/tools/acpidump \
29	"
30	echo $dirs
31}
32
33acpica_privs()
34{
35	local incs
36
37	incs="\
38		acapps.h \
39		accommon.h acconvert.h \
40		acdebug.h acdispat.h \
41		acevents.h \
42		acglobal.h \
43		achware.h \
44		acinterp.h \
45		aclocal.h \
46		acmacros.h \
47		acnamesp.h \
48		acobject.h acopcode.h \
49		acparser.h acpredef.h \
50		acresrc.h \
51		acstruct.h \
52		actables.h \
53		acutils.h \
54		amlcode.h amlresrc.h \
55	"
56	echo $incs
57}
58
59acpica_drivers_paths()
60{
61	local paths incs inc
62
63	paths="\
64		components/debugger \
65		components/dispatcher \
66		components/events \
67		components/executer \
68		components/hardware \
69		components/namespace \
70		components/parser \
71		components/resources \
72		components/tables \
73		components/utilities \
74	"
75	incs=`acpica_privs`
76	for inc in $incs; do
77		paths="$paths include/$inc"
78	done
79	echo $paths
80}
81
82acpica_tools_paths()
83{
84	local paths incs inc
85
86	paths="\
87		common/getopt.c \
88		common/cmfsize.c \
89		os_specific/service_layers/oslibcfs.c \
90		os_specific/service_layers/oslinuxtbl.c \
91		os_specific/service_layers/osunixdir.c \
92		os_specific/service_layers/osunixmap.c \
93		os_specific/service_layers/osunixxf.c \
94		tools/acpidump \
95	"
96	echo $paths
97}
98
99acpica_exclude_paths()
100{
101	local paths
102
103	paths="\
104		include/acpi/acclib.h \
105		include/acpi/acdisasm.h \
106		include/acpi/actbinfo.h \
107		include/acpi/platform/accygwin.h \
108		include/acpi/platform/acefi.h \
109		include/acpi/platform/acefiex.h \
110		include/acpi/platform/acfreebsd.h \
111		include/acpi/platform/achaiku.h \
112		include/acpi/platform/acmacosx.h \
113		include/acpi/platform/acmsvc.h \
114		include/acpi/platform/acmsvcex.h \
115		include/acpi/platform/acnetbsd.h \
116		include/acpi/platform/acos2.h \
117		include/acpi/platform/acqnx.h \
118		include/acpi/platform/acwin.h \
119		include/acpi/platform/acwinex.h \
120		include/acpi/platform/acwin64.h \
121		include/acpi/platform/acdragonfly.h \
122		include/acpi/platform/acdragonflyex.h \
123		drivers/acpi/acpica/utclib.c \
124	"
125	echo $paths
126}
127
128fulldir()
129{
130	( cd $1; pwd )
131}
132
133getdir()
134{
135	local lpath
136
137	lpath=`dirname $1`
138	fulldir $lpath
139}
140
141getfile()
142{
143	local d f
144
145	d=`getdir $1`
146	f=`basename $1`
147	echo $d/$f
148}
149
150identify_bits()
151{
152	local arch no64
153
154	arch=`uname -m`
155	no64=`echo ${arch%64}`
156
157	if [ "x$arch" != "x$no64" ] ; then
158		echo 64
159	else
160		echo 32
161	fi
162}
163
164make_tool()
165{
166	local srcdir acpi_tool tooldir bits
167
168	srcdir=$1
169	acpi_tool=$2
170
171	tooldir=$srcdir/generate/unix
172	bits=`identify_bits`
173
174	mkdir -p $TOOLDIR
175	make -C $srcdir $acpi_tool
176
177	if [ ! -f $tooldir/bin$bits/$acpi_tool ]; then
178		bits=
179	fi
180
181	cp $tooldir/bin$bits/$acpi_tool $TOOLDIR
182}
183
184clean_tool()
185{
186	local srcdir acpi_tool tooldir bits
187
188	srcdir=$1
189	acpi_tool=$2
190
191	tooldir=$srcdir/generate/unix
192	bits=`identify_bits`
193
194	if [ -f $tooldir/bin$bits/$acpi_tool ]; then
195		rm -f $tooldir/bin$bits/$acpi_tool
196		rm -rf $tooldir/$acpi_tool/obj$bits
197	fi
198	if [ -f $tooldir/bin/$acpi_tool ]; then
199		rm -f $tooldir/bin/$acpi_tool
200		rm -rf $tooldir/$acpi_tool/obj
201	fi
202
203	rm -f $tooldir/bin$bits/$acpi_tool
204}
205
206clean_acpisrc()
207{
208	clean_tool $1 acpisrc
209}
210
211make_acpisrc()
212{
213	if [ "x$2" = "xforce" ]; then
214		clean_tool $1 acpisrc
215	fi
216	make_tool $1 acpisrc
217}
218
219find_typedefs()
220{
221	cat $1 | awk '
222	{
223		if (match($0, /^typedef[_a-zA-Z0-9\* \t]+;/)) {
224			s1=substr($0, RSTART, RLENGTH)
225			if (match(s1, /^typedef[ \t]+/)) {
226				s2=substr(s1, RLENGTH+1)
227				if (match(s2, /(signed|unsigned)[ \t]+/)) {
228					s3=substr(s2, RLENGTH+1)
229				} else {
230					s3=s2
231				}
232				if (match(s3, /[_a-zA-Z0-9]+[ \t]+/)) {
233					s4=substr(s3, RLENGTH+1)
234					if (match(s4, /\*[ \t]*/)) {
235						s5=substr(s4, RLENGTH+1)
236					} else {
237						s5=s4
238					}
239					if (match(s5, /[_a-zA-Z0-9]+/)) {
240						def=substr(s5, RSTART, RLENGTH)
241						print def
242					}
243				}
244			}
245		}
246	}
247	'
248}
249
250find_all_typedefs()
251{
252	local f files
253
254	files=`find . -name "*.h"`
255	for f in $files; do
256		find_typedefs $f
257	done
258}
259
260lindent_find_typedefs()
261{
262	ACPI_TYPES=`find_all_typedefs | sort | uniq`
263}
264
265lindent_single()
266{
267	local t indent_flags
268
269	indent_flags="-npro -kr -i8 -ts8 -sob -l80 -ss -ncs -il0"
270	for t in $ACPI_TYPES; do
271		if [ "x$2" = "xtrue" ]; then
272			echo " Adding type $t"
273		fi
274		indent_flags="$indent_flags -T $t"
275	done
276
277	dos2unix $1 > /dev/null 2>&1
278	indent $indent_flags $1
279}
280
281lindent()
282{
283	local files f verbose=true
284
285	(
286		cd $1
287		lindent_find_typedefs
288		files=`find . -name "*.[ch]" | cut -c3-`
289		for f in $files; do
290			lindent_single $f $verbose
291			verbose=false
292		done
293		find . -name "*~" | xargs rm -f
294	)
295}
296
297linuxize_format()
298{
299	echo " Converting (acpisrc -l)..."
300	$ACPISRC -ldqy $1 $1 > /dev/null
301
302	echo " Converting (indent)..."
303	lindent $1
304
305	echo " Converting (acpisrc -i)..."
306	$ACPISRC -idqy $1 $1 > /dev/null
307}
308
309copy_linux_hierarchy()
310{
311	local from to dirs dir
312
313	from=$1
314	to=$2
315
316	rm -rf $to
317	dirs=`linux_dirs`
318
319	for dir in $dirs; do
320		echo " Copying ($dir)..."
321		mkdir -p $to/$dir
322		cp $from/$dir/*.[ch] $to/$dir/
323	done
324}
325
326linuxize_hierarchy_ref()
327{
328	local linux acpica to
329	local all_files
330	local t n d f
331	local private_includes inc
332
333	linux=$1
334	acpica=$2
335	to=$3
336
337	rm -rf $to
338	mkdir -p $to
339
340	#
341	# Ensure that the files in the two directories
342	# (native ACPICA and Linux ACPICA) match
343	#
344
345	all_files=`find $linux`
346
347	for t in $all_files ; do
348		if [ -f $t ] ; then
349			n=${t#${linux}/}
350			d=$to/`dirname $n`
351			f=`find $acpica/source -name \`basename $t\``
352			if [ ! -z $f ] ; then
353				mkdir -p $d
354				cp -f $f $d
355			else
356				echo " Removing ($n)..."
357				rm -f $t
358			fi
359		fi
360	done
361
362	private_includes=`acpica_privs`
363
364	# Do we need to perform things on private_includes?
365	for inc in $private_includes ; do
366		if [ -f $to/include/acpi/$inc ] ; then
367			echo "Warning: private include file $inc is now public.  Please check the linuxize.sh"
368		elif [ ! -f $to/drivers/acpi/acpica/$inc ] ; then
369			echo "Warning: private include file $inc does not exist.  Please check the linuxize.sh."
370		fi
371	done
372}
373
374linuxize_hierarchy_noref()
375{
376	local repo_linux paths path
377
378	repo_linux=$1
379
380	(
381		cd $repo_linux
382
383		# Making hierarchy
384
385		# Making source files
386		mkdir -p drivers/acpi/acpica
387		paths=`acpica_drivers_paths`
388		for path in $paths; do
389			if [ -d source/$path ]; then
390				echo " Moving directory $path..."
391				mv source/$path/*.[ch] drivers/acpi/acpica/
392			fi
393			if [ -f source/$path ]; then
394				echo " Moving file $path..."
395				mv source/$path drivers/acpi/acpica/
396			fi
397		done
398
399		# Making include files
400		mkdir -p include/acpi
401		mv -f source/include/* include/acpi
402
403		# Making tools/power/acpi files
404		paths=`acpica_tools_paths`
405		for path in $paths; do
406			if [ -d source/$path ]; then
407				echo " Moving directory $path..."
408				mkdir -p tools/power/acpi/$path
409				mv source/$path/*.[ch] tools/power/acpi/$path/
410			fi
411			if [ -f source/$path ]; then
412				echo " Moving file $path..."
413				mkdir -p `dirname tools/power/acpi/$path`
414				mv source/$path tools/power/acpi/$path
415			fi
416		done
417
418		# Removing non-Linux files
419		paths=`acpica_exclude_paths`
420		for path in $paths; do
421			if [ -d $path ]; then
422				echo " Removing directory $path..."
423				rm -rf $path
424			fi
425			if [ -f $path ]; then
426				echo " Removing file $path..."
427				rm -f $path
428			fi
429		done
430
431		rm -rf source
432	)
433}
434
435SRCDIR=`fulldir $SCRIPT/../..`
436