Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #36 from vmarkovtsev/master
Browse files Browse the repository at this point in the history
Add Windows installation instructions
  • Loading branch information
juanjux authored Jan 24, 2018
2 parents 10062db + 233e948 commit c134769
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea

# libuast source code
*.c
*.h
Expand Down
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ go_import_path: gopkg.in/bblfsh/client-go.v2

sudo: required

addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-6
- g++-6

services:
- docker

before_install:
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-6 90
- sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 90
- docker run --privileged -d -p 9432:9432 --name bblfsh bblfsh/bblfshd
- make dependencies

Expand Down
30 changes: 25 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Package configuration
PROJECT = client-go
LIBUAST_VERSION=1.6.1
LIBUAST_VERSION=.1.8.1
GOPATH ?= $(shell go env GOPATH)

ifneq ($(OS),Windows_NT)
COPY = cp
else
COPY = copy
endif

# Including ci Makefile
MAKEFILE = Makefile.main
Expand All @@ -10,22 +17,35 @@ CI_FOLDER = .ci
TOOLS_FOLDER = tools

$(MAKEFILE):
@git clone --quiet $(CI_REPOSITORY) $(CI_FOLDER); \
cp $(CI_FOLDER)/$(MAKEFILE) .;
@(git clone --quiet $(CI_REPOSITORY) $(CI_FOLDER) && \
$(COPY) $(CI_FOLDER)/$(MAKEFILE) .);

-include $(MAKEFILE)
GOGET ?= go get

clean: clean-libuast
clean-libuast:
find ./ -name '*.[h,c]' ! -name 'bindings.h' -exec rm -f {} +

