diff mbox

[RFC,1/8] golang/xenlight: Create stub package

Message ID aaacab85-cbbe-830f-40ae-044cf80a5a61@citrix.com (mailing list archive)
State New, archived
Headers show

Commit Message

George Dunlap Jan. 27, 2017, 7:40 p.m. UTC
On 23/01/17 16:43, Ronald Rojas wrote:
> 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.
> 
> Signed-off-by: Ronald Rojas <ronladred@gmail.com>

Actually, I've done a pretty big re-work of how the makefiles work, to
make sure that we can always be using stuff that is only in-tree, and
not anything that is in our outside environment.

Attached is the result.  Once we have this, then in your test makefiles
can look like this:

dominfo-go: dominfo.go
	GOPATH=$(XEN_GOPATH) $(GO) build -o $@ $<

And that will use the current in-tree version of the xenlight package.

You can probably tell I've got some comments on your testing patch as
well, but that will have to wait until Monday. :-)

Let me know what you think.

Peace,
 -George

> ---
>  tools/Makefile                    | 17 ++++++++
>  tools/golang/xenlight/Makefile    | 31 ++++++++++++++
>  tools/golang/xenlight/xenlight.go | 86 +++++++++++++++++++++++++++++++++++++++
>  3 files changed, 134 insertions(+)
>  create mode 100644 tools/golang/xenlight/Makefile
>  create mode 100644 tools/golang/xenlight/xenlight.go
> 
> diff --git a/tools/Makefile b/tools/Makefile
> index 77e0723..c1e975f 100644
> --- a/tools/Makefile
> +++ b/tools/Makefile
> @@ -11,6 +11,8 @@ SUBDIRS-y += xenstore
>  SUBDIRS-y += misc
>  SUBDIRS-y += examples
>  SUBDIRS-y += hotplug
> +#Uncomment line to build Golang libxl
> +#SUBDIRS-y += golang/xenlight
>  SUBDIRS-y += xentrace
>  SUBDIRS-$(CONFIG_XCUTILS) += xcutils
>  SUBDIRS-$(CONFIG_X86) += firmware
> @@ -303,6 +305,21 @@ subdir-clean-qemu-xen-dir:
>  		$(MAKE) -C qemu-xen-dir clean; \
>  	fi
>  
> +subdir-clean-golang/xenlight: .phony
> +	$(MAKE) -C golang/xenlight clean
> +
> +subdir-distclean-golang/xenlight: .phony
> +	$(MAKE) -C golang/xenlight distclean
> +
> +subdir-install-golang/xenlight: .phony
> +	$(MAKE) -C golang/xenlight install
> +
> +subdir-all-golang/xenlight: .phony
> +	$(MAKE) -C golang/xenlight all
> +
> +subdir-distclean-firmware: .phony
> +	$(MAKE) -C firmware distclean
> +
>  subdir-clean-debugger/gdbsx subdir-distclean-debugger/gdbsx: .phony
>  	$(MAKE) -C debugger/gdbsx clean
>  
> diff --git a/tools/golang/xenlight/Makefile b/tools/golang/xenlight/Makefile
> new file mode 100644
> index 0000000..1c2a2b7
> --- /dev/null
> +++ b/tools/golang/xenlight/Makefile
> @@ -0,0 +1,31 @@
> +XEN_ROOT=$(CURDIR)/../../..
> +GOLANG_SRC=$(GOPATH)/src/xenproject.org/xenlight
> +CGO_CFLAGS = -I$(DESTDIR)$(includedir)
> +CGO_LDFLAGS = -L$(DESTDIR)$(libdir) -Wl,-rpath-link=$(DESTDIR)$(libdir)
> +include $(XEN_ROOT)/tools/Rules.mk
> +
> +BINARY = xenlight.a
> +GO ?= go
> +
> +.PHONY: all
> +all: build
> +
> +.PHONY: build
> +build: xenlight.a
> +
> +.PHONY: install
> +install: build
> +	$(INSTALL_DIR) $(DESTDIR)$(GOLANG_SRC)
> +	$(INSTALL_DATA) xenlight.go $(DESTDIR)$(GOLANG_SRC)
> +
> +.PHONY: clean
> +clean:
> +	$(RM) $(BINARY)
> +
> +.PHONY: distclean
> +distclean: clean
> +
> +xenlight.a: xenlight.go
> +	CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -o $@ $<
> +
> +-include $(DEPS)
> diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
> new file mode 100644
> index 0000000..f82e14e
> --- /dev/null
> +++ b/tools/golang/xenlight/xenlight.go
> @@ -0,0 +1,86 @@
> +/*
> + * Copyright (C) 2016 George W. Dunlap, Citrix Systems UK Ltd
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License as
> + * published by the Free Software Foundation; version 2 of the
> + * License only.
> + *
> + * This program 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
> + * General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
> + * 02110-1301, USA.
> + */
> +package xenlight
> +
> +/*
> +#cgo LDFLAGS: -lxenlight -lyajl
> +#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
> +}
> +
> +/*
> + * Context
> + */
> +var Ctx Context
> +
> +func (Ctx *Context) IsOpen() bool {
> +	return Ctx.ctx != nil
> +}
> +
> +func (Ctx *Context) Open() (err error) {
> +	if Ctx.ctx != nil {
> +		return
> +	}
> +
> +	ret := C.libxl_ctx_alloc(unsafe.Pointer(&Ctx.ctx), C.LIBXL_VERSION, 0, nil)
> +
> +	if ret != 0 {
> +		err = Error(-ret)
> +	}
> +	return
> +}
> +
> +func (Ctx *Context) Close() (err error) {
> +	ret := C.libxl_ctx_free(unsafe.Pointer(Ctx.ctx))
> +	Ctx.ctx = nil
> +
> +	if ret != 0 {
> +		err = Error(-ret)
> +	}
> +	return
> +}
> +
> +func (Ctx *Context) CheckOpen() (err error) {
> +	if Ctx.ctx == nil {
> +		err = fmt.Errorf("Context not opened")
> +	}
> +	return
> +}
> \ No newline at end of file
>
diff mbox

