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:22:30 UTC

[3/9] lucy git commit: Add stub Go bindings and build.go "script".

Add stub Go bindings and build.go "script".


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

Branch: refs/heads/master
Commit: 52121a1a878ef412eee265e77122b62b6139332f
Parents: 1924548
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Sun Nov 16 20:39:07 2014 -0800
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Sun Mar 15 18:48:11 2015 -0700

----------------------------------------------------------------------
 go/build.go          | 220 ++++++++++++++++++++++++++++++++++++++++++++++
 go/lucy/lucy.go      |  27 ++++++
 go/lucy/lucy_test.go |  23 +++++
 3 files changed, 270 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/52121a1a/go/build.go
----------------------------------------------------------------------
diff --git a/go/build.go b/go/build.go
new file mode 100644
index 0000000..6f1607e
--- /dev/null
+++ b/go/build.go
@@ -0,0 +1,220 @@
+/* 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 main
+
+import "flag"
+import "fmt"
+import "io"
+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.git/go/lucy"
+var cfPackageName 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
+var installedLibPath string
+
+func init() {
+	_, buildGO, _, _ = runtime.Caller(1)
+	buildDir = path.Dir(buildGO)
+	hostSrcDir = path.Join(buildDir, "../c/src")
+	configGO = path.Join(buildDir, "lucy", "config.go")
+	var err error
+	installedLibPath, err = cfc.InstalledLibPath(packageName)
+	if err != nil {
+		log.Fatal(err)
+	}
+}
+
+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", "--enable-go",
+			"--enable-makefile", "--host=go", "--", "-std=gnu99", "-O2")
+	}
+}
+
+func runCFC() {
+	hierarchy := cfc.NewHierarchy("autogen")
+	hierarchy.AddSourceDir("../core")
+	hierarchy.Build()
+	autogenHeader := "Auto-generated by build.go."
+	coreBinding := cfc.NewBindCore(hierarchy, autogenHeader, "")
+	modified := coreBinding.WriteAllModified(false)
+	if modified {
+		cBinding := cfc.NewBindC(hierarchy, autogenHeader, "")
+		cBinding.WriteCallbacks()
+		cBinding.WriteHostDefs()
+		hierarchy.WriteLog()
+	}
+}
+
+func build() {
+	configure()
+	runCFC()
+	runCommand("make", "-j", "static")
+	writeConfigGO()
+	runCommand("go", "build", packageName)
+}
+
+func test() {
+	build()
+	runCommand("go", "test", packageName)
+}
+
+func copyFile(source, dest string) {
+	sourceFH, err := os.Open(source)
+	if err != nil {
+		log.Fatal(err)
+	}
+	defer sourceFH.Close()
+	destFH, err := os.Create(dest)
+	if err != nil {
+		log.Fatal(err)
+	}
+	defer destFH.Close()
+	_, err = io.Copy(destFH, sourceFH)
+	if err != nil {
+		log.Fatalf("io.Copy from %s to %s failed: %s", source, dest, err)
+	}
+}
+
+func installStaticLib() {
+	tempLibPath := path.Join(buildDir, "liblucy.a")
+	destDir := path.Dir(installedLibPath)
+	if _, err := os.Stat(destDir); os.IsNotExist(err) {
+		err = os.MkdirAll(destDir, 0755)
+		if err != nil {
+			log.Fatalf("Can't create dir '%s': %s", destDir, err)
+		}
+	}
+	os.Remove(installedLibPath)
+	copyFile(tempLibPath, installedLibPath)
+}
+
+func install() {
+	build()
+	runCommand("go", "install", packageName)
+	installStaticLib()
+}
+
+func writeConfigGO() {
+	if current(buildGO, configGO) {
+		return
+	}
+	installedLibDir := path.Dir(installedLibPath)
+	cfLibPath, err := cfc.InstalledLibPath(cfPackageName)
+	if err != nil {
+		log.Fatal(err)
+	}
+	cfLibDir := path.Dir(cfLibPath)
+	content := fmt.Sprintf(
+		"// Auto-generated by build.go, specifying absolute path to static lib.\n"+
+			"package lucy\n"+
+			"// #cgo CFLAGS: -I%s/../core\n"+
+			"// #cgo CFLAGS: -I%s\n"+
+			"// #cgo CFLAGS: -I%s/autogen/include\n"+
+			"// #cgo LDFLAGS: -L%s\n"+
+			"// #cgo LDFLAGS: -L%s\n"+
+			"// #cgo LDFLAGS: -L%s\n"+
+			"// #cgo LDFLAGS: -llucy\n"+
+			"// #cgo LDFLAGS: -lclownfish\n"+
+			"import \"C\"\n",
+		buildDir, buildDir, buildDir, buildDir, installedLibDir, cfLibDir)
+	ioutil.WriteFile(configGO, []byte(content), 0666)
+}
+
+func clean() {
+	fmt.Println("Cleaning")
+	if _, err := os.Stat("Makefile"); !os.IsNotExist(err) {
+		runCommand("make", "clean")
+	}
+	files := []string{charmonizerEXE, "charmony.h", "Makefile", configGO}
+	for _, file := range files {
+		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/blob/52121a1a/go/lucy/lucy.go
----------------------------------------------------------------------
diff --git a/go/lucy/lucy.go b/go/lucy/lucy.go
new file mode 100644
index 0000000..908599a
--- /dev/null
+++ b/go/lucy/lucy.go
@@ -0,0 +1,27 @@
+/* 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 lucy
+
+/*
+#include "lucy_parcel.h"
+*/
+import "C"
+import _ "git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish"
+
+func init() {
+	C.lucy_bootstrap_parcel()
+}

http://git-wip-us.apache.org/repos/asf/lucy/blob/52121a1a/go/lucy/lucy_test.go
----------------------------------------------------------------------
diff --git a/go/lucy/lucy_test.go b/go/lucy/lucy_test.go
new file mode 100644
index 0000000..6a4c6d8
--- /dev/null
+++ b/go/lucy/lucy_test.go
@@ -0,0 +1,23 @@
+/* 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 lucy_test
+
+import _ "git-wip-us.apache.org/repos/asf/lucy.git/go/lucy"
+import "testing"
+
+func TestStuff(t *testing.T) {
+}