blob: 4623808f94808559b9715256305a31bd16f17b70 [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))
Dominik Riebeling17a18672014-01-05 17:08:50 +010026rm = if exist $(subst /,\,$(1)) del /q /s $(subst /,\,$(1))
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +020027else
28mkdir = mkdir -p $(1)
Dominik Riebelingf3a1a332014-01-05 16:53:17 +010029rm = rm -rf $(1)
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +020030endif
Dominik Riebelingb24e5622011-12-14 21:59:37 +000031
32# overwrite for releases
Dominik Riebeling45cda1f2013-01-01 10:51:11 +010033APPVERSION ?= $(shell $(TOP)/../tools/version.sh $(TOP)/..)
Dominik Riebeling8568a8d2013-05-10 17:45:37 +020034CFLAGS += -DVERSION=\"$(APPVERSION)\"
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020035TARGET_DIR ?= $(abspath .)/
Dominik Riebelingb24e5622011-12-14 21:59:37 +000036
Dominik Riebeling460e9592013-05-03 23:50:12 +020037NATIVECC ?= gcc
38CC ?= gcc
Dominik Riebeling8568a8d2013-05-10 17:45:37 +020039CPPDEFINES := $(shell echo foo | $(CROSS)$(CC) -dM -E -)
Rafaël Carrédb3afb02012-02-05 14:03:10 -050040# use POSIX/C99 printf on windows
41CFLAGS += -D__USE_MINGW_ANSI_STDIO=1
42
Dominik Riebelingb24e5622011-12-14 21:59:37 +000043BINARY = $(OUTPUT)
44# when building a Windows binary add the correct file suffix
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020045ifeq ($(findstring CYGWIN,$(CPPDEFINES)),CYGWIN)
Dominik Riebelingb24e5622011-12-14 21:59:37 +000046BINARY = $(OUTPUT).exe
47CFLAGS+=-mno-cygwin
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020048COMPILETARGET = cygwin
Dominik Riebelingb24e5622011-12-14 21:59:37 +000049else
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020050ifeq ($(findstring MINGW,$(CPPDEFINES)),MINGW)
Dominik Riebelingb24e5622011-12-14 21:59:37 +000051BINARY = $(OUTPUT).exe
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020052COMPILETARGET = mingw
Dominik Riebelingb24e5622011-12-14 21:59:37 +000053else
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020054ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE)
55COMPILETARGET = darwin
Dominik Riebeling703bc402013-04-05 20:03:45 +020056LDOPTS += $(LDOPTS_OSX)
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020057else
58COMPILETARGET = posix
Dominik Riebelingb24e5622011-12-14 21:59:37 +000059endif
60endif
61endif
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020062$(info Compiler creates $(COMPILETARGET) binaries)
Dominik Riebelingb24e5622011-12-14 21:59:37 +000063
Dominik Riebelingc6dcec42012-01-05 22:26:08 +000064# OS X specifics. Needs to consider cross compiling for Windows.
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020065ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE)
Dominik Riebelingc6dcec42012-01-05 22:26:08 +000066# when building libs for OS X build for both i386 and ppc at the same time.
67# This creates fat objects, and ar can only create the archive but not operate
68# on it. As a result the ar call must NOT use the u (update) flag.
Dominik Riebeling33bda052013-06-09 19:43:37 +020069ARCHFLAGS += -arch ppc -arch i386
Dominik Riebelingb056e022011-12-16 18:35:01 +000070# building against SDK 10.4 is not compatible with gcc-4.2 (default on newer Xcode)
71# might need adjustment for older Xcode.
Dominik Riebeling703bc402013-04-05 20:03:45 +020072CC = gcc-4.0
Dominik Riebelingb056e022011-12-16 18:35:01 +000073CFLAGS += -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4
Dominik Riebeling703bc402013-04-05 20:03:45 +020074NATIVECC = gcc-4.0
Dominik Riebelingb056e022011-12-16 18:35:01 +000075endif
76WINDRES = windres
77
Dominik Riebelingea0bfe72013-04-05 19:49:04 +020078BUILD_DIR ?= $(TARGET_DIR)build$(COMPILETARGET)
Dominik Riebelingb24e5622011-12-14 21:59:37 +000079
80ifdef RBARCH
Dominik Riebeling33bda052013-06-09 19:43:37 +020081ARCHFLAGS += -arch $(RBARCH)
Dominik Riebelinge073bc92013-05-11 20:15:53 +020082OBJDIR = $(abspath $(BUILD_DIR)/$(RBARCH))/
83else
84OBJDIR = $(abspath $(BUILD_DIR))/
Dominik Riebelingb24e5622011-12-14 21:59:37 +000085endif
86
87all: $(BINARY)
88
89OBJS := $(patsubst %.c,%.o,$(addprefix $(OBJDIR),$(notdir $(SOURCES))))
90LIBOBJS := $(patsubst %.c,%.o,$(addprefix $(OBJDIR),$(notdir $(LIBSOURCES))))
91
Dominik Riebelinge073bc92013-05-11 20:15:53 +020092# create dependency files. Make sure to use the same prefix as with OBJS!
Dominik Riebeling8a4075d2013-05-11 11:28:08 +020093$(foreach src,$(SOURCES) $(LIBSOURCES),$(eval $(addprefix $(OBJDIR),$(subst .c,.o,$(notdir $(src)))): $(src)))
Dominik Riebelinge073bc92013-05-11 20:15:53 +020094$(foreach src,$(SOURCES) $(LIBSOURCES),$(eval $(addprefix $(OBJDIR),$(subst .c,.d,$(notdir $(src)))): $(src)))
95DEPS = $(addprefix $(OBJDIR),$(subst .c,.d,$(notdir $(SOURCES) $(LIBSOURCES))))
96-include $(DEPS)
Dominik Riebeling8a4075d2013-05-11 11:28:08 +020097
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +000098# additional link dependencies for the standalone executable
99# extra dependencies: libucl
100LIBUCL = libucl$(RBARCH).a
101$(LIBUCL): $(OBJDIR)$(LIBUCL)
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000102
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +0000103$(OBJDIR)$(LIBUCL):
104 $(SILENT)$(MAKE) -C $(TOP)/../tools/ucl/src TARGET_DIR=$(OBJDIR) $@
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000105
106# building the standalone executable
107$(BINARY): $(OBJS) $(EXTRADEPS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
108 @echo LD $@
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +0200109 $(SILENT)$(call mkdir,$(dir $@))
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000110# EXTRADEPS need to be built into OBJDIR.
Dominik Riebeling17a781d2013-11-12 22:51:59 +0100111 $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) -o $(BINARY) \
Dominik Riebeling17a0c832011-12-15 18:44:57 +0000112 $(OBJS) $(addprefix $(OBJDIR),$(EXTRADEPS)) \
Dominik Riebeling17a781d2013-11-12 22:51:59 +0100113 $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) $(LDOPTS)
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000114
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +0000115# common rules
Dominik Riebeling8a4075d2013-05-11 11:28:08 +0200116$(OBJDIR)%.o:
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +0000117 @echo CC $<
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +0200118 $(SILENT)$(call mkdir,$(dir $@))
Dominik Riebeling33bda052013-06-09 19:43:37 +0200119 $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) -c -o $@ $<
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +0000120
121# lib rules
122lib$(OUTPUT)$(RBARCH).a: $(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a
123lib$(OUTPUT)$(RBARCH): $(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a
124
Dominik Riebeling17a0c832011-12-15 18:44:57 +0000125$(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a: $(LIBOBJS) \
126 $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
Dominik Riebeling84205022011-12-16 21:10:29 +0000127# rules to build a DLL. Only works for W32 as target (i.e. MinGW toolchain)
128dll: $(OUTPUT).dll
129$(OUTPUT).dll: $(TARGET_DIR)$(OUTPUT).dll
130$(TARGET_DIR)$(OUTPUT).dll: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
131 @echo DLL $(notdir $@)
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +0200132 $(SILENT)$(call mkdir,$(dir $@))
Dominik Riebeling33bda052013-06-09 19:43:37 +0200133 $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) -shared -o $@ $^ \
Dominik Riebeling84205022011-12-16 21:10:29 +0000134 -Wl,--output-def,$(TARGET_DIR)$(OUTPUT).def
135
Dominik Riebelingc6dcec42012-01-05 22:26:08 +0000136# create lib file from objects
Dominik Riebeling84205022011-12-16 21:10:29 +0000137$(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
Dominik Riebeling6b8f0b02011-12-14 22:00:06 +0000138 @echo AR $(notdir $@)
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +0200139 $(SILENT)$(call mkdir,$(dir $@))
Dominik Riebelingf3a1a332014-01-05 16:53:17 +0100140 $(SILENT)$(call rm,$@)
Dominik Riebelingc6dcec42012-01-05 22:26:08 +0000141 $(SILENT)$(AR) rcs $@ $^
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000142
143clean:
Dominik Riebelingf3a1a332014-01-05 16:53:17 +0100144 $(call rm, $(OBJS) $(OUTPUT) $(TARGET_DIR)lib$(OUTPUT)*.a $(OUTPUT).dmg)
145 $(call rm, $(OUTPUT)-* i386 ppc $(OBJDIR))
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000146
Dominik Riebelinge073bc92013-05-11 20:15:53 +0200147%.d:
148 $(SILENT)$(call mkdir,$(BUILD_DIR))
149 $(SILENT)$(CC) -MG -MM -MT $(subst .d,.o,$@) $(CFLAGS) -o $(BUILD_DIR)/$(notdir $@) $<
150
Dominik Riebeling308f0992012-04-28 12:05:50 +0200151# extra tools
152BIN2C = $(TOP)/tools/bin2c
153$(BIN2C):
154 $(MAKE) -C $(TOP)/tools
155
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000156# OS X specifics
Dominik Riebelingded02d82012-01-06 09:24:38 +0000157$(OUTPUT).dmg: $(OUTPUT)
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000158 @echo DMG $@
Dominik Riebeling4f3fa9a2013-05-11 17:22:44 +0200159 $(SILENT)$(call mkdir,$(OUTPUT)-dmg))
Dominik Riebelingded02d82012-01-06 09:24:38 +0000160 $(SILENT)cp -p $(OUTPUT) $(OUTPUT)-dmg
Dominik Riebelingb24e5622011-12-14 21:59:37 +0000161 $(SILENT)hdiutil create -srcfolder $(OUTPUT)-dmg $@