Attached are proof-of-concept patches that generate "fakecutables" for nqp and
perl6 for MoarVM. Using NQP.

They create NQP and Rakudo makefile targets that aren't built by default, and
aren't installed anywhere. "proof-of-concept".

They work on "my" machine, but at this point I'd like someone else to take
ownership of them and figure out what's needed to get to a working state. In
particular, the current approach generates the blob as C source code
containing a large array, which may not work on all platforms. (but Perl 5's
Encode module is using this approach reliably $everywhere for blogs about
10% of the size). Jonathan thinks that it might be necessary to use resource
files instead on Win32.

For example - should there be a Configure.pl flag to decide whether to build
fakecutables? Should they be installed instead of the shell/batch wrappers?
Or alongside?
Do we plan to switch over to them at some point?


Why fakecutables? Well, the shell (or batch file) wrappers are all very nice,
but they're an extra process each time (a non-zero speed hit) and they get in
the way of using a debugger (or valgrind).

Behold the awesome power of a real executable:

$ gdb --args ~/Sandpit/moar-san-jit/bin/perl6 -e 'say "Hello world"'
GNU gdb (GDB) 7.6
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/nicholas/Sandpit/moar-san-jit/bin/perl6...done.
(gdb) start
Temporary breakpoint 1 at 0x400a38: file perl6.c, line 1704.
Starting program: /home/nicholas/Sandpit/moar-san-jit/bin/perl6 -e say\ 
\"Hello\ world\"
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Temporary breakpoint 1, main (argc=3, argv=0x7fffffffe3d8) at perl6.c:1704
1704        MVMInstance *instance = MVM_vm_create_instance();
(gdb) c
Continuing.
Hello world
[Inferior 1 (process 3523) exited normally]
(gdb) 


They're tested on exactly one machine - x86_64 CentOS.
They may work elsewhere, but that's why I'd like someone to volunteer to
figure this stuff out.

To avoid a lot of code duplication they require a new API for MoarVM, to
make it easy to run a blob of memory. Really it would be nice to make it
const-correct, but that seemed to require a lot more work than I had time
for (and seems to be an entire mini-project in itself)


