
@bpv(CC)@      = @moar::cc@
@bpv(LD)@      = @moar::ld@
@bpv(CCINC)@   = @moar::ccinc@
@bpv(CFLAGS)@  = @moar::cflags@
@bpv(LDFLAGS)@ = @ldflags@
@bpv(LDLIBS)@  = @moar::ldlibs@
@bpv(MINGW_UNICODE)@ = @mingw_unicode@

MOAR_PREFIX = @nfp(@moar::prefix@)@
MOAR        = @nfpq(@moar::bindir@/moar@moar::exe@)@
@bpv(NQP)@       = @nfpq(@m_nqp@)@
@bpv(NQP_RR)@    = @bpm(NQP)@
@bpv(RUN_RAKUDO_SCRIPT)@ = rakudo-m-early-build
@bpv(RUN_RAKUDO)@ = @shquot(@perl@)@ @bpm(RUN_RAKUDO_SCRIPT)@

@bpv(RUNNER_SUFFIX)@ = @moar::exe@

@bpv(INCPATH)@ = @nfp($(MOAR_PREFIX)/include)@
@bpv(MOAR_INC_PATHS)@ = \
    @insert_list(moar_includes)@

@bpv(RAKU)@ = raku@moar::exe@
@bpv(RAKU_DEBUG)@ = raku-debug@moar::exe@
@if(platform==windows @bpv(RAKU)@ = raku@moar::exe@
@bpv(INST_RAKU)@ = inst-raku@moar::exe@
@bpv(RAKU_DEBUG)@ = raku-debug@moar::exe@
@bpv(INST_RAKU_DEBUG)@ = inst-raku-debug@moar::exe@
@bpv(RAKUW)@ = rakuw@moar::exe@
@bpv(INST_RAKUW)@ = inst-rakuw@moar::exe@)@
@for_langalias(
@bpv(@uclangalias@)@ = @langalias@@moar::exe@
@bpv(@uclangalias@_DEBUG)@ = @langalias@-debug@moar::exe@
@bpv(@uclangalias@_M)@ = @langalias@-m@moar::exe@
@bpv(@uclangalias@_DEBUG_M)@ = @langalias@-debug-m@moar::exe@
@bpv(INST_@uclangalias@)@ = inst-@langalias@@moar::exe@
@bpv(INST_@uclangalias@_DEBUG)@ = inst-@langalias@-debug@moar::exe@
@bpv(INST_@uclangalias@_M)@ = inst-@langalias@-m@moar::exe@
@bpv(INST_@uclangalias@_DEBUG_M)@ = inst-@langalias@-debug-m@moar::exe@
@if(platform==windows @bpv(@uclangalias@W)@ = @langalias@w@moar::exe@
@bpv(@uclangalias@W_M)@ = @langalias@w-m@moar::exe@
@bpv(INST_@uclangalias@W)@ = inst-@langalias@w@moar::exe@
@bpv(INST_@uclangalias@W_M)@ = inst-@langalias@w-m@moar::exe@)@)@

@bpv(RAKUDO_OPS_DIR)@  = dynext
@bpv(RAKUDO_OPS_DLL)@  = @bpm(RAKUDO_OPS_DIR)@@nfp(/@perl6_ops_dll@)@
@bpv(RAKUDO_OPS_OBJ)@  = @nfp(@bpm(RAKUDO_OPS_DIR)@/perl6_ops@moar::obj@)@
@bpv(RAKUDO_OPS_DLL_SRC)@  = @nfp(src/vm/moar/ops/perl6_ops.c)@

@for_langalias(@for_toolchain(@bpv(@uclangalias@_@uctoolchain@_RUNNER)@ = @langalias@-@toolchain@-m@runner_suffix@
)@)@

@bpv(ALL_TARGETS)@ = \
@for_langalias(@tab@@bpm(INST_@uclangalias@)@ \
@tab@@bpm(INST_@uclangalias@_DEBUG)@ \
@tab@@bpm(INST_@uclangalias@_M)@ \
@tab@@bpm(INST_@uclangalias@_DEBUG_M)@ \
@if(platform==windows @tab@@bpm(INST_@uclangalias@W)@ \
@tab@@bpm(INST_@uclangalias@W_M)@ \
)@)@@if(platform==windows @tab@@bpm(INST_RAKU)@ \
@tab@@bpm(INST_RAKU_DEBUG)@ \
@tab@@bpm(INST_RAKUW)@ \
)@@for_langalias(@for_toolchain(@tab@@bpm(@uclangalias@_@uctoolchain@_RUNNER)@ \
)@)@

