Start Go bindings for runtime.

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

Branch: refs/heads/go_bindings_2
Commit: 047d52dc67ed91fb1e009e4f46173f7fe7a580a0
Parents: 53b7c28
Author: Marvin Humphrey <[email protected]>
Authored: Wed Nov 5 10:51:41 2014 -0800
Committer: Marvin Humphrey <[email protected]>
Committed: Sat Nov 29 21:07:53 2014 -0800

----------------------------------------------------------------------
 runtime/go/build.go                    | 179 ++++++++++++++++++++++++++++
 runtime/go/clownfish/clownfish.go      |  45 +++++++
 runtime/go/clownfish/clownfish_test.go |  24 ++++
 3 files changed, 248 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/047d52dc/runtime/go/build.go
----------------------------------------------------------------------
diff --git a/runtime/go/build.go b/runtime/go/build.go
new file mode 100644
index 0000000..1b24831
--- /dev/null
+++ b/runtime/go/build.go
@@ -0,0 +1,179 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* Build "script" for Apache Clownfish runtime.
+ */
+package main
+
+import "flag"
+import "fmt"
+import "io/ioutil"
+import "log"
+import "os"
+import "os/exec"
+import "path"
+import "runtime"
+
+import "git-wip-us.apache.org/repos/asf/lucy-clownfish.git/compiler/go/cfc"
+
+var packageName string = 
"git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish"
+var charmonizerC string = "../common/charmonizer.c"
+var charmonizerEXE string = "charmonizer"
+var charmonyH string = "charmony.h"
+var buildDir string
+var hostSrcDir string
+var buildGO string
+var configGO string
+
+func init() {
+       _, buildGO, _, _ = runtime.Caller(1)
+       buildDir = path.Dir(buildGO)
+       hostSrcDir = path.Join(buildDir, "../c/src")
+       configGO = path.Join(buildDir, "clownfish", "config.go")
+}
+
+func main() {
+       os.Chdir(buildDir)
+       flag.Parse()
+       action := "build"
+       args := flag.Args()
+       if len(args) > 0 {
+               action = args[0]
+       }
+       switch action {
+       case "build":
+               build()
+       case "clean":
+               clean()
+       case "test":
+               test()
+       case "install":
+               install()
+       default:
+               log.Fatalf("Unrecognized action specified: %s", action)
+       }
+}
+
+func current(orig, dest string) bool {
+       destInfo, err := os.Stat(dest)
+       if err != nil {
+               if os.IsNotExist(err) {
+                       // If dest doesn't exist, we're not current.
+                       return false
+               } else {
+                       log.Fatalf("Unexpected stat err: %s", err)
+               }
+       }
+
+       // If source is newer than dest, we're not current.
+       origInfo, err := os.Stat(orig)
+       if err != nil {
+               log.Fatalf("Unexpected: %s", err)
+       }
+       return origInfo.ModTime().Before(destInfo.ModTime())
+}
+
+func runCommand(name string, args ...string) {
+       command := exec.Command(name, args...)
+       command.Stdout = os.Stdout
+       command.Stderr = os.Stderr
+       err := command.Run()
+       if err != nil {
+               log.Fatal(err)
+       }
+}
+
+func configure() {
+       if !current(charmonizerC, charmonizerEXE) {
+               runCommand("cc", "-o", charmonizerEXE, charmonizerC)
+       }
+       if !current(charmonizerEXE, charmonyH) {
+               runCommand("./charmonizer", "--cc=cc", "--enable-c", 
"--host=go",
+                       "--host-src=" + hostSrcDir, "--enable-makefile",
+                       "--", "-std=gnu99", "-O2")
+       }
+}
+
+func runCFC() {
+       hierarchy := cfc.NewHierarchy("autogen")
+       hierarchy.AddSourceDir("../core")
+       hierarchy.Build()
+       autogenHeader := "/* Auto-generated by build.go. */\n"
+       coreBinding := cfc.NewBindCore(hierarchy, autogenHeader, "")
+       modified := coreBinding.WriteAllModified(false)
+       if modified {
+               cBinding := cfc.NewBindC(hierarchy, autogenHeader, "")
+               cBinding.WriteCallbacks()
+               cBinding.WriteHostDefs()
+               hierarchy.WriteLog()
+       }
+}
+
+func prep() {
+       configure()
+       runCFC()
+       runCommand("make", "-j", "static")
+       writeConfigGO()
+}
+
+func build() {
+       prep()
+       runCommand("go", "build", packageName)
+}
+
+func test() {
+       prep()
+       runCommand("go", "test", packageName)
+}
+
+func install() {
+       prep()
+       runCommand("go", "install", packageName)
+}
+
+func writeConfigGO() {
+       if current(buildGO, configGO) {
+               return
+       }
+       libPath := path.Join(buildDir, "libcfish.a")
+       content := fmt.Sprintf(
+               "// Auto-generated by build.go, specifying absolute path to 
static lib.\n"+
+                       "package clownfish\n"+
+                       "// #cgo CFLAGS: -I%s/../core\n"+
+                       "// #cgo CFLAGS: -I%s\n"+
+                       "// #cgo CFLAGS: -I%s/autogen/include\n"+
+                       "// #cgo LDFLAGS: %s\n"+
+                       "import \"C\"\n",
+               buildDir, buildDir, buildDir, libPath)
+       ioutil.WriteFile(configGO, []byte(content), 0666)
+}
+
+func clean() {
+       if _, err := os.Stat(charmonizerEXE); os.IsNotExist(err) {
+               return
+       }
+       fmt.Println("Cleaning")
+       runCommand("make", "clean")
+       cleanables := []string{charmonizerEXE, charmonyH, "Makefile", configGO}
+       for _, file := range cleanables {
+               err := os.Remove(file)
+               if err == nil {
+                       fmt.Println("Removing", file)
+               } else if !os.IsNotExist(err) {
+                       log.Fatal(err)
+               }
+       }
+}

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/047d52dc/runtime/go/clownfish/clownfish.go
----------------------------------------------------------------------
diff --git a/runtime/go/clownfish/clownfish.go 
b/runtime/go/clownfish/clownfish.go
new file mode 100644
index 0000000..5ed0b8c
--- /dev/null
+++ b/runtime/go/clownfish/clownfish.go
@@ -0,0 +1,45 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package clownfish
+
+/*
+
+#include "charmony.h"
+
+#include "Clownfish/Obj.h"
+#include "Clownfish/Err.h"
+#include "Clownfish/Class.h"
+#include "Clownfish/String.h"
+#include "Clownfish/Hash.h"
+#include "Clownfish/VArray.h"
+#include "Clownfish/String.h"
+#include "Clownfish/Util/Memory.h"
+#include "Clownfish/LockFreeRegistry.h"
+#include "Clownfish/Method.h"
+
+*/
+import "C"
+
+func init() {
+       C.cfish_bootstrap_parcel()
+}
+
+// Temporary test-only routine.
+func DoStuff() {
+       hash := C.cfish_Hash_new(C.uint32_t(0))
+       C.CFISH_Hash_Dec_RefCount(hash)
+}

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/047d52dc/runtime/go/clownfish/clownfish_test.go
----------------------------------------------------------------------
diff --git a/runtime/go/clownfish/clownfish_test.go 
b/runtime/go/clownfish/clownfish_test.go
new file mode 100644
index 0000000..3872b92
--- /dev/null
+++ b/runtime/go/clownfish/clownfish_test.go
@@ -0,0 +1,24 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package clownfish_test
+
+import 
"git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish"
+import "testing"
+
+func TestStuff(t *testing.T) {
+       clownfish.DoStuff()
+}

Reply via email to