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
22exampletest: .DEFAULT
23install:: .DEFAULT
24test:: .DEFAULT
25testdata:: .DEFAULT
26utiltest: .DEFAULT
27
28
29# Note: md5sum is not installed on OS X, but openssl is. Openssl may not be
30# installed on cygwin, so we need to autodetect here.
31md5sum := $(firstword $(wildcard \
32          $(foreach e,md5sum openssl,\
33          $(foreach p,$(subst :, ,$(PATH)),$(p)/$(e)*))\
34          ))
35md5sum := $(if $(filter %openssl,$(md5sum)),$(md5sum) dgst -md5,$(md5sum))
36
37TGT_CC:=$(word 3, $(subst -, ,$(TOOLCHAIN)))
38dist:
39	@for t in $(ALL_TARGETS); do \
40	     $(MAKE) --no-print-directory target=$$t $(MAKECMDGOALS) || exit $$?;\
41        done
42        # Run configure for the user with the current toolchain.
43	@if [ -d "$(DIST_DIR)/src" ]; then \
44            mkdir -p "$(DIST_DIR)/build"; \
45            cd "$(DIST_DIR)/build"; \
46            echo "Rerunning configure $(CONFIGURE_ARGS)"; \
47            ../src/configure $(CONFIGURE_ARGS); \
48            $(if $(filter vs%,$(TGT_CC)),make NO_LAUNCH_DEVENV=1;) \
49        fi
50	@if [ -d "$(DIST_DIR)" ]; then \
51            echo "    [MD5SUM] $(DIST_DIR)"; \
52	    cd $(DIST_DIR) && \
53	    $(md5sum) `find . -name md5sums.txt -prune -o -type f -print` \
54                | sed -e 's/MD5(\(.*\))= \([0-9a-f]\{32\}\)/\2  \1/' \
55                > md5sums.txt;\
56        fi
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 $(or $(verbose), $(V)),, 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: clean
98distclean: clean
99	if [ -z "$(target)" ]; then \
100      rm -f Makefile; \
101      rm -f config.log config.mk; \
102      rm -f vpx_config.[hc] vpx_config.asm; \
103    else \
104      rm -f $(target)-$(TOOLCHAIN).mk; \
105    fi
106
107.PHONY: dist
108dist:
109.PHONY: exampletest
110exampletest:
111.PHONY: install
112install::
113.PHONY: test
114test::
115.PHONY: testdata
116testdata::
117.PHONY: utiltest
118utiltest:
119
120# Add compiler flags for intrinsic files
121ifeq ($(TOOLCHAIN), x86-os2-gcc)
122STACKREALIGN=-mstackrealign
123else
124STACKREALIGN=
125endif
126
127$(BUILD_PFX)%_mmx.c.d: CFLAGS += -mmmx
128$(BUILD_PFX)%_mmx.c.o: CFLAGS += -mmmx
129$(BUILD_PFX)%_sse2.c.d: CFLAGS += -msse2 $(STACKREALIGN)
130$(BUILD_PFX)%_sse2.c.o: CFLAGS += -msse2 $(STACKREALIGN)
131$(BUILD_PFX)%_sse3.c.d: CFLAGS += -msse3 $(STACKREALIGN)
132$(BUILD_PFX)%_sse3.c.o: CFLAGS += -msse3 $(STACKREALIGN)
133$(BUILD_PFX)%_ssse3.c.d: CFLAGS += -mssse3 $(STACKREALIGN)
134$(BUILD_PFX)%_ssse3.c.o: CFLAGS += -mssse3 $(STACKREALIGN)
135$(BUILD_PFX)%_sse4.c.d: CFLAGS += -msse4.1 $(STACKREALIGN)
136$(BUILD_PFX)%_sse4.c.o: CFLAGS += -msse4.1 $(STACKREALIGN)
137$(BUILD_PFX)%_avx.c.d: CFLAGS += -mavx $(STACKREALIGN)
138$(BUILD_PFX)%_avx.c.o: CFLAGS += -mavx $(STACKREALIGN)
139$(BUILD_PFX)%_avx2.c.d: CFLAGS += -mavx2 $(STACKREALIGN)
140$(BUILD_PFX)%_avx2.c.o: CFLAGS += -mavx2 $(STACKREALIGN)
141
142$(BUILD_PFX)%.c.d: %.c
143	$(if $(quiet),@echo "    [DEP] $@")
144	$(qexec)mkdir -p $(dir $@)
145	$(qexec)$(CC) $(INTERNAL_CFLAGS) $(CFLAGS) -M $< | $(fmt_deps) > $@
146
147$(BUILD_PFX)%.c.o: %.c
148	$(if $(quiet),@echo "    [CC] $@")
149	$(qexec)$(CC) $(INTERNAL_CFLAGS) $(CFLAGS) -c -o $@ $<
150
151$(BUILD_PFX)%.cc.d: %.cc
152	$(if $(quiet),@echo "    [DEP] $@")
153	$(qexec)mkdir -p $(dir $@)
154	$(qexec)$(CXX) $(INTERNAL_CFLAGS) $(CXXFLAGS) -M $< | $(fmt_deps) > $@
155
156$(BUILD_PFX)%.cc.o: %.cc
157	$(if $(quiet),@echo "    [CXX] $@")
158	$(qexec)$(CXX) $(INTERNAL_CFLAGS) $(CXXFLAGS) -c -o $@ $<
159
160$(BUILD_PFX)%.cpp.d: %.cpp
161	$(if $(quiet),@echo "    [DEP] $@")
162	$(qexec)mkdir -p $(dir $@)
163	$(qexec)$(CXX) $(INTERNAL_CFLAGS) $(CXXFLAGS) -M $< | $(fmt_deps) > $@
164
165$(BUILD_PFX)%.cpp.o: %.cpp
166	$(if $(quiet),@echo "    [CXX] $@")
167	$(qexec)$(CXX) $(INTERNAL_CFLAGS) $(CXXFLAGS) -c -o $@ $<
168
169$(BUILD_PFX)%.asm.d: %.asm
170	$(if $(quiet),@echo "    [DEP] $@")
171	$(qexec)mkdir -p $(dir $@)
172	$(qexec)$(SRC_PATH_BARE)/build/make/gen_asm_deps.sh \
173            --build-pfx=$(BUILD_PFX) --depfile=$@ $(ASFLAGS) $< > $@
174
175$(BUILD_PFX)%.asm.o: %.asm
176	$(if $(quiet),@echo "    [AS] $@")
177	$(qexec)$(AS) $(ASFLAGS) -o $@ $<
178
179$(BUILD_PFX)%.s.d: %.s
180	$(if $(quiet),@echo "    [DEP] $@")
181	$(qexec)mkdir -p $(dir $@)
182	$(qexec)$(SRC_PATH_BARE)/build/make/gen_asm_deps.sh \
183            --build-pfx=$(BUILD_PFX) --depfile=$@ $(ASFLAGS) $< > $@
184
185$(BUILD_PFX)%.s.o: %.s
186	$(if $(quiet),@echo "    [AS] $@")
187	$(qexec)$(AS) $(ASFLAGS) -o $@ $<
188
189.PRECIOUS: %.c.S
190%.c.S: CFLAGS += -DINLINE_ASM
191$(BUILD_PFX)%.c.S: %.c
192	$(if $(quiet),@echo "    [GEN] $@")
193	$(qexec)$(CC) -S $(CFLAGS) -o $@ $<
194
195.PRECIOUS: %.asm.s
196$(BUILD_PFX)%.asm.s: %.asm
197	$(if $(quiet),@echo "    [ASM CONVERSION] $@")
198	$(qexec)mkdir -p $(dir $@)
199	$(qexec)$(ASM_CONVERSION) <$< >$@
200
201# If we're in debug mode, pretend we don't have GNU strip, to fall back to
202# the copy implementation
203HAVE_GNU_STRIP := $(if $(CONFIG_DEBUG),,$(HAVE_GNU_STRIP))
204ifeq ($(HAVE_GNU_STRIP),yes)
205# Older binutils strip global symbols not needed for relocation processing
206# when given --strip-unneeded. Using nm and awk to identify globals and
207# keep them caused command line length issues under mingw and segfaults in
208# test_libvpx were observed under OS/2: simply use --strip-debug.
209%.a: %_g.a
210	$(if $(quiet),@echo "    [STRIP] $@ < $<")
211	$(qexec)$(STRIP) --strip-debug \
212          -o $@ $<
213else
214%.a: %_g.a
215	$(if $(quiet),@echo "    [CP] $@ < $<")
216	$(qexec)cp $< $@
217endif
218
219#
220# Rule to extract assembly constants from C sources
221#
222obj_int_extract: build/make/obj_int_extract.c
223	$(if $(quiet),@echo "    [HOSTCC] $@")
224	$(qexec)$(HOSTCC) -I. -I$(SRC_PATH_BARE) -o $@ $<
225CLEAN-OBJS += obj_int_extract
226
227#
228# Utility functions
229#
230pairmap=$(if $(strip $(2)),\
231    $(call $(1),$(word 1,$(2)),$(word 2,$(2)))\
232    $(call pairmap,$(1),$(wordlist 3,$(words $(2)),$(2)))\
233)
234
235enabled=$(filter-out $($(1)-no),$($(1)-yes))
236cond_enabled=$(if $(filter yes,$($(1))), $(call enabled,$(2)))
237
238find_file1=$(word 1,$(wildcard $(subst //,/,$(addsuffix /$(1),$(2)))))
239find_file=$(foreach f,$(1),$(call find_file1,$(strip $(f)),$(strip $(2))) )
240obj_pats=.c=.c.o $(AS_SFX)=$(AS_SFX).o .cc=.cc.o .cpp=.cpp.o
241objs=$(addprefix $(BUILD_PFX),$(foreach p,$(obj_pats),$(filter %.o,$(1:$(p))) ))
242
243install_map_templates=$(eval $(call install_map_template,$(1),$(2)))
244
245not=$(subst yes,no,$(1))
246
247ifeq ($(CONFIG_MSVS),yes)
248lib_file_name=$(1).lib
249else
250lib_file_name=lib$(1).a
251endif
252#
253# Rule Templates
254#
255define linker_template
256$(1): $(filter-out -%,$(2))
257$(1):
258	$(if $(quiet),@echo    "    [LD] $$@")
259	$(qexec)$$(LD) $$(strip $$(INTERNAL_LDFLAGS) $$(LDFLAGS) -o $$@ $(2) $(3) $$(extralibs))
260endef
261define linkerxx_template
262$(1): $(filter-out -%,$(2))
263$(1):
264	$(if $(quiet),@echo    "    [LD] $$@")
265	$(qexec)$$(CXX) $$(strip $$(INTERNAL_LDFLAGS) $$(LDFLAGS) -o $$@ $(2) $(3) $$(extralibs))
266endef
267# make-3.80 has a bug with expanding large input strings to the eval function,
268# which was triggered in some cases by the following component of
269# linker_template:
270#   $(1): $$(call find_file, $(patsubst -l%,lib%.a,$(filter -l%,$(2))),\
271#                           $$(patsubst -L%,%,$$(filter -L%,$$(LDFLAGS) $(2))))
272# This may be useful to revisit in the future (it tries to locate libraries
273# in a search path and add them as prerequisites
274
275define install_map_template
276$(DIST_DIR)/$(1): $(2)
277	$(if $(quiet),@echo "    [INSTALL] $$@")
278	$(qexec)mkdir -p $$(dir $$@)
279	$(qexec)cp -p $$< $$@
280endef
281
282define archive_template
283# Not using a pattern rule here because we don't want to generate empty
284# archives when they are listed as a dependency in files not responsible
285# for creating them.
286$(1):
287	$(if $(quiet),@echo "    [AR] $$@")
288	$(qexec)$$(AR) $$(ARFLAGS) $$@ $$?
289endef
290
291define so_template
292# Not using a pattern rule here because we don't want to generate empty
293# archives when they are listed as a dependency in files not responsible
294# for creating them.
295#
296# This needs further abstraction for dealing with non-GNU linkers.
297$(1):
298	$(if $(quiet),@echo "    [LD] $$@")
299	$(qexec)$$(LD) -shared $$(LDFLAGS) \
300            -Wl,--no-undefined -Wl,-soname,$$(SONAME) \
301            -Wl,--version-script,$$(EXPORTS_FILE) -o $$@ \
302            $$(filter %.o,$$^) $$(extralibs)
303endef
304
305define dl_template
306# Not using a pattern rule here because we don't want to generate empty
307# archives when they are listed as a dependency in files not responsible
308# for creating them.
309$(1):
310	$(if $(quiet),@echo "    [LD] $$@")
311	$(qexec)$$(LD) -dynamiclib $$(LDFLAGS) \
312	    -exported_symbols_list $$(EXPORTS_FILE) \
313        -Wl,-headerpad_max_install_names,-compatibility_version,1.0,-current_version,$$(VERSION_MAJOR) \
314        -o $$@ \
315        $$(filter %.o,$$^) $$(extralibs)
316endef
317
318
319
320define lipo_lib_template
321$(1): $(addsuffix /$(1),$(FAT_ARCHS))
322	$(if $(quiet),@echo "    [LIPO] $$@")
323	$(qexec)libtool -static -o $$@ $$?
324endef
325
326define lipo_bin_template
327$(1): $(addsuffix /$(1),$(FAT_ARCHS))
328	$(if $(quiet),@echo "    [LIPO] $$@")
329	$(qexec)lipo -output $$@ -create $$?
330endef
331
332
333#
334# Get current configuration
335#
336ifneq ($(target),)
337include $(SRC_PATH_BARE)/$(target:-$(TOOLCHAIN)=).mk
338endif
339
340skip_deps := $(filter %clean,$(MAKECMDGOALS))
341skip_deps += $(findstring testdata,$(MAKECMDGOALS))
342ifeq ($(strip $(skip_deps)),)
343  # Older versions of make don't like -include directives with no arguments
344  ifneq ($(filter %.d,$(OBJS-yes:.o=.d)),)
345    -include $(filter %.d,$(OBJS-yes:.o=.d))
346  endif
347endif
348
349#
350# Configuration dependent rules
351#
352$(call pairmap,install_map_templates,$(INSTALL_MAPS))
353
354DOCS=$(call cond_enabled,CONFIG_INSTALL_DOCS,DOCS)
355.docs: $(DOCS)
356	@touch $@
357
358INSTALL-DOCS=$(call cond_enabled,CONFIG_INSTALL_DOCS,INSTALL-DOCS)
359ifeq ($(MAKECMDGOALS),dist)
360INSTALL-DOCS+=$(call cond_enabled,CONFIG_INSTALL_DOCS,DIST-DOCS)
361endif
362.install-docs: .docs $(addprefix $(DIST_DIR)/,$(INSTALL-DOCS))
363	@touch $@
364
365clean::
366	rm -f .docs .install-docs $(DOCS)
367
368BINS=$(call enabled,BINS)
369.bins: $(BINS)
370	@touch $@
371
372INSTALL-BINS=$(call cond_enabled,CONFIG_INSTALL_BINS,INSTALL-BINS)
373ifeq ($(MAKECMDGOALS),dist)
374INSTALL-BINS+=$(call cond_enabled,CONFIG_INSTALL_BINS,DIST-BINS)
375endif
376.install-bins: .bins $(addprefix $(DIST_DIR)/,$(INSTALL-BINS))
377	@touch $@
378
379clean::
380	rm -f .bins .install-bins $(BINS)
381
382LIBS=$(call enabled,LIBS)
383.libs: $(LIBS)
384	@touch $@
385$(foreach lib,$(filter %_g.a,$(LIBS)),$(eval $(call archive_template,$(lib))))
386$(foreach lib,$(filter %so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH),$(LIBS)),$(eval $(call so_template,$(lib))))
387$(foreach lib,$(filter %$(VERSION_MAJOR).dylib,$(LIBS)),$(eval $(call dl_template,$(lib))))
388
389INSTALL-LIBS=$(call cond_enabled,CONFIG_INSTALL_LIBS,INSTALL-LIBS)
390ifeq ($(MAKECMDGOALS),dist)
391INSTALL-LIBS+=$(call cond_enabled,CONFIG_INSTALL_LIBS,DIST-LIBS)
392endif
393.install-libs: .libs $(addprefix $(DIST_DIR)/,$(INSTALL-LIBS))
394	@touch $@
395
396clean::
397	rm -f .libs .install-libs $(LIBS)
398
399ifeq ($(CONFIG_EXTERNAL_BUILD),yes)
400PROJECTS=$(call enabled,PROJECTS)
401.projects: $(PROJECTS)
402	@touch $@
403
404INSTALL-PROJECTS=$(call cond_enabled,CONFIG_INSTALL_PROJECTS,INSTALL-PROJECTS)
405ifeq ($(MAKECMDGOALS),dist)
406INSTALL-PROJECTS+=$(call cond_enabled,CONFIG_INSTALL_PROJECTS,DIST-PROJECTS)
407endif
408.install-projects: .projects $(addprefix $(DIST_DIR)/,$(INSTALL-PROJECTS))
409	@touch $@
410
411clean::
412	rm -f .projects .install-projects $(PROJECTS)
413endif
414
415# If there are any source files to be distributed, then include the build
416# system too.
417ifneq ($(call enabled,DIST-SRCS),)
418    DIST-SRCS-yes            += configure
419    DIST-SRCS-yes            += build/make/configure.sh
420    DIST-SRCS-yes            += build/make/gen_asm_deps.sh
421    DIST-SRCS-yes            += build/make/Makefile
422    DIST-SRCS-$(CONFIG_MSVS)  += build/make/gen_msvs_def.sh
423    DIST-SRCS-$(CONFIG_MSVS)  += build/make/gen_msvs_proj.sh
424    DIST-SRCS-$(CONFIG_MSVS)  += build/make/gen_msvs_sln.sh
425    DIST-SRCS-$(CONFIG_MSVS)  += build/make/gen_msvs_vcxproj.sh
426    DIST-SRCS-$(CONFIG_MSVS)  += build/make/msvs_common.sh
427    DIST-SRCS-$(CONFIG_MSVS)  += build/x86-msvs/obj_int_extract.bat
428    DIST-SRCS-$(CONFIG_MSVS)  += build/arm-msvs/obj_int_extract.bat
429    DIST-SRCS-$(CONFIG_RVCT) += build/make/armlink_adapter.sh
430    # Include obj_int_extract if we use offsets from *_asm_*_offsets
431    DIST-SRCS-$(ARCH_ARM)$(ARCH_X86)$(ARCH_X86_64)    += build/make/obj_int_extract.c
432    DIST-SRCS-$(ARCH_ARM)    += build/make/ads2gas.pl
433    DIST-SRCS-$(ARCH_ARM)    += build/make/ads2gas_apple.pl
434    DIST-SRCS-$(ARCH_ARM)    += build/make/ads2armasm_ms.pl
435    DIST-SRCS-$(ARCH_ARM)    += build/make/thumb.pm
436    DIST-SRCS-yes            += $(target:-$(TOOLCHAIN)=).mk
437endif
438INSTALL-SRCS := $(call cond_enabled,CONFIG_INSTALL_SRCS,INSTALL-SRCS)
439ifeq ($(MAKECMDGOALS),dist)
440INSTALL-SRCS += $(call cond_enabled,CONFIG_INSTALL_SRCS,DIST-SRCS)
441endif
442.install-srcs: $(addprefix $(DIST_DIR)/src/,$(INSTALL-SRCS))
443	@touch $@
444
445clean::
446	rm -f .install-srcs
447
448ifeq ($(CONFIG_EXTERNAL_BUILD),yes)
449    BUILD_TARGETS += .projects
450    INSTALL_TARGETS += .install-projects
451endif
452BUILD_TARGETS += .docs .libs .bins
453INSTALL_TARGETS += .install-docs .install-srcs .install-libs .install-bins
454all: $(BUILD_TARGETS)
455install:: $(INSTALL_TARGETS)
456dist: $(INSTALL_TARGETS)
457test::
458