It's also clear that what the MoarVM config contains is not exactly useful
to anyone using it. Some of the stuff is build-directory specific (so of
academic interest once it's installed), and none of it is easy to use.

I think what's needed are 6 configuration variables:

C pre-processor flags for making an executable
C code generation flags for making an executable
Linker flags for making an executable
C pre-processor flags for making an extension library
C code generation flags for making an extension library
Dynamic linker flags for making an extension library


What I've done reveals that we've got a lot of duplication of tools between
NQP and Rakudo. It also seems a bit strange that the only CLI parsing code
is in a module called "NQPHLL"

It's also showing up the LTA-ness of the configured library paths.
It's necessary to generate one nqp-fakecutable for testing, and a different
one to be installed. And again, one perl6 to test, one to install.



What is cool is that it's possible to can dump MoarVM config with the stage0
NQP - this means that we can do other stuff in a similar way.
This means that a lot more dogfood possible a lot earlier than is obvious.

Nicholas Clark
>From 1e3fbde3562d77ba8f50d6c62d28e61c87ad6e1f Mon Sep 17 00:00:00 2001
From: Nicholas Clark <n...@ccl4.org>
Date: Wed, 3 Dec 2014 17:19:14 +0100
Subject: [PATCH] Extract MVM_vm_run_cu() from MVM_vm_run_file() - runs a
 loaded CompUnit.

This makes it easy to run bytecode blobs embedded into an executable.
---
 src/moar.c | 20 +++++++++++++-------
 src/moar.h |  1 +
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/moar.c b/src/moar.c
index af2f67d..f241cfd 100644
--- a/src/moar.c
+++ b/src/moar.c
@@ -185,17 +185,23 @@ static void toplevel_initial_invoke(MVMThreadContext *tc, void *data) {
 
 /* Loads bytecode from the specified file name and runs it. */
 void MVM_vm_run_file(MVMInstance *instance, const char *filename) {
-    MVMStaticFrame *start_frame;
-
     /* Map the compilation unit into memory and dissect it. */
     MVMThreadContext *tc = instance->main_thread;
-    MVMCompUnit      *cu = MVM_cu_map_from_file(tc, filename);
+    MVM_vm_run_cu(instance, MVM_cu_map_from_file(tc, filename), filename);
+}
+
+/* Runs bytecode already loaded in the given CompUnit, with optional filename. */
+void MVM_vm_run_cu(MVMInstance *instance, MVMCompUnit *cu, const char *filename) {
+    MVMStaticFrame *start_frame;
+    MVMThreadContext *tc = instance->main_thread;
 
     MVMROOT(tc, cu, {
-        /* The call to MVM_string_utf8_decode() may allocate, invalidating the
-           location cu->body.filename */
-        MVMString *const str = MVM_string_utf8_decode(tc, instance->VMString, filename, strlen(filename));
-        cu->body.filename = str;
+        if (filename) {
+            /* The call to MVM_string_utf8_decode() may allocate, invalidating
+               the location cu->body.filename */
+            MVMString *const str = MVM_string_utf8_decode(tc, instance->VMString, filename, strlen(filename));
+            cu->body.filename = str;
+        }
 
         /* Run deserialization frame, if there is one. */
         if (cu->body.deserialize_frame) {
diff --git a/src/moar.h b/src/moar.h
index 63d64cf..aabe237 100644
--- a/src/moar.h
+++ b/src/moar.h
@@ -168,6 +168,7 @@ MVMObject *MVM_backend_config(MVMThreadContext *tc);
 
 /* Top level VM API functions. */
 MVM_PUBLIC MVMInstance * MVM_vm_create_instance(void);
+MVM_PUBLIC void MVM_vm_run_cu(MVMInstance *instance, MVMCompUnit *cu, const char *filename);
 MVM_PUBLIC void MVM_vm_run_file(MVMInstance *instance, const char *filename);
 MVM_PUBLIC void MVM_vm_dump_file(MVMInstance *instance, const char *filename);
 MVM_PUBLIC void MVM_vm_exit(MVMInstance *instance);
-- 
2.2.0-268-ga0de725

>From 1d2fedc10cf129e9082f1c00b77f1917cc98a0b2 Mon Sep 17 00:00:00 2001
From: Nicholas Clark <n...@ccl4.org>
Date: Wed, 3 Dec 2014 17:35:18 +0100
Subject: [PATCH 1/3] If using MoarVM, read its configuration using the stage0
 NQP executable.

We can't use the regular read_config() function as it assumes a --show-config
option, and unlike the NQP flag, the raw MoarVM output doesn't prefix the
keys with moar::
---
 Configure.pl | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/Configure.pl b/Configure.pl
index ec8522d..39cfbd7 100644
--- a/Configure.pl
+++ b/Configure.pl
@@ -201,7 +201,23 @@ MAIN: {
         my @errors;
         my ($moar_want) = split(' ', slurp('tools/build/MOAR_REVISION'));
         my $moar_path = gen_moar($moar_want, %config, %options);
-        if (!$moar_path) {
+        if ($moar_path) {
+            open my $CONFIG, '-|', $moar_path,
+                '--libpath=src/vm/moar/stage0', 'src/vm/moar/stage0/nqp.moarvm',
+                '-e', 'say(qq{moar::{$_.key}={$_.value}}) for nqp::backendconfig'
+                    or die "Can't read config for $moar_path: $!";
+            while (<$CONFIG>) {
+                if (/\A([^=]+)=(.*)$/) {
+                    $config{$1} = $2;
+                } else {
+                    chomp;
+                    warn "Bad config output from $moar_path: '$_'";
+                }
+            }
+            close $CONFIG
+                or die "Bad close after reading config from $moar_path: $?";
+        }
+        else {
             push @errors,
                 "No suitable MoarVM (moar executable) found using the --prefix\n" .
                 "(You can get a MoarVM built automatically with --gen-moar.)";
-- 
2.2.0-268-ga0de725

>From 8838cefb5363a71ea4a725f2c00ac84abdb459e5 Mon Sep 17 00:00:00 2001
From: Nicholas Clark <n...@ccl4.org>
Date: Wed, 3 Dec 2014 17:35:18 +0100
Subject: [PATCH 2/3] Generate a MoarVM "fakecutable" for nqp.

Adds a Makefile rule that generates nqp(.exe), but not yet used or installed.
If renamed to nqp-m, tests pass, but it can't (usefully) be installed as it
doesn't set the MoarVM libpath. (install-moar-runner.pl != gen-moar-runner.pl)

The Makefile generation also shows up deficiencies in the configuration
information available from MoarVM - there are no useful C include flags, or
linker library flags for making an executable linked against "libmoar.so"
---
 Configure.pl                         |  6 +++
 tools/build/Makefile-Moar.in         | 22 +++++++-
 tools/build/gen-moar-fakecutable.nqp | 97 ++++++++++++++++++++++++++++++++++++
 3 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 tools/build/gen-moar-fakecutable.nqp

diff --git a/Configure.pl b/Configure.pl
index 39cfbd7..2d270fa 100644
--- a/Configure.pl
+++ b/Configure.pl
@@ -216,6 +216,12 @@ MAIN: {
             }
             close $CONFIG
                 or die "Bad close after reading config from $moar_path: $?";
+            # FIXME - really this value should be calculated by the MoarVM
+            # build system, and stored somewhere in the config
+            $config{'moar::cincludes'}
+                = join ' ', map {"$config{'moar::ccinc'}$_"} grep {-d $_ && !/msinttypes/} glob "$config{'moar::prefix'}/include/*";
+            # Likewise this value is what's really needed by anyone else:
+            $config{'moar::mainlibs'} = sprintf "$config{'moar::lddir'}$config{'moar::prefix'}/lib $config{'moar::ldusr'}", "moar";
         }
         else {
             push @errors,
diff --git a/tools/build/Makefile-Moar.in b/tools/build/Makefile-Moar.in
index 15d2c89..326cc6d 100644
--- a/tools/build/Makefile-Moar.in
+++ b/tools/build/Makefile-Moar.in
@@ -2,6 +2,16 @@ BAT          = @bat@
 MOAR         = $(BIN_DIR)/moar
 M_RUNNER     = nqp-m$(BAT)
 
+CC           = @moar::cc@
+CFLAGS       = @moar::cflags@ @moar::ccmiscflags@ @moar::ccoptiflags@ @moar::ccwarnflags@ @moar::mainflags@
+CINCLUDES    = @moar::cincludes@
+
+LD           = @moar::ld@
+LDFLAGS      = @moar::ldflags@ @moar::ldmiscflags@ @moar::ldoptiflags@ @moar::ldlibs@
+MAIN_LIBS    = @moar::mainlibs@
+MAIN_OBJECTS = nqp@moar::obj@
+
+
 M_STAGE0 = src/vm/moar/stage0
 M_STAGE1 = gen/moar/stage1
 M_STAGE2 = gen/moar/stage2
@@ -74,7 +84,8 @@ m-install: m-all
 	$(PERL) tools/build/install-moar-runner.pl "$(DESTDIR)" $(PREFIX)
 
 # FIXME:
-M_CLEANUPS = *.moarvm  gen/moar/* $(M_RUNNER)
+M_CLEANUPS = *.moarvm  gen/moar/* $(M_RUNNER) \
+                nqp@moar::exe@ $(MAIN_OBJECTS) nqp.c
 
 m-clean:
 	$(RM_RF) $(M_CLEANUPS)
@@ -237,6 +248,15 @@ $(NQP_MOAR): $(M_STAGE2_OUTPUT)
 $(M_RUNNER): tools/build/gen-moar-runner.pl
 	$(PERL) tools/build/gen-moar-runner.pl "$(MOAR)"
 
+nqp.c: $(NQP_MOAR) tools/build/gen-moar-fakecutable.nqp
+	$(MOAR) $(NQP_MOAR) tools/build/gen-moar-fakecutable.nqp --target=nqp.c $(NQP_MOAR)
+
+nqp@moar::obj@: nqp.c
+	$(CC) @moar::ccswitch@ $(CFLAGS) $(CINCLUDES) @moar::ccout@nqp@moar::obj@ $*.c
+
+nqp@moar::exe@: $(MAIN_OBJECTS)
+	$(LD) @moar::ldout@$@ $(LDFLAGS) $(MAIN_OBJECTS) $(MAIN_LIBS)
+
 m-runner-default: m-all
 	$(CP) $(M_RUNNER) nqp$(BAT)
 	$(CHMOD) 755 nqp$(BAT)
diff --git a/tools/build/gen-moar-fakecutable.nqp b/tools/build/gen-moar-fakecutable.nqp
new file mode 100644
index 0000000..87186bf
--- /dev/null
+++ b/tools/build/gen-moar-fakecutable.nqp
@@ -0,0 +1,97 @@
+use NQPHLL;
+
+my $uint8 := nqp::newtype(NQPNativeHOW.new, 'P6int');
+my $info := nqp::hash();
+$info<integer> := nqp::hash();
+$info<integer><bits> := 8;
+$info<integer><unsigned> := 1;
+$info<float> := nqp::hash();
+$info<float><bits> := 8;
+nqp::composetype($uint8, $info);
+
+my $at := nqp::newtype(NQPClassHOW.new, 'VMArray');
+nqp::composetype($at, nqp::hash('array', nqp::hash('type', $uint8)));
+
+my @hex;
+{
+    my int $i := 0;
+    while $i < 256 {
+        @hex[$i] := nqp::sprintf("0x%02X", [$i]);
+        $i := $i + 1;
+    }
+}
+
+sub MAIN(*@ARGS) {
+    my $parser := HLL::CommandLine::Parser.new(['target=s']);
+    my $cl := $parser.parse(@ARGS);
+    my str $infile := $cl.arguments[1];
+    my str $outfile := $cl.options{'target'};
+
+    nqp::die(@ARGS[0] ~ " --target=[file] [input]")
+       if +$cl.arguments != 2 || !nqp::defined($outfile);
+
+    my int $len := nqp::stat($infile, nqp::const::STAT_FILESIZE);
+    nqp::die("$infile is too short (" ~ $len ~ ")")
+       if $len < 1;
+
+    my $in := nqp::open($infile, 'r');
+    my $out := nqp::open($outfile, 'w');
+
+    # Explicit length for the array, so that the C compiler should warn if we
+    # don't generate an initialiser with a consistent length
+    nqp::sayfh($out, '#include <stdlib.h>
+#include "moar.h"
+
+/* It is important that this is const, so that it can go in a read-only section
+   of the object file, and hence be mapped in shared between all processes.  */
+
+static const MVMuint8 blob[' ~ $len ~ '] = {');
+
+    my int $remaining := $len;
+    while !nqp::eoffh($in) {
+        my $buf := nqp::create($at);
+        nqp::readfh($in, $buf, 8);
+        my @out;
+        for $buf {
+            nqp::push(@out, @hex[$_]);
+            # Doing *this* in the loop is about 100 times slower:
+            # nqp::push(@out, nqp::sprintf("0x%02X", [$_]));
+        }
+        $remaining := $remaining - nqp::elems($buf);
+        my str $line := "    " ~ join(", ", @out);
+        if $remaining {
+            nqp::sayfh($out, $line ~ ",");
+        } else {
+            nqp::sayfh($out, $line);
+        }
+    }
+    # If there's going to be an error from this, let's have it now, so that we
+    # generate partial output, and the C compiler then chokes.
+    nqp::closefh($in);
+
+    nqp::printfh($out, '};
+
+int main(int argc, char *argv[])
+{
+    MVMInstance *instance = MVM_vm_create_instance();
+
+    instance->num_clargs  = argc - 1;
+    instance->raw_clargs  = argv + 1;
+    instance->prog_name   = argv[0];
+    instance->exec_name   = argv[0];
+    instance->lib_path[0] = NULL;
+
+    /* FIXME - casting away const sucks, but right now pushing const-correctness
+       down into MoarVM is rather more than I want to tackle, as it seems to end
+       up with conflicting desires for the const-ness of pointers in some of the
+       key data structures which are used both for compiletime and runtime.  */
+    MVM_vm_run_cu(instance,
+                  MVM_cu_from_bytes(instance->main_thread, (MVMuint8 *)blob, (MVMuint32)sizeof(blob)),
+                  argv[0]);
+    MVM_vm_exit(instance);
+    return 0;
+}
+');
+
+    nqp::closefh($out);
+}
-- 
2.2.0-268-ga0de725

>From db85d618dcc38875cddcea8efce2d6da8e5b6d0c Mon Sep 17 00:00:00 2001
From: Nicholas Clark <n...@ccl4.org>
Date: Wed, 3 Dec 2014 17:35:18 +0100
Subject: [PATCH 3/3] Generate an installable MoarVM "fakecutable" with an
 embedded libpath.

Unfortunately this now means that we generate one fakecutable for testing, and
a second, *different* fakecutable for installation. This is L.T.A.
---
 tools/build/Makefile-Moar.in         | 15 +++++++++++++--
 tools/build/gen-moar-fakecutable.nqp | 30 ++++++++++++++++++++++++------
 2 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/tools/build/Makefile-Moar.in b/tools/build/Makefile-Moar.in
index 326cc6d..201cc86 100644
--- a/tools/build/Makefile-Moar.in
+++ b/tools/build/Makefile-Moar.in
@@ -10,6 +10,7 @@ LD           = @moar::ld@
 LDFLAGS      = @moar::ldflags@ @moar::ldmiscflags@ @moar::ldoptiflags@ @moar::ldlibs@
 MAIN_LIBS    = @moar::mainlibs@
 MAIN_OBJECTS = nqp@moar::obj@
+TEST_OBJECTS = nqp-test@moar::obj@
 
 
 M_STAGE0 = src/vm/moar/stage0
@@ -85,7 +86,8 @@ m-install: m-all
 
 # FIXME:
 M_CLEANUPS = *.moarvm  gen/moar/* $(M_RUNNER) \
-                nqp@moar::exe@ $(MAIN_OBJECTS) nqp.c
+                nqp@moar::exe@ $(MAIN_OBJECTS) nqp.c \
+                nqp-test@moar::exe@ $(TEST_OBJECTS) nqp-test.c
 
 m-clean:
 	$(RM_RF) $(M_CLEANUPS)
@@ -248,8 +250,17 @@ $(NQP_MOAR): $(M_STAGE2_OUTPUT)
 $(M_RUNNER): tools/build/gen-moar-runner.pl
 	$(PERL) tools/build/gen-moar-runner.pl "$(MOAR)"
 
+nqp-test.c: $(NQP_MOAR) tools/build/gen-moar-fakecutable.nqp
+	$(MOAR) $(NQP_MOAR) tools/build/gen-moar-fakecutable.nqp --target=nqp-test.c $(NQP_MOAR)
+
+nqp-test@moar::obj@: nqp-test.c
+	$(CC) @moar::ccswitch@ $(CFLAGS) $(MAINFLAGS) $(CINCLUDES) @moar::ccout@nqp-test@moar::obj@ $*.c
+
+nqp-test@moar::exe@: $(TEST_OBJECTS)
+	$(LD) @moar::ldout@$@ $(LDFLAGS) $(TEST_OBJECTS) $(MAIN_LIBS)
+
 nqp.c: $(NQP_MOAR) tools/build/gen-moar-fakecutable.nqp
-	$(MOAR) $(NQP_MOAR) tools/build/gen-moar-fakecutable.nqp --target=nqp.c $(NQP_MOAR)
+	$(MOAR) $(NQP_MOAR) tools/build/gen-moar-fakecutable.nqp --target=nqp.c $(NQP_MOAR) @prefix@/languages/nqp/lib
 
 nqp@moar::obj@: nqp.c
 	$(CC) @moar::ccswitch@ $(CFLAGS) $(CINCLUDES) @moar::ccout@nqp@moar::obj@ $*.c
diff --git a/tools/build/gen-moar-fakecutable.nqp b/tools/build/gen-moar-fakecutable.nqp
index 87186bf..f219d89 100644
--- a/tools/build/gen-moar-fakecutable.nqp
+++ b/tools/build/gen-moar-fakecutable.nqp
@@ -1,4 +1,5 @@
 use NQPHLL;
+use QRegex;
 
 my $uint8 := nqp::newtype(NQPNativeHOW.new, 'P6int');
 my $info := nqp::hash();
@@ -12,6 +13,10 @@ nqp::composetype($uint8, $info);
 my $at := nqp::newtype(NQPClassHOW.new, 'VMArray');
 nqp::composetype($at, nqp::hash('array', nqp::hash('type', $uint8)));
 
+my %escapes;
+%escapes<"> := q{\\"};
+%escapes<\\> := q{\\\\};
+
 my @hex;
 {
     my int $i := 0;
@@ -24,11 +29,15 @@ my @hex;
 sub MAIN(*@ARGS) {
     my $parser := HLL::CommandLine::Parser.new(['target=s']);
     my $cl := $parser.parse(@ARGS);
-    my str $infile := $cl.arguments[1];
     my str $outfile := $cl.options{'target'};
 
-    nqp::die(@ARGS[0] ~ " --target=[file] [input]")
-       if +$cl.arguments != 2 || !nqp::defined($outfile);
+    nqp::die(@ARGS[0] ~ " --target=[file] [input] ([libpath]...)")
+       if +$cl.arguments < 2 || !nqp::defined($outfile);
+    nqp::die(@ARGS[0] ~ " Too many libpaths")
+        if +$cl.arguments > 9;
+
+    nqp::shift($cl.arguments);
+    my str $infile := nqp::shift($cl.arguments);
 
     my int $len := nqp::stat($infile, nqp::const::STAT_FILESIZE);
     nqp::die("$infile is too short (" ~ $len ~ ")")
@@ -69,7 +78,7 @@ static const MVMuint8 blob[' ~ $len ~ '] = {');
     # generate partial output, and the C compiler then chokes.
     nqp::closefh($in);
 
-    nqp::printfh($out, '};
+    nqp::sayfh($out, '};
 
 int main(int argc, char *argv[])
 {
@@ -78,9 +87,18 @@ int main(int argc, char *argv[])
     instance->num_clargs  = argc - 1;
     instance->raw_clargs  = argv + 1;
     instance->prog_name   = argv[0];
-    instance->exec_name   = argv[0];
-    instance->lib_path[0] = NULL;
+    instance->exec_name   = argv[0];');
+
+    my int $i := 0;
+    for ($cl.arguments) {
+        my $escaped := subst($_, /(<["\\]>)/, -> $m { %escapes{$m[0]} }, :global);
+        nqp::sayfh($out, qq{    instance->lib_path[{$i}] = "{$escaped}";});
+        $i := $i + 1;
+    }
+
+    nqp::sayfh($out, "    instance->lib_path[{$i}] = NULL;");
 
+    nqp::printfh($out, '
     /* FIXME - casting away const sucks, but right now pushing const-correctness
        down into MoarVM is rather more than I want to tackle, as it seems to end
        up with conflicting desires for the const-ness of pointers in some of the
-- 
2.2.0-268-ga0de725

>From 3400232e920db2cf02c3848b652b9b7600422d24 Mon Sep 17 00:00:00 2001
From: Nicholas Clark <n...@ccl4.org>
Date: Wed, 3 Dec 2014 19:32:36 +0100
Subject: [PATCH 1/2] Blatantly copy the MoarVM config fixups from NQP.

Use these to simplify the Makefile.
---
 tools/build/Makefile-Moar.in | 13 +++++--------
 tools/lib/NQP/Configure.pm   |  8 ++++++++
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/tools/build/Makefile-Moar.in b/tools/build/Makefile-Moar.in
index 044cdc2..c7f360e 100644
--- a/tools/build/Makefile-Moar.in
+++ b/tools/build/Makefile-Moar.in
@@ -1,6 +1,7 @@
 M_CC      = @moar::cc@
 M_LD      = @moar::ld@
 M_CFLAGS  = @moar::cflags@ @moar::ccmiscflags@ @moar::ccoptiflags@ @moar::ccwarnflags@
+M_CINCLUDES = @moar::cincludes@
 M_LDFLAGS = @moar::ldflags@ @moar::ldmiscflags@ @moar::ldoptiflags@ @moar::ldlibs@
 
 M_GEN_CAT = tools/build/gen-cat.nqp moar
@@ -84,14 +85,10 @@ HARNESS = $(PERL) t/harness
 m-all: $(PERL6_MOAR) $(SETTING_MOAR) $(R_SETTING_MOAR) $(M_RUNNER) lib/Test.pm.moarvm lib/lib.pm6.moarvm lib/Pod/To/Text.pm.moarvm $(PERL6_DEBUG_MOAR) $(M_DEBUG_RUNNER)
 
 $(M_PERL6_OPS_DLL): $(M_PERL6_OPS_SRC) $(M_PERL6_CONT_SRC) Makefile
-	$(M_CC) @moar::ccswitch@ @moar::ccshared@ $(M_CFLAGS) -I$(PREFIX)/include/libatomic_ops \
-	    -I$(PREFIX)/include/dyncall -I$(PREFIX)/include/linenoise -I$(PREFIX)/include/moar \
-	    -I$(PREFIX)/include/sha1 -I$(PREFIX)/include/tinymt  -I$(PREFIX)/include/libtommath \
-	    -I$(PREFIX)/include/libuv -I$(PREFIX)/include @moar::ccout@$(M_PERL6_OPS_OBJ) $(M_PERL6_OPS_SRC)
-	$(M_CC) @moar::ccswitch@ @moar::ccshared@ $(M_CFLAGS) -I$(PREFIX)/include/libatomic_ops \
-	    -I$(PREFIX)/include/dyncall -I$(PREFIX)/include/linenoise -I$(PREFIX)/include/moar \
-	    -I$(PREFIX)/include/sha1 -I$(PREFIX)/include/tinymt  -I$(PREFIX)/include/libtommath \
-	    -I$(PREFIX)/include/libuv -I$(PREFIX)/include @moar::ccout@$(M_PERL6_CONT_OBJ) $(M_PERL6_CONT_SRC)
+	$(M_CC) @moar::ccswitch@ @moar::ccshared@ $(M_CFLAGS) $(M_CINCLUDES) \
+	    @moar::ccout@$(M_PERL6_OPS_OBJ) $(M_PERL6_OPS_SRC)
+	$(M_CC) @moar::ccswitch@ @moar::ccshared@ $(M_CFLAGS) $(M_CINCLUDES) \
+	    @moar::ccout@$(M_PERL6_CONT_OBJ) $(M_PERL6_CONT_SRC)
 	$(M_LD) @moar::ldswitch@ @moar::ldshared@ $(M_LDFLAGS) @moar::ldout@$(M_PERL6_OPS_DLL) $(M_PERL6_OPS_OBJ) $(M_PERL6_CONT_OBJ) @moarimplib@
 
 $(PERL6_ML_MOAR): src/Perl6/ModuleLoader.nqp src/vm/moar/ModuleLoaderVMConfig.nqp
diff --git a/tools/lib/NQP/Configure.pm b/tools/lib/NQP/Configure.pm
index 49bd5bc..7d135b4 100644
--- a/tools/lib/NQP/Configure.pm
+++ b/tools/lib/NQP/Configure.pm
@@ -317,6 +317,14 @@ sub gen_nqp {
             else {
                 $need{$b} = 1;
             }
+            if ($backends =~ /moar/) {
+                # FIXME - really this value should be calculated by the MoarVM
+                # build system, and stored somewhere in the config
+                $c{'moar::cincludes'}
+                    = join ' ', map {"$c{'moar::ccinc'}$_"} grep {-d $_ && !/msinttypes/} glob "$c{'moar::prefix'}/include/*";
+                # Likewise this value is what's really needed by anyone else:
+                $c{'moar::mainlibs'} = sprintf "$c{'moar::lddir'}$c{'moar::prefix'}/lib $c{'moar::ldusr'}", "moar";
+            }
         }
     }
 
-- 
2.2.0-268-ga0de725

>From df0d350404bd9fbfedc384c9c9bdfb2e0deeebb9 Mon Sep 17 00:00:00 2001
From: Nicholas Clark <n...@ccl4.org>
Date: Wed, 3 Dec 2014 20:33:14 +0100
Subject: [PATCH 2/2] Proof of concept fakecutable.

This is an incomplete patch - it needs a copy of gen-moar-fakecutable.nqp from
nqp.
---
 tools/build/Makefile-Moar.in | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/tools/build/Makefile-Moar.in b/tools/build/Makefile-Moar.in
index c7f360e..1b57898 100644
--- a/tools/build/Makefile-Moar.in
+++ b/tools/build/Makefile-Moar.in
@@ -1,8 +1,12 @@
 M_CC      = @moar::cc@
 M_LD      = @moar::ld@
 M_CFLAGS  = @moar::cflags@ @moar::ccmiscflags@ @moar::ccoptiflags@ @moar::ccwarnflags@
+M_MAIN_CFLAGS = $(M_CFLAGS) @moar::mainflags@
 M_CINCLUDES = @moar::cincludes@
 M_LDFLAGS = @moar::ldflags@ @moar::ldmiscflags@ @moar::ldoptiflags@ @moar::ldlibs@
+M_MAIN_LIBS = @moar::mainlibs@
+MAIN_OBJECTS = perl6@moar::obj@
+TEST_OBJECTS = perl6-test@moar::obj@
 
 M_GEN_CAT = tools/build/gen-cat.nqp moar
 
@@ -91,6 +95,24 @@ $(M_PERL6_OPS_DLL): $(M_PERL6_OPS_SRC) $(M_PERL6_CONT_SRC) Makefile
 	    @moar::ccout@$(M_PERL6_CONT_OBJ) $(M_PERL6_CONT_SRC)
 	$(M_LD) @moar::ldswitch@ @moar::ldshared@ $(M_LDFLAGS) @moar::ldout@$(M_PERL6_OPS_DLL) $(M_PERL6_OPS_OBJ) $(M_PERL6_CONT_OBJ) @moarimplib@
 
+perl6-test.c: $(PERL6_MOAR) tools/build/gen-moar-fakecutable.nqp
+	$(M_NQP) tools/build/gen-moar-fakecutable.nqp --target=perl6-test.c $(PERL6_MOAR) "$(M_LIBPATH)" .
+
+perl6-test@moar::obj@: perl6-test.c
+	$(M_CC) @moar::ccswitch@ $(M_MAIN_CFLAGS) $(M_MAIN_CFLAGS) $(M_CINCLUDES) @moar::ccout@perl6-test@moar::obj@ $*.c
+
+perl6-test@moar::exe@: $(TEST_OBJECTS)
+	$(M_LD) @moar::ldout@$@ $(M_LDFLAGS) $(TEST_OBJECTS) $(M_MAIN_LIBS)
+
+perl6.c: $(PERL6_MOAR) tools/build/gen-moar-fakecutable.nqp
+	$(M_NQP) tools/build/gen-moar-fakecutable.nqp --target=perl6.c $(PERL6_MOAR) "$(M_LIBPATH)" "$(PERL6_LANG_DIR)/lib" "$(PERL6_LANG_DIR)/runtime"
+
+perl6@moar::obj@: perl6.c
+	$(M_CC) @moar::ccswitch@ $(M_MAIN_CFLAGS) $(M_MAIN_CFLAGS) $(M_CINCLUDES) @moar::ccout@perl6@moar::obj@ $*.c
+
+perl6@moar::exe@: $(MAIN_OBJECTS)
+	$(M_LD) @moar::ldout@$@ $(M_LDFLAGS) $(MAIN_OBJECTS) $(M_MAIN_LIBS)
+
 $(PERL6_ML_MOAR): src/Perl6/ModuleLoader.nqp src/vm/moar/ModuleLoaderVMConfig.nqp
 	$(M_NQP) $(M_GEN_CAT) src/vm/moar/ModuleLoaderVMConfig.nqp src/Perl6/ModuleLoader.nqp > src/gen/m-ModuleLoader.nqp
 	$(M_NQP) --target=mbc --output=$(PERL6_ML_MOAR) --encoding=utf8 \
-- 
2.2.0-268-ga0de725

Reply via email to