1# Copyright (c) 2012 The Chromium Authors.   All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5#
6# GNU Make based build file.  For details on GNU Make see:
7#   http://www.gnu.org/software/make/manual/make.html
8#
9
10#
11# Paths to Tools
12#
13PNACL_CC := $(shell $(NACL_CONFIG) -t $(TOOLCHAIN) --tool=cc)
14PNACL_CXX := $(shell $(NACL_CONFIG) -t $(TOOLCHAIN) --tool=c++)
15PNACL_LINK := $(shell $(NACL_CONFIG) -t $(TOOLCHAIN) --tool=c++)
16PNACL_LIB := $(shell $(NACL_CONFIG) -t $(TOOLCHAIN) --tool=ar)
17PNACL_STRIP := $(shell $(NACL_CONFIG) -t $(TOOLCHAIN) --tool=strip)
18PNACL_FINALIZE := $(shell $(NACL_CONFIG) -t $(TOOLCHAIN) --tool=finalize)
19
20#
21# Compile Macro
22#
23# $1 = Source Name
24# $2 = Compile Flags
25# $3 = Include Directories
26#
27define C_COMPILER_RULE
28-include $(call SRC_TO_DEP,$(1))
29$(call SRC_TO_OBJ,$(1)): $(1) $(TOP_MAKE) | $(dir $(call SRC_TO_OBJ,$(1)))dir.stamp
30	$(call LOG,CC  ,$$@,$(PNACL_CC) -o $$@ -c $$< $(POSIX_FLAGS) $(2) $(NACL_CFLAGS))
31	@$(FIXDEPS) $(call SRC_TO_DEP_PRE_FIXUP,$(1))
32endef
33
34define CXX_COMPILER_RULE
35-include $(call SRC_TO_DEP,$(1))
36$(call SRC_TO_OBJ,$(1)): $(1) $(TOP_MAKE) | $(dir $(call SRC_TO_OBJ,$(1)))dir.stamp
37	$(call LOG,CXX ,$$@,$(PNACL_CXX) -o $$@ -c $$< $(POSIX_FLAGS) $(2) $(NACL_CFLAGS))
38	@$(FIXDEPS) $(call SRC_TO_DEP_PRE_FIXUP,$(1))
39endef
40
41
42# $1 = Source Name
43# $2 = POSIX Compile Flags
44# $3 = Include Directories
45# $4 = VC Flags (unused)
46define COMPILE_RULE
47ifeq ($(suffix $(1)),.c)
48$(call C_COMPILER_RULE,$(1),$(2) $(foreach inc,$(INC_PATHS),-I$(inc)) $(3))
49else
50$(call CXX_COMPILER_RULE,$(1),$(2) $(foreach inc,$(INC_PATHS),-I$(inc)) $(3))
51endif
52endef
53
54
55#
56# SO Macro
57#
58# $1 = Target Name
59# $2 = List of Sources
60#
61#
62define SO_RULE
63$(error 'Shared libraries not supported by PNaCl')
64endef
65
66
67#
68# LIB Macro
69#
70# $1 = Target Name
71# $2 = List of Sources
72# $3 = POSIX Link Flags
73# $4 = VC Link Flags (unused)
74define LIB_RULE
75$(STAMPDIR)/$(1).stamp: $(LIBDIR)/$(TOOLCHAIN)/$(CONFIG)/lib$(1).a
76	@echo "TOUCHED $$@" > $(STAMPDIR)/$(1).stamp
77
78all: $(LIBDIR)/$(TOOLCHAIN)/$(CONFIG)/lib$(1).a
79$(LIBDIR)/$(TOOLCHAIN)/$(CONFIG)/lib$(1).a: $(foreach src,$(2),$(call SRC_TO_OBJ,$(src)))
80	$(MKDIR) -p $$(dir $$@)
81	$(RM) -f $$@
82	$(call LOG,LIB,$$@,$(PNACL_LIB) -cr $$@ $$^ $(3))
83endef
84
85
86#
87# Specific Link Macro
88#
89# $1 = Target Name
90# $2 = List of inputs
91# $3 = List of libs
92# $4 = List of deps
93# $5 = List of lib dirs
94# $6 = Other Linker Args
95#
96define LINKER_RULE
97all: $(1).pexe 
98$(1).pexe: $(1).bc
99	$(call LOG,FINALIZE,$$@,$(PNACL_FINALIZE) -o $$@ $$^)
100
101$(1).bc: $(2) $(foreach dep,$(4),$(STAMPDIR)/$(dep).stamp)
102	$(call LOG,LINK,$$@,$(PNACL_LINK) -o $$@ $(2) $(PNACL_LDFLAGS) $(foreach path,$(5),-L$(path)/pnacl/$(CONFIG)) $(foreach lib,$(3),-l$(lib)) $(6))
103endef
104
105
106#
107# Generalized Link Macro
108#
109# $1 = Target Name
110# $2 = List of Sources
111# $3 = List of LIBS
112# $4 = List of DEPS
113# $5 = POSIX Linker Switches
114# $6 = VC Linker Switches
115#
116define LINK_RULE
117$(call LINKER_RULE,$(OUTDIR)/$(1),$(foreach src,$(2),$(call SRC_TO_OBJ,$(src))),$(filter-out pthread,$(3)),$(4),$(LIB_PATHS),$(5))
118endef
119
120
121#
122# Strip Macro
123#
124# NOTE: pnacl-strip does not really do much for finalized pexes (in a
125# sense, they are already stripped), but set this rule up for uniformity.
126#
127# $1 = Target Name
128# $2 = Input Name
129#
130define STRIP_RULE
131all: $(OUTDIR)/$(1).pexe
132$(OUTDIR)/$(1).pexe: $(OUTDIR)/$(2).pexe
133	$(call LOG,STRIP,$$@,$(PNACL_STRIP) $$^ -o $$@)
134endef
135
136
137#
138# NMF Manifest generation
139#
140# Use the python script create_nmf to scan the binaries for dependencies using
141# objdump.  Pass in the (-L) paths to the default library toolchains so that we
142# can find those libraries and have it automatically copy the files (-s) to
143# the target directory for us.
144#
145# $1 = Target Name (the basename of the nmf)
146# $2 = Additional create_nmf.py arguments
147#
148NMF:=python $(NACL_SDK_ROOT)/tools/create_nmf.py
149
150EXECUTABLES=$(OUTDIR)/$(1).pexe $(OUTDIR)/$(1)_unstripped.bc
151
152define NMF_RULE
153all: $(OUTDIR)/$(1).nmf
154$(OUTDIR)/$(1).nmf: $(EXECUTABLES)
155	$(call LOG,CREATE_NMF,$$@,$(NMF) -o $$@ $$^ -s $(OUTDIR) $(2))
156endef
157
158#
159# HTML file generation
160#
161CREATE_HTML := python $(NACL_SDK_ROOT)/tools/create_html.py
162
163define HTML_RULE
164all: $(OUTDIR)/$(1).html
165$(OUTDIR)/$(1).html: $(EXECUTABLES)
166	$(call LOG,CREATE_HTML,$$@,$(CREATE_HTML) -o $$@ $$^)
167endef
168