summaryrefslogtreecommitdiff
path: root/utils/libtools.make
diff options
context:
space:
mode:
Diffstat (limited to 'utils/libtools.make')
-rw-r--r--utils/libtools.make183
1 files changed, 183 insertions, 0 deletions
diff --git a/utils/libtools.make b/utils/libtools.make
new file mode 100644
index 0000000000..3ab150e876
--- /dev/null
+++ b/utils/libtools.make
@@ -0,0 +1,183 @@
1# __________ __ ___.
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)))
24ifeq ($(OS),Windows_NT)
25SHELL = cmd.exe
26mkdir = if not exist $(subst /,\,$(1)) mkdir $(subst /,\,$(1))
27rm = if exist $(subst /,\,$(1)) del /q /s $(subst /,\,$(1))
28else
29mkdir = mkdir -p $(1)
30rm = rm -rf $(1)
31endif
32
33# overwrite for releases
34APPVERSION ?= $(shell $(TOP)/../tools/version.sh $(TOP)/..)
35GCCFLAGS += -DVERSION=\"$(APPVERSION)\"
36TARGET_DIR ?= $(abspath .)/
37
38CC := gcc
39CXX := g++
40# use either CC or CXX to link: this makes sure the compiler knows about its
41# internal dependencies. Use CXX if we have at least one c++ file, since we
42# then need to link the c++ standard library (which CXX does for us).
43ifeq ($(strip $(filter %.cpp,$(SOURCES) $(LIBSOURCES))),)
44LD := $(CC)
45else
46LD := $(CXX)
47endif
48CPPDEFINES := $(shell echo foo | $(CROSS)$(CC) -dM -E -)
49
50BINARY = $(OUTPUT)
51# when building a Windows binary add the correct file suffix
52ifeq ($(findstring CYGWIN,$(CPPDEFINES)),CYGWIN)
53BINARY = $(OUTPUT).exe
54GCCFLAGS += -mno-cygwin
55COMPILETARGET = cygwin
56else
57ifeq ($(findstring MINGW,$(CPPDEFINES)),MINGW)
58BINARY = $(OUTPUT).exe
59COMPILETARGET = mingw
60# use POSIX/C99 printf on windows
61GCCFLAGS += -D__USE_MINGW_ANSI_STDIO=1
62else
63ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE)
64COMPILETARGET = darwin
65LDOPTS += $(LDOPTS_OSX)
66else
67COMPILETARGET = posix
68endif
69endif
70endif
71$(info Compiler creates $(COMPILETARGET) binaries)
72
73# OS X specifics. Needs to consider cross compiling for Windows.
74ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE)
75# When building for 10.4+ we need to use gcc. Otherwise clang is used, so use
76# that to determine if we need to set arch and isysroot.
77ifeq ($(findstring __clang__,$(CPPDEFINES)),__clang__)
78GCCFLAGS += -mmacosx-version-min=10.5
79ifneq ($(ISYSROOT),)
80GCCFLAGS += -isysroot $(ISYSROOT)
81endif
82else
83# when building libs for OS X 10.4+ build for both i386 and ppc at the same time.
84# This creates fat objects, and ar can only create the archive but not operate
85# on it. As a result the ar call must NOT use the u (update) flag.
86ARCHFLAGS += -arch ppc -arch i386
87# building against SDK 10.4 is not compatible with gcc-4.2 (default on newer Xcode)
88# might need adjustment for older Xcode.
89CC = gcc-4.0
90GCCFLAGS += -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4
91endif
92endif
93
94BUILD_DIR ?= $(TARGET_DIR)build$(COMPILETARGET)
95OBJDIR = $(abspath $(BUILD_DIR))/
96
97all: $(BINARY)
98
99OBJS := $(addsuffix .o,$(addprefix $(OBJDIR),$(notdir $(SOURCES))))
100LIBOBJS := $(addsuffix .o,$(addprefix $(OBJDIR),$(notdir $(LIBSOURCES))))
101
102# create dependency files. Make sure to use the same prefix as with OBJS!
103$(foreach src,$(SOURCES) $(LIBSOURCES),$(eval $(addprefix $(OBJDIR),$(notdir $(src).o)): $(src)))
104$(foreach src,$(SOURCES) $(LIBSOURCES),$(eval $(addprefix $(OBJDIR),$(notdir $(src).d)): $(src)))
105DEPS = $(addprefix $(OBJDIR),$(addsuffix .d,$(notdir $(SOURCES) $(LIBSOURCES))))
106-include $(DEPS)
107
108# additional link dependencies for the standalone executable
109# extra dependencies: libucl
110LIBUCL = libucl.a
111$(LIBUCL): $(OBJDIR)$(LIBUCL)
112
113$(OBJDIR)$(LIBUCL):
114 $(SILENT)$(MAKE) -C $(TOP)/../tools/ucl/src TARGET_DIR=$(OBJDIR) CC=$(CC) $@
115
116LIBBZIP2 = libbz2.a
117$(LIBBZIP2): $(OBJDIR)$(LIBBZIP2)
118$(OBJDIR)$(LIBBZIP2):
119 $(SILENT)$(MAKE) -C $(TOP)/bzip2 TARGET_DIR=$(OBJDIR) CC=$(CC) $@
120
121LIBMICROTAR = libmicrotar.a
122$(LIBMICROTAR): $(OBJDIR)$(LIBMICROTAR)
123
124$(OBJDIR)$(LIBMICROTAR):
125 $(SILENT)$(MAKE) -C $(TOP)/../lib/microtar/src TARGET_DIR=$(OBJDIR) CC=$(CC) $@
126
127# building the standalone executable
128$(BINARY): $(OBJS) $(EXTRADEPS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) $(TARGET_DIR)lib$(OUTPUT).a
129 $(info LD $@)
130 $(SILENT)$(call mkdir,$(dir $@))
131# EXTRADEPS need to be built into OBJDIR.
132 $(SILENT)$(CROSS)$(LD) $(ARCHFLAGS) $(LDFLAGS) -o $(BINARY) \
133 $(OBJS) $(addprefix $(OBJDIR),$(EXTRADEPS)) \
134 $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) lib$(OUTPUT).a $(addprefix $(OBJDIR),$(EXTRADEPS)) $(LDOPTS)
135
136# common rules
137$(OBJDIR)%.c.o:
138 $(info CC $<)
139 $(SILENT)$(call mkdir,$(dir $@))
140 $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(GCCFLAGS) $(CFLAGS) -MMD -c -o $@ $<
141
142$(OBJDIR)%.cpp.o:
143 $(info CXX $<)
144 $(SILENT)$(call mkdir,$(dir $@))
145 $(SILENT)$(CROSS)$(CXX) $(ARCHFLAGS) $(GCCFLAGS) $(CXXFLAGS) -MMD -c -o $@ $<
146
147# lib rules
148lib$(OUTPUT).a: $(TARGET_DIR)lib$(OUTPUT).a
149lib$(OUTPUT): $(TARGET_DIR)lib$(OUTPUT).a
150
151$(TARGET_DIR)lib$(OUTPUT).a: $(LIBOBJS) \
152 $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
153# rules to build a DLL. Only works for W32 as target (i.e. MinGW toolchain)
154dll: $(OUTPUT).dll
155$(OUTPUT).dll: $(TARGET_DIR)$(OUTPUT).dll
156$(TARGET_DIR)$(OUTPUT).dll: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
157 $(info DLL $(notdir $@))
158 $(SILENT)$(call mkdir,$(dir $@))
159 $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(GCCFLAGS) -shared -o $@ $^ \
160 -Wl,--output-def,$(TARGET_DIR)$(OUTPUT).def
161
162# create lib file from objects
163$(TARGET_DIR)lib$(OUTPUT).a: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
164 $(info AR $(notdir $@))
165 $(SILENT)$(call mkdir,$(dir $@))
166 $(SILENT)$(call rm,$@)
167 $(SILENT)$(CROSS)$(AR) rcs $@ $^
168
169clean:
170 $(call rm, $(OBJS) $(OUTPUT) $(TARGET_DIR)lib$(OUTPUT)*.a $(OUTPUT).dmg)
171 $(call rm, $(OUTPUT)-* i386 ppc $(OBJDIR))
172
173# extra tools
174BIN2C = $(TOP)/tools/bin2c
175$(BIN2C):
176 $(MAKE) -C $(TOP)/tools
177
178# OS X specifics
179$(OUTPUT).dmg: $(OUTPUT)
180 $(info DMG $@)
181 $(SILENT)$(call mkdir,"$(OUTPUT)-$(APPVERSION)")
182 $(SILENT)cp -p $(OUTPUT) "$(OUTPUT)-$(APPVERSION)"
183 $(SILENT)hdiutil create -srcfolder "$(OUTPUT)-$(APPVERSION)" $@