]> xenbits.xensource.com Git - people/royger/xen.git/commitdiff
golang/xenlight: Create stub package
authorRonald Rojas <ronladred@gmail.com>
Wed, 5 Apr 2017 16:05:45 +0000 (17:05 +0100)
committerGeorge Dunlap <george.dunlap@citrix.com>
Thu, 6 Apr 2017 14:33:27 +0000 (15:33 +0100)
Create a basic Makefile to build and install libxenlight Golang
bindings. Also add a stub package which only opens libxl context.

Include a global xenlight.Ctx variable which can be used as the
default context by the entire program if desired.

For now, return simple errors. Proper error handling will be
added in next patch.

Until we get configure support, disable it by default.  It can be
enabled either by adding "CONFIG_GOLANG=y" to .config, or adding it to
the 'make' line.

Signed-off-by: Ronald Rojas <ronladred@gmail.com>
Signed-off-by: George Dunlap <george.dunlap@citrix.com>
Acked-by: Ian Jackson <ian.jackson@citrix.com>
tools/Makefile
tools/Rules.mk
tools/golang/Makefile [new file with mode: 0644]
tools/golang/xenlight/Makefile [new file with mode: 0644]
tools/golang/xenlight/xenlight.go [new file with mode: 0644]

index b40bd730b9f906f0e1ccf06aa4815398b55fcece..1396d95b5034483d44fa93a587a0d7a2064931f5 100644 (file)
@@ -34,6 +34,7 @@ endif
 
 SUBDIRS-y += xenpmd
 SUBDIRS-y += libxl
+SUBDIRS-$(CONFIG_GOLANG) += golang
 SUBDIRS-y += xl
 SUBDIRS-y += helpers
 SUBDIRS-$(CONFIG_X86) += xenpaging
index ee972e4e18aa3927fb80680851584a1627e400f2..7cb28f5c4e3b939d88a48b442f91a609c9b0fe2a 100644 (file)
@@ -33,6 +33,11 @@ XENSTORE_XENSTORED ?= y
 debug ?= y
 debug_symbols ?= $(debug)
 
+# Set CONFIG_GOLANG=y in .config (or in make) to build golang
+CONFIG_GOLANG ?= n
+XEN_GOPATH        = $(XEN_ROOT)/tools/golang
+XEN_GOCODE_URL    = golang.xenproject.org
+
 ifeq ($(debug_symbols),y)
 CFLAGS += -g3
 endif