@bpv(CLEANUPS)@ = \
  @bpm(RUN_RAKUDO_SCRIPT)@ \
@for_langalias(  @bpm(INST_@uclangalias@)@ \
  @bpm(INST_@uclangalias@_DEBUG)@ \
  @bpm(INST_@uclangalias@_M)@ \
  @bpm(INST_@uclangalias@_DEBUG_M)@ \
  @bpm(INST_@uclangalias@W)@ \
  @bpm(INST_@uclangalias@W_M)@ \
  @bpm(INST_@uclangalias@)@@moar::obj@ \
  @bpm(INST_@uclangalias@_DEBUG)@@moar::obj@ \
  @bpm(INST_@uclangalias@_M)@@moar::obj@ \
  @bpm(INST_@uclangalias@_DEBUG_M)@@moar::obj@ \
  @bpm(INST_@uclangalias@W)@@moar::obj@ \
  @bpm(INST_@uclangalias@W_M)@@moar::obj@ \
)@  @bpm(INST_RAKU)@ \
  @bpm(INST_RAKU_DEBUG)@ \
  @bpm(INST_RAKUW)@ \
  @bpm(INST_RAKU)@@moar::obj@ \
  @bpm(INST_RAKU_DEBUG)@@moar::obj@ \
  @bpm(INST_RAKUW)@@moar::obj@ \
  @bpm(RAKUDO_OPS_DLL)@ \
  @bpm(RAKUDO_OPS_OBJ)@ \
@for_langalias(@for_toolchain(  @bpm(@uclangalias@_@uctoolchain@_RUNNER)@ \
)@)@  rakudo-m.c \
  rakudo-m@moar::obj@ \
  rakudo.c \
  rakudo@moar::obj@ \
  rakudo@moar::exe@ \
  perl6@moar::exe@

@bpv(RAKUDO_OPS_EXTRA)@ = @bpm(RAKUDO_OPS_DLL)@

@bpv(NQP_FLAGS_EXTRA)@ = --vmlibs=@bpm(RAKUDO_OPS_DLL)@=Rakudo_ops_init

#@bpv(HARNESS_TYPE)@ = $(HARNESS_TYPE)

@bpv(HARNESS6)@ = @nfp(@base_dir@/@bpm(RUNNER)@)@ -Ilib @nfp(t/harness6)@
@bpv(HARNESS6_WITH_FUDGE)@ = @bpm(HARNESS6)@ --fudge

@bpv(RUN_CLEAN_TARGET_FILES)@ = @nfp(@base_dir@/@bpm(RUNNER)@)@ @script(clean-target-files.raku)@ $(VERBOSE)

@bsv(SPECIFIC_TEST_DIRS)@ = @nfp(t/09-moar)@

@include(Makefile-backend-common)@

@bpm(RAKUDO_OPS_DLL)@: @bpm(RAKUDO_OPS_DLL_SRC)@
	@echo(+++ Compiling	$@)@
	$(NOECHO)@bpm(CC)@ @moar::ccswitch@ @moar::ccshared@ @bpm(CFLAGS)@ @bpm(MOAR_INC_PATHS)@ @moar::ccout@@bpm(RAKUDO_OPS_OBJ)@ @bpm(RAKUDO_OPS_DLL_SRC)@
	$(NOECHO)@bpm(LD)@ @moar::ldswitch@ @moar::lddir@"@moar::libdir@" @moar::ldshared@ @bpm(LDFLAGS)@ @bpm(LDLIBS)@ @moar::ldout@@bpm(RAKUDO_OPS_DLL)@ @bpm(RAKUDO_OPS_OBJ)@ @moar_lib@

@backend_prefix@-runner-default: @bpm(INST_RAKUDO)@ @bpm(INST_RAKUDO_DEBUG)@

@if(platform!=windows @for_langalias(@for_toolchain(
@bpm(@uclangalias@_@uctoolchain@_RUNNER)@: @@configure_script@@ @bpm(C_RUNNER)@ @bsm(RAKUDO)@ @bsm(SETTING)@ @@template(rakudo-@toolchain@-m)@@ @@template(rakudo-debug-notice)@@ @@template(sh-prelude)@@
	@echo(+++ Generating	$@)@
	$(NOECHO)$(RM_F) $@
	$(NOECHO)$(CONFIGURE) --expand @nfpq(@backend_subdir@/rakudo-@toolchain@-m)@ --out $@ \
		--set-var=MOAR=$(MOAR) \
		--set-var=ctx_subdir=@backend_subdir@ \
		--set-var=runner_opts=@chomp(@insert(Makefile-runner_opts)@)@
	-$(NOECHO)$(CHMOD) 755 $@
)@)@)@

