1
0

jtalkc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #!/bin/bash
  2. #
  3. # This is a "compiler" for JTalk code. Run without arguments for help.
  4. # Get JTalk root directory from the location of this script.
  5. JTALK=$(readlink -f `dirname ${0}`/..)
  6. function usage {
  7. cat <<ENDOFHELP
  8. Usage: $0 [-N|D] [-K|C] [-o] [-O] [-m class] [-M file]
  9. [-i] [-I file] file1 [file2 ...] [Program]
  10. Will compile Jtalk files - either separately or into a runnable complete
  11. program. Files listed will be handled using these rules:
  12. *.js
  13. Files are concatenated in listed order.
  14. If not found we look in $JTALK/js
  15. *.st
  16. Files are compiled into .js files before concatenated.
  17. If not found we look in $JTALK/st.
  18. NOTE: Each file is currently considered to be a fileout of a single class
  19. category of the same name as the file!
  20. If no Program is specified each given .st file will be compiled into
  21. a .js file. Otherwise a <Program>.js file is linked together based on
  22. the options:
  23. -N or -D or -E
  24. Compilation target. Currently Node.js, D8 (V8 shell) or Enyo (webOS 3.0).
  25. All Implies "-K -I" so boot.js and Kernel.js are added first and init.js
  26. is added last.
  27. -K
  28. Add libraries to get minimal JTalk Kernel running.
  29. -C
  30. Add libraries to get minimal JTalk Compiler running.
  31. -o
  32. Optimize each js file using the Google closure compiler.
  33. Using Closure at ~/compiler.jar
  34. -O
  35. Optimize final <Program>.js using the Google closure compiler.
  36. Using Closure at ~/compiler.jar
  37. -l library1,library2
  38. Load listed libraries (no spaces) into Compiler before compiling.
  39. -L library1,library2
  40. Load listed libraries (no spaces) into Compiler before compiling and also
  41. into Program.js in listed order.
  42. -i
  43. Add library initializer <file>.
  44. -I file
  45. Add library standard initializer $JTALK/js/init.js
  46. -m class
  47. Add call to #main in class <class>.
  48. -M file
  49. Add javascript file <file> at the end acting as main.
  50. Example invocations:
  51. Just compile Kernel.st to Kernel.js:
  52. jtalkc Kernel.st
  53. Compile Hello.st to Hello.js and create complete program called
  54. Program.js for Node.js including boot.js, Kernel.js, init.js and
  55. adding a call to class method #main in class Hello:
  56. jtalkc -N -m Hello Hello.st Program
  57. Compile two .st files into corresponding .js files,
  58. and link with specific myboot.js, myKernel.js, myinit.js
  59. and main.js and create complete program called Program.js:
  60. jtalkc -M main.js -I myinit.js myboot.js myKernel.js Cat1.st Cat2.st Program
  61. ENDOFHELP
  62. exit 1;
  63. }
  64. # Check we at least got one argument
  65. if [ -z $1 ] ; then
  66. usage
  67. fi
  68. # Define our predefined library combinations
  69. BOOT="$JTALK/js/boot.js"
  70. KERNEL="$BOOT $JTALK/js/Kernel.js"
  71. COMPILER="$KERNEL $JTALK/js/Parser.js $JTALK/js/Compiler.js"
  72. KITCHENSINK="$COMPILER $JTALK/js/Canvas.js"
  73. # Predefined initializer
  74. INITIALIZER="$JTALK/js/init.js"
  75. # Default values
  76. ENV=
  77. INIT=
  78. MAIN=
  79. MAINFILE=
  80. BASE=$KERNEL
  81. LOAD=
  82. LOADANDADD=
  83. # Read options and shift them away
  84. while getopts "NDKCoOl:L:i:IM:m:h?" o; do
  85. case "$o" in
  86. N) ENV=NODE
  87. BASE=$KERNEL
  88. INIT=$INITIALIZER
  89. MAIN=Main;;
  90. D) ENV=D8
  91. BASE=$KERNEL
  92. INIT=$INITIALIZER
  93. MAIN=Main;;
  94. K) BASE=$KERNEL;;
  95. C) BASE=$COMPILER;;
  96. o) CLOSURE=true
  97. CLOSUREPARTS=true;;
  98. O) CLOSURE=true
  99. CLOSUREFULL=true;;
  100. l) LOAD=$OPTARG;;
  101. L) LOADANDADD=$OPTARG;;
  102. I) INIT=$INITIALIZER;;
  103. i) INIT=$OPTARG;;
  104. M) MAINFILE=$OPTARG;;
  105. m) MAIN=$OPTARG;;
  106. h) usage;;
  107. [?]) usage;;
  108. esac
  109. done
  110. shift $(($OPTIND - 1))
  111. # Check for Closure compiler and Java
  112. if [ ! -z $CLOSURE ]; then
  113. java > /dev/null
  114. if [ $? -eq 0 ]; then
  115. if [ ! -f ~/compiler.jar ]; then
  116. echo "Can not find Closure compiler at ~/compiler.jar"
  117. exit 1
  118. fi
  119. else
  120. echo "java is not installed and is needed for -O or -o (Closure compiler)."
  121. exit 1
  122. fi
  123. fi
  124. # Function for looking up listed js files
  125. function resolvejs {
  126. if [ -f "$1" ]; then
  127. RESOLVED="$1"
  128. else
  129. if [ -f $JTALK/js/$1 ]; then
  130. RESOLVED="$JTALK/js/$1"
  131. else
  132. echo "Javascript file not found: $1"
  133. exit 1
  134. fi
  135. fi
  136. }
  137. # Resolve listed libraries in $LOAD separated by spaces
  138. LOAD=${LOAD//,/\ }
  139. for FILE in $LOAD
  140. do
  141. resolvejs $FILE
  142. TOLOAD="$TOLOAD $RESOLVED"
  143. done
  144. # Resolve listed libraries in $LOADANDADD separated by spaces
  145. LOADANDADD=${LOADANDADD//,/\ }
  146. for FILE in $LOADANDADD
  147. do
  148. resolvejs $FILE
  149. TOLOAD="$TOLOAD $RESOLVED"
  150. TOADD="$TOADD $RESOLVED"
  151. done
  152. # Define our Compiler loading supplied libraries
  153. OURCOMPILER="$KITCHENSINK $TOLOAD $JTALK/js/init.js $JTALK/nodejs/nodecompile.js"
  154. # Add supplied libraries
  155. LIBS="$BASE $TOADD"
  156. # Get a unique tempdir and make it get auto removed on exit
  157. TMPDIR=`mktemp -d`
  158. trap "rm -rf $TMPDIR" EXIT
  159. # --------------------------------------------------
  160. # Collect libraries and Smalltalk files looking
  161. # both locally and in $JTALK/js and $JTALK/st
  162. # --------------------------------------------------
  163. PROGRAM=
  164. until [ "$*" = "" ]
  165. do
  166. case $1 in
  167. *.st)
  168. CATEGORY=`basename $1 .st`
  169. if [ -f "$1" ]; then
  170. COMPILE="$COMPILE $1 $CATEGORY"
  171. COMPILED="$COMPILED $CATEGORY.js"
  172. else
  173. if [ -f $JTALK/st/$1 ]; then
  174. COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
  175. COMPILED="$COMPILED $CATEGORY.js"
  176. else
  177. echo "JTalk file not found: $1"
  178. exit 1
  179. fi
  180. fi
  181. ;;
  182. *.js)
  183. resolvejs $1
  184. LIBS="$LIBS $RESOLVED"
  185. ;;
  186. *)
  187. # Will end up being the last non js/st argument
  188. PROGRAM=$1
  189. ;;
  190. esac
  191. shift
  192. done
  193. # --------------------------------------------------
  194. # Actual compilation phase of collected .st files
  195. # --------------------------------------------------
  196. # Create compiler dynamically
  197. cat $OURCOMPILER > $TMPDIR/compiler.js
  198. # Compile all collected .st files to .js
  199. echo "Loading libraries $KITCHENSINK $TOLOAD and compiling ..."
  200. node $TMPDIR/compiler.js $COMPILE
  201. # Verify all .js files corresponding to .st files were created, otherwise exit
  202. IFS=" "
  203. for FILE in $COMPILED
  204. do
  205. if [ ! -f "$FILE" ]; then
  206. echo "Failed compilation of $FILE, exiting."
  207. exit 1
  208. fi
  209. done
  210. if [ ! -z $CLOSUREPARTS ]; then
  211. echo "Compiling all js files using Google closure compiler."
  212. ALLJSFILES="$COMPILED $LIBS"
  213. for FILE in $ALLJSFILES
  214. do
  215. mv $FILE $FILE.original
  216. java -jar ~/compiler.jar --js $FILE.original --js_output_file $FILE
  217. rm $FILE.original
  218. done
  219. fi
  220. if [ -z $PROGRAM ]; then
  221. echo "Done."
  222. exit 0
  223. fi
  224. # --------------------------------------------------
  225. # Now we start composing resulting javascript file.
  226. # --------------------------------------------------
  227. # Add collected libraries to libs.js file.
  228. if [ ! -z "$LIBS" ]; then
  229. echo "Adding libraries $LIBS ..."
  230. cat $LIBS > $TMPDIR/libs.js
  231. LIBS=$TMPDIR/libs.js
  232. fi
  233. echo "Adding Jtalk code$COMPILED ..."
  234. # Check for init file
  235. if [ ! -z "$INIT" ]; then
  236. if [ -f "$INIT" ]; then
  237. echo "Adding initializer $INIT ..."
  238. else
  239. echo "Can not find init file $INIT, exiting."
  240. exit 1
  241. fi
  242. fi
  243. # Check for adding main
  244. if [ ! -z "$MAIN" ]; then
  245. echo "Adding call to $MAIN class >> main ..."
  246. echo "smalltalk.$MAIN._main()" > $TMPDIR/main.js
  247. MAIN=$TMPDIR/main.js
  248. fi
  249. # Check for adding main file
  250. if [ ! -z "$MAINFILE" ]; then
  251. if [ -f "$MAINFILE" ]; then
  252. echo "Adding main as $MAINFILE ..."
  253. else
  254. echo "Can not find main file $MAINFILE, exiting."
  255. exit 1
  256. fi
  257. MAIN=$MAINFILE
  258. fi
  259. # And finally concatenate Program.js
  260. echo "Writing $PROGRAM.js ..."
  261. cat $LIBS $COMPILED $INIT $MAIN > $PROGRAM.js
  262. echo "Done."
  263. if [ ! -z $CLOSUREFULL ]; then
  264. echo "Compiling $PROGRAM.js file using Google closure compiler."
  265. mv $PROGRAM.js $PROGRAM.js.original
  266. java -jar ~/compiler.jar --js $PROGRAM.js.original --js_output_file $PROGRAM.js
  267. rm $PROGRAM.js.original
  268. echo "Done."
  269. fi