#CSC=mcs.exe
#CSCFlags=-nologo

CSC=./my.pl csc
CSCFlags=-nologo
XSD=./my.pl xsd
RESGEN=resgen
CC=cl

default: \
    build/Omega.dll \
    build/OmegaLIB.dll \
    build/XTensions.dll \
    build/BC.Core.ASTs.dll \
    build/BC.Core.Passes.dll \
    build/BC.Core.dll \
    build/BC.C.dll \
    build/BCC.exe \
    build/BXC.exe \
    build/XmlSignature.exe \
#

########################################################################

build/Omega.dll: $(shell find Omega -name '*.cs') 
	-@mkdir -p build
	$(CSC) $(CSCFlags) -unsafe -target:library -out:$@ $^

########################################################################

INC = /I"OmegaLIB\omega_lib\include" /I"OmegaLIB\basic\include" /I"C:\Program Files\Microsoft Visual Studio .NET\Vc7\include" 
LIB = /LIBPATH:"C:\Program Files\Microsoft Visual Studio .NET\Vc7\lib"

CCFLAGS = /LD /MD /TP /Gd /EHsc /D"WIN32" /Fobuild\\ /O2 /Ob1 /Oy /W0 /nologo

build/OmegaLIB.dll: OmegaLIB/omega_lib/OmegaLIB.c OmegaLIB/basic/src/*.c  OmegaLIB/omega_lib/src/*.c OmegaLIB/omega_lib/src/omega_core/*.c 
	-@mkdir -p build
	$(CC) $(INC) $(CCFLAGS) $^ -link -out:build/OmegaLIB.dll -implib:build/OmegaLIB.lib $(LIB)

########################################################################

build/XTensions.dll: $(shell find XTensions -name '*.cs')
	-@mkdir -p build
	$(CSC) $(CSCFlags) -target:library -out:$@ $^

########################################################################

build/BC.Core.ASTs.dll: ASTs/ASTs.cs
	-@mkdir -p build
	$(CSC) $(CSCFlags) -target:library -out:$@ $(patsubst %.dll,-r:%.dll,$^)

ifneq ($(XSD),)
ASTs/ASTs.cs : ASTs/ASTs.xsd
	-@mkdir -p build
	$(XSD) -nologo ASTs/ASTs.xsd -n:BC.Core.ASTs -c -out:ASTs
realclean::
	rm -f ASTs/ASTs.cs
endif

########################################################################

build/BC.Core.Passes.dll: build/Passes.cs build/BC.Core.ASTs.dll
	-@mkdir -p build
	$(CSC) $(CSCFlags) -target:library -out:$@ $(patsubst %.dll,-r:%.dll,$^)

build/Passes.cs : build/PassGenerator.exe
	-@mkdir -p build
	./build/PassGenerator.exe > build/Passes.cs

build/PassGenerator.exe : $(shell find PassGenerator -name '*.cs') build/BC.Core.ASTs.dll build/XTensions.dll
	-@mkdir -p build
	$(CSC) $(CSCFlags) -out:$@ $(patsubst %.dll,-r:%.dll,$^)

########################################################################

build/BC.Core.dll: $(shell find Core -name '*.cs') build/BC.Core.ASTs.dll build/BC.Core.Passes.dll build/XTensions.dll Core/BC.Core.UI.resources
	-@mkdir -p build
	$(CSC) $(CSCFlags) -target:library -out:$@ $(patsubst %.dll,-r:%.dll,$(patsubst %.resources,-res:%.resources,$^))

ifneq ($(RESGEN),)
Core/BC.Core.UI.resources: Core/UI.txt
	-@mkdir -p build
	$(RESGEN) /compile 'Core\UI.txt,Core\BC.Core.UI.resources'
realclean::
	rm -f Core/BC.Core.UI.resources
endif

########################################################################

build/BC.C.dll: C/C.cs $(shell find C -name '*.cs') build/BC.Core.ASTs.dll build/BC.Core.Passes.dll build/BC.Core.dll build/XTensions.dll C/BC.C.UI.resources
	-@mkdir -p build
	$(CSC) $(CSCFlags) -target:library -out:$@ $(patsubst %.dll,-r:%.dll,$(patsubst %.resources,-res:%.resources,$^))

ifneq ($(RESGEN),)
C/BC.C.UI.resources: C/UI.txt
	-@mkdir -p build
	$(RESGEN) /compile 'C\UI.txt,C\BC.C.UI.resources'
realclean::
	rm -f C/BC.C.UI.resources
endif

C/C.cs: build/XmlSignature.exe C/C.xml XmlSignature/Signature.xsd
	./build/XmlSignature.exe C/C.xml XmlSignature/Signature.xsd > C/C.cs
clean::
	rm -f C/C.cs

########################################################################

build/BCC.exe: $(shell find BCC -name '*.cs') build/Omega.dll build/BC.Core.ASTs.dll build/BC.Core.Passes.dll build/BC.Core.dll build/BC.C.dll
	$(CSC) $(CSCFlags) -out:$@ $(patsubst %.dll,-r:%.dll,$^)

########################################################################

build/BXC.exe: X/X.cs $(shell find X -name '*.cs') build/BC.Core.ASTs.dll build/BC.Core.dll build/XTensions.dll
	$(CSC) $(CSCFlags) -out:$@ $(patsubst %.dll,-r:%.dll,$^)

X/X.cs : build/XmlSignature.exe X/X.xml XmlSignature/Signature.xsd
	./build/XmlSignature.exe X/X.xml XmlSignature/Signature.xsd > X/X.cs
clean::
	rm -f X/X.cs

########################################################################

build/XmlSignature.exe: $(shell find XmlSignature -name '*.cs') build/BC.Core.ASTs.dll build/BC.Core.dll
	-@mkdir -p build
	$(CSC) $(CSCFlags) -out:$@ $(patsubst %.dll,-r:%.dll,$^)

########################################################################

realclean clean::
	rm -rf build