@bpm(RUN_RAKUDO_SCRIPT)@: @@nfp(@template(@backend_subdir@/rakudo-m-early-build.in)@)@@
	$(NOECHO)$(RM_F) @q(@bpm(RUN_RAKUDO_SCRIPT)@)@
	$(NOECHO)$(CONFIGURE) --expand @nfpq(@backend_subdir@/@bpm(RUN_RAKUDO_SCRIPT)@)@ --out @bpm(RUN_RAKUDO_SCRIPT)@ \
		--set-var=base_dir=@nfpq(@base_dir@)@ \
		--set-var=moar=$(MOAR) \
		--set-var=nqp_libdir=@nfpq(@nqp::libdir@)@ \
		--set-var=rakudo=@bsm(RAKUDO)@

@bpm(RUNNER)@: @@configure_script@@ @bsm(RAKUDO)@@for_specs( @bsm(SETTING_@ucspec@)@)@ @@nfp(@template(@backend_subdir@/rakudo-m-build.c.in)@)@@ @@nfp(@template(@backend_subdir@/rakudo-m-build.c.windows)@)@@
	@echo(+++ Generating	$@)@
	$(NOECHO)$(RM_F) @q(@bpm(RUNNER)@)@
	$(NOECHO)$(CONFIGURE) --expand @nfpq(@backend_subdir@/rakudo-m-build.c)@ --out rakudo-m.c \
		--set-var=MOAR=$(MOAR) \
		--set-var=base_dir=@shquot(@nfp(@base_dir@)@)@ \
		--set-var=nqp_lib_dir=@shquot(@nfp(@nqp::libdir@)@)@ \
		--set-var=exec_name=@shquot(@nfp(@base_dir@/@bpm(RUNNER)@)@)@ \
		--set-var=mbc=@bsm(RAKUDO)@ \
		--set-var=nqp_home=@shquot(@nfp(@nqp_home@)@)@
	$(NOECHO)@bpm(CC)@ @moar::ccswitch@ @bpm(CFLAGS)@ @moar::ccout@rakudo-m@moar::obj@ rakudo-m.c
	$(NOECHO)@bpm(LD)@ @moar::ldout@$@ @bpm(LDFLAGS)@ @bpm(MINGW_UNICODE)@ rakudo-m@moar::obj@

@perl(
	# Use keys and values as macros to conform with possible future changes to
	# variable name standards.
    my %execs = (
        '@bpm(INST_PERL6)@'          => '@bpm(PERL6)@',
        '@bpm(INST_PERL6_DEBUG)@'    => '@bpm(PERL6_DEBUG)@',
        '@bpm(INST_PERL6_M)@'        => '@bpm(PERL6_M)@',
        '@bpm(INST_PERL6_DEBUG_M)@'  => '@bpm(PERL6_DEBUG_M)@',
        '@bpm(INST_RAKUDO)@'         => '@bpm(RAKUDO)@',
        '@bpm(INST_RAKUDO_DEBUG)@'   => '@bpm(RAKUDO_DEBUG)@',
        '@bpm(INST_RAKUDO_M)@'       => '@bpm(RAKUDO_M)@',
        '@bpm(INST_RAKUDO_DEBUG_M)@' => '@bpm(RAKUDO_DEBUG_M)@',
    );
    if ($cfg->cfg('platform') eq 'windows') {
		$execs{'@bpm(INST_PERL6W)@'}     = '@bpm(PERL6W)@';
		$execs{'@bpm(INST_PERL6W_M)@'}   = '@bpm(PERL6W_M)@';
		$execs{'@bpm(INST_RAKUDOW)@'}    = '@bpm(RAKUDOW)@';
		$execs{'@bpm(INST_RAKUDOW_M)@'}  = '@bpm(RAKUDOW_M)@';
		$execs{'@bpm(INST_RAKU)@'}       = '@bpm(RAKU)@';
		$execs{'@bpm(INST_RAKU_DEBUG)@'} = '@bpm(RAKU_DEBUG)@';
		$execs{'@bpm(INST_RAKUW)@'}      = '@bpm(RAKUW)@';
	}
    for my $build (keys %execs) {
        my $installed = $macros->expand($execs{$build});
		$build = $macros->expand($build);
        my %vars = (
            exec_name => $build,
        );
		my $obj = $build;
        my $qchar = $config{'quote'};
        $vars{exec_path_define} =
            '-DSTATIC_EXEC_PATH=' . $qchar . $cfg->c_escape_string($cfg->nfp($config{'prefix'} . '/bin/' . $installed )) . $qchar
            if $config{relocatable} eq 'nonreloc';
        $vars{debug_flag} = '-DMOAR_RAKUDO_RUNNER_DEBUG' if $build =~ /DEBUG/;
        if ($build !~ /RAKUW|RAKUDOW|PERL6W/) {
            $vars{subsystem_win_ld_flags} = '';
            $vars{subsystem_win_cc_flags} = '';
        }
        $vars{obj_file} = $obj;
        my $scope = $cfg->push_config(%vars);
        $out .= $macros->expand('@insert(Makefile-gen-c-runner)@');
    }
)@

