Skip to content

Commit

Permalink
Update for go 1.4, use the go build to run SWIG
Browse files Browse the repository at this point in the history
Signed-off-by: Steeve Morin <steeve.morin@gmail.com>
  • Loading branch information
steeve committed Nov 5, 2014
1 parent 1df5540 commit 3bb8532
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 121 deletions.
173 changes: 53 additions & 120 deletions Makefile
@@ -1,10 +1,17 @@
NAME = libtorrent-go
MODULE_NAME = libtorrent
GO_PACKAGE = github.com/steeve/$(NAME)
CC = cc
CXX = c++
SWIG = swig
SED = sed
PKG_CONFIG = pkg-config
DOCKER = docker
DOCKER_IMAGE = steeve/$(NAME)
PLATFORMS = android-arm \
darwin-x64 \
linux-x86 \
linux-x64 \
linux-arm \
windows-x86 \
windows-x64

include platform_host.mk

Expand All @@ -15,139 +22,65 @@ endif

include platform_target.mk

ifeq ($(TARGET_ARCH), x86)
SWIG_INT_GO_SIZE = 32
else ifeq ($(TARGET_ARCH), x64)
SWIG_INT_GO_SIZE = 64
else ifeq ($(TARGET_ARCH), arm)
SWIG_INT_GO_SIZE = 32
ifeq ($(TARGET_ARCH),x86)
GOARCH = 386
else ifeq ($(TARGET_ARCH),x64)
GOARCH = amd64
else ifeq ($(TARGET_ARCH),arm)
GOARCH = arm
GOARM = 6
endif

ifneq ($(CROSS_ROOT),)
CROSS_CFLAGS = -I$(CROSS_ROOT)/include -I$(CROSS_ROOT)/$(CROSS_TRIPLE)/include
CROSS_LDFLAGS = -L$(CROSS_ROOT)/lib
PKG_CONFIG := PKG_CONFIG_LIBDIR=$(CROSS_ROOT)/lib/pkgconfig $(PKG_CONFIG)
endif

LIBTORRENT_CFLAGS = $(shell $(PKG_CONFIG) --cflags libtorrent-rasterbar)
LIBTORRENT_LDFLAGS = $(shell $(PKG_CONFIG) --static --libs libtorrent-rasterbar)

DEFINE_IGNORES = __STDC__|_cdecl|__cdecl|_fastcall|__fastcall|_stdcall|__stdcall|__declspec

CC_DEFINES = $(shell echo | $(CC) -dM -E - | grep -v -E "$(DEFINE_IGNORES)" | sed -E "s/\#define[[:space:]]+([a-zA-Z0-9_()]+)[[:space:]]+(.*)/-D'\1'='\2'/g" | tr '\n' ' ')
CC_INCLUDES = $(shell $(CC) -x c++ -v -E /dev/null 2>&1 | sed -n "/search starts here/,/End of search list./p" | grep -e "^ .*" | sed -E "s/[[:space:]]+(.*)/-I'\1'/g" | tr '\n' ' ')
CFLAGS = -O2 -Wno-deprecated -Wno-deprecated-declarations $(CROSS_CFLAGS) $(CC_INCLUDES) $(LIBTORRENT_CFLAGS)
LDFLAGS = $(CROSS_LDFLAGS)

SWIG_FLAGS = -go -c++\
-intgosize $(SWIG_INT_GO_SIZE) \
$(CROSS_CFLAGS) \
$(LIBTORRENT_CFLAGS) \
$(CC_DEFINES) $(CC_INCLUDES)

