jtalkc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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|E] [-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 imply "-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 "NDEKCoOl:L:i:IM:m:h?" o; do
  85. case "$o" in
  86. N) ENV=NODE
  87. BASE=$KERNEL
  88. INIT=$INITIALIZER;;
  89. D) ENV=D8
  90. BASE=$KERNEL
  91. INIT=$INITIALIZER;;
  92. D) ENV=ENYO
  93. BASE=$KERNEL
  94. INIT=$INITIALIZER;;
  95. K) BASE=$KERNEL;;
  96. C) BASE=$COMPILER;;
  97. o) CLOSURE=true
  98. CLOSUREPARTS=true;;
  99. O) CLOSURE=true
  100. CLOSUREFULL=true;;
  101. l) LOAD=$OPTARG;;
  102. L) LOADANDADD=$OPTARG;;
  103. I) INIT=$INITIALIZER;;
  104. i) INIT=$OPTARG;;
  105. M) MAINFILE=$OPTARG;;
  106. m) MAIN=$OPTARG;;
  107. h) usage;;
  108. [?]) usage;;
  109. esac
  110. done
  111. shift $(($OPTIND - 1))
  112. # Check for Closure compiler and Java
  113. if [ ! -z $CLOSURE ]; then
  114. java > /dev/null
  115. if [ $? -eq 0 ]; then
  116. if [ ! -f ~/compiler.jar ]; then
  117. echo "Can not find Closure compiler at ~/compiler.jar"
  118. exit 1
  119. fi
  120. else
  121. echo "java is not installed and is needed for -O or -o (Closure compiler)."
  122. exit 1
  123. fi
  124. fi
  125. # Function for looking up listed js files
  126. function resolvejs {
  127. if [ -f "$1" ]; then
  128. RESOLVED="$1"
  129. else
  130. if [ -f $JTALK/js/$1 ]; then
  131. RESOLVED="$JTALK/js/$1"
  132. else
  133. echo "Javascript file not found: $1"
  134. exit 1
  135. fi
  136. fi
  137. }
  138. # Resolve listed libraries in $LOAD separated by spaces
  139. LOAD=${LOAD//,/\ }
  140. for FILE in $LOAD
  141. do
  142. resolvejs $FILE
  143. TOLOAD="$TOLOAD $RESOLVED"
  144. done
  145. # Resolve listed libraries in $LOADANDADD separated by spaces
  146. LOADANDADD=${LOADANDADD//,/\ }
  147. for FILE in $LOADANDADD
  148. do
  149. resolvejs $FILE
  150. TOLOAD="$TOLOAD $RESOLVED"
  151. TOADD="$TOADD $RESOLVED"
  152. done
  153. # Define our Compiler loading supplied libraries
  154. OURCOMPILER="$KITCHENSINK $TOLOAD $JTALK/js/init.js $JTALK/nodejs/nodecompile.js"
  155. # Add supplied libraries
  156. LIBS="$BASE $TOADD"
  157. # Get a unique tempdir and make it get auto removed on exit
  158. TMPDIR=`mktemp -d`
  159. trap "rm -rf $TMPDIR" EXIT
  160. # --------------------------------------------------
  161. # Collect libraries and Smalltalk files looking
  162. # both locally and in $JTALK/js and $JTALK/st
  163. # --------------------------------------------------
  164. PROGRAM=
  165. until [ "$*" = "" ]
  166. do
  167. case $1 in
  168. *.st)
  169. CATEGORY=`basename $1 .st`
  170. if [ -f "$1" ]; then
  171. COMPILE="$COMPILE $1 $CATEGORY"
  172. COMPILED="$COMPILED $CATEGORY.js"
  173. else
  174. if [ -f $JTALK/st/$1 ]; then
  175. COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
  176. COMPILED="$COMPILED $CATEGORY.js"
  177. else
  178. echo "JTalk file not found: $1"
  179. exit 1
  180. fi
  181. fi
  182. ;;
  183. *.js)
  184. resolvejs $1
  185. LIBS="$LIBS $RESOLVED"
  186. ;;
  187. *)
  188. # Will end up being the last non js/st argument
  189. PROGRAM=$1
  190. ;;
  191. esac
  192. shift
  193. done
  194. # --------------------------------------------------
  195. # Actual compilation phase of collected .st files
  196. # --------------------------------------------------
  197. # Create compiler dynamically
  198. cat $OURCOMPILER > $TMPDIR/compiler.js
  199. # Compile all collected .st files to .js
  200. echo "Loading libraries $KITCHENSINK $TOLOAD and compiling ..."
  201. node $TMPDIR/compiler.js $COMPILE
  202. # Verify all .js files corresponding to .st files were created, otherwise exit
  203. IFS=" "
  204. for FILE in $COMPILED
  205. do
  206. if [ ! -f "$FILE" ]; then
  207. echo "Failed compilation of $FILE, exiting."
  208. exit 1
  209. fi
  210. done
  211. if [ ! -z $CLOSUREPARTS ]; then
  212. echo "Compiling all js files using Google closure compiler."
  213. ALLJSFILES="$COMPILED $LIBS"
  214. for FILE in $ALLJSFILES
  215. do
  216. mv $FILE $FILE.original
  217. java -jar ~/compiler.jar --js $FILE.original --js_output_file $FILE
  218. rm $FILE.original
  219. done
  220. fi
  221. if [ -z $PROGRAM ]; then
  222. echo "Done."
  223. exit 0
  224. fi
  225. # --------------------------------------------------
  226. # Now we start composing resulting javascript file.
  227. # --------------------------------------------------
  228. # Add collected libraries to libs.js file.
  229. if [ ! -z "$LIBS" ]; then
  230. echo "Adding libraries $LIBS ..."
  231. cat $LIBS > $TMPDIR/libs.js
  232. LIBS=$TMPDIR/libs.js
  233. fi
  234. echo "Adding Jtalk code$COMPILED ..."
  235. # Check for init file
  236. if [ ! -z "$INIT" ]; then
  237. if [ -f "$INIT" ]; then
  238. echo "Adding initializer $INIT ..."
  239. else
  240. echo "Can not find init file $INIT, exiting."
  241. exit 1
  242. fi
  243. fi
  244. # Check for adding main
  245. if [ ! -z "$MAIN" ]; then
  246. echo "Adding call to $MAIN class >> main ..."
  247. echo "smalltalk.$MAIN._main()" > $TMPDIR/main.js
  248. MAIN=$TMPDIR/main.js
  249. fi
  250. # Check for adding main file
  251. if [ ! -z "$MAINFILE" ]; then
  252. if [ -f "$MAINFILE" ]; then
  253. echo "Adding main as $MAINFILE ..."
  254. else
  255. echo "Can not find main file $MAINFILE, exiting."
  256. exit 1
  257. fi
  258. MAIN=$MAINFILE
  259. fi
  260. # And finally concatenate Program.js
  261. echo "Writing $PROGRAM.js ..."
  262. cat $LIBS $COMPILED $INIT $MAIN > $PROGRAM.js
  263. echo "Done."
  264. if [ ! -z $CLOSUREFULL ]; then
  265. echo "Compiling $PROGRAM.js file using Google closure compiler."
  266. mv $PROGRAM.js $PROGRAM.js.original
  267. java -jar ~/compiler.jar --js $PROGRAM.js.original --js_output_file $PROGRAM.js
  268. rm $PROGRAM.js.original
  269. echo "Done."
  270. fi