Patch

From 1756fdbdf6abf8ce9b7c83722cc4f91709e93756 Mon Sep 17 00:00:00 2001
From: Ronald Rojas <ronladred@gmail.com>
Date: Mon, 23 Jan 2017 11:43:30 -0500
Subject: [PATCH] golang/xenlight: Create stub package

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.

Signed-off-by: Ronald Rojas <ronladred@gmail.com>
Signed-off-by: George Dunlap <george.dunlap@citrix.com>
---
Changes:

- Moved manual-enable config option to tools/Rules.mk, use
  CONFIG_GOLANG in tools/Makefile

- Added XEN_GOPATH, pointing to tools/golang

- Modified tools/golang/xenlight makefile to construct necessary $GOPATH

- Added tools/golang/Makefile, so we don't need special rules in tools
  to make tools/golang/xenlight; and so we have a single place to remove the
  $GOPATH build side-effects ($GOPATH/src and $GOPATH/pkg)

- Build of tools/golang/xenlight sets $GOPATH and does a 'go install'

- Use tree-provided CFLAGS_libxenlight and LDLIBS_libxenlight, rather
  than hard-coding our own

- Made a PKGSOURCES variable to track dependencies of all target files
  which need to be part of the output package (i.e., what's put in
  $GOPATH/src).  At the moment this is one file, but it will probably
  be more once we start using the IDL.
---
 tools/Makefile                    |  1 +
 tools/Rules.mk                    |  6 +++
 tools/golang/Makefile             | 27 ++++++++++++
 tools/golang/xenlight/Makefile    | 49 ++++++++++++++++++++++
 tools/golang/xenlight/xenlight.go | 86 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 169 insertions(+)
 create mode 100644 tools/golang/Makefile
 create mode 100644 tools/golang/xenlight/Makefile
 create mode 100644 tools/golang/xenlight/xenlight.go

diff --git a/tools/Makefile b/tools/Makefile
index 77e0723..df1fda1 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -31,6 +31,7 @@  endif
 
 SUBDIRS-y += xenpmd
 SUBDIRS-y += libxl
+SUBDIRS-$(CONFIG_GOLANG) += golang
 SUBDIRS-y += helpers
 SUBDIRS-$(CONFIG_X86) += xenpaging
 SUBDIRS-$(CONFIG_X86) += debugger/gdbsx
diff --git a/tools/Rules.mk b/tools/Rules.mk
index b35999b..24e5220 100644
--- a/tools/Rules.mk
+++ b/tools/Rules.mk
@@ -30,6 +30,12 @@  XENSTORE_XENSTORED ?= y
 debug ?= y
 debug_symbols ?= $(debug)
 
+# Uncomment to compile with Go
+CONFIG_GOLANG ?= y
+ifeq ($(CONFIG_GOLANG),y)
+XEN_GOPATH        = $(XEN_ROOT)/tools/golang
+endif
+
 ifeq ($(debug_symbols),y)
 CFLAGS += -g3
 endif
diff --git a/tools/golang/Makefile b/tools/golang/Makefile
new file mode 100644
index 0000000..47a9235
--- /dev/null
+++ b/tools/golang/Makefile
@@ -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
index 0000000..5d578f3
--- /dev/null
+++ b/tools/golang/xenlight/Makefile
@@ -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/xenproject.org/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/xenproject.org/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)" CGO_LDFLAGS="$(LDLIBS_libxenlight) -L$(XEN_XENLIGHT)" GOPATH=$(XEN_GOPATH) $(GO) install -x xenproject.org/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/*/xenproject.org/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
index 0000000..9199e3d
--- /dev/null
+++ b/tools/golang/xenlight/xenlight.go
@@ -0,0 +1,86 @@ 
+/*
+ * Copyright (C) 2016 George W. Dunlap, Citrix Systems UK Ltd
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2 of the
+ * License only.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+package xenlight
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl
+#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
+}
+
+/*
+ * Context
+ */
+var Ctx Context
+
+func (Ctx *Context) IsOpen() bool {
+	return Ctx.ctx != nil
+}
+
+func (Ctx *Context) Open() (err error) {
+	if Ctx.ctx != nil {
+		return
+	}
+
+	ret := C.libxl_ctx_alloc(unsafe.Pointer(&Ctx.ctx), C.LIBXL_VERSION, 0, nil)
+
+	if ret != 0 {
+		err = fmt.Errorf("Error: %d", -ret)
+	}
+	return
+}
+
+func (Ctx *Context) Close() (err error) {
+	ret := C.libxl_ctx_free(unsafe.Pointer(Ctx.ctx))
+	Ctx.ctx = nil
+
+	if ret != 0 {
+		err = fmt.Errorf("Error: %d", -ret)
+	}
+	return
+}
+
+func (Ctx *Context) CheckOpen() (err error) {
+	if Ctx.ctx == nil {
+		err = fmt.Errorf("Context not opened")
+	}
+	return
+}
-- 
2.1.4