CXXFLAGS = -Wall -std=c++23 -O3 -ffast-math
CODE = $(filter %.cs,$^)
LIBS = $(addprefix -reference:,$(filter %.dll,$^))
MKEXE = mcs -target:exe -out:$@ $(LIBS) $(CODE)
MKLIB = mcs -target:library -out:$@ $(LIBS) $(CODE)
TIME = time --output=$@ --append --format "$$nthreads %e %U"

#NPROC=`getconf _NPROCESSORS_ONLN`
NPROC=$(shell nproc)
ARCH=$(shell uname -m)
Times.png : out.times.txt Makefile
	echo '\
		set term png ;\
		set out "$@" ;\
		set title "Box: $(NPROC) $(ARCH) processors" ;\
		set xlabel "number of threads" ;\
		set ylabel "program running time [sec]" ;\
		plot \
			 "$<" index 0 with linespoints title "C++" \
			,"$<" index 1 with linespoints title "C#" \
	'|tee log.gpi|gnuplot

N=2e8
out.times.txt: main main.exe
	>$@
	for nthreads in `seq 10`; \
		do $(TIME) ./main -threads $$nthreads -terms $(N) ;\
	done
	echo "\n" >> $@
	for nthreads in `seq 10`; \
		do $(TIME) mono main.exe -threads $$nthreads -terms $(N) ;\
	done

main.exe: main.cs ; $(MKEXE)

test: out1 out2 out3 out4 # make test --jobs=4
out1: main.exe ; mono $< -terms 2e8 >$@ 2>log1
out2: main.exe ; mono $< -terms 2e8 >$@ 2>log2
out3: main.exe ; mono $< -terms 2e8 >$@ 2>log3
out4: main.exe ; mono $< -terms 2e8 >$@ 2>log4

test2 : main.exe
	mono $< -terms 5e8 >out1 2>log1 &
	mono $< -terms 5e8 >out2 2>log2 &
	mono $< -terms 5e8 >out3 2>log3 &
	mono $< -terms 5e8 >out4 2>log4 &

test3 : main.exe
	for n in 1 2 3 4; \
		do mono main.exe -terms 1e8 >out$$n 2>log$$n & \
	done; wait

clean:
	$(RM) main main.exe [Ll]og* [Oo]ut* *.png
