blob: e16c521d38352613d560769c8e1a8fdf26c2ca62 [file] [log] [blame]
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
all: package
package: _obj/$(TARG).a
testpackage: _test/$(TARG).a
# ugly hack to deal with whitespaces in $GOROOT
nullstring :=
space := $(nullstring) # a space at the end
QUOTED_GOROOT:=$(subst $(space),\ ,$(GOROOT))
include $(QUOTED_GOROOT)/src/Make.common
# GNU Make 3.80 has a bug in lastword
# elem=$(lastword $(subst /, ,$(TARG)))
TARG_words=$(subst /, ,$(TARG))
elem=$(word $(words $(TARG_words)),$(TARG_words))
ifeq ($(elem),$(TARG))
dir=
else
dir=$(patsubst %/$(elem),%,$(TARG))
endif
pkgdir=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH)
INSTALLFILES+=$(pkgdir)/$(TARG).a
# The rest of the cgo rules are below, but these variable updates
# must be done here so they apply to the main rules.
ifdef CGOFILES
GOFILES+=$(patsubst %.go,%.cgo1.go,$(CGOFILES))
GOFILES+=_cgo_gotypes.go
OFILES+=_cgo_defun.$O
GCC_OFILES=$(patsubst %.go,%.cgo2.o,$(CGOFILES))
INSTALLFILES+=$(pkgdir)/$(TARG).so
endif
PREREQ+=$(patsubst %,%.make,$(DEPS))
coverage:
$(QUOTED_GOBIN)/gotest
$(QUOTED_GOBIN)/6cov -g $(shell pwd) $O.out | grep -v '_test\.go:'
CLEANFILES+=*.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* *.so _obj _test _testmain.go
test:
$(QUOTED_GOBIN)/gotest
bench:
$(QUOTED_GOBIN)/gotest -benchmarks=. -match="Do not run tests"
nuke: clean
rm -f $(pkgdir)/$(TARG).a
testpackage-clean:
rm -f _test/$(TARG).a _gotest_.$O
install: $(INSTALLFILES)
$(pkgdir)/$(TARG).a: _obj/$(TARG).a
@test -d $(QUOTED_GOROOT)/pkg && mkdir -p $(pkgdir)/$(dir)
cp _obj/$(TARG).a "$@"
_go_.$O: $(GOFILES) $(PREREQ)
$(QUOTED_GOBIN)/$(GC) -o $@ $(GOFILES)
_gotest_.$O: $(GOFILES) $(GOTESTFILES) $(PREREQ)
$(QUOTED_GOBIN)/$(GC) -o $@ $(GOFILES) $(GOTESTFILES)
_obj/$(TARG).a: _go_.$O $(OFILES)
@mkdir -p _obj/$(dir)
rm -f _obj/$(TARG).a
$(QUOTED_GOBIN)/gopack grc $@ _go_.$O $(OFILES)
_test/$(TARG).a: _gotest_.$O $(OFILES)
@mkdir -p _test/$(dir)
rm -f _test/$(TARG).a
$(QUOTED_GOBIN)/gopack grc $@ _gotest_.$O $(OFILES)
importpath:
@echo $(TARG)
dir:
@echo $(dir)
# To use cgo in a Go package, add a line
#
# CGOFILES=x.go y.go
#
# to the main Makefile. This signals that cgo should process x.go
# and y.go when building the package.
# There are three optional variables to set, CGO_CFLAGS, CGO_LDFLAGS,
# and CGO_DEPS, which specify compiler flags, linker flags, and linker
# dependencies to use when compiling (using gcc) the C support for
# x.go and y.go.
# Cgo translates each x.go file listed in $(CGOFILES) into a basic
# translation of x.go, called x.cgo1.go. Additionally, three other
# files are created:
#
# _cgo_gotypes.go - declarations needed for all .go files in the package; imports "unsafe"
# _cgo_defun.c - C trampoline code to be compiled with 6c and linked into the package
# x.cgo2.c - C implementations compiled with gcc to create a dynamic library
#
_cgo_defun.c _cgo_gotypes.go: $(CGOFILES)
CGOPKGPATH=$(dir) $(QUOTED_GOBIN)/cgo $(CGO_CFLAGS) $(CGOFILES)
# Ugly but necessary
%.cgo1.go: _cgo_defun.c _cgo_gotypes.go
@true
%.cgo2.c: _cgo_defun.c _cgo_gotypes.go
@true
%.cgo2.o: %.cgo2.c
gcc $(_CGO_CFLAGS_$(GOARCH)) -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $*.cgo2.c
_cgo_export.o: _cgo_export.c _cgo_export.h
gcc $(_CGO_CFLAGS_$(GOARCH)) -fPIC -O2 -o $@ -c $(CGO_CFLAGS) _cgo_export.c
# The rules above added x.cgo1.go and _cgo_gotypes.go to $(GOFILES),
# added _cgo_defun.$O to $OFILES, and added the installed copy of
# package_x.so (built from x.cgo2.c) to $(INSTALLFILES).
RUNTIME_CFLAGS_amd64=-D_64BIT
RUNTIME_CFLAGS=-I"$(GOROOT)/src/pkg/runtime" $(RUNTIME_CFLAGS_$(GOARCH))
# Have to run gcc with the right size argument on hybrid 32/64 machines.
_CGO_CFLAGS_386=-m32
_CGO_CFLAGS_amd64=-m64
_CGO_LDFLAGS_freebsd=-shared -lpthread -lm
_CGO_LDFLAGS_linux=-shared -lpthread -lm
_CGO_LDFLAGS_darwin=-dynamiclib -Wl,-undefined,dynamic_lookup
# Compile x.cgo4.c with gcc to make package_x.so.
# Compile _cgo_defun.c with 6c; needs access to the runtime headers.
_cgo_defun.$O: _cgo_defun.c
$(QUOTED_GOBIN)/$(CC) $(CFLAGS) $(RUNTIME_CFLAGS) _cgo_defun.c
_cgo_.so: $(GCC_OFILES) $(CGO_DEPS)
gcc $(_CGO_CFLAGS_$(GOARCH)) -o $@ $(GCC_OFILES) $(CGO_LDFLAGS) $(_CGO_LDFLAGS_$(GOOS))
$(pkgdir)/$(TARG).so: _cgo_.so
@test -d $(QUOTED_GOROOT)/pkg && mkdir -p $(pkgdir)/$(dir)
cp _cgo_.so "$@"
# Generic build rules.
# These come last so that the rules above can override them
# for more specific file names.
%.$O: %.c
$(QUOTED_GOBIN)/$(CC) $(CFLAGS) $*.c
%.$O: %.s
$(QUOTED_GOBIN)/$(AS) $*.s
%.$O: $(HFILES)