diff --git a/tools/golang/Makefile b/tools/golang/Makefile
new file mode 100644 (file)
index 0000000..47a9235
--- /dev/null
@@ -0,0 +1,27 @@
+XEN_ROOT=$(CURDIR)/../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+# In order to link against a package in Go, the package must live in a
+# directory tree in the way that Go expects.  To make this possible,
+# there must be a directory such that we can set GOPATH=${dir}, and
+# the package will be under $GOPATH/src/${full-package-path}.
+
+# So we set XEN_GOPATH to $XEN_ROOT/tools/golang.  The xenlight
+# "package build" directory ($PWD/xenlight) will create the "package
+# source" directory in the proper place.  Go programs can use this
+# package by setting GOPATH=$(XEN_GOPATH).
+
+SUBDIRS-y = xenlight
+
+.PHONY: build all
+all build: subdirs-all
+
+.PHONY: install
+install: subdirs-install
+
+.PHONY: clean
+clean: subdirs-clean
+       $(RM) -r src pkg
+
+.PHONY: distclean
+distclean: clean
diff --git a/tools/golang/xenlight/Makefile b/tools/golang/xenlight/Makefile
new file mode 100644 (file)
index 0000000..a4a1a5b
--- /dev/null
@@ -0,0 +1,49 @@
+XEN_ROOT=$(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+# Standing boldly against convention, we insist on installing the
+# package source under $(prefix)/share/gocode
+GOCODE_DIR ?= $(prefix)/share/gocode/
+GOXL_PKG_DIR = /src/$(XEN_GOCODE_URL)/xenlight/
+GOXL_INSTALL_DIR = $(GOCODE_DIR)$(GOXL_PKG_DIR)
+
+# PKGSOURCES: Files which comprise the distributed source package
+PKGSOURCES = xenlight.go
+
+GO ?= go
+
+.PHONY: all
+all: build
+
+.PHONY: package
+package: $(XEN_GOPATH)$(GOXL_PKG_DIR)$(PKGSOURCES)
+
+$(XEN_GOPATH)/src/$(XEN_GOCODE_URL)/xenlight/$(PKGSOURCES): $(PKGSOURCES)
+       $(INSTALL_DIR) $(XEN_GOPATH)$(GOXL_PKG_DIR)
+       $(INSTALL_DATA) $(PKGSOURCES) $(XEN_GOPATH)$(GOXL_PKG_DIR)
+
+# Go will do its own dependency checking, and not actuall go through
+# with the build if none of the input files have changed.
+#
+# NB that because the users of this library need to be able to
+# recompile the library from source, it needs to include '-lxenlight'
+# in the LDFLAGS; and thus we need to add -L$(XEN_XENLIGHT) here
+# so that it can find the actual library.
+.PHONY: build
+build: package
+       CGO_CFLAGS="$(CFLAGS_libxenlight) $(CFLAGS_libxentoollog)" CGO_LDFLAGS="$(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) -L$(XEN_XENLIGHT) -L$(XEN_LIBXENTOOLLOG)" GOPATH=$(XEN_GOPATH) $(GO) install -x $(XEN_GOCODE_URL)/xenlight
+
+.PHONY: install
+install: build
+       $(INSTALL_DIR) $(DESTDIR)$(GOXL_INSTALL_DIR)
+       $(INSTALL_DATA) $(XEN_GOPATH)$(GOXL_PKG_DIR)$(PKGSOURCES) $(DESTDIR)$(GOXL_INSTALL_DIR)
+
+.PHONY: clean
+clean:
+       $(RM) -r $(XEN_GOPATH)$(GOXL_PKG_DIR)
+       $(RM) $(XEN_GOPATH)/pkg/*/$(XEN_GOCODE_URL)/xenlight.a
+
+.PHONY: distclean
+distclean: clean
+
+-include $(DEPS)
diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
new file mode 100644 (file)
index 0000000..7be180c
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2016 George W. Dunlap, Citrix Systems UK Ltd
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; If not, see <http://www.gnu.org/licenses/>.
+ */
+package xenlight
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl -lxentoollog
+#include <stdlib.h>
+#include <libxl.h>
+*/
+import "C"
+
+/*
+ * Other flags that may be needed at some point:
+ *  -lnl-route-3 -lnl-3
+ *
+ * To get back to static linking:
+ * #cgo LDFLAGS: -lxenlight -lyajl_s -lxengnttab -lxenstore -lxenguest -lxentoollog -lxenevtchn -lxenctrl -lblktapctl -lxenforeignmemory -lxencall -lz -luuid -lutil
+ */
+
+import (
+       "fmt"
+       "unsafe"
+)
+
+/*
+ * Types: Builtins
+ */
+type Context struct {
+       ctx    *C.libxl_ctx
+       logger *C.xentoollog_logger_stdiostream
+}
+
+/*
+ * Context
+ */
+var Ctx Context
+
+func (Ctx *Context) IsOpen() bool {
+       return Ctx.ctx != nil
+}
+
+func (Ctx *Context) Open() (err error) {
+       if Ctx.ctx != nil {
+               return
+       }
+
+       Ctx.logger = C.xtl_createlogger_stdiostream(C.stderr, C.XTL_ERROR, 0)
+       if Ctx.logger == nil {
+               err = fmt.Errorf("Cannot open stdiostream")
+               return
+       }
+
+       ret := C.libxl_ctx_alloc(&Ctx.ctx, C.LIBXL_VERSION,
+               0, unsafe.Pointer(Ctx.logger))
+
+       if ret != 0 {
+               err = fmt.Errorf("Error: %d", -ret)
+       }
+       return
+}
+
+func (Ctx *Context) Close() (err error) {
+       ret := C.libxl_ctx_free(Ctx.ctx)
+       Ctx.ctx = nil
+
+       if ret != 0 {
+               err = fmt.Errorf("Error: %d", -ret)
+       }
+       C.xtl_logger_destroy(unsafe.Pointer(Ctx.logger))
+       return
+}
+
+func (Ctx *Context) CheckOpen() (err error) {
+       if Ctx.ctx == nil {
+               err = fmt.Errorf("Context not opened")
+       }
+       return
+}