FLANG=flang-new
FFLAGS=-O3
LLC=llc
LD=wasm-ld
AR=llvm-ar

SRC_DIR ?= ../../src

# List of FORTRAN files.
.ftrn_src_noprefix = \
	fft.F corth.F ceigv.F cmtms.F mtms.F cmslv1.F \
	mslv.F dmslv.F dcmslv.F sgefa.F sgeco.F dgefa.F \
	sgesl.F dgesl.F fmin.F daxpy.F ddot.F saxpy.F \
	sdot.F idamax.F dscal.F sasum.F sscal.F isamax.F \
	dcfact.F dcsol.F dcminv.F dgeco.F dgedi.F cbal.F \
	sgedi.F cgefa.F cgesl.F comqr2.F cbabk2.F sfft.F \
	spmpar.F cdivid.F dswap.F dasum.F cgedi.F sswap.F \
	caxpy.F cdotc.F icamax.F cscal.F ipmpar.F cswap.F \
	dpmpar.F

# Add $SRC_DIR prefix to all files to allow out-of-tree build
.ftrn_src = $(addprefix $(SRC_DIR), $(.ftrn_src_noprefix))

# Replace .F to .o to get output object code file
.ftrn_obj = $(.ftrn_src_noprefix:.F=.o)

# All object code files are archived as nswc.a library
nswc.a: $(.ftrn_obj)
	$(AR) rcs nswc.a $(.ftrn_obj)

# Invoke flang to build FORTRAN to WebAssembly
%.o: $(SRC_DIR)/%.F
	$(FLANG) $(FFLAGS) --target=i386-unknown-linux -emit-llvm -S $(SRC_DIR)/$*.F
	$(LLC) --march=wasm32 -filetype=obj $*.ll

clean:
	rm -rf *.ll *.o *.wasm *.a
