Skip to content

Instantly share code, notes, and snippets.

@dweinstein
Created November 15, 2013 18:21
Show Gist options
  • Save dweinstein/7489146 to your computer and use it in GitHub Desktop.
Save dweinstein/7489146 to your computer and use it in GitHub Desktop.
Android standalone native makefile
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(NDK_DIR)),)
$(error "set NDK_DIR in your environment. export NDK_DIR=<path to android-ndk-r8e>")
endif
#determine OS string
OS=$(shell uname)
ARCH_STR=
ifneq ($(findstring Linux,$(OS)),)
ARCH=$(shell uname -m)
ifneq ($(findstring x86_64,$(ARCH)),)
ARCH_STR=linux-x86_64
else
ARCH_STR=linux-x86
endif
else
ifeq (Darwin,$(OS))
ARCH_STR=darwin-x86_64
else
$(error "Uname $(OS) not recognized. I probably need to fix my makefile")
endif
endif
API_LEVEL := android-18
ifeq ($(strip $(TARGET_ABI)),)
TARGET_ABI := arch-arm
endif
TOOLCHAIN_VER := 4.6
ifeq (arch-arm, $(TARGET_ABI))
TOOLCHAIN_PREFIX := arm-linux-androideabi
TOOLCHAIN_BIN_PREFIX := arm-linux-androideabi-
TOOLCHAIN := $(TOOLCHAIN_PREFIX)-$(TOOLCHAIN_VER)
endif
ifeq (arch-x86, $(TARGET_ABI))
TOOLCHAIN_PREFIX := x86
TOOLCHAIN_BIN_PREFIX := i686-linux-android-
TOOLCHAIN := $(TOOLCHAIN_PREFIX)-$(TOOLCHAIN_VER)
endif
$(echo "Toolchain: $(TOOLCHAIN)")
ifeq ($(strip $(TOOLCHAIN)),)
$(error "Toolchain could not be detected.")
endif
NDK_LIB_DIR := $(NDK_DIR)/platforms/$(API_LEVEL)/$(TARGET_ABI)/usr
PREFIX := $(NDK_DIR)/toolchains/$(TOOLCHAIN)/prebuilt/$(ARCH_STR)/bin/$(TOOLCHAIN_BIN_PREFIX)
LINKROOT = $(NDK_LIB_DIR)/lib/
#---------------------------------------------------------------------------------
# the prefix on the compiler executables
#---------------------------------------------------------------------------------
export CC := $(PREFIX)gcc
export CXX := $(PREFIX)g++
export AS := $(PREFIX)as
export AR := $(PREFIX)ar
export OBJCOPY := $(PREFIX)objcopy
export BIN2S := bin2s
export STRIP := $(PREFIX)strip
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET := test-reloc
BUILD := build
SOURCES := source
DATA := data
INCLUDES :=
#STATIC = true
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project (order is important)
#---------------------------------------------------------------------------------
LIBS := -llog
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(NDK_LIB_DIR)
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
CFLAGS := -Wall -pipe -Os
CFLAGS += $(INCLUDE)
CXXFLAGS := $(CFLAGS)
ASFLAGS := -g
LDFLAGS = -g
ifeq ($(strip $(STATIC)),true)
LDFLAGS += -static -static-libgcc
endif
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT := $(CURDIR)/$(TARGET)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
# use CXX for linking C++ projects, CC for standard C
ifeq ($(strip $(CPPFILES)),)
export LD := $(CC)
else
export LD := $(CXX)
endif
export OFILES := $(addsuffix .o,$(BINFILES)) \
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
.PHONY: $(BUILD) clean all
#---------------------------------------------------------------------------------
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET) $(TARGET).debug
all: $(BUILD)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT) : $(OFILES)
@echo linking $(notdir $@).debug
@$(LD) $(LDFLAGS) $(OFILES) --sysroot=$(NDK_LIB_DIR) $(LIBPATHS) $(LIBS) -o $@.debug
@echo strip $(notdir $@).debug "->" $(notdir $@)
@$(STRIP) -s $@.debug -o $@
-include $(DEPSDIR)/*.d
#---------------------------------------------------------------------------------
%.o: %.cpp
@echo $(notdir $<)
@$(CXX) -MMD -MP -MF $(DEPSDIR)/$*.d $(CXXFLAGS) -c $< -o $@
#---------------------------------------------------------------------------------
%.o: %.c
@echo $(notdir $<)
@$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(CFLAGS) -c $< -o $@
#---------------------------------------------------------------------------------
%.o: %.s
@echo $(notdir $<)
@$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d -x assembler-with-cpp $(ASFLAGS) -c $< -o $@
#---------------------------------------------------------------------------------
%.o: %.S
@echo $(notdir $<)
@$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d -x assembler-with-cpp $(ASFLAGS) -c $< -o $@
%.bin.o : %.bin
@echo $(notdir $<)
@$(BIN2S) -a 32 $< | $(AS) -o $(@)
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment