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:24 UTC
[04/19] lucy-clownfish git commit: Start Go bindings for CFC.
Start Go bindings for CFC.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/77d82947
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/77d82947
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/77d82947
Branch: refs/heads/master
Commit: 77d82947741d1c2cefc425b7df0f017608f72bc7
Parents: 13ca278
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Tue Nov 4 16:23:25 2014 -0800
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Sun Mar 15 18:05:43 2015 -0700
----------------------------------------------------------------------
compiler/go/build.go | 158 +++++++++++++++++++++++++++++++++++++++
compiler/go/cfc/cfc.go | 51 +++++++++++++
compiler/go/cfc/cfc_test.go | 26 +++++++
3 files changed, 235 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/77d82947/compiler/go/build.go
----------------------------------------------------------------------
diff --git a/compiler/go/build.go b/compiler/go/build.go
new file mode 100644
index 0000000..d4a585b
--- /dev/null
+++ b/compiler/go/build.go
@@ -0,0 +1,158 @@
+/* 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 cfc (Apache Clownfish compiler).
+ */
+package main
+
+import "flag"
+import "fmt"
+import "io/ioutil"
+import "log"
+import "os"
+import "os/exec"
+import "path"
+import "runtime"
+
+var packageName string = "git-wip-us.apache.org/repos/asf/lucy-clownfish.git/compiler/go/cfc"
+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, "cfc", "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 destInfo.ModTime().Before(origInfo.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 prep() {
+ configure()
+ 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, "libcfc.a")
+ content := fmt.Sprintf(
+ "// Auto-generated by build.go, specifying absolute path to static lib.\n"+
+ "package cfc\n"+
+ "// #cgo CFLAGS: -I%s/../include -I%s/../src -I%s\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")
+ if _, err := os.Stat("Makefile"); !os.IsNotExist(err) {
+ 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/77d82947/compiler/go/cfc/cfc.go
----------------------------------------------------------------------
diff --git a/compiler/go/cfc/cfc.go b/compiler/go/cfc/cfc.go
new file mode 100644
index 0000000..b46fafe
--- /dev/null
+++ b/compiler/go/cfc/cfc.go
@@ -0,0 +1,51 @@
+/* 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 cfc provides a compiler for Apache Clownfish.
+ */
+package cfc
+
+// #include "CFC.h"
+// #include <stdlib.h>
+import "C"
+
+import "runtime"
+import "unsafe"
+
+func DoStuff() {
+ hierarchy := NewHierarchy("autogen")
+ hierarchy.Build()
+}
+
+type Hierarchy struct {
+ ref *C.CFCHierarchy
+}
+
+func NewHierarchy(dest string) Hierarchy {
+ destCString := C.CString(dest)
+ defer C.free(unsafe.Pointer(destCString))
+ obj := Hierarchy{C.CFCHierarchy_new(destCString)}
+ runtime.SetFinalizer(&obj, (*Hierarchy).RunDecRef)
+ return obj
+}
+
+func (obj *Hierarchy) RunDecRef() {
+ C.CFCBase_decref((*C.CFCBase)(unsafe.Pointer(obj.ref)))
+}
+
+func (obj *Hierarchy) Build() {
+ C.CFCHierarchy_build(obj.ref)
+}
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/77d82947/compiler/go/cfc/cfc_test.go
----------------------------------------------------------------------
diff --git a/compiler/go/cfc/cfc_test.go b/compiler/go/cfc/cfc_test.go
new file mode 100644
index 0000000..1bfc6f6
--- /dev/null
+++ b/compiler/go/cfc/cfc_test.go
@@ -0,0 +1,26 @@
+/* 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 cfc_test
+
+import "git-wip-us.apache.org/repos/asf/lucy-clownfish.git/compiler/go/cfc"
+import "testing"
+
+func TestStuff(t *testing.T) {
+ hierarchy := cfc.NewHierarchy("autogen")
+ hierarchy.Build()
+}
+