home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / a2ixlibrary-1.0-bin.lha / bin / a2ixlibrary next >
Text File  |  1996-10-12  |  8KB  |  287 lines

  1. #!/bin/sh
  2.  
  3. echo
  4. echo ".a to .ixlibrary converter, version 1.0"
  5. echo
  6.  
  7. prefix="/ade/share/a2ixlibrary"
  8.  
  9. eval `/ade/bin/date "+day=%d;month=%m;year=%y"`
  10. day=`expr $day + 0`
  11. month=`expr $month + 0`
  12. year=`expr $year + 0`
  13.  
  14. if [ X"$1" = X ]
  15. then
  16.     data=a2ixlibrary.data
  17. else
  18.     data=$1
  19. fi
  20.  
  21. if [ ! -f $data ]
  22. then
  23.     echo could not find $data!
  24.     exit 2
  25. fi
  26.  
  27. echo scan $data
  28. name=`egrep "^#define NAME " $data | sed -e "s/#define NAME *//" -e "s/ *$//"`
  29. safename=`$prefix/a2ixsafename $name`
  30. libs=`egrep "^#define LIBS_LIST " $data | sed -e "s/#define LIBS_LIST *//" -e "s/ *$//"`
  31. internals=`egrep "^#define INTERNALS " $data`
  32.  
  33. if [ -f lib$name.am ]
  34. then
  35.     cp lib$name.am lib$name.a
  36. fi
  37.  
  38. if [ ! -f lib$name.a ]
  39. then
  40.     echo could not find lib$name.a!
  41.     exit 2
  42. fi
  43.  
  44. if [ X"$libs" != X ]
  45. then
  46.     for i in $libs
  47.     do
  48.         if [ -f $prefix/ldscripts/${i}.x ]
  49.         then
  50.             shared_libs="$shared_libs $i"
  51.         else
  52.             static_libs="$static_libs -l$i"
  53.         fi
  54.     done
  55. fi
  56.  
  57. rm -rf amigaobj
  58. mkdir amigaobj
  59.  
  60. if [ ! X"$internals" = X ]
  61. then
  62.     cp $data amigaobj/x.c
  63.     echo "
  64.     INTERNALS" >>amigaobj/x.c
  65.     internals=`gcc -E -P amigaobj/x.c`
  66.     rm -f amigaobj/x.c
  67. fi
  68.  
  69. echo collect init routines
  70. call_inits="#define CALL_INITS "`egrep amiga_init_ *.c | sed -e 's/^.*amiga/amiga/' -e 's/(void)/();\\\\/'`
  71.  
  72. cd amigaobj
  73.  
  74. echo find public functions in lib$name.a
  75. nm --extern-only ../lib$name.a | egrep " T " | egrep -v "_amiga_init_" | sort +2 >f2.lst
  76. cut -c 13- <f2.lst >f.lst
  77.  
  78. echo create lib_funcs.s
  79. echo '.text
  80. .global ___link_text_hashes
  81. ___link_text_hashes:' >lib_funcs.s
  82. $prefix/string_hash <f.lst >>lib_funcs.s
  83. echo '.global ___link_text_offsets
  84. ___link_text_offsets:' >>lib_funcs.s
  85. sed -e 's/^/.long _/' <f.lst >>lib_funcs.s
  86. echo ".long -1" >>lib_funcs.s
  87. rm -f f.lst
  88.  
  89. echo >shared_ptr.s
  90. if [ X"$shared_libs" != X ]
  91. then
  92.     for i in $shared_libs
  93.     do
  94.         safei=`$prefix/a2ixsafename $i`
  95.         echo find "*_${safei}_shared_ptr" externals in lib$name.a
  96.         nm --extern-only ../lib$name.a | egrep _${safei}_shared_ptr | \
  97.           sed -e "s/_${safei}_shared_ptr$//" | cut -c 13- | sort | uniq >sp.lst
  98.         echo ".data
  99.         .global __${safei}_shared_ptr_table
  100.         __${safei}_shared_ptr_table:
  101.         .long " `wc sp.lst | cut -c -7` >>shared_ptr.s
  102.         $prefix/string_hash ${safei} <sp.lst >>shared_ptr.s
  103.     done
  104. fi
  105.  
  106. echo find globals in lib$name.a
  107. nm --extern-only ../lib$name.a | egrep " D | C " | sort +2 >g2.lst
  108. if [ X"$internals" != X ]
  109. then
  110.     echo remove internals
  111.     for i in $internals
  112.     do
  113.         egrep -v " ${i}$" <g2.lst >gg.lst
  114.         cp gg.lst g2.lst
  115.     done
  116.     rm -f gg.lst
  117. fi 
  118. cut -c 13- <g2.lst >g.lst
  119.  
  120. echo create lib_data.s
  121. echo '.text
  122. .global ___link_data_hashes
  123. ___link_data_hashes:' >lib_data.s
  124. $prefix/string_hash <g.lst >>lib_data.s
  125. echo '.data
  126. .global ___link_data_offsets
  127. ___link_data_offsets:' >>lib_data.s
  128. sed -e 's/^/.long _/' <g.lst >>lib_data.s
  129. echo ".long -1" >>lib_data.s
  130.  
  131. echo create $name.h
  132. mkdir -p /ade/include/a2ixlibrary
  133. hdr=/ade/include/a2ixlibrary/$name.h
  134. echo "/* This file is automatically generated by a2ixlibrary. Do not change.
  135.    Only shared library sources can include this header, do not use this header
  136.    in your own programs! */
  137. #ifndef UNDEF_amiga_$safename
  138. " >$hdr
  139. sed -e 's/^/extern void *__/' -e 's/$/_'$safename'_shared_ptr;/' <g.lst >>$hdr
  140. sed -e 's/^\(.*\)/#define \1 (*((__typeof__(\1) *)__\1_'$safename'_shared_ptr))/' <g.lst >>$hdr
  141. echo "#define UNDEF_amiga_$safename
  142. #else" >>$hdr
  143. sed -e 's/^/#undef /' <g.lst >>$hdr
  144. echo "#undef UNDEF_amiga_$safename
  145. #endif" >>$hdr
  146. rm -f g.lst
  147.  
  148. echo create a2ixlibrary.h
  149. cd ..
  150. cp $data amigaobj/a2ixlibrary.h
  151. cd amigaobj
  152. cat <<EOF >>a2ixlibrary.h
  153. #define xstr(s) str(s)
  154. #define str(s) #s
  155. #define SNAME xstr(NAME)
  156. #define SVERSION xstr(VERSION)
  157. #define SREVISION xstr(REVISION)
  158. #define FULLNAME \"lib$name.ixlibrary\"
  159. #define IDSTRING \"lib$name \" SVERSION \".\" SREVISION \" ($day.$month.$year)\"
  160. #define BASE ${safename}Base
  161. #define CLOSEINST __${safename}CloseInstance
  162. #define SETVARSINST __${safename}SetVarsInstance
  163. #define TFSTART _shared_textfunctions_start_lib${safename}
  164. #define TDSTART _shared_textdata_start_lib${safename}
  165. #define DFSTART _shared_datafunctions_start_lib${safename}
  166. #define DDSTART _shared_datadata_start_lib${safename}
  167. $call_inits
  168.  
  169. EOF
  170.  
  171. if [ X"$shared_libs" != X ]
  172. then
  173.     echo "    #define INSTANCE_LIBS void _stext(); void _etext(); \\
  174.         void _sdata(); void __text_size(); \\" >>a2ixlibrary.h
  175.     for i in $shared_libs
  176.     do
  177.         safei=`$prefix/a2ixsafename $i`
  178.         linklibs="$linklibs -l$i" 
  179.         echo "struct Library *${safei}Base = NULL; \\" >>a2ixlibrary.h
  180.         echo "extern unsigned long _${safei}_shared_ptr_table[]; \\" >>a2ixlibrary.h
  181.         echo "void _shared_textfunctions_start_lib${safei}(); \\" >>a2ixlibrary.h
  182.         echo "void _shared_datafunctions_start_lib${safei}(); \\" >>a2ixlibrary.h
  183.         echo "void _shared_datadata_start_lib${safei}(); \\" >>a2ixlibrary.h
  184.         echo "asm(\".text\"); \\
  185. asm(\"___${safei}SetVarsInstance:     movel    a4@(_${safei}Base:W),a0\"); \\
  186. asm(\"jmp    a0@(-36:w)\"); asm(\".data\"); \\" >>a2ixlibrary.h
  187.     done
  188.     echo "asm(\".text\"); static int open_libs(void) { \\
  189. long offset = _etext -_sdata + 4 + 4 * *((long *)__datadata_relocs); \\
  190. long A4; asm volatile (\"movel a4,%0\" : \"=g\" (A4)); A4 -= 0x7ffe; \\" >>a2ixlibrary.h
  191.  
  192.     for i in $shared_libs
  193.     do
  194.         safei=`$prefix/a2ixsafename $i`
  195.         echo "if (!(${safei}Base = OpenLibrary(\"lib$i.ixlibrary\", 0))) return 0; \\" >>a2ixlibrary.h
  196.         echo "if (!__${safei}SetVarsInstance( \\
  197.                       _stext, _shared_textfunctions_start_lib${safei} + offset, NULL, \\
  198.                       (void *)A4, _shared_datafunctions_start_lib${safei} + offset, \\
  199.                       _shared_datadata_start_lib${safei} + offset, \\
  200.               _${safei}_shared_ptr_table, 0)) \\" >>a2ixlibrary.h
  201.                 echo "{ CloseLibrary(${safei}Base); return 0; } \\" >>a2ixlibrary.h
  202.     done
  203.     echo "CacheClearE(_stext, (long)__text_size, CACRF_ClearI|CACRF_ClearD); \\" >>a2ixlibrary.h
  204.     echo "return 1; } \\" >>a2ixlibrary.h
  205.     echo "static void close_libs(void) { \\" >>a2ixlibrary.h
  206.     for i in $shared_libs
  207.     do
  208.         safei=`$prefix/a2ixsafename $i`
  209.         echo "if (${safei}Base) CloseLibrary(${safei}Base); \\" >>a2ixlibrary.h
  210.     done
  211.     echo "}" >>a2ixlibrary.h
  212. fi
  213.  
  214. echo assemble start.S
  215. gcc -I. -c $prefix/start.S -o start.o
  216. echo compile instance.c
  217. gcc -resident -malways-restore-a4 -I. -c -O $prefix/instance.c -o instance.o
  218. echo compile setvars.c
  219. gcc -I. -resident -malways-restore-a4 -c -O $prefix/setvars.c -o setvars.o
  220. echo assemble lib_data.s, lib_funcs.s and shared_ptr.s
  221. gcc -c -O lib_data.s
  222. gcc -c -O lib_funcs.s
  223. gcc -c -O shared_ptr.s
  224. echo link lib$name.ixlibrary
  225. cp $prefix/amiga_lib_script.x .
  226.  
  227. if [ X"$shared_libs" != X ]
  228. then
  229.     for i in $shared_libs
  230.     do
  231.         cat $prefix/ldscripts/${i}.x >>amiga_lib_script.x
  232.     done
  233. fi
  234. echo "}}" >>amiga_lib_script.x
  235.  
  236. if [ X"$internals" != X ]
  237. then
  238.     echo rename internals
  239.     for i in $internals
  240.     do
  241.         sed -e "s/${i}/XX${i}/g" <amiga_lib_script.x >a
  242.         cp a amiga_lib_script.x
  243.     done
  244.     rm -f a
  245. fi 
  246.  
  247. gcc -resident -o ../lib$name.ixlibrary -nostdlib -nostartfiles -L.. start.o \
  248.                 instance.o setvars.o lib_data.o lib_funcs.o shared_ptr.o \
  249.         -l$name $static_libs -lc -lgcc -lc -Xlinker -Tamiga_lib_script.x
  250.  
  251. if [ ! -f ../lib$name.ixlibrary ]
  252. then
  253.     echo failed to link lib$name.ixlibrary
  254.     rm -f ../lib$name.a
  255.     exit 2
  256. fi
  257.  
  258. echo create $name.x
  259. mkdir -p $prefix/ldscripts
  260. nm --extern-only ../lib$name.ixlibrary | sort +2 | egrep " T " >f1.lst
  261. join -j 3 f1.lst f2.lst -o "1.1 1.2 1.3" | sort >f.lst
  262. perl $prefix/deltafunctions.pl `$prefix/a2ixgetsize ../lib$name.ixlibrary` \
  263.     $safename <f.lst >$prefix/ldscripts/$name.x
  264. rm f1.lst f2.lst f.lst
  265.  
  266. nm --extern-only ../lib$name.ixlibrary | sort +2 | egrep " D " >g1.lst
  267. join -j 3 g1.lst g2.lst -o "1.1 1.2 1.3" | sort >g.lst
  268. perl $prefix/deltadata.pl `$prefix/a2ixgetsize ../lib$name.ixlibrary` \
  269.     $safename <g.lst >>$prefix/ldscripts/$name.x
  270. rm g1.lst g2.lst g.lst
  271.  
  272. echo compile auto_init.c
  273. gcc -g -I. -c -O $prefix/auto_init.c -o auto_init.o
  274. echo create new lib$name.a
  275. rm ../lib$name.a
  276. ar q ../lib$name.a auto_init.o
  277. ranlib ../lib$name.a
  278.  
  279. cd ..
  280.  
  281. echo postprocess lib$name.ixlibrary
  282. postlink -library lib$name.ixlibrary
  283. strip lib$name.ixlibrary
  284. cp lib$name.ixlibrary /libs
  285.  
  286. rm -rf amigaobj
  287.