ifeq ($(TARGET_OS), windows)
EXT = dll
SWIG_FLAGS += -D_WIN32_WINNT=0x0501 -DSWIGWIN
ifeq ($(TARGET_ARCH), x64)
SWIG_FLAGS += -DSWIGWORDSIZE32
endif
CFLAGS += -mthreads
LDFLAGS += -shared -static-libgcc -static-libstdc++ $(LIBTORRENT_LDFLAGS)
GOOS = windows
else ifeq ($(TARGET_OS), darwin)
GOOS = darwin
else ifeq ($(TARGET_OS), linux)
CFLAGS += -fPIC
LDFLAGS += $(LIBTORRENT_LDFLAGS) -lm -lstdc++ -ldl
GOOS = linux
else ifeq ($(TARGET_OS), android)
CFLAGS += -fPIC -ggdb -fstack-protector-all
LDFLAGS += -Wl,-Bstatic $(LIBTORRENT_LDFLAGS) -lm -Wl,-Bdynamic -lstdc++
else ifeq ($(TARGET_OS), darwin)
SWIG_FLAGS += -DSWIGMAC -DBOOST_HAS_PTHREADS
CFLAGS += -fPIC -mmacosx-version-min=10.6
LDFLAGS += $(LIBTORRENT_LDFLAGS) -lm -lssl -lcrypto -lstdc++
GOOS = linux
GOARM = 7
endif

ifneq ($(CROSS_ROOT),)
LIB_SEARCH_PATH = $(CROSS_ROOT)
else
ifeq ($(HOST_OS), windows)
ifeq ($(HOST_ARCH), x64)
LIB_SEARCH_PATH = c:/windows/syswow64
else
LIB_SEARCH_PATH = c:/windows/system32
endif
else
LIB_SEARCH_PATH = /usr/lib /usr/local/lib
endif
CROSS_CFLAGS = -I$(CROSS_ROOT)/include -I$(CROSS_ROOT)/$(CROSS_TRIPLE)/include
CROSS_LDFLAGS = -L$(CROSS_ROOT)/lib
PKG_CONFIG_PATH = $(CROSS_ROOT)/lib/pkgconfig
endif

SWIG_FILES = libtorrent.i
SRCS = $(SWIG_FILES:%.i=%_wrap.cxx)
GCFILES = $(SWIG_FILES:%.i=%_gc.c)
GOFILES = $(SWIG_FILES:%.i=%.go)
OBJS = $(SRCS:%.cxx=%.o)

BUILD_PATH = build/$(TARGET_OS)_$(TARGET_ARCH)
LIBRARY_NAME = $(NAME).$(EXT)

all: dist

re: clean all

force:
@true
LIBTORRENT_CFLAGS = $(shell $(PKG_CONFIG) --cflags libtorrent-rasterbar)
DEFINE_IGNORES = __STDC__|_cdecl|__cdecl|_fastcall|__fastcall|_stdcall|__stdcall|__declspec|__LDBL_MAX__

$(BUILD_PATH):
mkdir -p $(BUILD_PATH)
all: install

$(SRCS): $(SWIG_FILES)
$(SWIG) $(SWIG_FLAGS) -o $@ -outdir . $<
# It should always be like this, according to https://code.google.com/p/go/source/browse/src/cmd/cgo/out.go#530
$(SED) -i -E 's/} (\*swig_a|a)/} SWIG_PACKED_STRUCT \1/g' $@
$(SED) -i -e 's/_wrap_/SWIGEXPORT _wrap_/g' $@
compiler_defines.i:
@PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKG_CONFIG) --cflags libtorrent-rasterbar | sed -E -e 's/-D+/\n#define /g' -e 's/-I[^ ]+//g' -e 's/=/ /g' > $@
@echo | $(CC) -dM -E - | grep -v -E "$(DEFINE_IGNORES)" >> $@
ifeq ($(TARGET_OS), windows)
cat swig_packed_struct.h dllmain.h $@ > $@.tmp
$(SED) -i -e 's/\(extern void\)/\/\/\1/' $@.tmp
else
cat swig_packed_struct.h $@ > $@.tmp
@echo "#define _WIN32_WINNT 0x0501" >> $@
@echo "#define SWIGWIN" >> $@
ifeq ($(TARGET_ARCH), x64)
@echo "#define SWIGWORDSIZE32" >> $@
endif
else ifeq ($(TARGET_OS), darwin)
@echo "#define SWIGMAC" >> $@
@echo "#define BOOST_HAS_PTHREADS" >> $@
endif
mv $@.tmp $@

dist_linux dist_android dist_darwin: swig_static $(OBJS)

dist_windows: swig_shared shared_library

