1##
2##  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3##
4##  Use of this source code is governed by a BSD-style license
5##  that can be found in the LICENSE file in the root of the source
6##  tree. An additional intellectual property rights grant can be found
7##  in the file PATENTS.  All contributing project authors may
8##  be found in the AUTHORS file in the root of the source tree.
9##
10
11
12include config.mk
13quiet?=true
14ifeq ($(target),)
15# If a target wasn't specified, invoke for all enabled targets.
16.DEFAULT:
17	@for t in $(ALL_TARGETS); do \
18	     $(MAKE) --no-print-directory target=$$t $(MAKECMDGOALS) || exit $$?;\
19        done
20all: .DEFAULT
21clean:: .DEFAULT
22install:: .DEFAULT
23test:: .DEFAULT
24testdata:: .DEFAULT
25
26
27# Note: md5sum is not installed on OS X, but openssl is. Openssl may not be
28# installed on cygwin, so we need to autodetect here.
29md5sum := $(firstword $(wildcard \
30          $(foreach e,md5sum openssl,\
31          $(foreach p,$(subst :, ,$(PATH)),$(p)/$(e)*))\
32          ))
33md5sum := $(if $(filter %openssl,$(md5sum)),$(md5sum) dgst -md5,$(md5sum))
34
35TGT_CC:=$(word 3, $(subst -, ,$(TOOLCHAIN)))
36dist:
37	@for t in $(ALL_TARGETS); do \
38	     $(MAKE) --no-print-directory target=$$t $(MAKECMDGOALS) || exit $$?;\
39        done
40        # Run configure for the user with the current toolchain.
41	@if [ -d "$(DIST_DIR)/src" ]; then \
42            mkdir -p "$(DIST_DIR)/build"; \
43            cd "$(DIST_DIR)/build"; \
44            echo "Rerunning configure $(CONFIGURE_ARGS)"; \
45            ../src/configure $(CONFIGURE_ARGS); \
46            $(if $(filter vs%,$(TGT_CC)),make NO_LAUNCH_DEVENV=1;) \
47        fi
48	@if [ -d "$(DIST_DIR)" ]; then \
49            echo "    [MD5SUM] $(DIST_DIR)"; \
50	    cd $(DIST_DIR) && \
51	    $(md5sum) `find . -name md5sums.txt -prune -o -type f -print` \
52                | sed -e 's/MD5(\(.*\))= \([0-9a-f]\{32\}\)/\2  \1/' \
53                > md5sums.txt;\
54        fi
55
56
57endif
58
59ifneq ($(target),)
60# Normally, we want to build the filename from the target and the toolchain.
61# This disambiguates from the $(target).mk file that exists in the source tree.
62# However, the toolchain is part of the target in universal builds, so we
63# don't want to include TOOLCHAIN in that case. FAT_ARCHS is used to test
64# if we're in the universal case.
65include $(target)$(if $(FAT_ARCHS),,-$(TOOLCHAIN)).mk
66endif
67BUILD_ROOT?=.
68VPATH=$(SRC_PATH_BARE)
69CFLAGS+=-I$(BUILD_PFX)$(BUILD_ROOT) -I$(SRC_PATH)
70CXXFLAGS+=-I$(BUILD_PFX)$(BUILD_ROOT) -I$(SRC_PATH)
71ASFLAGS+=-I$(BUILD_PFX)$(BUILD_ROOT)/ -I$(SRC_PATH)/
72DIST_DIR?=dist
73HOSTCC?=gcc
74TGT_ISA:=$(word 1, $(subst -, ,$(TOOLCHAIN)))
75TGT_OS:=$(word 2, $(subst -, ,$(TOOLCHAIN)))
76TGT_CC:=$(word 3, $(subst -, ,$(TOOLCHAIN)))
77quiet:=$(if $(verbose),,yes)
78qexec=$(if $(quiet),@)
79
80# Cancel built-in implicit rules
81%: %.o
82%.asm:
83%.a:
84%: %.cc
85
86#
87# Common rules"
88#
89.PHONY: all
90all:
91
92.PHONY: clean
93clean::
94	rm -f $(OBJS-yes) $(OBJS-yes:.o=.d) $(OBJS-yes:.asm.s.o=.asm.s)
95	rm -f $(CLEAN-OBJS)
96
97.PHONY: dist
98dist:
99.PHONY: install
100install::
101.PHONY: test
102test::
103.PHONY: testdata
104testdata::
105
106$(BUILD_PFX)%.c.d: %.c
107	$(if $(quiet),@echo "    [DEP] $@")
108	$(qexec)mkdir -p $(dir $@)
109	$(qexec)$(CC) $(INTERNAL_CFLAGS) $(CFLAGS) -M $< | $(fmt_deps) > $@
110
111$(BUILD_PFX)%.c.o: %.c
112	$(if $(quiet),@echo "    [CC] $@")
113	$(qexec)$(CC) $(INTERNAL_CFLAGS) $(CFLAGS) -c -o $@ $<
114
115$(BUILD_PFX)%.cc.d: %.cc
116	$(if $(quiet),@echo "    [DEP] $@")
117	$(qexec)mkdir -p $(dir $@)
118	$(qexec)$(CXX) $(INTERNAL_CFLAGS) $(CXXFLAGS) -M $< | $(fmt_deps) > $@
119
120$(BUILD_PFX)%.cc.o: %.cc
121	$(if $(quiet),@echo "    [CXX] $@")
122	$(qexec)$(CXX) $(INTERNAL_CFLAGS) $(CXXFLAGS) -c -o $@ $<
123
124$(BUILD_PFX)%.asm.d: %.asm
125	$(if $(quiet),@echo "    [DEP] $@")
126	$(qexec)mkdir -p $(dir $@)
127	$(qexec)$(SRC_PATH_BARE)/build/make/gen_asm_deps.sh \
128            --build-pfx=$(BUILD_PFX) --depfile=$@ $(ASFLAGS) $< > $@
129
130$(BUILD_PFX)%.asm.o: %.asm
131	$(if $(quiet),@echo "    [AS] $@")
132	$(qexec)$(AS) $(ASFLAGS) -o $@ $<
133
134$(BUILD_PFX)%.s.d: %.s
135	$(if $(quiet),@echo "    [DEP] $@")
136	$(qexec)mkdir -p $(dir $@)
137	$(qexec)$(SRC_PATH_BARE)/build/make/gen_asm_deps.sh \
138            --build-pfx=$(BUILD_PFX) --depfile=$@ $(ASFLAGS) $< > $@
139
140$(BUILD_PFX)%.s.o: %.s
141	$(if $(quiet),@echo "    [AS] $@")
142	$(qexec)$(AS) $(ASFLAGS) -o $@ $<
143
144.PRECIOUS: %.c.S
145%.c.S: CFLAGS += -DINLINE_ASM
146$(BUILD_PFX)%.c.S: %.c
147	$(if $(quiet),@echo "    [GEN] $@")
148	$(qexec)$(CC) -S $(CFLAGS) -o $@ $<
149
150.PRECIOUS: %.asm.s
151$(BUILD_PFX)%.asm.s: %.asm
152	$(if $(quiet),@echo "    [ASM CONVERSION] $@")
153	$(qexec)mkdir -p $(dir $@)
154	$(qexec)$(ASM_CONVERSION) <$< >$@
155
156# If we're in debug mode, pretend we don't have GNU strip, to fall back to
157# the copy implementation
158HAVE_GNU_STRIP := $(if $(CONFIG_DEBUG),,$(HAVE_GNU_STRIP))
159ifeq ($(HAVE_GNU_STRIP),yes)
160# Older binutils strip global sybols not needed for relocation processing
161# when given --strip-unneeded. Use nm and awk to identify globals and
162# keep them.
163%.a: %_g.a
164	$(if $(quiet),@echo "    [STRIP] $@ < $<")
165	$(qexec)$(STRIP) --strip-unneeded \
166         `$(NM) $< | grep ' [A-TV-Z] ' | awk '{print "-K"$$3'}`\
167          -o $@ $<
168else
169%.a: %_g.a
170	$(if $(quiet),@echo "    [CP] $@ < $<")
171	$(qexec)cp $< $@
172endif
173
174#
175# Rule to extract assembly constants from C sources
176#
177obj_int_extract: build/make/obj_int_extract.c
178	$(if $(quiet),@echo "    [HOSTCC] $@")
179	$(qexec)$(HOSTCC) -I. -I$(SRC_PATH_BARE) -o $@ $<
180CLEAN-OBJS += obj_int_extract
181
182#
183# Utility functions
184#
185pairmap=$(if $(strip $(2)),\
186    $(call $(1),$(word 1,$(2)),$(word 2,$(2)))\
187    $(call pairmap,$(1),$(wordlist 3,$(words $(2)),$(2)))\
188)
189
190enabled=$(filter-out $($(1)-no),$($(1)-yes))
191cond_enabled=$(if $(filter yes,$($(1))), $(call enabled,$(2)))
192
193find_file1=$(word 1,$(wildcard $(subst //,/,$(addsuffix /$(1),$(2)))))
194find_file=$(foreach f,$(1),$(call find_file1,$(strip $(f)),$(strip $(2))) )
195obj_pats=.c=.c.o $(AS_SFX)=$(AS_SFX).o .cc=.cc.o
196objs=$(addprefix $(BUILD_PFX),$(foreach p,$(obj_pats),$(filter %.o,$(1:$(p))) ))
197
198install_map_templates=$(eval $(call install_map_template,$(1),$(2)))
199
200not=$(subst yes,no,$(1))
201
202ifeq ($(CONFIG_MSVS),yes)
203lib_file_name=$(1).lib
204else
205lib_file_name=lib$(1).a
206endif
207#
208# Rule Templates
209#
210define linker_template
211$(1): $(filter-out -%,$(2))
212$(1):
213	$(if $(quiet),@echo    "    [LD] $$@")
214	$(qexec)$$(LD) $$(strip $$(INTERNAL_LDFLAGS) $$(LDFLAGS) -o $$@ $(2) $(3) $$(extralibs))
215endef
216define linkerxx_template
217$(1): $(filter-out -%,$(2))
218$(1):
219	$(if $(quiet),@echo    "    [LD] $$@")
220	$(qexec)$$(CXX) $$(strip $$(INTERNAL_LDFLAGS) $$(LDFLAGS) -o $$@ $(2) $(3) $$(extralibs))
221endef
222# make-3.80 has a bug with expanding large input strings to the eval function,
223# which was triggered in some cases by the following component of
224# linker_template:
225#   $(1): $$(call find_file, $(patsubst -l%,lib%.a,$(filter -l%,$(2))),\
226#                           $$(patsubst -L%,%,$$(filter -L%,$$(LDFLAGS) $(2))))
227# This may be useful to revisit in the future (it tries to locate libraries
228# in a search path and add them as prerequisites
229
230define install_map_template
231$(DIST_DIR)/$(1): $(2)
232	$(if $(quiet),@echo "    [INSTALL] $$@")
233	$(qexec)mkdir -p $$(dir $$@)
234	$(qexec)cp -p $$< $$@
235endef
236
237define archive_template
238# Not using a pattern rule here because we don't want to generate empty
239# archives when they are listed as a dependency in files not responsible
240# for creating them.
241$(1):
242	$(if $(quiet),@echo "    [AR] $$@")
243	$(qexec)$$(AR) $$(ARFLAGS) $$@ $$?
244endef
245
246define so_template
247# Not using a pattern rule here because we don't want to generate empty
248# archives when they are listed as a dependency in files not responsible
249# for creating them.
250#
251# This needs further abstraction for dealing with non-GNU linkers.
252$(1):
253	$(if $(quiet),@echo "    [LD] $$@")
254	$(qexec)$$(LD) -shared $$(LDFLAGS) \
255            -Wl,--no-undefined -Wl,-soname,$$(SONAME) \
256            -Wl,--version-script,$$(SO_VERSION_SCRIPT) -o $$@ \
257            $$(filter %.o,$$?) $$(extralibs)
258endef
259
260define lipo_lib_template
261$(1): $(addsuffix /$(1),$(FAT_ARCHS))
262	$(if $(quiet),@echo "    [LIPO] $$@")
263	$(qexec)libtool -static -o $$@ $$?
264endef
265
266define lipo_bin_template
267$(1): $(addsuffix /$(1),$(FAT_ARCHS))
268	$(if $(quiet),@echo "    [LIPO] $$@")
269	$(qexec)lipo -output $$@ -create $$?
270endef
271
272
273#
274# Get current configuration
275#
276ifneq ($(target),)
277include $(SRC_PATH_BARE)/$(target:-$(TOOLCHAIN)=).mk
278endif
279ifeq ($(filter clean,$(MAKECMDGOALS)),)
280  # Older versions of make don't like -include directives with no arguments
281  ifneq ($(filter %.d,$(OBJS-yes:.o=.d)),)
282    -include $(filter %.d,$(OBJS-yes:.o=.d))
283  endif
284endif
285
286#
287# Configuration dependent rules
288#
289$(call pairmap,install_map_templates,$(INSTALL_MAPS))
290
291DOCS=$(call cond_enabled,CONFIG_INSTALL_DOCS,DOCS)
292.docs: $(DOCS)
293	@touch $@
294
295INSTALL-DOCS=$(call cond_enabled,CONFIG_INSTALL_DOCS,INSTALL-DOCS)
296ifeq ($(MAKECMDGOALS),dist)
297INSTALL-DOCS+=$(call cond_enabled,CONFIG_INSTALL_DOCS,DIST-DOCS)
298endif
299.install-docs: .docs $(addprefix $(DIST_DIR)/,$(INSTALL-DOCS))
300	@touch $@
301
302clean::
303	rm -f .docs .install-docs $(DOCS)
304
305BINS=$(call enabled,BINS)
306.bins: $(BINS)
307	@touch $@
308
309INSTALL-BINS=$(call cond_enabled,CONFIG_INSTALL_BINS,INSTALL-BINS)
310ifeq ($(MAKECMDGOALS),dist)
311INSTALL-BINS+=$(call cond_enabled,CONFIG_INSTALL_BINS,DIST-BINS)
312endif
313.install-bins: .bins $(addprefix $(DIST_DIR)/,$(INSTALL-BINS))
314	@touch $@
315
316clean::
317	rm -f .bins .install-bins $(BINS)
318
319LIBS=$(call enabled,LIBS)
320.libs: $(LIBS)
321	@touch $@
322$(foreach lib,$(filter %_g.a,$(LIBS)),$(eval $(call archive_template,$(lib))))
323$(foreach lib,$(filter %so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH),$(LIBS)),$(eval $(call so_template,$(lib))))
324
325INSTALL-LIBS=$(call cond_enabled,CONFIG_INSTALL_LIBS,INSTALL-LIBS)
326ifeq ($(MAKECMDGOALS),dist)
327INSTALL-LIBS+=$(call cond_enabled,CONFIG_INSTALL_LIBS,DIST-LIBS)
328endif
329.install-libs: .libs $(addprefix $(DIST_DIR)/,$(INSTALL-LIBS))
330	@touch $@
331
332clean::
333	rm -f .libs .install-libs $(LIBS)
334
335ifeq ($(CONFIG_EXTERNAL_BUILD),yes)
336PROJECTS=$(call enabled,PROJECTS)
337.projects: $(PROJECTS)
338	@touch $@
339
340INSTALL-PROJECTS=$(call cond_enabled,CONFIG_INSTALL_PROJECTS,INSTALL-PROJECTS)
341ifeq ($(MAKECMDGOALS),dist)
342INSTALL-PROJECTS+=$(call cond_enabled,CONFIG_INSTALL_PROJECTS,DIST-PROJECTS)
343endif
344.install-projects: .projects $(addprefix $(DIST_DIR)/,$(INSTALL-PROJECTS))
345	@touch $@
346
347clean::
348	rm -f .projects .install-projects $(PROJECTS)
349endif
350
351# If there are any source files to be distributed, then include the build
352# system too.
353ifneq ($(call enabled,DIST-SRCS),)
354    DIST-SRCS-yes            += configure
355    DIST-SRCS-yes            += build/make/configure.sh
356    DIST-SRCS-yes            += build/make/gen_asm_deps.sh
357    DIST-SRCS-yes            += build/make/Makefile
358    DIST-SRCS-$(CONFIG_MSVS)  += build/make/gen_msvs_def.sh
359    DIST-SRCS-$(CONFIG_MSVS)  += build/make/gen_msvs_proj.sh
360    DIST-SRCS-$(CONFIG_MSVS)  += build/make/gen_msvs_sln.sh
361    DIST-SRCS-$(CONFIG_MSVS)  += build/x86-msvs/yasm.rules
362    DIST-SRCS-$(CONFIG_MSVS)  += build/x86-msvs/obj_int_extract.bat
363    DIST-SRCS-$(CONFIG_RVCT) += build/make/armlink_adapter.sh
364    # Include obj_int_extract if we use offsets from asm_*_offsets
365    DIST-SRCS-$(ARCH_ARM)$(ARCH_X86)$(ARCH_X86_64)    += build/make/obj_int_extract.c
366    DIST-SRCS-$(ARCH_ARM)    += build/make/ads2gas.pl
367    DIST-SRCS-yes            += $(target:-$(TOOLCHAIN)=).mk
368endif
369INSTALL-SRCS := $(call cond_enabled,CONFIG_INSTALL_SRCS,INSTALL-SRCS)
370ifeq ($(MAKECMDGOALS),dist)
371INSTALL-SRCS += $(call cond_enabled,CONFIG_INSTALL_SRCS,DIST-SRCS)
372endif
373.install-srcs: $(addprefix $(DIST_DIR)/src/,$(INSTALL-SRCS))
374	@touch $@
375
376clean::
377	rm -f .install-srcs
378
379ifeq ($(CONFIG_EXTERNAL_BUILD),yes)
380    BUILD_TARGETS += .projects
381    INSTALL_TARGETS += .install-projects
382endif
383BUILD_TARGETS += .docs .libs .bins
384INSTALL_TARGETS += .install-docs .install-srcs .install-libs .install-bins
385all: $(BUILD_TARGETS)
386install:: $(INSTALL_TARGETS)
387dist: $(INSTALL_TARGETS)
388test::
389