dependencies: cgo-dependencies
ifneq ($(OS),Windows_NT)
cgo-dependencies:
curl -SL https://github.com/bblfsh/libuast/releases/download/v$(LIBUAST_VERSION)/libuast-v$(LIBUAST_VERSION).tar.gz | tar xz
mv libuast-v$(LIBUAST_VERSION)/src/* $(TOOLS_FOLDER)/.
rm -rf libuast-v$(LIBUAST_VERSION)
$(GOGET) .
$(GOGET) ./...
else
binaries.win64.mingw\lib:
go get -v github.com/mholt/archiver/cmd/archiver
cd $(TOOLS_FOLDER) && \
curl -SLko binaries.win64.mingw.zip https://github.com/bblfsh/libuast/releases/download/v$(LIBUAST_VERSION)/binaries.win64.mingw.zip && \
$(GOPATH)\bin\archiver open binaries.win64.mingw.zip && \
del /q binaries.win64.mingw.zip && echo done

cgo-dependencies: binaries.win64.mingw\lib
$(GOGET) ./...
endif # !Windows_NT

# $(DEPENDENCIES) it's allowed to file since the code it's not compilable
# $(DEPENDENCIES) it's allowed to file since the code is not compilable
# without libuast.
.IGNORE: $(DEPENDENCIES)
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# client-go [![GoDoc](https://godoc.org/gopkg.in/bblfsh/client-go.v2?status.svg)](https://godoc.org/gopkg.in/bblfsh/client-go.v2) [![Build Status](https://travis-ci.org/bblfsh/client-go.svg?branch=master)](https://travis-ci.org/bblfsh/client-go) [![codecov](https://codecov.io/gh/bblfsh/client-go/branch/master/graph/badge.svg)](https://codecov.io/gh/bblfsh/client-go)
# client-go [![GoDoc](https://godoc.org/gopkg.in/bblfsh/client-go.v2?status.svg)](https://godoc.org/gopkg.in/bblfsh/client-go.v2) [![Build Status](https://travis-ci.org/bblfsh/client-go.svg?branch=master)](https://travis-ci.org/bblfsh/client-go) [![Build status](https://ci.appveyor.com/api/projects/status/5u1dutc8xdgfoh29?svg=true)](https://ci.appveyor.com/project/vmarkovtsev/client-go) [![codecov](https://codecov.io/gh/bblfsh/client-go/branch/master/graph/badge.svg)](https://codecov.io/gh/bblfsh/client-go)

[Babelfish](https://doc.bblf.sh) Go client library provides functionality to both
connect to the Babelfish server to parse code
Expand All @@ -15,6 +15,9 @@ cd $GOPATH/src/gopkg.in/bblfsh/client-go.v2
make dependencies
```

Windows build is supported, provided by you have `make` and `curl` in your `%PATH%`.
It is also possible to link against custom `libuast` on Windows, read [WINDOWS.md](WINDOWS.md).

## Example

This small example illustrates how to retrieve the [UAST](https://doc.bblf.sh/uast/specification.html) from a small Python script.
Expand Down
46 changes: 46 additions & 0 deletions WINDOWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Installing client-go on Windows

There are two supported options how to build client-go on Windows:
static linking using MinGW or dynamic linking with Visual Studio.
The former does not require any external DLL files at runtime and is "Go style".

## Prerequisites

Same as for [libuast](https://github.com/src-d/libuast/blob/master/WINDOWS.md)
and additionally:

* MinGW is always required because CGo expects a GNU compiler under the hood.
* MSVC++ option requires [pexports](https://sourceforge.net/projects/mingw/files/MinGW/Extension/pexports/pexports-0.47/)

The following options correspond to how `libuast` was built: CGo always
uses MinGW internally.

## MinGW static

```
SET CGO_CFLAGS=-I%PREFIX%\include -DLIBUAST_STATIC
SET CGO_LDFLAGS=-L%PREFIX%\lib -luast -lxml2 -static -lstdc++ -static-libgcc
go get -v -tags custom_libuast gopkg.in/bblfsh/client-go.v2/...
```

`-static-libstdc++` instead of `-static -lstdc++` may be used if works.

## MSVC++ dynamic

We use `dlltool` to build the interface static libraries to call the foreign DLLs.

```
pexports %PREFIX%/bin/libxml2.dll > %PREFIX%/bin/libxml2.def
dlltool -k --no-leading-underscore -d %PREFIX%/bin/libxml2.def -l %PREFIX%/lib/libxml2.a
pexports %PREFIX%/bin/uast.dll > %PREFIX%/bin/uast.def
dlltool -k --no-leading-underscore -d %PREFIX%/bin/libxml2.def -l %PREFIX%/lib/libuast.a
SET CGO_CFLAGS=-I%PREFIX%\include
SET CGO_LDFLAGS=-L%PREFIX%\lib -luast -lxml2
go get -v -tags custom_libuast gopkg.in/bblfsh/client-go.v2/...
```

You have to carry `%PREFIX%\bin\libxml2.dll` and `%PREFIX%\bin\uast.dll`
to the path where you call a Go binary which depends on client-go.
You also need to install [Microsoft Visual C++ Redistributable](https://www.visualstudio.com/downloads/#title-39324)
on clients.
21 changes: 21 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: "{build}"
platform: x64
image: Visual Studio 2017

matrix:
allow_failures:
- platform: x64

clone_folder: c:\gopath\src\gopkg.in\bblfsh\client-go.v2

environment:
GOPATH: c:\gopath

install:
- choco install make
- c:\msys64\usr\bin\pacman --noconfirm --needed -S mingw-w64-x86_64-toolchain

build_script:
- set PATH=%PATH:C:\Program Files\Git\usr\bin;=%
- set PATH=C:\msys64\mingw64\bin;%PATH%
- make dependencies
34 changes: 32 additions & 2 deletions tools/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,38 @@ import (
"gopkg.in/bblfsh/sdk.v1/uast"
)

// #cgo CXXFLAGS: -I/usr/local/include -I/usr/local/include/libxml2 -I/usr/include -I/usr/include/libxml2
// #cgo LDFLAGS: -lxml2
// libuast can be linked in two modes on UNIX platforms: hosted and embedded.
// Hosted mode - libuast is installed globally in the system.
// Embedded mode - libuast source is inside "tools" directory and we compile it with cgo.
// This is what happens during `make dependencies`. It is the default.
//
// Build tags:
// custom_libuast - disables all the default CXXFLAGS and LDFLAGS.
// host_libuast - forces hosted mode.
//
// !unix defaults:
// CFLAGS: -Iinclude -DLIBUAST_STATIC
// CXXFLAGS: -Iinclude -DLIBUAST_STATIC
// LDFLAGS: -luast -lxml2 -Llib -static -lstdc++ -static-libgcc
// Notes: static linkage, libuast installation prefix is expected
// to be extracted into . ("tools΅ directory). Windows requires *both*
// CFLAGS and CXXFLAGS be set.
//
// unix defaults:
// CXXFLAGS: -I/usr/local/include -I/usr/local/include/libxml2 -I/usr/include -I/usr/include/libxml2
// LDFLAGS: -lxml2
// Notes: expects the embedded mode. "host_libuast" tag prepends -luast to LDFLAGS.
//
// Final notes:
// Cannot actually use "unix" tag until this is resolved: https://github.com/golang/go/issues/20322
// So inverted the condition: unix == !windows here.

// #cgo !custom_libuast,windows CFLAGS: -Iinclude -DLIBUAST_STATIC
// #cgo !custom_libuast,windows CXXFLAGS: -Iinclude -DLIBUAST_STATIC
// #cgo !custom_libuast,!windows CXXFLAGS: -I/usr/local/include -I/usr/local/include/libxml2 -I/usr/include -I/usr/include/libxml2
// #cgo !custom_libuast,host_libuast !custom_libuast,windows LDFLAGS: -luast
// #cgo !custom_libuast LDFLAGS: -lxml2
// #cgo !custom_libuast,windows LDFLAGS: -Llib -static -lstdc++ -static-libgcc
// #include "bindings.h"
import "C"

Expand Down
6 changes: 6 additions & 0 deletions tools/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
#include <stdint.h>
#include <stdlib.h>

#if __has_include("uast.h") // std C++17, GCC 5.x || Clang || VSC++ 2015u2+
// Embedded mode on UNIX, MSVC build on Windows.
#include "uast.h"
#else
// Hosted mode on UNIX, MinGW build on Windows.
#include "libuast/uast.h"
#endif

extern char* goGetInternalType(uintptr_t);
extern char* goGetToken(uintptr_t);
Expand Down

0 comments on commit c134769

Please sign in to comment.