dist: dist_$(TARGET_OS)
rm -rf libtorrent_wrap.cxx

fix_exports:
# Patch SWIG generated files for succesful compilation on Windows.
# Based on https://groups.google.com/forum/#!topic/golang-nuts/9L0U4Q7AtyE
# Comment out externs
# Insert packed struct definition

fix_imports:
# Fix imports by specifying them to the external dll, and commenting the x_wrap_* symbols
$(SED) -i -e 's/\(#pragma dynimport _ _\)/\/\/\1/' \
-e 's/\(#pragma dynimport.*\)\(""\)/\1"$(LIBRARY_NAME)"/g' \
-e 's/\(static void (\*x_wrap_\)/\/\/\1/g' \
-e 's/cgocall(x\(_wrap_.*\)/cgocall(\1/g' libtorrent_gc.c
install: compiler_defines.i
CC=$(CC) CXX=$(CXX) \
PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
CGO_ENABLED=1 \
GOOS=$(GOOS) GOARCH=$(GOARCH) GOARM=$(GOARM) \
go install -v

swig_shared: $(SRCS) $(GCFILES) $(GOFILES) fix_imports fix_exports
clean:
rm -rf compiler_defines.i
rm -rf $(shell go env GOPATH)/pkg/$(GOOS)_$(GOARCH)/$(GO_PACKAGE).a

shared_library: swig_shared $(OBJS) $(BUILD_PATH)
$(CXX) -o $(BUILD_PATH)/$(LIBRARY_NAME) $(CFLAGS) $(OBJS) $(LDFLAGS)
re: clean all

swig_static: $(SRCS) $(GCFILES) $(GOFILES)
# Ensure the ldflags are properly set for static compilation
echo "#pragma cgo_ldflag \"$(shell pwd)/libtorrent_wrap.o\"" > libtorrent_gc.cgo
for flag in $(CFLAGS) $(LDFLAGS); do\
echo "#pragma cgo_ldflag \"$$flag\"" >> libtorrent_gc.cgo ;\
build-envs:
for i in $(PLATFORMS); do \
$(DOCKER) build -t $(DOCKER_IMAGE):$$i $$i ; \
done
cat libtorrent_gc.c >> libtorrent_gc.cgo
# Overwrite the file
mv libtorrent_gc.cgo libtorrent_gc.c

$(OBJS): $(SRCS)
$(CXX) $(CFLAGS) -c $< -o $@

clean:
rm -rf $(OBJS) $(SRCS) $(GOFILES) *.o $(BUILD_PATH)
1 change: 1 addition & 0 deletions libtorrent.go
@@ -0,0 +1 @@
package libtorrent
6 changes: 5 additions & 1 deletion libtorrent.i → libtorrent.swigcxx
Expand Up @@ -4,6 +4,8 @@
#include <libtorrent/session.hpp>
%}

%include "compiler_defines.i"

%include <stl.i>
%include <stdint.i>
%include <typemaps.i>
Expand All @@ -29,6 +31,7 @@ namespace libtorrent
}

// These are problematic, ignore them for now
%ignore libtorrent::absolutetime_nodeprecate_;
%ignore libtorrent::throw_invalid_handle;
%ignore libtorrent::session::add_extension;
%ignore libtorrent::web_seed_entry;
Expand All @@ -51,7 +54,7 @@ namespace libtorrent
%include <libtorrent/error.hpp>

%include <libtorrent/fingerprint.hpp>
%include <libtorrent/bitfield.hpp>
%include "bitfield.i"
%include "socket.i"
%include <libtorrent/address.hpp>
%include "torrent_info.i"
Expand All @@ -64,3 +67,4 @@ namespace libtorrent
%include <libtorrent/ptime.hpp>
%include <libtorrent/time.hpp>
%include "session.i"
%include "create_torrent.i"
5 changes: 5 additions & 0 deletions libtorrent_cgo.go
@@ -0,0 +1,5 @@
package libtorrent

// #cgo pkg-config: libtorrent-rasterbar openssl
// #cgo LDFLAGS: -lm -lstdc++
import "C"

0 comments on commit 3bb8532

Please sign in to comment.