Allow suppression of autogenerated Go init().

Bootstrapping is a problem when there are multiple init() functions
because the order in which they run is not defined.  Therefore, allow
the user to suppress the autogenerated init and perform bootstrapping in
their own init() function.


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/b9a2f521
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/b9a2f521
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/b9a2f521

Branch: refs/heads/master
Commit: b9a2f521858d84c0bc3fc745b13f7ca054825589
Parents: 2bf2a05
Author: Marvin Humphrey <[email protected]>
Authored: Thu Apr 9 15:45:59 2015 -0700
Committer: Marvin Humphrey <[email protected]>
Committed: Wed May 6 14:28:15 2015 -0700

----------------------------------------------------------------------
 compiler/go/cfc/cfc.go |  8 ++++++++
 compiler/src/CFCGo.c   | 21 +++++++++++++++------
 compiler/src/CFCGo.h   |  7 +++++++
 runtime/go/build.go    |  1 +
 4 files changed, 31 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/b9a2f521/compiler/go/cfc/cfc.go
----------------------------------------------------------------------
diff --git a/compiler/go/cfc/cfc.go b/compiler/go/cfc/cfc.go
index 1f30cd2..e2a2560 100644
--- a/compiler/go/cfc/cfc.go
+++ b/compiler/go/cfc/cfc.go
@@ -208,6 +208,14 @@ func (obj *BindGo) SetFooter(header string) {
        C.CFCGo_set_header(obj.ref, headerCString)
 }
 
+func (obj *BindGo) SetSuppressInit(suppressInit bool) {
+       if suppressInit {
+               C.CFCGo_set_suppress_init(obj.ref, C.int(1))
+       } else {
+               C.CFCGo_set_suppress_init(obj.ref, C.int(0))
+       }
+}
+
 func (obj *BindGo) WriteBindings(parcel *Parcel, dest string) {
        destC := C.CString(dest)
        defer C.free(unsafe.Pointer(destC))

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/b9a2f521/compiler/src/CFCGo.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCGo.c b/compiler/src/CFCGo.c
index be67747..18fd063 100644
--- a/compiler/src/CFCGo.c
+++ b/compiler/src/CFCGo.c
@@ -33,6 +33,11 @@
 #include "CFCGoMethod.h"
 #include "CFCGoTypeMap.h"
 
+#ifndef true
+  #define true 1
+  #define false 0
+#endif
+
 static void
 S_CFCGo_destroy(CFCGo *self);
 
@@ -43,6 +48,7 @@ struct CFCGo {
     char *footer;
     char *c_header;
     char *c_footer;
+    int suppress_init;
 };
 
 static const CFCMeta CFCGO_META = {
@@ -60,6 +66,7 @@ CFCGo_new(CFCHierarchy *hierarchy) {
     self->footer     = CFCUtil_strdup("");
     self->c_header   = CFCUtil_strdup("");
     self->c_footer   = CFCUtil_strdup("");
+    self->suppress_init = false;
     return self;
 }
 
@@ -91,6 +98,11 @@ CFCGo_set_footer(CFCGo *self, const char *footer) {
     self->c_footer = CFCUtil_make_c_comment(footer);
 }
 
+void
+CFCGo_set_suppress_init(CFCGo *self, int suppress_init) {
+    self->suppress_init = !!suppress_init;
+}
+
 static void
 S_write_hostdefs(CFCGo *self) {
     const char pattern[] =
@@ -171,12 +183,9 @@ S_gen_cgo_comment(CFCGo *self, CFCParcel *parcel, const 
char *h_includes) {
 }
 
 static char*
-S_gen_init_code(CFCParcel *parcel) {
+S_gen_init_code(CFCGo *self, CFCParcel *parcel) {
     const char *prefix = CFCParcel_get_prefix(parcel);
-
-    // Hack to allow custom init for Clownfish runtime.
-    // In the future we may want to facilitate customizable init.
-    if (strcmp(prefix, "cfish_") == 0) {
+    if (self->suppress_init) {
         return CFCUtil_strdup("");
     }
 
@@ -244,7 +253,7 @@ S_write_cfbind_go(CFCGo *self, CFCParcel *parcel, const 
char *dest,
     const char *PREFIX = CFCParcel_get_PREFIX(parcel);
     char *go_short_package = CFCGoTypeMap_go_short_package(parcel);
     char *cgo_comment   = S_gen_cgo_comment(self, parcel, h_includes);
-    char *init_code     = S_gen_init_code(parcel);
+    char *init_code     = S_gen_init_code(self, parcel);
     char *autogen_go    = S_gen_autogen_go(self, parcel);
     const char pattern[] =
         "%s"

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/b9a2f521/compiler/src/CFCGo.h
----------------------------------------------------------------------
diff --git a/compiler/src/CFCGo.h b/compiler/src/CFCGo.h
index 0953236..395fce6 100644
--- a/compiler/src/CFCGo.h
+++ b/compiler/src/CFCGo.h
@@ -50,6 +50,13 @@ CFCGo_set_header(CFCGo *self, const char *header);
 void
 CFCGo_set_footer(CFCGo *self, const char *footer);
 
+/** If true, suppress autogeneration of the init() function which invokes the
+ * Clownfish parcel bootstrapping.  The caller then becomes responsible for
+ * writing their own init() which performs the bootstrapping.
+ */
+void
+CFCGo_set_suppress_init(CFCGo *self, int suppress_init);
+
 /** Generate CGO bindings for the specified parcel.
  */
 void

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/b9a2f521/runtime/go/build.go
----------------------------------------------------------------------
diff --git a/runtime/go/build.go b/runtime/go/build.go
index d7ad8e6..cf4cb82 100644
--- a/runtime/go/build.go
+++ b/runtime/go/build.go
@@ -125,6 +125,7 @@ func runCFC() {
        if modified {
                goBinding := cfc.NewBindGo(hierarchy)
                goBinding.SetHeader(autogenHeader)
+               goBinding.SetSuppressInit(true)
                parcel := cfc.FetchParcel("Clownfish")
                packageDir := path.Join(buildDir, "clownfish")
                goBinding.WriteBindings(parcel, packageDir)

Reply via email to