updated on Sat Jan 21 04:00:54 UTC 2012
[aur-mirror.git] / catalyst-switch / make.sh
blob857f6d03e90e66e5560a24a6625b8e86b203d7cd
1 #!/bin/bash
3 # Copyright 1999-2005 ATI Technologies Inc., Markham, Ontario, CANADA.
4 # All Rights Reserved.
5 #
6 # Your use and or redistribution of this software in source and \ or
7 # binary form, with or without modification, is subject to: (i) your
8 # ongoing acceptance of and compliance with the terms and conditions of
9 # the ATI Technologies Inc. software End User License Agreement; and (ii)
10 # your inclusion of this notice in any version of this software that you
11 # use or redistribute. A copy of the ATI Technologies Inc. software End
12 # User License Agreement is included with this software and is also
13 # available by contacting ATI Technologies Inc. at http://www.ati.com
15 # ==============================================================
16 # External environment variables
18 # FGLRX_DEBUG
19 # Set this variable to backup kernel module before stripping
20 # The nonstripped version will be names like fglrx_dbg.ko
22 # ==============================================================
23 # local variables and files
25 current_wd=`pwd`
26 logfile=$current_wd/make.sh.log # DKMS uses name 'make.log', so that we need another name
27 rootcheck=0
28 MODULE=fglrx
29 LIBIP_PREFIX=.
31 # ==============================================================
32 # sets the GCC to use to the one required by the module (if available)
34 set_GCC_version () {
35 #identify GCC default version major number
36 GCC_MAJOR="`gcc --version | grep -o -e ") ." | head -1 | cut -d " " -f 2`"
38 #identify the GCC major version that compiled the kernel
39 KERNEL_GCC_MAJOR="`cat /proc/version | grep -o -e "gcc version ." | cut -d " " -f 3`"
41 #see if they don't match
42 if [ ${GCC_MAJOR} != ${KERNEL_GCC_MAJOR} ]; then
43 #use kernel GCC version hopefully
44 KERNEL_GCC="`cat /proc/version | grep -o -e "gcc version [0-9]\.[0-9]" | cut -d " " -f 3`"
45 CC="gcc-${KERNEL_GCC}"
47 # check if gcc version requested exists
48 GCC_AVAILABLE="`${CC} --version | grep -e ")" | head -1 | cut -d " " -f 4 | cut -c-3`"
50 if [ ${GCC_AVAILABLE} != ${KERNEL_GCC} ]; then
51 echo "The GCC version that is required to compile this module is version ${KERNEL_GCC}."
52 echo "Please install this GCC or recompile your kernel with ${GCC_AVAILABLE}"
53 exit 1
58 if [ -z "${CC}" ]; then
59 CC=gcc
60 set_GCC_version
63 # ==============================================================
64 # system/kernel identification
66 uname_r=`uname -r`
67 uname_v=`uname -v`
68 uname_s=`uname -s`
69 uname_m=`uname -m`
70 uname_a=`uname -a`
72 # ==============================================================
73 # parse options
75 while [ $# -gt 0 ]; do
76 case $1 in
77 --rootcheck)
78 rootcheck=1
79 shift
81 --nohints)
82 OPTIONS_HINTS=0
83 shift
85 --uname_r*)
86 if echo $1 | grep '=' >/dev/null ; then
87 uname_r=`echo $1 | sed 's/^.*=//'`
88 else
89 uname_r="$2"
90 shift
92 shift
94 --uname_v*)
95 if echo $1 | grep '=' >/dev/null ; then
96 uname_v=`echo $1 | sed 's/^.*=//'`
97 else
98 uname_v="$2"
99 shift
101 shift
103 --uname_s*)
104 if echo $1 | grep '=' >/dev/null ; then
105 uname_s=`echo $1 | sed 's/^.*=//'`
106 else
107 uname_s="$2"
108 shift
110 shift
112 --uname_m*)
113 if echo $1 | grep '=' >/dev/null ; then
114 uname_m=`echo $1 | sed 's/^.*=//'`
115 else
116 uname_m="$2"
117 shift
119 shift
121 --uname_a*)
122 if echo $1 | grep '=' >/dev/null ; then
123 uname_a=`echo $1 | sed 's/^.*=//'`
124 else
125 uname_a="$2"
126 shift
128 shift
130 esac
131 done
133 # ==============================================================
134 # check if we are running as root with typical login shell paths
136 if [ $rootcheck -ne 0 -a `id -u` -ne 0 ]; then
137 echo "You must be logged in as root to run this script."
138 exit 1
141 which depmod >/dev/null 2>&1
143 if [ $? -ne 0 ]; then
144 PATH=/usr/local/sbin:/usr/sbin:/sbin:${PATH}
145 which depmod >/dev/null 2>&1
147 if [ $? -ne 0 ]; then
148 echo "You arent running in a 'login shell'."
149 echo "Please login directly from a console"
150 echo "or use 'su -l' to get the same result."
151 exit 1
155 # ==============================================================
156 # assing default location of linux kernel headers
158 linuxbuild=/usr/src/linux
159 linuxsources=$linuxbuild
161 # in /lib/modules/<kernel-version> there is a symlink for latest kernel
162 # which calls "build" and points to the directory where modules were built.
163 # On some distros some configuration-independent kernel headers can't be
164 # found in "build" but in a different directory pointed to by a symlink
165 # called "source".
166 if [ -d /lib/modules/${uname_r}/build/include ]; then
167 # just comment this line out if you already set an alternative location
168 linuxbuild=/lib/modules/${uname_r}/build
169 linuxsources=/lib/modules/${uname_r}/source
172 linuxincludes=$linuxbuild/include
174 # ==============================================================
175 # print some statistics, helpful for analyzing any build failures
177 echo AMD kernel module generator version 2.1 | tee $logfile
178 echo . >> $logfile
180 echo Active kernel: >> $logfile
181 echo uname -a = `uname -a` >> $logfile
182 echo uname -s = `uname -s` >> $logfile
183 echo uname -m = `uname -m` >> $logfile
184 echo uname -r = `uname -r` >> $logfile
185 echo uname -v = `uname -v` >> $logfile
186 echo . >> $logfile
188 echo Target kernel: >> $logfile
189 echo uname -a = ${uname_a} >> $logfile
190 echo uname -s = ${uname_s} >> $logfile
191 echo uname -m = ${uname_m} >> $logfile
192 echo uname -r = ${uname_r} >> $logfile
193 echo uname -v = ${uname_v} >> $logfile
194 echo . >> $logfile
196 # ==============================================================
197 cd $current_wd
199 # ==============================================================
200 # locate and verify contents of kernel include file path
202 # verify match with respective line in linux/version.h
203 # sample: #define UTS_RELEASE "2.4.0-test7"
205 src_file=$linuxincludes/linux/version.h
207 if [ ! -e $src_file ]; then
208 echo "kernel includes at $linuxincludes not found or incomplete" | tee -a $logfile
209 echo "file: $src_file" | tee -a $logfile
210 exit 1
213 OsRelease=${uname_r}
215 # Before 2.6.18 UTS-defines are in linux/version.h.
216 # For 2.6.18 to 2.6.32, UTS-defines are in linux/utsrelease.h.
217 # For 2.6.33 and later UTS-defines are in generated/utsrelease.h.
218 # Some old distributions used to define UTS-defines in external
219 # version-*.h files, e.g. linux-2.2.14-5.0-RedHat. Is this kludge still needed?
220 for src_file in \
221 "$linuxincludes/linux/version.h" \
222 "$linuxincludes/linux/utsrelease.h" \
223 "$linuxincludes/generated/utsrelease.h" \
224 "$linuxincludes/linux/version-*.h";
226 UTS_REL_COUNT=`cat $src_file 2>/dev/null | grep UTS_RELEASE -c`
227 if [ $UTS_REL_COUNT -gt 1 ]; then
228 kernel_release=`cat $src_file | grep UTS_RELEASE | grep \"$OsRelease\" | cut -d'"' -f2`
229 break
230 elif [ $UTS_REL_COUNT -gt 0 ]; then
231 kernel_release=`cat $src_file | grep UTS_RELEASE | cut -d'"' -f2`
232 break
234 done
236 # compare release string of running kernel with kernel name from headers
238 hit=0
240 if [ "$OsRelease" = "$kernel_release" ]; then
241 hit=1
242 else
243 # Red Hat 7.0 source and some newer 2.4.x might not have smp suffix in UTS_RELEASE text
244 if [ `echo $OsRelease | grep smp -c` -ne 0 ]; then
245 if [ "$OsRelease" = "${kernel_release}smp" ]; then
246 hit=1
251 if [ $hit -eq 0 ]; then
252 echo "Error:" | tee -a $logfile
253 echo "kernel includes at $linuxincludes do not match current kernel." | tee -a $logfile
254 echo "they are versioned as \"$kernel_release\"" | tee -a $logfile
255 echo "instead of \"$OsRelease\"." | tee -a $logfile
256 echo "you might need to adjust your symlinks:" | tee -a $logfile
257 echo "- /usr/include" | tee -a $logfile
258 echo "- /usr/src/linux" | tee -a $logfile
259 exit 1
262 kernel_release_major=${kernel_release%%.*}
263 kernel_release_rest=${kernel_release#*.}
264 kernel_release_minor=${kernel_release_rest%%.*}
266 if [ "$kernel_release_major" -lt 2 -o \
267 \( "$kernel_release_major" -eq 2 -a "$kernel_release_minor" -lt 6 \) ]; then
268 echo "Error:"
269 echo "Your kernel version $kernel_release is not supported by this driver release."
270 echo "Only 2.6.0 and newer kernels are supported."
271 exit 1
274 # ==============================================================
275 # resolve if we are running a SMP enabled kernel
277 SMP=0
280 # grep in OsVersion string for SMP specific keywords
281 OsVersion=${uname_v}
283 if [ `echo $OsVersion | grep [sS][mM][pP] -c` -ne 0 ]; then
284 SMP=1
285 echo "OsVersion says: SMP=$SMP" >> $logfile
289 # grep in /proc/ksyms for SMP specific kernel symbols
290 # use triggerlevel of 10 occurences
291 # (UP kernels might have 0-1, SMP kernels might have 32-45 or much more)
293 src_file=/proc/ksyms
295 if [ -e $src_file ]; then
296 if [ `fgrep smp $src_file -c` -gt 10 ]; then
297 SMP=1
298 echo "file $src_file says: SMP=$SMP" >> $logfile
302 src_file=/proc/kallsyms
304 if [ -e $src_file ]; then
305 if [ `fgrep smp $src_file -c` -gt 10 ]; then
306 SMP=1
307 echo "file $src_file says: SMP=$SMP" >> $logfile
312 # linux/autoconf.h may contain this: #define CONFIG_SMP 1
314 # Before 2.6.33 autoconf.h is under linux/.
315 # For 2.6.33 and later autoconf.h is under generated/.
316 if [ -f $linuxincludes/generated/autoconf.h ]; then
317 autoconf_h=$linuxincludes/generated/autoconf.h
318 else
319 autoconf_h=$linuxincludes/linux/autoconf.h
321 src_file=$autoconf_h
323 if [ ! -e $src_file ]; then
324 echo "Warning:" >> $logfile
325 echo "kernel includes at $linuxincludes not found or incomplete" >> $logfile
326 echo "file: $src_file" >> $logfile
327 echo "" >> $logfile
328 else
329 if [ `cat $src_file | grep "#undef" | grep "CONFIG_SMP" -c` = 0 ]; then
330 SMP=`cat $src_file | grep CONFIG_SMP | cut -d' ' -f3`
331 echo "file $src_file says: SMP=$SMP" >> $logfile
335 if [ "$SMP" = 0 ]; then
336 echo "assuming default: SMP=$SMP" >> $logfile
339 # act on final result
340 if [ ! "$SMP" = 0 ]; then
341 smp="-SMP"
342 def_smp=-D__SMP__
345 # ==============================================================
346 # resolve whether we need to set PAGE_ATTR_FIX
348 PAGE_ATTR_FIX=0
350 src_file=/proc/ksyms
352 if [ -e $src_file ]; then
353 if [ `fgrep " change_page_attr\$" $src_file -c` -gt 0 ]; then
354 PAGE_ATTR_FIX=1
355 echo "file $src_file says: PAGE_ATTR_FIX=$PAGE_ATTR_FIX" >> $logfile
359 src_file=/proc/kallsyms
361 if [ -e $src_file ]; then
362 if [ `fgrep " change_page_attr\$" $src_file -c` -gt 0 ]; then
363 PAGE_ATTR_FIX=1
364 echo "file $src_file says: PAGE_ATTR_FIX=$PAGE_ATTR_FIX" >> $logfile
368 # ==============================================================
369 # resolve if we are running a MODVERSIONS enabled kernel
371 MODVERSIONS=0
373 # autoconf.h may contain this: #define CONFIG_MODVERSIONS 1
374 src_file=$autoconf_h
375 if [ ! -e $src_file ];
376 then
377 echo "Warning:" >> $logfile
378 echo "kernel includes at $linuxincludes not found or incomplete" >> $logfile
379 echo "file: $src_file" >> $logfile
380 echo "" >> $logfile
381 else
382 if [ `cat $src_file | grep "#undef" | grep "CONFIG_MODVERSIONS" -c` = 0 ]
383 then
384 MODVERSIONS=`cat $src_file | grep CONFIG_MODVERSIONS | cut -d' ' -f3`
385 echo "file $src_file says: MODVERSIONS=$MODVERSIONS" >> $logfile
389 if [ $MODVERSIONS = 0 ]
390 then
391 echo "assuming default: MODVERSIONS=$MODVERSIONS" >> $logfile
394 # act on final result
395 if [ ! $MODVERSIONS = 0 ]
396 then
397 def_modversions="-DMODVERSIONS"
400 # ==============================================================
401 # resolve if we are building for a kernel with a fix for CVE-2010-3081
402 # On kernels with the fix, use arch_compat_alloc_user_space instead
403 # of compat_alloc_user_space since the latter is GPL-only
405 COMPAT_ALLOC_USER_SPACE=compat_alloc_user_space
407 for src_file in \
408 $linuxbuild/arch/x86/include/asm/compat.h \
409 $linuxsources/arch/x86/include/asm/compat.h \
410 $linuxbuild/include/asm-x86_64/compat.h \
411 $linuxsources/include/asm-x86_64/compat.h;
413 if [ -e $src_file ];
414 then
415 break
417 done
418 if [ ! -e $src_file ];
419 then
420 echo "Warning: x86 compat.h not found in kernel headers" >> $logfile
421 echo "neither arch/x86/include/asm/compat.h nor include/asm-x86_64/compat.h" >> $logfile
422 echo "could be found in $linuxbuild or $linuxsources" >> $logfile
423 echo "" >> $logfile
424 else
425 if [ `cat $src_file | grep -c arch_compat_alloc_user_space` -gt 0 ]
426 then
427 COMPAT_ALLOC_USER_SPACE=arch_compat_alloc_user_space
429 echo "file $src_file says: COMPAT_ALLOC_USER_SPACE=$COMPAT_ALLOC_USER_SPACE" >> $logfile
433 # ==============================================================
434 # break down OsRelease string into its components
436 major=`echo $OsRelease | sed -n -e s/"^\([[:digit:]]*\)\.\([[:digit:]]*\)\.\([[:digit:]]*\)\(.*\)"/"\\1"/p`
437 minor=`echo $OsRelease | sed -n -e s/"^\([[:digit:]]*\)\.\([[:digit:]]*\)\.\([[:digit:]]*\)\(.*\)"/"\\2"/p`
438 patch=`echo $OsRelease | sed -n -e s/"^\([[:digit:]]*\)\.\([[:digit:]]*\)\.\([[:digit:]]*\)\(.*\)"/"\\3"/p`
439 extra=`echo $OsRelease | sed -n -e s/"^\([[:digit:]]*\)\.\([[:digit:]]*\)\.\([[:digit:]]*\)\(.*\)"/"\\4"/p`
441 if [ "$1" = "verbose" ]
442 then
443 echo OsRelease=$OsRelease | tee -a $logfile
444 echo major=$major | tee -a $logfile
445 echo minor=$minor | tee -a $logfile
446 echo patch=$patch | tee -a $logfile
447 echo extra=$extra | tee -a $logfile
448 echo SMP=$SMP | tee -a $logfile
449 echo smp=$smp | tee -a $logfile
452 echo . >>$logfile
454 # ==============================================================
455 # make kernel module and check results
457 kmod_extension=.ko
459 echo "doing Makefile based build for kernel 2.6.x and higher" | tee -a $logfile
461 cd 2.6.x
463 make clean
465 echo 'This is a dummy file created to suppress this warning: could not find /lib/modules/fglrx/build_mod/2.6.x/.libfglrx_ip.a.cmd for /lib/modules/fglrx/build_mod/2.6.x/libfglrx_ip.a' > .lib${MODULE}_ip.a.cmd
467 CFLAGS_MODULE="-DMODULE -DATI -DFGL -DPAGE_ATTR_FIX=$PAGE_ATTR_FIX -DCOMPAT_ALLOC_USER_SPACE=$COMPAT_ALLOC_USER_SPACE $def_smp $def_modversions"
468 make CC=${CC} \
469 LIBIP_PREFIX=$(echo "$LIBIP_PREFIX" | sed -e 's|^\([^/]\)|../\1|') \
470 MODFLAGS="$CFLAGS_MODULE" \
471 CFLAGS_MODULE="$CFLAGS_MODULE" \
472 KVER=${uname_r} \
473 PAGE_ATTR_FIX=$PAGE_ATTR_FIX \
474 > tlog 2>&1
476 res=$?
478 tee -a $logfile < tlog
479 rm -f tlog
481 cd ..
483 if [ $res -eq 0 ]; then
484 echo "build succeeded with return value $res" | tee -a $logfile
485 else
486 echo "build failed with return value $res" | tee -a $logfile
487 exit 1
490 if [ -e ${MODULE}${kmod_extension} ]; then
491 rm -f ${MODULE}${kmod_extension}
494 ln -s 2.6.x/${MODULE}${kmod_extension}
496 echo . >> $logfile
498 # ==============================================================
499 # install generated file at required location
501 TERMINAL_HINT=0
503 if [ `pwd | grep "/lib/modules/${MODULE}/build_mod\$" -c` -gt 0 ]; then
504 echo duplicating results into driver repository... | tee -a $logfile
506 target_dir=`pwd`/..
507 target_dir=`cd $target_dir;pwd`
509 echo "target location: $target_dir" >> $logfile
511 which strip > /dev/null 2>&1
513 if test $? = 0; then
514 if [ ! -z "${FGLRX_DEBUG}" ]; then
515 cp -f ${MODULE}${kmod_extension} ${MODULE}_dbg${kmod_extension}
518 strip -g ${MODULE}${kmod_extension} > /dev/null 2>&1
520 if test $? = 0; then
521 echo "the debug info stripped from the kernel module" >> $logfile
522 else
523 echo "could not strip the debug info of kernel module" >> $logfile
525 else
526 echo "could not find the strip utility on your system" >> $logfile
529 echo "copying ${MODULE}${kmod_extension}" >> $logfile
531 cp -f ${MODULE}${kmod_extension} $target_dir/${MODULE}.$OsRelease${kmod_extension} | tee -a $logfile
533 echo "copying logfile of build" >> $logfile
534 echo "*** end of build log ***" >> $logfile
536 cp -f $logfile $target_dir/make.$OsRelease.log
538 # terminal hint message
539 TERMINAL_HINT=1
540 else
541 # the build was done from an external location - installation not intended
542 echo "duplication skipped - generator was not called from regular lib tree" | tee -a $logfile
545 # ==============================================================
546 # finale
548 echo done.
550 if [ $TERMINAL_HINT -eq 1 ]; then
551 echo "You must change your working directory to $target_dir"
552 echo "and then call ./make_install.sh in order to install the built module."
555 #EOF