2009-08-12 14:18:46 -06:00
|
|
|
# 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.
|
|
|
|
|
2009-08-13 15:41:10 -06:00
|
|
|
all: package
|
2009-08-12 14:18:46 -06:00
|
|
|
package: _obj/$(TARG).a
|
|
|
|
testpackage: _test/$(TARG).a
|
|
|
|
|
2009-11-11 13:00:50 -07:00
|
|
|
# GNU Make 3.80 has a bug in lastword
|
|
|
|
# elem=$(lastword $(subst /, ,$(TARG)))
|
|
|
|
TARG_words=$(subst /, ,$(TARG))
|
|
|
|
elem=$(word $(words $(TARG_words)),$(TARG_words))
|
|
|
|
|
2009-08-13 15:41:10 -06:00
|
|
|
dir=$(patsubst %/$(elem),%,./$(TARG))
|
2009-11-23 18:32:51 -07:00
|
|
|
|
|
|
|
# ugly hack to deal with whitespaces in $GOROOT
|
|
|
|
nullstring :=
|
|
|
|
space := $(nullstring) # a space at the end
|
|
|
|
QUOTED_GOROOT=$(subst $(space),\ ,$(GOROOT))
|
|
|
|
|
|
|
|
pkgdir=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH)
|
2009-08-12 14:18:46 -06:00
|
|
|
|
2009-09-30 12:51:08 -06:00
|
|
|
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.
|
|
|
|
GOFILES+=$(patsubst %.go,%.cgo1.go,$(CGOFILES))
|
|
|
|
GOFILES+=$(patsubst %.go,%.cgo2.go,$(CGOFILES))
|
|
|
|
OFILES+=$(patsubst %.go,%.cgo3.$O,$(CGOFILES))
|
|
|
|
INSTALLFILES+=$(patsubst %.go,$(pkgdir)/$(dir)/$(elem)_%.so,$(CGOFILES))
|
2009-11-01 06:43:26 -07:00
|
|
|
PREREQ+=$(patsubst %,%.make,$(DEPS))
|
2009-09-30 12:51:08 -06:00
|
|
|
|
2009-08-12 14:18:46 -06:00
|
|
|
coverage:
|
|
|
|
gotest
|
2009-11-17 09:39:26 -07:00
|
|
|
6cov -g $(shell pwd) $O.out | grep -v '_test\.go:'
|
2009-08-12 14:18:46 -06:00
|
|
|
|
|
|
|
clean:
|
2009-10-03 11:37:12 -06:00
|
|
|
rm -rf *.[$(OS)o] *.a [$(OS)].out *.cgo[12].go *.cgo[34].c *.so _obj _test _testmain.go $(CLEANFILES)
|
2009-08-12 14:18:46 -06:00
|
|
|
|
|
|
|
test:
|
|
|
|
gotest
|
|
|
|
|
|
|
|
nuke: clean
|
|
|
|
rm -f $(pkgdir)/$(TARG).a
|
|
|
|
|
|
|
|
testpackage-clean:
|
|
|
|
rm -f _test/$(TARG).a _gotest_.$O
|
|
|
|
|
2009-09-30 12:51:08 -06:00
|
|
|
install: $(INSTALLFILES)
|
2009-08-12 15:40:29 -06:00
|
|
|
|
|
|
|
$(pkgdir)/$(TARG).a: package
|
2009-11-23 18:32:51 -07:00
|
|
|
@test -d $(QUOTED_GOROOT)/pkg && mkdir -p $(pkgdir)/$(dir)
|
|
|
|
cp _obj/$(TARG).a "$@"
|
2009-08-12 14:18:46 -06:00
|
|
|
|
2009-11-01 06:43:26 -07:00
|
|
|
_go_.$O: $(GOFILES) $(PREREQ)
|
2009-08-12 14:18:46 -06:00
|
|
|
$(GC) -o $@ $(GOFILES)
|
|
|
|
|
2009-11-01 06:43:26 -07:00
|
|
|
_gotest_.$O: $(GOFILES) $(GOTESTFILES) $(PREREQ)
|
2009-08-12 14:18:46 -06:00
|
|
|
$(GC) -o $@ $(GOFILES) $(GOTESTFILES)
|
|
|
|
|
|
|
|
_obj/$(TARG).a: _go_.$O $(OFILES)
|
2009-09-30 12:51:08 -06:00
|
|
|
@mkdir -p _obj/$(dir)
|
2009-08-12 14:18:46 -06:00
|
|
|
rm -f _obj/$(TARG).a
|
|
|
|
gopack grc $@ _go_.$O $(OFILES)
|
|
|
|
|
|
|
|
_test/$(TARG).a: _gotest_.$O $(OFILES)
|
2009-09-30 12:51:08 -06:00
|
|
|
@mkdir -p _test/$(dir)
|
2009-08-12 14:18:46 -06:00
|
|
|
rm -f _test/$(TARG).a
|
|
|
|
gopack grc $@ _gotest_.$O $(OFILES)
|
|
|
|
|
|
|
|
importpath:
|
|
|
|
@echo $(TARG)
|
|
|
|
|
2009-08-13 15:41:10 -06:00
|
|
|
dir:
|
|
|
|
@echo $(dir)
|
|
|
|
|
2009-11-01 06:43:26 -07:00
|
|
|
%.make:
|
2009-11-14 16:29:09 -07:00
|
|
|
(cd $* && gomake)
|
2009-09-30 12:51:08 -06:00
|
|
|
|
|
|
|
# To use cgo in a Go package, add a line
|
|
|
|
#
|
|
|
|
# CGOFILES=x.go
|
|
|
|
#
|
|
|
|
# to the main Makefile. This signals that cgo should process x.go.
|
|
|
|
# There are two optional variables to set, CGO_CFLAGS and CGO_LDFLAGS,
|
|
|
|
# which specify compiler and linker flags to use when compiling
|
|
|
|
# (using gcc) the C support for x.go.
|
|
|
|
|
|
|
|
# Cgo translates each x.go file listed in $(CGOFILES) into
|
|
|
|
#
|
|
|
|
# x.cgo1.go - basic translation of x.go
|
|
|
|
# x.cgo2.go - declarations needed for x.cgo1.go; imports "unsafe"
|
|
|
|
# x.cgo3.c - C trampoline code to be compiled with 6c and linked into the package
|
|
|
|
# x.cgo4.c - C implementations compiled with gcc to create dynamic library
|
|
|
|
#
|
|
|
|
%.cgo1.go %.cgo2.go %.cgo3.c %.cgo4.c: %.go
|
2009-11-20 01:31:03 -07:00
|
|
|
CGOPKGPATH=$(dir) cgo $(CGO_CFLAGS) $*.go
|
2009-09-30 12:51:08 -06:00
|
|
|
|
|
|
|
# The rules above added x.cgo1.go and x.cgo2.go to $(GOFILES),
|
|
|
|
# added x.cgo3.$O to $OFILES, and added the installed copy of
|
|
|
|
# package_x.so (built from x.cgo4.c) to $(INSTALLFILES).
|
|
|
|
|
|
|
|
# Compile x.cgo3.c with 6c; needs access to the runtime headers.
|
|
|
|
RUNTIME_CFLAGS_amd64=-D_64BIT
|
2009-11-23 18:32:51 -07:00
|
|
|
RUNTIME_CFLAGS=-I"$(GOROOT)/src/pkg/runtime" $(RUNTIME_CFLAGS_$(GOARCH))
|
2009-09-30 12:51:08 -06:00
|
|
|
%.cgo3.$O: %.cgo3.c
|
|
|
|
$(CC) $(CFLAGS) $(RUNTIME_CFLAGS) $*.cgo3.c
|
|
|
|
|
2009-09-30 14:47:15 -06:00
|
|
|
# Have to run gcc with the right size argument on hybrid 32/64 machines.
|
|
|
|
_CGO_CFLAGS_386=-m32
|
|
|
|
_CGO_CFLAGS_amd64=-m64
|
2009-11-18 10:18:51 -07:00
|
|
|
_CGO_LDFLAGS_freebsd=-shared -lpthread -lm
|
2009-10-03 11:37:12 -06:00
|
|
|
_CGO_LDFLAGS_linux=-shared -lpthread -lm
|
|
|
|
_CGO_LDFLAGS_darwin=-dynamiclib -Wl,-undefined,dynamic_lookup
|
|
|
|
|
2009-09-30 14:47:15 -06:00
|
|
|
|
2009-09-30 12:51:08 -06:00
|
|
|
# Compile x.cgo4.c with gcc to make package_x.so.
|
|
|
|
%.cgo4.o: %.cgo4.c
|
2009-09-30 14:47:15 -06:00
|
|
|
gcc $(_CGO_CFLAGS_$(GOARCH)) -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $*.cgo4.c
|
2009-09-30 12:51:08 -06:00
|
|
|
|
|
|
|
$(elem)_%.so: %.cgo4.o
|
2009-11-15 13:57:28 -07:00
|
|
|
gcc $(_CGO_CFLAGS_$(GOARCH)) -o $@ $*.cgo4.o $(CGO_LDFLAGS) $(_CGO_LDFLAGS_$(GOOS))
|
2009-09-30 12:51:08 -06:00
|
|
|
|
|
|
|
$(pkgdir)/$(dir)/$(elem)_%.so: $(elem)_%.so
|
2009-11-23 18:32:51 -07:00
|
|
|
@test -d $(QUOTED_GOROOT/pkg && mkdir -p $(pkgdir)/$(dir)
|
|
|
|
cp $(elem)_$*.so "$@"
|
2009-09-30 12:51:08 -06:00
|
|
|
|
|
|
|
# Generic build rules.
|
|
|
|
# These come last so that the rules above can override them
|
|
|
|
# for more specific file names.
|
|
|
|
%.$O: %.c
|
|
|
|
$(CC) $(CFLAGS) $*.c
|
|
|
|
|
|
|
|
%.$O: %.s
|
|
|
|
$(AS) $*.s
|
|
|
|
|
|
|
|
%.$O: $(HFILES)
|
|
|
|
|