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 | 8568a8d | 2013-05-10 17:45:37 +0200 | [diff] [blame] | 34 | CFLAGS += -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 | 460e959 | 2013-05-03 23:50:12 +0200 | [diff] [blame] | 37 | NATIVECC ?= gcc |
| 38 | CC ?= gcc |
Dominik Riebeling | 8568a8d | 2013-05-10 17:45:37 +0200 | [diff] [blame] | 39 | CPPDEFINES := $(shell echo foo | $(CROSS)$(CC) -dM -E -) |
Rafaël Carré | db3afb0 | 2012-02-05 14:03:10 -0500 | [diff] [blame] | 40 | # use POSIX/C99 printf on windows |
| 41 | CFLAGS += -D__USE_MINGW_ANSI_STDIO=1 |
| 42 | |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 43 | BINARY = $(OUTPUT) |
| 44 | # when building a Windows binary add the correct file suffix |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 45 | ifeq ($(findstring CYGWIN,$(CPPDEFINES)),CYGWIN) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 46 | BINARY = $(OUTPUT).exe |
| 47 | CFLAGS+=-mno-cygwin |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 48 | COMPILETARGET = cygwin |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 49 | else |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 50 | ifeq ($(findstring MINGW,$(CPPDEFINES)),MINGW) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 51 | BINARY = $(OUTPUT).exe |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 52 | COMPILETARGET = mingw |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 53 | else |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 54 | ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE) |
| 55 | COMPILETARGET = darwin |
Dominik Riebeling | 703bc40 | 2013-04-05 20:03:45 +0200 | [diff] [blame] | 56 | LDOPTS += $(LDOPTS_OSX) |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 57 | else |
| 58 | COMPILETARGET = posix |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 59 | endif |
| 60 | endif |
| 61 | endif |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 62 | $(info Compiler creates $(COMPILETARGET) binaries) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 63 | |
Dominik Riebeling | c6dcec4 | 2012-01-05 22:26:08 +0000 | [diff] [blame] | 64 | # OS X specifics. Needs to consider cross compiling for Windows. |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 65 | ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE) |
Dominik Riebeling | c6dcec4 | 2012-01-05 22:26:08 +0000 | [diff] [blame] | 66 | # 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 Riebeling | 33bda05 | 2013-06-09 19:43:37 +0200 | [diff] [blame] | 69 | ARCHFLAGS += -arch ppc -arch i386 |
Dominik Riebeling | b056e02 | 2011-12-16 18:35:01 +0000 | [diff] [blame] | 70 | # building against SDK 10.4 is not compatible with gcc-4.2 (default on newer Xcode) |
| 71 | # might need adjustment for older Xcode. |
Dominik Riebeling | 703bc40 | 2013-04-05 20:03:45 +0200 | [diff] [blame] | 72 | CC = gcc-4.0 |
Dominik Riebeling | b056e02 | 2011-12-16 18:35:01 +0000 | [diff] [blame] | 73 | CFLAGS += -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4 |
Dominik Riebeling | 703bc40 | 2013-04-05 20:03:45 +0200 | [diff] [blame] | 74 | NATIVECC = gcc-4.0 |
Dominik Riebeling | b056e02 | 2011-12-16 18:35:01 +0000 | [diff] [blame] | 75 | endif |
| 76 | WINDRES = windres |
| 77 | |
Dominik Riebeling | ea0bfe7 | 2013-04-05 19:49:04 +0200 | [diff] [blame] | 78 | BUILD_DIR ?= $(TARGET_DIR)build$(COMPILETARGET) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 79 | |
| 80 | ifdef RBARCH |
Dominik Riebeling | 33bda05 | 2013-06-09 19:43:37 +0200 | [diff] [blame] | 81 | ARCHFLAGS += -arch $(RBARCH) |
Dominik Riebeling | e073bc9 | 2013-05-11 20:15:53 +0200 | [diff] [blame] | 82 | OBJDIR = $(abspath $(BUILD_DIR)/$(RBARCH))/ |
| 83 | else |
| 84 | OBJDIR = $(abspath $(BUILD_DIR))/ |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 85 | endif |
| 86 | |
| 87 | all: $(BINARY) |
| 88 | |
| 89 | OBJS := $(patsubst %.c,%.o,$(addprefix $(OBJDIR),$(notdir $(SOURCES)))) |
| 90 | LIBOBJS := $(patsubst %.c,%.o,$(addprefix $(OBJDIR),$(notdir $(LIBSOURCES)))) |
| 91 | |
Dominik Riebeling | e073bc9 | 2013-05-11 20:15:53 +0200 | [diff] [blame] | 92 | # create dependency files. Make sure to use the same prefix as with OBJS! |
Dominik Riebeling | 8a4075d | 2013-05-11 11:28:08 +0200 | [diff] [blame] | 93 | $(foreach src,$(SOURCES) $(LIBSOURCES),$(eval $(addprefix $(OBJDIR),$(subst .c,.o,$(notdir $(src)))): $(src))) |
Dominik Riebeling | e073bc9 | 2013-05-11 20:15:53 +0200 | [diff] [blame] | 94 | $(foreach src,$(SOURCES) $(LIBSOURCES),$(eval $(addprefix $(OBJDIR),$(subst .c,.d,$(notdir $(src)))): $(src))) |
| 95 | DEPS = $(addprefix $(OBJDIR),$(subst .c,.d,$(notdir $(SOURCES) $(LIBSOURCES)))) |
| 96 | -include $(DEPS) |
Dominik Riebeling | 8a4075d | 2013-05-11 11:28:08 +0200 | [diff] [blame] | 97 | |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 98 | # additional link dependencies for the standalone executable |
| 99 | # extra dependencies: libucl |
| 100 | LIBUCL = libucl$(RBARCH).a |
| 101 | $(LIBUCL): $(OBJDIR)$(LIBUCL) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 102 | |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 103 | $(OBJDIR)$(LIBUCL): |
| 104 | $(SILENT)$(MAKE) -C $(TOP)/../tools/ucl/src TARGET_DIR=$(OBJDIR) $@ |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 105 | |
| 106 | # building the standalone executable |
| 107 | $(BINARY): $(OBJS) $(EXTRADEPS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) |
| 108 | @echo LD $@ |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 109 | $(SILENT)$(call mkdir,$(dir $@)) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 110 | # EXTRADEPS need to be built into OBJDIR. |
Dominik Riebeling | 17a781d | 2013-11-12 22:51:59 +0100 | [diff] [blame] | 111 | $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) -o $(BINARY) \ |
Dominik Riebeling | 17a0c83 | 2011-12-15 18:44:57 +0000 | [diff] [blame] | 112 | $(OBJS) $(addprefix $(OBJDIR),$(EXTRADEPS)) \ |
Dominik Riebeling | 17a781d | 2013-11-12 22:51:59 +0100 | [diff] [blame] | 113 | $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) $(LDOPTS) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 114 | |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 115 | # common rules |
Dominik Riebeling | 8a4075d | 2013-05-11 11:28:08 +0200 | [diff] [blame] | 116 | $(OBJDIR)%.o: |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 117 | @echo CC $< |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 118 | $(SILENT)$(call mkdir,$(dir $@)) |
Dominik Riebeling | 33bda05 | 2013-06-09 19:43:37 +0200 | [diff] [blame] | 119 | $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) -c -o $@ $< |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 120 | |
| 121 | # lib rules |
| 122 | lib$(OUTPUT)$(RBARCH).a: $(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a |
| 123 | lib$(OUTPUT)$(RBARCH): $(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a |
| 124 | |
Dominik Riebeling | 17a0c83 | 2011-12-15 18:44:57 +0000 | [diff] [blame] | 125 | $(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a: $(LIBOBJS) \ |
| 126 | $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) |
Dominik Riebeling | 8420502 | 2011-12-16 21:10:29 +0000 | [diff] [blame] | 127 | # rules to build a DLL. Only works for W32 as target (i.e. MinGW toolchain) |
| 128 | dll: $(OUTPUT).dll |
| 129 | $(OUTPUT).dll: $(TARGET_DIR)$(OUTPUT).dll |
| 130 | $(TARGET_DIR)$(OUTPUT).dll: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) |
| 131 | @echo DLL $(notdir $@) |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 132 | $(SILENT)$(call mkdir,$(dir $@)) |
Dominik Riebeling | 33bda05 | 2013-06-09 19:43:37 +0200 | [diff] [blame] | 133 | $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) -shared -o $@ $^ \ |
Dominik Riebeling | 8420502 | 2011-12-16 21:10:29 +0000 | [diff] [blame] | 134 | -Wl,--output-def,$(TARGET_DIR)$(OUTPUT).def |
| 135 | |
Dominik Riebeling | c6dcec4 | 2012-01-05 22:26:08 +0000 | [diff] [blame] | 136 | # create lib file from objects |
Dominik Riebeling | 8420502 | 2011-12-16 21:10:29 +0000 | [diff] [blame] | 137 | $(TARGET_DIR)lib$(OUTPUT)$(RBARCH).a: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) |
Dominik Riebeling | 6b8f0b0 | 2011-12-14 22:00:06 +0000 | [diff] [blame] | 138 | @echo AR $(notdir $@) |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 139 | $(SILENT)$(call mkdir,$(dir $@)) |
Dominik Riebeling | f3a1a33 | 2014-01-05 16:53:17 +0100 | [diff] [blame] | 140 | $(SILENT)$(call rm,$@) |
Dominik Riebeling | c6dcec4 | 2012-01-05 22:26:08 +0000 | [diff] [blame] | 141 | $(SILENT)$(AR) rcs $@ $^ |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 142 | |
| 143 | clean: |
Dominik Riebeling | f3a1a33 | 2014-01-05 16:53:17 +0100 | [diff] [blame] | 144 | $(call rm, $(OBJS) $(OUTPUT) $(TARGET_DIR)lib$(OUTPUT)*.a $(OUTPUT).dmg) |
| 145 | $(call rm, $(OUTPUT)-* i386 ppc $(OBJDIR)) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 146 | |
Dominik Riebeling | e073bc9 | 2013-05-11 20:15:53 +0200 | [diff] [blame] | 147 | %.d: |
| 148 | $(SILENT)$(call mkdir,$(BUILD_DIR)) |
| 149 | $(SILENT)$(CC) -MG -MM -MT $(subst .d,.o,$@) $(CFLAGS) -o $(BUILD_DIR)/$(notdir $@) $< |
| 150 | |
Dominik Riebeling | 308f099 | 2012-04-28 12:05:50 +0200 | [diff] [blame] | 151 | # extra tools |
| 152 | BIN2C = $(TOP)/tools/bin2c |
| 153 | $(BIN2C): |
| 154 | $(MAKE) -C $(TOP)/tools |
| 155 | |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 156 | # OS X specifics |
Dominik Riebeling | ded02d8 | 2012-01-06 09:24:38 +0000 | [diff] [blame] | 157 | $(OUTPUT).dmg: $(OUTPUT) |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 158 | @echo DMG $@ |
Dominik Riebeling | 4f3fa9a | 2013-05-11 17:22:44 +0200 | [diff] [blame] | 159 | $(SILENT)$(call mkdir,$(OUTPUT)-dmg)) |
Dominik Riebeling | ded02d8 | 2012-01-06 09:24:38 +0000 | [diff] [blame] | 160 | $(SILENT)cp -p $(OUTPUT) $(OUTPUT)-dmg |
Dominik Riebeling | b24e562 | 2011-12-14 21:59:37 +0000 | [diff] [blame] | 161 | $(SILENT)hdiutil create -srcfolder $(OUTPUT)-dmg $@ |