blob: ca0fa9ee2ca1151029a1e727b79d3bdfb5a8a454 [file] [log] [blame]
Russ Coxeda49f32009-08-12 13:18:46 -07001# 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.
4
Russ Cox090efde2009-08-13 14:41:10 -07005all: package
Russ Coxeda49f32009-08-12 13:18:46 -07006package: _obj/$(TARG).a
7testpackage: _test/$(TARG).a
8
Rob Pike14abacf2010-03-19 15:14:46 -07009include $(QUOTED_GOROOT)/src/Make.common
Devon H. O'Dell857d4cf2009-12-11 15:14:09 -080010
Russ Cox9f24d362010-08-26 20:21:34 -040011# The quietgcc wrapper is for our own source code
12# while building the libraries, not arbitrary source code
13# as encountered by cgo.
14ifeq ($(HOST_CC),quietgcc)
15HOST_CC:=gcc
16endif
17ifeq ($(HOST_LD),quietgcc)
18HOST_LD:=gcc
19endif
20
Russ Cox38493252009-11-11 12:00:50 -080021# GNU Make 3.80 has a bug in lastword
22# elem=$(lastword $(subst /, ,$(TARG)))
23TARG_words=$(subst /, ,$(TARG))
24elem=$(word $(words $(TARG_words)),$(TARG_words))
25
Russ Coxc276d872009-12-21 08:31:02 -080026ifeq ($(elem),$(TARG))
27dir=
28else
29dir=$(patsubst %/$(elem),%,$(TARG))
30endif
Sergio Luis O. B. Correia6fc82072009-11-23 17:32:51 -080031
Sergio Luis O. B. Correia6fc82072009-11-23 17:32:51 -080032pkgdir=$(QUOTED_GOROOT)/pkg/$(GOOS)_$(GOARCH)
Russ Coxeda49f32009-08-12 13:18:46 -070033
Ian Lance Taylor7c99dcb2010-02-24 17:00:25 -080034INSTALLFILES+=$(pkgdir)/$(TARG).a
Russ Coxcce01112009-09-30 11:51:08 -070035
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.
Devon H. O'Dell9277b022009-12-17 13:20:56 -080038ifdef CGOFILES
Russ Cox14d677e2010-12-08 13:56:51 -050039GOFILES+=$(patsubst %.go,%.cgo1.go,$(CGOFILES)) _cgo_gotypes.go
Russ Cox0cd34752010-12-17 09:51:55 -080040CGO_OFILES+=$(patsubst %.go,%.cgo2.o,$(CGOFILES)) _cgo_export.o
41OFILES+=_cgo_defun.$O _cgo_import.$O $(CGO_OFILES)
Devon H. O'Dell9277b022009-12-17 13:20:56 -080042endif
Russ Coxcce01112009-09-30 11:51:08 -070043
Rob Pike14abacf2010-03-19 15:14:46 -070044PREREQ+=$(patsubst %,%.make,$(DEPS))
45
Russ Coxeda49f32009-08-12 13:18:46 -070046coverage:
Russ Coxaafe474e2010-08-24 20:00:33 -040047 gotest
48 6cov -g $(shell pwd) $O.out | grep -v '_test\.go:'
Russ Coxeda49f32009-08-12 13:18:46 -070049
Russ Cox14d677e2010-12-08 13:56:51 -050050CLEANFILES+=*.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.*
Gustavo Niemeyer4fd867b2011-02-01 08:44:18 -050051CLEANFILES+=_cgo_.c _cgo_import.c _cgo_main.c _cgo_flags _cgo_run
Russ Cox14d677e2010-12-08 13:56:51 -050052CLEANFILES+=*.so _obj _test _testmain.go *.exe
Russ Coxeda49f32009-08-12 13:18:46 -070053
54test:
Russ Coxaafe474e2010-08-24 20:00:33 -040055 gotest
Russ Coxeda49f32009-08-12 13:18:46 -070056
Adam Langleycbd3b2e2010-01-12 15:43:20 -080057bench:
Russ Coxaafe474e2010-08-24 20:00:33 -040058 gotest -benchmarks=. -match="Do not run tests"
Adam Langleycbd3b2e2010-01-12 15:43:20 -080059
Russ Coxeda49f32009-08-12 13:18:46 -070060nuke: clean
61 rm -f $(pkgdir)/$(TARG).a
62
63testpackage-clean:
64 rm -f _test/$(TARG).a _gotest_.$O
65
Russ Coxcce01112009-09-30 11:51:08 -070066install: $(INSTALLFILES)
Russ Coxff5618e2009-08-12 14:40:29 -070067
Russ Cox6962e2b2010-03-30 10:44:51 -070068$(pkgdir)/$(TARG).a: _obj/$(TARG).a
Sergio Luis O. B. Correia6fc82072009-11-23 17:32:51 -080069 @test -d $(QUOTED_GOROOT)/pkg && mkdir -p $(pkgdir)/$(dir)
70 cp _obj/$(TARG).a "$@"
Russ Coxeda49f32009-08-12 13:18:46 -070071
Russ Coxbf991bb2009-11-01 05:43:26 -080072_go_.$O: $(GOFILES) $(PREREQ)
Russ Coxaafe474e2010-08-24 20:00:33 -040073 $(GC) -o $@ $(GOFILES)
Russ Coxeda49f32009-08-12 13:18:46 -070074
Russ Coxbf991bb2009-11-01 05:43:26 -080075_gotest_.$O: $(GOFILES) $(GOTESTFILES) $(PREREQ)
Russ Coxaafe474e2010-08-24 20:00:33 -040076 $(GC) -o $@ $(GOFILES) $(GOTESTFILES)
Russ Coxeda49f32009-08-12 13:18:46 -070077
Russ Coxeda49f32009-08-12 13:18:46 -070078_obj/$(TARG).a: _go_.$O $(OFILES)
Russ Coxcce01112009-09-30 11:51:08 -070079 @mkdir -p _obj/$(dir)
Russ Coxeda49f32009-08-12 13:18:46 -070080 rm -f _obj/$(TARG).a
Russ Coxaafe474e2010-08-24 20:00:33 -040081 gopack grc $@ _go_.$O $(OFILES)
Russ Coxeda49f32009-08-12 13:18:46 -070082
83_test/$(TARG).a: _gotest_.$O $(OFILES)
Russ Coxcce01112009-09-30 11:51:08 -070084 @mkdir -p _test/$(dir)
Russ Coxeda49f32009-08-12 13:18:46 -070085 rm -f _test/$(TARG).a
Russ Coxaafe474e2010-08-24 20:00:33 -040086 gopack grc $@ _gotest_.$O $(OFILES)
Russ Coxeda49f32009-08-12 13:18:46 -070087
88importpath:
89 @echo $(TARG)
90
Russ Cox090efde2009-08-13 14:41:10 -070091dir:
92 @echo $(dir)
93
Russ Coxcce01112009-09-30 11:51:08 -070094# To use cgo in a Go package, add a line
95#
Devon H. O'Dell9277b022009-12-17 13:20:56 -080096# CGOFILES=x.go y.go
Russ Coxcce01112009-09-30 11:51:08 -070097#
Devon H. O'Dell9277b022009-12-17 13:20:56 -080098# to the main Makefile. This signals that cgo should process x.go
99# and y.go when building the package.
Ian Lance Taylor12e7ce92010-04-09 13:31:05 -0700100# There are three optional variables to set, CGO_CFLAGS, CGO_LDFLAGS,
101# and CGO_DEPS, which specify compiler flags, linker flags, and linker
102# dependencies to use when compiling (using gcc) the C support for
103# x.go and y.go.
Russ Coxcce01112009-09-30 11:51:08 -0700104
Devon H. O'Dell9277b022009-12-17 13:20:56 -0800105# Cgo translates each x.go file listed in $(CGOFILES) into a basic
106# translation of x.go, called x.cgo1.go. Additionally, three other
107# files are created:
Russ Coxcce01112009-09-30 11:51:08 -0700108#
Devon H. O'Dell9277b022009-12-17 13:20:56 -0800109# _cgo_gotypes.go - declarations needed for all .go files in the package; imports "unsafe"
110# _cgo_defun.c - C trampoline code to be compiled with 6c and linked into the package
111# x.cgo2.c - C implementations compiled with gcc to create a dynamic library
Russ Coxcce01112009-09-30 11:51:08 -0700112#
Russ Coxcce01112009-09-30 11:51:08 -0700113
Russ Cox0cd34752010-12-17 09:51:55 -0800114ifdef CGOFILES
Gustavo Niemeyer4fd867b2011-02-01 08:44:18 -0500115_cgo_run: $(CGOFILES)
Russ Coxaafe474e2010-08-24 20:00:33 -0400116 CGOPKGPATH=$(dir) cgo -- $(CGO_CFLAGS) $(CGOFILES)
Gustavo Niemeyer34336bd2011-02-02 22:37:40 -0500117 touch _cgo_run
Russ Coxcce01112009-09-30 11:51:08 -0700118
Gustavo Niemeyer4fd867b2011-02-01 08:44:18 -0500119# _CGO_CFLAGS and _CGO_LDFLAGS are defined via the evaluation of _cgo_flags.
120# The include happens before the commands in the recipe run,
121# so it cannot be done in the same recipe that runs cgo.
Gustavo Niemeyerb9183b92011-02-08 15:40:11 -0500122_load_cgo_flags: _cgo_run
Gustavo Niemeyer4fd867b2011-02-01 08:44:18 -0500123 $(eval include _cgo_flags)
124
125# Include any previous flags in case cgo files are up to date.
126-include _cgo_flags
127
Russ Cox6c6d5302010-12-17 11:37:11 -0800128# Ugly but necessary - cgo writes these files too.
Gustavo Niemeyerb9183b92011-02-08 15:40:11 -0500129_cgo_gotypes.go _cgo_export.c _cgo_export.h _cgo_main.c _cgo_defun.c: _load_cgo_flags
Jukka-Pekka Kekkonen1cec72c2010-09-07 15:14:14 -0400130 @true
131
132%.cgo1.go %.cgo2.c: _cgo_defun.c
Devon H. O'Dell9277b022009-12-17 13:20:56 -0800133 @true
Russ Cox38323892010-12-17 13:22:20 -0800134endif
Devon H. O'Dell9277b022009-12-17 13:20:56 -0800135
Russ Cox14d677e2010-12-08 13:56:51 -0500136# Compile rules for gcc source files.
Russ Cox0cd34752010-12-17 09:51:55 -0800137%.o: %.c
Gustavo Niemeyer4fd867b2011-02-01 08:44:18 -0500138 $(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -g -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $(_CGO_CFLAGS) $*.c
Russ Cox14d677e2010-12-08 13:56:51 -0500139
140# To find out which symbols are needed from external libraries
141# and which libraries are needed, we build a simple a.out that
142# links all the objects we just created and then use cgo -dynimport
143# to inspect it. That is, we make gcc tell us which dynamic symbols
144# and libraries are involved, instead of duplicating gcc's logic ourselves.
Russ Cox0cd34752010-12-17 09:51:55 -0800145# After main we have to define all the symbols that will be provided
146# by Go code. That's crosscall2 and any exported symbols.
Russ Cox14d677e2010-12-08 13:56:51 -0500147
148_cgo_main.o: _cgo_main.c
Gustavo Niemeyer4fd867b2011-02-01 08:44:18 -0500149 $(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -g -fPIC -O2 -o $@ -c $(CGO_CFLAGS) $(_CGO_CFLAGS) _cgo_main.c
Russ Cox14d677e2010-12-08 13:56:51 -0500150
Russ Cox0cd34752010-12-17 09:51:55 -0800151_cgo1_.o: _cgo_main.o $(CGO_OFILES)
Gustavo Niemeyer4fd867b2011-02-01 08:44:18 -0500152 $(HOST_CC) $(_CGO_CFLAGS_$(GOARCH)) -g -fPIC -O2 -o $@ $^ $(CGO_LDFLAGS) $(_CGO_LDFLAGS)
Russ Cox14d677e2010-12-08 13:56:51 -0500153
154_cgo_import.c: _cgo1_.o
155 cgo -dynimport _cgo1_.o >_$@ && mv -f _$@ $@
Ian Lance Taylor12e7ce92010-04-09 13:31:05 -0700156
Devon H. O'Dell9277b022009-12-17 13:20:56 -0800157# 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).
160
Russ Coxe67a5082009-09-30 13:47:15 -0700161# Have to run gcc with the right size argument on hybrid 32/64 machines.
162_CGO_CFLAGS_386=-m32
163_CGO_CFLAGS_amd64=-m64
William Josephson72e74442009-11-18 09:18:51 -0800164_CGO_LDFLAGS_freebsd=-shared -lpthread -lm
Russ Cox133a1582009-10-03 10:37:12 -0700165_CGO_LDFLAGS_linux=-shared -lpthread -lm
166_CGO_LDFLAGS_darwin=-dynamiclib -Wl,-undefined,dynamic_lookup
Wei Guangjing035696c52010-11-01 17:52:26 -0400167_CGO_LDFLAGS_windows=-shared -lm -mthreads
Russ Cox133a1582009-10-03 10:37:12 -0700168
Russ Cox14d677e2010-12-08 13:56:51 -0500169# Have to compile the runtime header.
Christopher Nielsen31ccf192011-02-03 00:42:03 -0500170RUNTIME_CFLAGS=-I$(pkgdir)
Russ Coxcce01112009-09-30 11:51:08 -0700171
Devon H. O'Dell9277b022009-12-17 13:20:56 -0800172# Compile _cgo_defun.c with 6c; needs access to the runtime headers.
173_cgo_defun.$O: _cgo_defun.c
Russ Coxaafe474e2010-08-24 20:00:33 -0400174 $(CC) $(CFLAGS) $(RUNTIME_CFLAGS) _cgo_defun.c
Russ Coxcce01112009-09-30 11:51:08 -0700175
Russ Coxcce01112009-09-30 11:51:08 -0700176# Generic build rules.
177# These come last so that the rules above can override them
178# for more specific file names.
179%.$O: %.c
Russ Coxaafe474e2010-08-24 20:00:33 -0400180 $(CC) $(CFLAGS) $*.c
Russ Coxcce01112009-09-30 11:51:08 -0700181
182%.$O: %.s
Russ Coxaafe474e2010-08-24 20:00:33 -0400183 $(AS) $*.s
Russ Coxcce01112009-09-30 11:51:08 -0700184
185%.$O: $(HFILES)
186