#
# This Makefile takes .st files in the amber/st directory and produces compiled
# javascript files from them, for both debug and deployment.
#
# Where we find the current runnable code and where we put our js files on install
JS      := ../js/

# The compiler script
AMBERC  := ../bin/amberc

# Generic flags to AMBERC
FLAGS   := -d

# All corresponding js filenames for every st file available
# In other words, if we have Kernel.st and Compiler.st, then OBJECTS will be "Kernel.js Compiler.js"
OBJECTS := $(patsubst %.st,%.js,$(wildcard *.st))

# Default make target since it is the first target in this Makefile
all: $(OBJECTS)

# Step by step
#
# First we copy the core javascript files from current working files
# into this directory. These files are hand written or generated using
# other tools (parser.js). $@ is the target name.
boot.js init.js:
	cp ../js/$@ .

# generate the parser
# $@ is the target
# $< is the prerequisite
parser.js: ../js/parser.pegjs
	pegjs --track-line-and-column --cache -e smalltalk.parser $< $@

# Then we compile Kernel-*.st files depending on having boot.js, init.js and parser.js
# $< means the first dependency - in other words Kernel-*.st
Kernel-Objects.js: Kernel-Objects.st boot.js init.js parser.js
	$(AMBERC) $(FLAGS) $<

Kernel-Classes.js: Kernel-Classes.st boot.js init.js parser.js
	$(AMBERC) $(FLAGS) $<

Kernel-Methods.js: Kernel-Methods.st boot.js init.js parser.js
	$(AMBERC) $(FLAGS) $<

Kernel-Collections.js: Kernel-Collections.st boot.js init.js parser.js
	$(AMBERC) $(FLAGS) $<

Kernel-Exceptions.js: Kernel-Exceptions.st boot.js init.js parser.js
	$(AMBERC) $(FLAGS) $<

Kernel-Transcript.js: Kernel-Transcript.st boot.js init.js parser.js
	$(AMBERC) $(FLAGS) $<

Kernel-Announcements.js: Kernel-Announcements.st boot.js init.js parser.js
	$(AMBERC) $(FLAGS) $<

# ...and Compiler, but using the new Kernel from above.
# We only need to depend on Kernel js files since it in turn depends on boot.js etc
Compiler.st: Importer-Exporter.st Compiler-Exceptions.st Compiler-Core.st \
    Compiler-AST.st Compiler-Semantic.st Compiler-IR.st Compiler-Inlining.st \
    Compiler-Interpreter.st
	echo "Smalltalk current createPackage: 'Compiler' properties: #{}!" >$@
	sed -e '/^Smalltalk current createPackage:.*!$$/ d' \
      -e 's/package: '"'[^':]*'"'!/package:'"'Compiler'"'!/' \
      -e 's/ methodsFor: '"'[^']*'"'!$$/ methodsFor: '"'"'*Compiler'"'"'!/' \
      $^ >>$@

Compiler.js: Compiler.st Kernel-Objects.js Kernel-Classes.js Kernel-Methods.js Kernel-Collections.js \
	Kernel-Exceptions.js Kernel-Transcript.js
	$(AMBERC) $(FLAGS) -l Importer-Exporter,Compiler-Exceptions,Compiler-Core,Compiler-AST,Compiler-Semantic,Compiler-IR,Compiler-Inlining $<

# ...now that we have a new Kernel and Compiler we use them
# to compile the rest of st files presuming that they only depend on Kernel, like
# for example Canvas.js and Benchfib.js.
%.js: %.st Compiler.js
	$(AMBERC) $(FLAGS) $<

# But for some libraries there are more dependencies to care for. Then
# we need to use -l so that the compiler first loads that library
# before compiling the .st file. Otherwise bindings will fail.
#
# NOTE: With the new dependency model in class Package etc this will change!
#
Canvas.js: Canvas.st Compiler.js
	$(AMBERC) $(FLAGS) $<

# IDE uses JQuery
IDE.js: IDE.st Canvas.js Compiler.js
	$(AMBERC) $(FLAGS) -l Canvas $<

TrySmalltalk.js: TrySmalltalk.st IDE.js Compiler.js
	$(AMBERC) $(FLAGS) -l Canvas,IDE $<

# Some Examples use SUnit and also IDE
Examples.js: Examples.st SUnit.js IDE.js Compiler.js
	$(AMBERC) $(FLAGS) -l SUnit,Canvas,IDE $<

# Tests typically also use SUnit
Kernel-Tests.js: Kernel-Tests.st SUnit.js Compiler.js
	$(AMBERC) $(FLAGS) -l SUnit $<

Compiler-Tests.js: Compiler-Tests.st SUnit.js Compiler.js
	$(AMBERC) $(FLAGS) -l SUnit $<

SUnit-Tests.js: SUnit-Tests.st SUnit.js Compiler.js
	$(AMBERC) $(FLAGS) -l SUnit $<

# Documentation
Documentation.js: Documentation.st Canvas.js Compiler.js
	$(AMBERC) $(FLAGS) -l Canvas $<;

#Helios
Helios-Core.js: Helios-Core.st Canvas.js Compiler.js
	$(AMBERC) $(FLAGS) -l Canvas $<;

Helios-Browser.js: Helios-Browser.st Helios-Core.js Compiler.js
	$(AMBERC) $(FLAGS) -l Helios-Core $<;

# Installing is simply copying all js files to js directory.
install: all
	cp *.js $(JS)

# And cleaning is trivial also
clean:
	rm -f *.js

# These three are phony
.PHONY: all install clean