##  testing targets

# No sense in moving this into Makefile-backend-common.in because moar backend
# is the only one supporting harness6
@backend_prefix@-test6   : @backend_prefix@-coretest6

@backend_prefix@-coretest6: @backend_prefix@-all
	@bpm(HARNESS6)@ $(COMMON_TEST_DIRS) @bsm(SPECIFIC_TEST_DIRS)@

# Run the spectests that we know work.
@backend_prefix@-spectest6: @backend_prefix@-testable $(SPECTEST_DATA)
	@bpm(HARNESS6_WITH_FUDGE)@ --tests-from-file=$(SPECTEST_DATA)

@backend_prefix@-spectest_full6: @backend_prefix@-testable
	@bpm(HARNESS6_WITH_FUDGE)@ @nfp(t/spec)@

@backend_prefix@-quicktest6: @backend_prefix@-testable $(SPECTEST_DATA)
	@bpm(HARNESS6_WITH_FUDGE)@ --tests-from-file=$(SPECTEST_DATA) --quick

@backend_prefix@-stresstest6: @backend_prefix@-testable $(SPECTEST_DATA)
	@bpm(HARNESS6_WITH_FUDGE)@ --tests-from-file=$(SPECTEST_DATA) --stress

#spectest_smolder: rakudo_test_run.tar.gz
#	./perl6 -e "shell qqx[git log -1 --pretty=format:%H].fmt(qq[curl -F architecture=@cpuarch@ -F platform=@osname@ -F revision=%s -F report_file=@rakudo_test_run.tar.gz -F username=parrot-autobot -F password=qa_rocks http://smolder.parrot.org/app/projects/process_add_report/5])"

# Run the tests in t/localtest.data
@backend_prefix@-localtest: @backend_prefix@-all spectest_checkout @nfp(t/localtest.data)@
	@bpm(HARNESS6_WITH_FUDGE)@ --tests-from-file=@nfp(t/localtest.data)@

# Run the tests in t/localtest.data with a higher verbosity
@backend_prefix@-localtest_loud: @backend_prefix@-all spectest_checkout @nfp(t/localtest.data)@
	@bpm(HARNESS6_WITH_FUDGE)@ --tests-from-file=@nfp(t/localtest.data)@ --verbosity=1

# Run many tests of your choice.
# make sometests TESTFILES=t/foo/bar
@backend_prefix@-sometests: @backend_prefix@-all
	@bpm(HARNESS6_WITH_FUDGE)@ $(TESTFILES)

@backend_prefix@-install-dirs::
	$(NOECHO)$(MKPATH) @nfpq($(DESTDIR)$(RAKUDO_HOME)/runtime/dynext)@

@backend_prefix@-install-pre::
	@echo(+++ Removing old files)@
	$(NOECHO)@bpm(RUN_CLEAN_TARGET_FILES)@ @nfpq(@nop($(DESTDIR))@@bpm(LIBDIR)@/Perl6)@ @bpm(RAKUDO_PRECOMPS)@
	$(NOECHO)@bpm(RUN_CLEAN_TARGET_FILES)@ @nfpq(@nop($(DESTDIR))@@bpm(LIBDIR)@/Perl6/BOOTSTRAP)@ @bpm(RAKUDO_BOOTSTRAP_PRECOMPS)@
	$(NOECHO)@bpm(RUN_CLEAN_TARGET_FILES)@ @nfpq($(DESTDIR)$(RAKUDO_HOME)/runtime)@ @for_specs($(SETTING_@ucspec@_MOAR) )@
	$(NOECHO)@bpm(RUN_CLEAN_TARGET_FILES)@ @nfpq($(DESTDIR)$(PREFIX)/bin)@ @bsm(RAKUDO)@ @bsm(RAKUDO_DEBUG)@ @bsm(PERL6)@ @bsm(PERL6_DEBUG)@

