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