Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 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))) |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 24 | ifeq ($(OS),Windows_NT) |
| 25 | mkdir = if not exist $(subst /,\,$(1)) mkdir $(subst /,\,$(1)) |
Dominik Riebeling | 17a1867 | 2014-01-05 17:08:50 +0100 | [diff] [blame] | 26 | rm = if exist $(subst /,\,$(1)) del /q /s $(subst /,\,$(1)) |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 27 | else |
| 28 | mkdir = mkdir -p $(1) |
Dominik Riebeling | f3a1a33 | 2014-01-05 16:53:17 +0100 | [diff] [blame] | 29 | rm = rm -rf $(1) |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 30 | endif |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 31 | |
| 32 | # overwrite for releases |
Dominik Riebeling | 45cda1f | 2013-01-01 10:51:11 +0100 | [diff] [blame] | 33 | APPVERSION ?= $(shell $(TOP)/../tools/version.sh $(TOP)/..) |
Dominik Riebeling | 3ee7972 | 2016-12-16 21:50:36 +0100 | [diff] [blame] | 34 | GCCFLAGS += -DVERSION=\"$(APPVERSION)\" |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 35 | TARGET_DIR ?= $(abspath .)/ |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 36 | |
Dominik Riebeling | 78cb7f0 | 2016-12-16 19:29:52 +0100 | [diff] [blame] | 37 | CC := gcc |
| 38 | CXX := g++ |
Dominik Riebeling | 3ee7972 | 2016-12-16 21:50:36 +0100 | [diff] [blame] | 39 | # use either CC or CXX to link: this makes sure the compiler knows about its |
| 40 | # internal dependencies. Use CXX if we have at least one c++ file, since we |
| 41 | # then need to link the c++ standard library (which CXX does for us). |
| 42 | ifeq ($(strip $(filter %.cpp,$(SOURCES) $(LIBSOURCES))),) |
| 43 | LD := $(CC) |
| 44 | else |
| 45 | LD := $(CXX) |
| 46 | endif |
Dominik Riebeling | 8568a8d | 2013-05-10 17:45:37 +0200 | [diff] [blame] | 47 | CPPDEFINES := $(shell echo foo | $(CROSS)$(CC) -dM -E -) |
Rafaël Carré | db3afb0 | 2012-02-05 14:03:10 -0500 | [diff] [blame] | 48 | |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 49 | BINARY = $(OUTPUT) |
| 50 | # when building a Windows binary add the correct file suffix |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 51 | ifeq ($(findstring CYGWIN,$(CPPDEFINES)),CYGWIN) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 52 | BINARY = $(OUTPUT).exe |
Dominik Riebeling | 3ee7972 | 2016-12-16 21:50:36 +0100 | [diff] [blame] | 53 | GCCFLAGS += -mno-cygwin |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 54 | COMPILETARGET = cygwin |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 55 | else |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 56 | ifeq ($(findstring MINGW,$(CPPDEFINES)),MINGW) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 57 | BINARY = $(OUTPUT).exe |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 58 | COMPILETARGET = mingw |
Dominik Riebeling | 6f4beaa | 2016-12-16 19:39:07 +0100 | [diff] [blame] | 59 | # use POSIX/C99 printf on windows |
Dominik Riebeling | 3ee7972 | 2016-12-16 21:50:36 +0100 | [diff] [blame] | 60 | GCCFLAGS += -D__USE_MINGW_ANSI_STDIO=1 |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 61 | else |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 62 | ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE) |
| 63 | COMPILETARGET = darwin |
Dominik Riebeling | 703bc40 | 2013-04-05 20:03:45 +0200 | [diff] [blame] | 64 | LDOPTS += $(LDOPTS_OSX) |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 65 | else |
| 66 | COMPILETARGET = posix |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 67 | endif |
| 68 | endif |
| 69 | endif |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 70 | $(info Compiler creates $(COMPILETARGET) binaries) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 71 | |
Dominik Riebeling | c6dcec4 | 2012-01-05 22:26:08 +0000 | [diff] [blame] | 72 | # OS X specifics. Needs to consider cross compiling for Windows. |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 73 | ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE) |
Dominik Riebeling | 258e4ad | 2014-03-20 22:22:17 +0100 | [diff] [blame] | 74 | # When building for 10.4+ we need to use gcc. Otherwise clang is used, so use |
| 75 | # that to determine if we need to set arch and isysroot. |
| 76 | ifeq ($(findstring __clang__,$(CPPDEFINES)),__clang__) |
Dominik Riebeling | 3ee7972 | 2016-12-16 21:50:36 +0100 | [diff] [blame] | 77 | GCCFLAGS += -mmacosx-version-min=10.5 |
Dominik Riebeling | 258e4ad | 2014-03-20 22:22:17 +0100 | [diff] [blame] | 78 | else |
| 79 | # when building libs for OS X 10.4+ build for both i386 and ppc at the same time. |
Dominik Riebeling | c6dcec4 | 2012-01-05 22:26:08 +0000 | [diff] [blame] | 80 | # This creates fat objects, and ar can only create the archive but not operate |
| 81 | # on it. As a result the ar call must NOT use the u (update) flag. |
Dominik Riebeling | 33bda05 | 2013-06-09 19:43:37 +0200 | [diff] [blame] | 82 | ARCHFLAGS += -arch ppc -arch i386 |
Dominik Riebeling | b056e02 | 2011-12-16 18:35:01 +0000 | [diff] [blame] | 83 | # building against SDK 10.4 is not compatible with gcc-4.2 (default on newer Xcode) |
| 84 | # might need adjustment for older Xcode. |
Dominik Riebeling | 703bc40 | 2013-04-05 20:03:45 +0200 | [diff] [blame] | 85 | CC = gcc-4.0 |
Dominik Riebeling | 3ee7972 | 2016-12-16 21:50:36 +0100 | [diff] [blame] | 86 | GCCFLAGS += -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4 |
Dominik Riebeling | b056e02 | 2011-12-16 18:35:01 +0000 | [diff] [blame] | 87 | endif |
Dominik Riebeling | 258e4ad | 2014-03-20 22:22:17 +0100 | [diff] [blame] | 88 | endif |
Dominik Riebeling | b056e02 | 2011-12-16 18:35:01 +0000 | [diff] [blame] | 89 | |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 90 | BUILD_DIR ?= $(TARGET_DIR)build$(COMPILETARGET) |
Dominik Riebeling | e073bc9 | 2013-05-11 20:15:53 +0200 | [diff] [blame] | 91 | OBJDIR = $(abspath $(BUILD_DIR))/ |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 92 | |
| 93 | all: $(BINARY) |
| 94 | |
Dominik Riebeling | 78cb7f0 | 2016-12-16 19:29:52 +0100 | [diff] [blame] | 95 | OBJS := $(addsuffix .o,$(addprefix $(OBJDIR),$(notdir $(SOURCES)))) |
| 96 | LIBOBJS := $(addsuffix .o,$(addprefix $(OBJDIR),$(notdir $(LIBSOURCES)))) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 97 | |
Dominik Riebeling | e073bc9 | 2013-05-11 20:15:53 +0200 | [diff] [blame] | 98 | # create dependency files. Make sure to use the same prefix as with OBJS! |
Dominik Riebeling | 78cb7f0 | 2016-12-16 19:29:52 +0100 | [diff] [blame] | 99 | $(foreach src,$(SOURCES) $(LIBSOURCES),$(eval $(addprefix $(OBJDIR),$(notdir $(src).o)): $(src))) |
| 100 | $(foreach src,$(SOURCES) $(LIBSOURCES),$(eval $(addprefix $(OBJDIR),$(notdir $(src).d)): $(src))) |
| 101 | DEPS = $(addprefix $(OBJDIR),$(addsuffix .d,$(notdir $(SOURCES) $(LIBSOURCES)))) |
Dominik Riebeling | e073bc9 | 2013-05-11 20:15:53 +0200 | [diff] [blame] | 102 | -include $(DEPS) |
Dominik Riebeling | 8a4075d | 2013-05-11 11:28:08 +0200 | [diff] [blame] | 103 | |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 104 | # additional link dependencies for the standalone executable |
| 105 | # extra dependencies: libucl |
Dominik Riebeling | 6f4beaa | 2016-12-16 19:39:07 +0100 | [diff] [blame] | 106 | LIBUCL = libucl.a |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 107 | $(LIBUCL): $(OBJDIR)$(LIBUCL) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 108 | |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 109 | $(OBJDIR)$(LIBUCL): |
Dominik Riebeling | 78cb7f0 | 2016-12-16 19:29:52 +0100 | [diff] [blame] | 110 | $(SILENT)$(MAKE) -C $(TOP)/../tools/ucl/src TARGET_DIR=$(OBJDIR) CC=$(CC) $@ |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 111 | |
| 112 | # building the standalone executable |
Dominik Riebeling | 6f4beaa | 2016-12-16 19:39:07 +0100 | [diff] [blame] | 113 | $(BINARY): $(OBJS) $(EXTRADEPS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) $(TARGET_DIR)lib$(OUTPUT).a |
Dominik Riebeling | 78cb7f0 | 2016-12-16 19:29:52 +0100 | [diff] [blame] | 114 | $(info LD $@) |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 115 | $(SILENT)$(call mkdir,$(dir $@)) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 116 | # EXTRADEPS need to be built into OBJDIR. |
Dominik Riebeling | 3ee7972 | 2016-12-16 21:50:36 +0100 | [diff] [blame] | 117 | $(SILENT)$(CROSS)$(LD) $(ARCHFLAGS) $(LDFLAGS) -o $(BINARY) \ |
Dominik Riebeling | 17a0c83 | 2011-12-15 18:44:57 +0000 | [diff] [blame] | 118 | $(OBJS) $(addprefix $(OBJDIR),$(EXTRADEPS)) \ |
Dominik Riebeling | 6f4beaa | 2016-12-16 19:39:07 +0100 | [diff] [blame] | 119 | $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) lib$(OUTPUT).a $(addprefix $(OBJDIR),$(EXTRADEPS)) $(LDOPTS) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 120 | |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 121 | # common rules |
Dominik Riebeling | 78cb7f0 | 2016-12-16 19:29:52 +0100 | [diff] [blame] | 122 | $(OBJDIR)%.c.o: |
| 123 | $(info CC $<) |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 124 | $(SILENT)$(call mkdir,$(dir $@)) |
Dominik Riebeling | 3ee7972 | 2016-12-16 21:50:36 +0100 | [diff] [blame] | 125 | $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(GCCFLAGS) $(CFLAGS) -MMD -c -o $@ $< |
Dominik Riebeling | 78cb7f0 | 2016-12-16 19:29:52 +0100 | [diff] [blame] | 126 | |
| 127 | $(OBJDIR)%.cpp.o: |
| 128 | $(info CXX $<) |
| 129 | $(SILENT)$(call mkdir,$(dir $@)) |
Dominik Riebeling | 3ee7972 | 2016-12-16 21:50:36 +0100 | [diff] [blame] | 130 | $(SILENT)$(CROSS)$(CXX) $(ARCHFLAGS) $(GCCFLAGS) $(CXXFLAGS) -MMD -c -o $@ $< |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 131 | |
| 132 | # lib rules |
Dominik Riebeling | 6f4beaa | 2016-12-16 19:39:07 +0100 | [diff] [blame] | 133 | lib$(OUTPUT).a: $(TARGET_DIR)lib$(OUTPUT).a |
| 134 | lib$(OUTPUT): $(TARGET_DIR)lib$(OUTPUT).a |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 135 | |
Dominik Riebeling | 6f4beaa | 2016-12-16 19:39:07 +0100 | [diff] [blame] | 136 | $(TARGET_DIR)lib$(OUTPUT).a: $(LIBOBJS) \ |
Dominik Riebeling | 17a0c83 | 2011-12-15 18:44:57 +0000 | [diff] [blame] | 137 | $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) |
Dominik Riebeling | 8420502 | 2011-12-16 21:10:29 +0000 | [diff] [blame] | 138 | # rules to build a DLL. Only works for W32 as target (i.e. MinGW toolchain) |
| 139 | dll: $(OUTPUT).dll |
| 140 | $(OUTPUT).dll: $(TARGET_DIR)$(OUTPUT).dll |
| 141 | $(TARGET_DIR)$(OUTPUT).dll: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) |
Dominik Riebeling | 78cb7f0 | 2016-12-16 19:29:52 +0100 | [diff] [blame] | 142 | $(info DLL $(notdir $@)) |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 143 | $(SILENT)$(call mkdir,$(dir $@)) |
Dominik Riebeling | 3ee7972 | 2016-12-16 21:50:36 +0100 | [diff] [blame] | 144 | $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(GCCFLAGS) -shared -o $@ $^ \ |
Dominik Riebeling | 8420502 | 2011-12-16 21:10:29 +0000 | [diff] [blame] | 145 | -Wl,--output-def,$(TARGET_DIR)$(OUTPUT).def |
| 146 | |
Dominik Riebeling | c6dcec4 | 2012-01-05 22:26:08 +0000 | [diff] [blame] | 147 | # create lib file from objects |
Dominik Riebeling | 6f4beaa | 2016-12-16 19:39:07 +0100 | [diff] [blame] | 148 | $(TARGET_DIR)lib$(OUTPUT).a: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) |
Dominik Riebeling | 78cb7f0 | 2016-12-16 19:29:52 +0100 | [diff] [blame] | 149 | $(info AR $(notdir $@)) |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 150 | $(SILENT)$(call mkdir,$(dir $@)) |
Dominik Riebeling | f3a1a33 | 2014-01-05 16:53:17 +0100 | [diff] [blame] | 151 | $(SILENT)$(call rm,$@) |
Dominik Riebeling | 78cb7f0 | 2016-12-16 19:29:52 +0100 | [diff] [blame] | 152 | $(SILENT)$(CROSS)$(AR) rcs $@ $^ |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 153 | |
| 154 | clean: |
Dominik Riebeling | f3a1a33 | 2014-01-05 16:53:17 +0100 | [diff] [blame] | 155 | $(call rm, $(OBJS) $(OUTPUT) $(TARGET_DIR)lib$(OUTPUT)*.a $(OUTPUT).dmg) |
| 156 | $(call rm, $(OUTPUT)-* i386 ppc $(OBJDIR)) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 157 | |
Dominik Riebeling | 308f099 | 2012-04-28 12:05:50 +0200 | [diff] [blame] | 158 | # extra tools |
| 159 | BIN2C = $(TOP)/tools/bin2c |
| 160 | $(BIN2C): |
| 161 | $(MAKE) -C $(TOP)/tools |
| 162 | |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 163 | # OS X specifics |
Dominik Riebeling | ded02d8 | 2012-01-06 09:24:38 +0000 | [diff] [blame] | 164 | $(OUTPUT).dmg: $(OUTPUT) |
Dominik Riebeling | 78cb7f0 | 2016-12-16 19:29:52 +0100 | [diff] [blame] | 165 | $(info DMG $@) |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 166 | $(SILENT)$(call mkdir,$(OUTPUT)-dmg)) |
Dominik Riebeling | ded02d8 | 2012-01-06 09:24:38 +0000 | [diff] [blame] | 167 | $(SILENT)cp -p $(OUTPUT) $(OUTPUT)-dmg |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 168 | $(SILENT)hdiutil create -srcfolder $(OUTPUT)-dmg $@ |