summaryrefslogtreecommitdiff
path: root/src/Make.pkg
blob: 3dd57b71046677709a658a4e6917551a6d020ea4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# 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

nullstring :=
space := $(nullstring) # a space at the end
ifndef GOBIN
QUOTED_HOME=$(subst $(space),\ ,$(HOME))
GOBIN=$(QUOTED_HOME)/bin
endif
QUOTED_GOBIN=$(subst $(space),\ ,$(GOBIN))

# 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

# ugly hack to deal with whitespaces in $GOROOT
QUOTED_GOROOT=$(subst $(space),\ ,$(GOROOT))

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
PREREQ+=$(patsubst %,%.make,$(DEPS))
endif

coverage:
	$(QUOTED_GOBIN)/gotest
	$(QUOTED_GOBIN)/6cov -g $(shell pwd) $O.out | grep -v '_test\.go:'

clean:
	rm -rf *.[$(OS)o] *.a [$(OS)].out *.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go *.so _obj _test _testmain.go $(CLEANFILES)

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: package
	@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)

%.make:
	(cd $* && $(QUOTED_GOBIN)/gomake)

# 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 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 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

# 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)
	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)