blob: 4037276104481724cc27ef04c4d4f9cf902ce36c [file] [log] [blame]
Dominik Riebelingb24e5622011-12-14 21:59:37 +00001# __________ __ ___.
2# Open \______ \ ____ ____ | | _\_ |__ _______ ___
3# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
4# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
5# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
6# \/ \/ \/ \/ \/
7
8# libtools.make
9#
10# Common Makefile for tools used by Rockbox Utility.
11# Provides rules for building as library, universal library (OS X) and
12# standalone binary.
13#
14# LIBSOURCES is a list of files to build the lib
15# SOURCES is a list of files to build for the main binary
16# EXTRADEPS is a list of make targets dependencies
17#
18ifndef V
19SILENT = @
20endif
21
22# Get directory this Makefile is in for relative paths.
23TOP := $(dir $(lastword $(MAKEFILE_LIST)))
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +020024ifeq ($(OS),Windows_NT)
25mkdir = if not exist $(subst /,\,$(1)) mkdir $(subst /,\,$(1))
26else
27mkdir = mkdir -p $(1)
28endif
Dominik Riebelingb24e5622011-12-14 21:59:37 +000029
30# overwrite for releases
Dominik Riebeling45cda1f2013-01-01 10:51:11 +010031APPVERSION ?= $(shell $(TOP)/../tools/version.sh $(TOP)/..)
Dominik Riebeling8568a8d2013-05-10 17:45:37 +020032CFLAGS += -DVERSION=\"$(APPVERSION)\"
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020033TARGET_DIR ?= $(abspath .)/
Dominik Riebelingb24e5622011-12-14 21:59:37 +000034
Dominik Riebeling460e9592013-05-03 23:50:12 +020035NATIVECC ?= gcc
36CC ?= gcc
Dominik Riebeling8568a8d2013-05-10 17:45:37 +020037CPPDEFINES := $(shell echo foo | $(CROSS)$(CC) -dM -E -)
Rafaël Carrédb3afb02012-02-05 14:03:10 -050038# use POSIX/C99 printf on windows
39CFLAGS += -D__USE_MINGW_ANSI_STDIO=1
40
Dominik Riebelingb24e5622011-12-14 21:59:37 +000041BINARY = $(OUTPUT)
42# when building a Windows binary add the correct file suffix
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020043ifeq ($(findstring CYGWIN,$(CPPDEFINES)),CYGWIN)
Dominik Riebelingb24e5622011-12-14 21:59:37 +000044BINARY = $(OUTPUT).exe
45CFLAGS+=-mno-cygwin
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020046COMPILETARGET = cygwin
Dominik Riebelingb24e5622011-12-14 21:59:37 +000047else
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020048ifeq ($(findstring MINGW,$(CPPDEFINES)),MINGW)
Dominik Riebelingb24e5622011-12-14 21:59:37 +000049BINARY = $(OUTPUT).exe
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020050COMPILETARGET = mingw
Dominik Riebelingb24e5622011-12-14 21:59:37 +000051else
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020052ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE)
53COMPILETARGET = darwin
Dominik Riebeling703bc402013-04-05 20:03:45 +020054LDOPTS += $(LDOPTS_OSX)
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020055else
56COMPILETARGET = posix
Dominik Riebelingb24e5622011-12-14 21:59:37 +000057endif
58endif
59endif
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020060$(info Compiler creates $(COMPILETARGET) binaries)
Dominik Riebelingb24e5622011-12-14 21:59:37 +000061
Dominik Riebelingc6dcec42012-01-05 22:26:08 +000062# OS X specifics. Needs to consider cross compiling for Windows.
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020063ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE)
Dominik Riebelingc6dcec42012-01-05 22:26:08 +000064# when building libs for OS X build for both i386 and ppc at the same time.
65# This creates fat objects, and ar can only create the archive but not operate
66# on it. As a result the ar call must NOT use the u (update) flag.
Dominik Riebeling33bda052013-06-09 19:43:37 +020067ARCHFLAGS += -arch ppc -arch i386
Dominik Riebelingb056e022011-12-16 18:35:01 +000068# building against SDK 10.4 is not compatible with gcc-4.2 (default on newer Xcode)
69# might need adjustment for older Xcode.
Dominik Riebeling703bc402013-04-05 20:03:45 +020070CC = gcc-4.0
Dominik Riebelingb056e022011-12-16 18:35:01 +000071CFLAGS += -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4
Dominik Riebeling703bc402013-04-05 20:03:45 +020072NATIVECC = gcc-4.0
Dominik Riebelingb056e022011-12-16 18:35:01 +000073endif
74WINDRES = windres
75
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020076BUILD_DIR ?= $(TARGET_DIR)build$(COMPILETARGET)
Dominik Riebelingb24e5622011-12-14 21:59:37 +000077
78ifdef RBARCH
Dominik Riebeling33bda052013-06-09 19:43:37 +020079ARCHFLAGS += -arch $(RBARCH)
Dominik Riebelinge073bc92013-05-11 20:15:53 +020080OBJDIR = $(abspath $(BUILD_DIR)/$(RBARCH))/
81else
82OBJDIR = $(abspath $(BUILD_DIR))/
Dominik Riebelingb24e5622011-12-14 21:59:37 +000083endif
84
85all: $(BINARY)
86
87OBJS := $(patsubst %.c,%.o,$(addprefix $(OBJDIR),$(notdir $(SOURCES))))
88LIBOBJS := $(patsubst %.c,%.o,$(addprefix $(OBJDIR),$(notdir $(LIBSOURCES))))
89
Dominik Riebelinge073bc92013-05-11 20:15:53 +020090# create dependency files. Make sure to use the same prefix as with OBJS!
Dominik Riebeling8a4075d2013-05-11 11:28:08 +020091$(foreach src,$(SOURCES) $(LIBSOURCES),$(eval $(addprefix $(OBJDIR),$(subst .c,.o,$(notdir $(src)))): $(src)))
Dominik Riebelinge073bc92013-05-11 20:15:53 +020092$(foreach src,$(SOURCES) $(LIBSOURCES),$(eval $(addprefix $(OBJDIR),$(subst .c,.d,$(notdir $(src)))): $(src)))
93DEPS = $(addprefix $(OBJDIR),$(subst .c,.d,$(notdir $(SOURCES) $(LIBSOURCES))))
94-include $(DEPS)
Dominik Riebeling8a4075d2013-05-11 11:28:08 +020095
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +000096# additional link dependencies for the standalone executable
97# extra dependencies: libucl
98LIBUCL = libucl$(RBARCH).a
99$(LIBUCL): $(OBJDIR)$(LIBUCL)
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000100
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +0000101$(OBJDIR)$(LIBUCL):
102 $(SILENT)$(MAKE) -C $(TOP)/../tools/ucl/src TARGET_DIR=$(OBJDIR) $@
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000103
104# building the standalone executable
105$(BINARY): $(OBJS) $(EXTRADEPS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
106 @echo LD $@
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +0200107 $(SILENT)$(call mkdir,$(dir $@))
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000108# EXTRADEPS need to be built into OBJDIR.
Dominik Riebeling33bda052013-06-09 19:43:37 +0200109 $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) $(LDOPTS) -o $(BINARY) \
Dominik Riebeling17a0c832011-12-15 18:44:57 +0000110 $(OBJS) $(addprefix $(OBJDIR),$(EXTRADEPS)) \
111 $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000112
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +0000113# common rules
Dominik Riebeling8a4075d2013-05-11 11:28:08 +0200114$(OBJDIR)%.o:
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +0000115 @echo CC $<
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +0200116 $(SILENT)$(call mkdir,$(dir $@))
Dominik Riebeling33bda052013-06-09 19:43:37 +0200117 $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) -c -o $@ $<
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +0000118
119# lib rules
120lib$(OUTPUT)$(RBARCH).a: $(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a
121lib$(OUTPUT)$(RBARCH): $(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a
122
Dominik Riebeling17a0c832011-12-15 18:44:57 +0000123$(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a: $(LIBOBJS) \
124 $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
Dominik Riebeling84205022011-12-16 21:10:29 +0000125# rules to build a DLL. Only works for W32 as target (i.e. MinGW toolchain)
126dll: $(OUTPUT).dll
127$(OUTPUT).dll: $(TARGET_DIR)$(OUTPUT).dll
128$(TARGET_DIR)$(OUTPUT).dll: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
129 @echo DLL $(notdir $@)
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +0200130 $(SILENT)$(call mkdir,$(dir $@))
Dominik Riebeling33bda052013-06-09 19:43:37 +0200131 $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) -shared -o $@ $^ \
Dominik Riebeling84205022011-12-16 21:10:29 +0000132 -Wl,--output-def,$(TARGET_DIR)$(OUTPUT).def
133
Dominik Riebelingc6dcec42012-01-05 22:26:08 +0000134# create lib file from objects
Dominik Riebeling84205022011-12-16 21:10:29 +0000135$(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +0000136 @echo AR $(notdir $@)
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +0200137 $(SILENT)$(call mkdir,$(dir $@))
Dominik Riebelingc6dcec42012-01-05 22:26:08 +0000138 $(SILENT)$(AR) rcs $@ $^
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000139
140clean:
141 rm -f $(OBJS) $(OUTPUT) $(TARGET_DIR)lib$(OUTPUT)*.a $(OUTPUT).dmg
142 rm -rf $(OUTPUT)-* i386 ppc $(OBJDIR)
143
Dominik Riebelinge073bc92013-05-11 20:15:53 +0200144%.d:
145 $(SILENT)$(call mkdir,$(BUILD_DIR))
146 $(SILENT)$(CC) -MG -MM -MT $(subst .d,.o,$@) $(CFLAGS) -o $(BUILD_DIR)/$(notdir $@) $<
147
Dominik Riebeling308f0992012-04-28 12:05:50 +0200148# extra tools
149BIN2C = $(TOP)/tools/bin2c
150$(BIN2C):
151 $(MAKE) -C $(TOP)/tools
152
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000153# OS X specifics
Dominik Riebelingded02d82012-01-06 09:24:38 +0000154$(OUTPUT).dmg: $(OUTPUT)
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000155 @echo DMG $@
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +0200156 $(SILENT)$(call mkdir,$(OUTPUT)-dmg))
Dominik Riebelingded02d82012-01-06 09:24:38 +0000157 $(SILENT)cp -p $(OUTPUT) $(OUTPUT)-dmg
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000158 $(SILENT)hdiutil create -srcfolder $(OUTPUT)-dmg $@