Start autogenerating Go binding code.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/7a801bb4 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/7a801bb4 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/7a801bb4 Branch: refs/heads/master Commit: 7a801bb42f1c683a73237bff69cdc6bb557eaae6 Parents: 9bbfad4 Author: Marvin Humphrey <[email protected]> Authored: Mon Mar 23 12:06:24 2015 -0700 Committer: Marvin Humphrey <[email protected]> Committed: Wed May 6 14:25:26 2015 -0700 ---------------------------------------------------------------------- compiler/src/CFCGo.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++- runtime/go/build.go | 4 +- 2 files changed, 94 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/7a801bb4/compiler/src/CFCGo.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCGo.c b/compiler/src/CFCGo.c index 3e210a8..fee057f 100644 --- a/compiler/src/CFCGo.c +++ b/compiler/src/CFCGo.c @@ -120,10 +120,99 @@ S_write_hostdefs(CFCGo *self) { FREEMEM(content); } +// Pound-includes for generated headers. +static char* +S_gen_h_includes(CFCGo *self) { + CFCClass **ordered = CFCHierarchy_ordered_classes(self->hierarchy); + char *h_includes = CFCUtil_strdup(""); + for (size_t i = 0; ordered[i] != NULL; i++) { + CFCClass *klass = ordered[i]; + const char *include_h = CFCClass_include_h(klass); + h_includes = CFCUtil_cat(h_includes, "#include \"", include_h, + "\"\n", NULL); + } + FREEMEM(ordered); + return h_includes; +} + +static char* +S_gen_cgo_comment(CFCGo *self, CFCParcel *parcel, const char *h_includes) { + const char *prefix = CFCParcel_get_prefix(parcel); + // Bake in parcel privacy define, so that binding code can be compiled + // without extra compiler flags. + const char *privacy_sym = CFCParcel_get_privacy_sym(parcel); + char pattern[] = + "#define %s\n" + "\n" + "%s\n" + "\n" + ; + return CFCUtil_sprintf(pattern, privacy_sym, h_includes, prefix); +} + +static char* +S_gen_init_code(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) { + return CFCUtil_strdup(""); + } + + const char pattern[] = + "func init() {\n" + " C.%sbootstrap_parcel()\n" + "}\n"; + return CFCUtil_sprintf(pattern, prefix); +} + +static void +S_write_cfbind_go(CFCGo *self, CFCParcel *parcel, const char *dest, + const char *h_includes) { + 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); + const char pattern[] = + "%s" + "\n" + "package %s\n" + "\n" + "/*\n" + "%s\n" + "*/\n" + "import \"C\"\n" + "\n" + "%s\n" + "\n" + "//export %sDummyExport\n" + "func %sDummyExport() int {\n" + "\treturn 1\n" + "}\n" + "%s"; + char *content + = CFCUtil_sprintf(pattern, self->c_header, go_short_package, + cgo_comment, init_code, PREFIX, PREFIX, + self->c_footer); + + char *filepath = CFCUtil_sprintf("%s" CHY_DIR_SEP "cfbind.go", dest); + CFCUtil_write_if_changed(filepath, content, strlen(content)); + + FREEMEM(filepath); + FREEMEM(content); + FREEMEM(init_code); + FREEMEM(cgo_comment); + FREEMEM(go_short_package); +} + void CFCGo_write_bindings(CFCGo *self, CFCParcel *parcel, const char *dest) { - CHY_UNUSED_VAR(parcel); - CHY_UNUSED_VAR(dest); + char *h_includes = S_gen_h_includes(self); + S_write_hostdefs(self); + S_write_cfbind_go(self, parcel, dest, h_includes); + + FREEMEM(h_includes); } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/7a801bb4/runtime/go/build.go ---------------------------------------------------------------------- diff --git a/runtime/go/build.go b/runtime/go/build.go index 3ef57fb..d7ad8e6 100644 --- a/runtime/go/build.go +++ b/runtime/go/build.go @@ -39,12 +39,14 @@ var charmonyH string = "charmony.h" var buildDir string var buildGO string var configGO string +var cfbindGO string var installedLibPath string func init() { _, buildGO, _, _ = runtime.Caller(1) buildDir = path.Dir(buildGO) configGO = path.Join(buildDir, "clownfish", "config.go") + cfbindGO = path.Join(buildDir, "clownfish", "cfbind.go") var err error installedLibPath, err = cfc.InstalledLibPath(packageName) if err != nil { @@ -229,7 +231,7 @@ func clean() { } fmt.Println("Cleaning") runCommand("make", "clean") - cleanables := []string{charmonizerEXE, charmonyH, "Makefile", configGO} + cleanables := []string{charmonizerEXE, charmonyH, "Makefile", configGO, cfbindGO} for _, file := range cleanables { err := os.Remove(file) if err == nil {
