You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2015/03/19 01:09:31 UTC
[11/19] lucy-clownfish git commit: Start Go bindings for runtime.
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/3d11dea1
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/3d11dea1
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/3d11dea1
Branch: refs/heads/master
Commit: 3d11dea1f974a9403a459c4bb91c4ff09ea858b7
Parents: 0a181fe
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Wed Nov 5 10:51:41 2014 -0800
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Sun Mar 15 18:28:47 2015 -0700
----------------------------------------------------------------------
runtime/go/build.go | 176 ++++++++++++++++++++++++++++
runtime/go/clownfish/clownfish.go | 45 +++++++
runtime/go/clownfish/clownfish_test.go | 24 ++++
runtime/go/ext/clownfish.c | 0
4 files changed, 245 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d11dea1/runtime/go/build.go
----------------------------------------------------------------------
diff --git a/runtime/go/build.go b/runtime/go/build.go
new file mode 100644
index 0000000..269290b
--- /dev/null
+++ b/runtime/go/build.go
@@ -0,0 +1,176 @@
+/* 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 buildGO string
+var configGO string
+
+func init() {
+ _, buildGO, _, _ = runtime.Caller(1)
+ buildDir = path.Dir(buildGO)
+ 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",
+ "--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/3d11dea1/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..7859709
--- /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_dec_refcount(unsafe.Pointer(hash))
+}
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d11dea1/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()
+}
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d11dea1/runtime/go/ext/clownfish.c
----------------------------------------------------------------------
diff --git a/runtime/go/ext/clownfish.c b/runtime/go/ext/clownfish.c
new file mode 100644
index 0000000..e69de29