diff options
Diffstat (limited to 'utils/libtools.make')
-rw-r--r-- | utils/libtools.make | 183 |
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 | # | ||
18 | ifndef V | ||
19 | SILENT = @ | ||
20 | endif | ||
21 | |||
22 | # Get directory this Makefile is in for relative paths. | ||
23 | TOP := $(dir $(lastword $(MAKEFILE_LIST))) | ||
24 | ifeq ($(OS),Windows_NT) | ||
25 | SHELL = cmd.exe | ||
26 | mkdir = if not exist $(subst /,\,$(1)) mkdir $(subst /,\,$(1)) | ||
27 | rm = if exist $(subst /,\,$(1)) del /q /s $(subst /,\,$(1)) | ||
28 | else | ||
29 | mkdir = mkdir -p $(1) | ||
30 | rm = rm -rf $(1) | ||
31 | endif | ||
32 | |||
33 | # overwrite for releases | ||
34 | APPVERSION ?= $(shell $(TOP)/../tools/version.sh $(TOP)/..) | ||
35 | GCCFLAGS += -DVERSION=\"$(APPVERSION)\" | ||
36 | TARGET_DIR ?= $(abspath .)/ | ||
37 | |||
38 | CC := gcc | ||
39 | CXX := 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). | ||
43 | ifeq ($(strip $(filter %.cpp,$(SOURCES) $(LIBSOURCES))),) | ||
44 | LD := $(CC) | ||
45 | else | ||
46 | LD := $(CXX) | ||
47 | endif | ||
48 | CPPDEFINES := $(shell echo foo | $(CROSS)$(CC) -dM -E -) | ||
49 | |||
50 | BINARY = $(OUTPUT) | ||
51 | # when building a Windows binary add the correct file suffix | ||
52 | ifeq ($(findstring CYGWIN,$(CPPDEFINES)),CYGWIN) | ||
53 | BINARY = $(OUTPUT).exe | ||
54 | GCCFLAGS += -mno-cygwin | ||
55 | COMPILETARGET = cygwin | ||
56 | else | ||
57 | ifeq ($(findstring MINGW,$(CPPDEFINES)),MINGW) | ||
58 | BINARY = $(OUTPUT).exe | ||
59 | COMPILETARGET = mingw | ||
60 | # use POSIX/C99 printf on windows | ||
61 | GCCFLAGS += -D__USE_MINGW_ANSI_STDIO=1 | ||
62 | else | ||
63 | ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE) | ||
64 | COMPILETARGET = darwin | ||
65 | LDOPTS += $(LDOPTS_OSX) | ||
66 | else | ||
67 | COMPILETARGET = posix | ||
68 | endif | ||
69 | endif | ||
70 | endif | ||
71 | $(info Compiler creates $(COMPILETARGET) binaries) | ||
72 | |||
73 | # OS X specifics. Needs to consider cross compiling for Windows. | ||
74 | ifeq ($(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. | ||
77 | ifeq ($(findstring __clang__,$(CPPDEFINES)),__clang__) | ||
78 | GCCFLAGS += -mmacosx-version-min=10.5 | ||
79 | ifneq ($(ISYSROOT),) | ||
80 | GCCFLAGS += -isysroot $(ISYSROOT) | ||
81 | endif | ||
82 | else | ||
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. | ||
86 | ARCHFLAGS += -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. | ||
89 | CC = gcc-4.0 | ||
90 | GCCFLAGS += -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4 | ||
91 | endif | ||
92 | endif | ||
93 | |||
94 | BUILD_DIR ?= $(TARGET_DIR)build$(COMPILETARGET) | ||
95 | OBJDIR = $(abspath $(BUILD_DIR))/ | ||
96 | |||
97 | all: $(BINARY) | ||
98 | |||
99 | OBJS := $(addsuffix .o,$(addprefix $(OBJDIR),$(notdir $(SOURCES)))) | ||
100 | LIBOBJS := $(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))) | ||
105 | DEPS = $(addprefix $(OBJDIR),$(addsuffix .d,$(notdir $(SOURCES) $(LIBSOURCES)))) | ||
106 | -include $(DEPS) | ||
107 | |||
108 | # additional link dependencies for the standalone executable | ||
109 | # extra dependencies: libucl | ||
110 | LIBUCL = libucl.a | ||
111 | $(LIBUCL): $(OBJDIR)$(LIBUCL) | ||
112 | |||
113 | $(OBJDIR)$(LIBUCL): | ||
114 | $(SILENT)$(MAKE) -C $(TOP)/../tools/ucl/src TARGET_DIR=$(OBJDIR) CC=$(CC) $@ | ||
115 | |||
116 | LIBBZIP2 = libbz2.a | ||
117 | $(LIBBZIP2): $(OBJDIR)$(LIBBZIP2) | ||
118 | $(OBJDIR)$(LIBBZIP2): | ||
119 | $(SILENT)$(MAKE) -C $(TOP)/bzip2 TARGET_DIR=$(OBJDIR) CC=$(CC) $@ | ||
120 | |||
121 | LIBMICROTAR = 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 | ||
148 | lib$(OUTPUT).a: $(TARGET_DIR)lib$(OUTPUT).a | ||
149 | lib$(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) | ||
154 | dll: $(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 | |||
169 | clean: | ||
170 | $(call rm, $(OBJS) $(OUTPUT) $(TARGET_DIR)lib$(OUTPUT)*.a $(OUTPUT).dmg) | ||
171 | $(call rm, $(OUTPUT)-* i386 ppc $(OBJDIR)) | ||
172 | |||
173 | # extra tools | ||
174 | BIN2C = $(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)" $@ | ||