@backend_prefix@-install-main:: @@bpm(RAKUDO_OPS_DLL)@@ @for_langalias(@@bpm(INST_@uclangalias@_M)@@ @@bpm(INST_@uclangalias@_DEBUG_M)@@ @if(platform==windows @@bpm(INST_@uclangalias@W_M)@@ )@)@
	$(NOECHO)$(CP) @bpm(RAKUDO_OPS_DLL)@ @nfpq($(DESTDIR)$(RAKUDO_HOME)/runtime/dynext)@
@for_langalias(
	$(NOECHO)$(CP) @bpm(INST_@uclangalias@_M)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(@uclangalias@_M)@)@
	$(NOECHO)$(CP) @bpm(INST_@uclangalias@_DEBUG_M)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(@uclangalias@_DEBUG_M)@)@
	@if(platform==windows $(NOECHO)$(CP) @bpm(INST_@uclangalias@W_M)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(@uclangalias@W_M)@)@)@
)@@if(platform!=windows @for_langalias(@for_toolchain(@insert(Makefile-install)@)@)@
)@@if(platform==windows @m_install@)@

@backend_prefix@-install-post::
@expand(@clean_old_p6_libs@)@

@backend_prefix@-runner-default-install: @backend_prefix@-install
	@echo(+++ Installing @uc(@backend@)@ launchers)@
	$(NOECHO)$(CP) @bpm(INST_RAKUDO)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(RAKUDO)@)@
	$(NOECHO)$(CP) @bpm(INST_RAKUDO_DEBUG)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(RAKUDO_DEBUG)@)@
	$(NOECHO)$(CP) @bpm(INST_PERL6)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(PERL6)@)@
	$(NOECHO)$(CP) @bpm(INST_PERL6_DEBUG)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(PERL6_DEBUG)@)@
@if(platform==windows
	$(NOECHO)$(CP) @bpm(INST_RAKUDOW)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(RAKUDOW)@)@
	$(NOECHO)$(CP) @bpm(INST_PERL6W)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(PERL6W)@)@
)@
@if(with_raku_alias==on @tab@@echo(+++ Creating Raku executable alias)@
@if(platform!=windows
	$(NOECHO)$(LN_S) @nfpq(./@bpm(RAKUDO)@)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(RAKU)@)@
	$(NOECHO)$(LN_S) @nfpq(./@bpm(RAKUDO_DEBUG)@)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(RAKU_DEBUG)@)@
)@@if(platform==windows
	$(NOECHO)$(CP) @bpm(INST_RAKU)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(RAKU)@)@
	$(NOECHO)$(CP) @bpm(INST_RAKU_DEBUG)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(RAKU_DEBUG)@)@
	$(NOECHO)$(CP) @bpm(INST_RAKUW)@ @nfpq($(DESTDIR)$(PREFIX)/bin/@bpm(RAKUW)@)@
)@
)@
manifest:
	@echo(+++ Creating	MANIFEST)@
	$(NOECHO)echo MANIFEST >MANIFEST
	@nowcho@git ls-files | $(PERL5) -ne '/^\./ || print' >>MANIFEST
	@nowcho@cd @nfp(t/spec)@ && git clean -xdf
	@nowcho@find @nfp(t/spec)@ -type f | grep -v '\.git' >>MANIFEST
	@nowcho@sort -u -o MANIFEST MANIFEST

release: manifest @bpm(RUN_RAKUDO_SCRIPT)@
	$(NOECHO)[ -n "$(VERSION)" ] || ( echo "\nTry 'make release VERSION=yyyy.mm'\n\n"; exit 1 )
	@echo(+++ Releasing $(VERSION))@
	$(NOECHO)bash -c '[ "$$(cat VERSION)" == "$(VERSION)" ] || ( echo -e "\nVersion on command line and in VERSION file differ\n"; exit 1 )'
	$(NOECHO)[ -d rakudo-$(VERSION) ] || ln -s . rakudo-$(VERSION)
	$(NOECHO)@bpm(RUN_RAKUDO)@ -ne 'say "rakudo-$(VERSION)@slash@$$_"' MANIFEST | \
		tar -zcv --owner=0 --group=0 --numeric-owner -T - -f rakudo-$(VERSION).tar.gz
	$(NOECHO)rm rakudo-$(VERSION)


##  cleaning
@backend_prefix@-clean:
	@echo(+++ Cleaning up @uc(@backend@)@)@
	$(NOECHO)$(RM_F) @bpm(CLEANUPS_ALL)@
	$(NOECHO)$(RM_F) non-existent-copy-mtgt non-existent-copy-stgt

@backend_prefix@-testclean:

# vim: ft=make noexpandtab ts=4 sw=4
