1
# Copyright 2009 The Go Authors. All rights reserved.
2
# Use of this source code is governed by a BSD-style
3
# license that can be found in the LICENSE file.
6
package: _obj/$(TARG).a
7
testpackage: _test/$(TARG).a
9
include $(QUOTED_GOROOT)/src/Make.common
11
# The quietgcc wrapper is for our own source code
12
# while building the libraries, not arbitrary source code
13
# as encountered by cgo.
14
ifeq ($(HOST_CC),quietgcc)
17
ifeq ($(HOST_LD),quietgcc)
21
# GNU Make 3.80 has a bug in lastword
22
# elem=$(lastword $(subst /, ,$(TARG)))
23
TARG_words=$(subst /, ,$(TARG))
24
elem=$(word $(words $(TARG_words)),$(TARG_words))
26
ifeq ($(elem),$(TARG))
29
dir=$(patsubst %/$(elem),%,$(TARG))
32
pkgdir=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH)
34
INSTALLFILES+=$(pkgdir)/$(TARG).a
36
# The rest of the cgo rules are below, but these variable updates
37
# must be done here so they apply to the main rules.
39
GOFILES+=$(patsubst %.go,_obj/%.cgo1.go,$(CGOFILES)) _obj/_cgo_gotypes.go
40
CGO_OFILES+=$(patsubst %.go,%.cgo2.o,$(CGOFILES)) _cgo_export.o
41
OFILES+=_cgo_defun.$O _cgo_import.$O $(CGO_OFILES)
44
PREREQ+=$(patsubst %,%.make,$(DEPS))
48
6cov -g $(shell pwd) $O.out | grep -v '_test\.go:'
50
CLEANFILES+=*.so _obj _test _testmain.go *.exe _cgo* *.cgo[12].*
56
gotest -test.bench=. -test.run="Do not run tests"
59
rm -f $(pkgdir)/$(TARG).a
64
install: $(INSTALLFILES)
66
$(pkgdir)/$(TARG).a: _obj/$(TARG).a
67
@test -d $(QUOTED_GOROOT)/pkg && mkdir -p $(pkgdir)/$(dir)
68
cp _obj/$(TARG).a "$@"
70
_go_.$O: $(GOFILES) $(PREREQ)
71
$(GC) -o $@ $(GOFILES)
73
_gotest_.$O: $(GOFILES) $(GOTESTFILES) $(PREREQ)
74
$(GC) -o $@ $(GOFILES) $(GOTESTFILES)
76
_obj/$(TARG).a: _go_.$O $(OFILES)
79
gopack grc $@ _go_.$O $(OFILES)
81
_test/$(TARG).a: _gotest_.$O $(OFILES)
82
@mkdir -p _test/$(dir)
84
gopack grc $@ _gotest_.$O $(OFILES)
92
# To use cgo in a Go package, add a line
96
# to the main Makefile. This signals that cgo should process x.go
97
# and y.go when building the package.
98
# There are three optional variables to set, CGO_CFLAGS, CGO_LDFLAGS,
99
# and CGO_DEPS, which specify compiler flags, linker flags, and linker
100
# dependencies to use when compiling (using gcc) the C support for
103
# Cgo translates each x.go file listed in $(CGOFILES) into a basic
104
# translation of x.go, called _obj/x.cgo1.go. Additionally, three other
107
# _obj/_cgo_gotypes.go - declarations needed for all .go files in the package; imports "unsafe"
108
# _obj/_cgo_defun.c - C trampoline code to be compiled with 6c and linked into the package
109
# _obj/x.cgo2.c - C implementations compiled with gcc to create a dynamic library
113
_obj/_cgo_run: $(CGOFILES)
115
CGOPKGPATH=$(dir) cgo -- $(CGO_CFLAGS) $(CGOFILES)
118
# _CGO_CFLAGS and _CGO_LDFLAGS are defined via the evaluation of _cgo_flags.
119
# The include happens before the commands in the recipe run,
120
# so it cannot be done in the same recipe that runs cgo.
121
_obj/_load_cgo_flags: _obj/_cgo_run
122
$(eval include _obj/_cgo_flags)
124
# Include any previous flags in case cgo files are up to date.
125
-include _obj/_cgo_flags
127
# Ugly but necessary - cgo writes these files too.
128
_obj/_cgo_gotypes.go _obj/_cgo_export.c _obj/_cgo_export.h _obj/_cgo_main.c _obj/_cgo_defun.c: _obj/_load_cgo_flags
131
_obj/%.cgo1.go _obj/%.cgo2.c: _obj/_cgo_defun.c
135
# Compile rules for gcc source files.
137
$(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -g -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $(_CGO_CFLAGS) $*.c
140
$(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -I . -g -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $(_CGO_CFLAGS) $^
142
# To find out which symbols are needed from external libraries
143
# and which libraries are needed, we build a simple a.out that
144
# links all the objects we just created and then use cgo -dynimport
145
# to inspect it. That is, we make gcc tell us which dynamic symbols
146
# and libraries are involved, instead of duplicating gcc's logic ourselves.
147
# After main we have to define all the symbols that will be provided
148
# by Go code. That's crosscall2 and any exported symbols.
150
_cgo1_.o: _cgo_main.o $(CGO_OFILES)
151
$(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -g -fPIC -O2 -o $@ $^ $(CGO_LDFLAGS) $(_CGO_LDFLAGS)
153
_obj/_cgo_import.c: _cgo1_.o
155
cgo -dynimport _cgo1_.o >$@_ && mv -f $@_ $@
157
# The rules above added x.cgo1.go and _cgo_gotypes.go to $(GOFILES),
158
# added _cgo_defun.$O to $OFILES, and added the installed copy of
159
# package_x.so (built from x.cgo2.c) to $(INSTALLFILES).
161
# Have to run gcc with the right size argument on hybrid 32/64 machines.
163
_CGO_CFLAGS_amd64=-m64
164
_CGO_LDFLAGS_freebsd=-shared -lpthread -lm
165
_CGO_LDFLAGS_linux=-shared -lpthread -lm
166
_CGO_LDFLAGS_darwin=-dynamiclib -Wl,-undefined,dynamic_lookup
167
_CGO_LDFLAGS_windows=-shared -lm -mthreads
169
# Have to compile the runtime header.
170
RUNTIME_CFLAGS=-I$(pkgdir)
172
# Compile _cgo_defun.c with 6c; needs access to the runtime headers.
173
_cgo_defun.$O: _obj/_cgo_defun.c
174
$(CC) $(CFLAGS) $(RUNTIME_CFLAGS) -I . -o "$@" _obj/_cgo_defun.c
176
# Generic build rules.
177
# These come last so that the rules above can override them
178
# for more specific file names.
180
$(CC) $(CFLAGS) -o "$@" $*.c
182
%.$O: _obj/%.c $(HFILES)
183
$(CC) $(CFLAGS) -I . -o "$@" _obj/$*.c