I have a main makefile to compile my lib: sdklib.a :
all: sdklib.a
sdklib_arm64.a: FORCE
$(MAKE) -f makefile.iphone TARGET=arm64 all
sdklib_armv7.a: FORCE
$(MAKE) -f makefile.iphone TARGET=armv7 all
sdklib_armv7s.a: FORCE
$(MAKE) -f makefile.iphone TARGET=armv7s all
FORCE:
sdklib.a: sdklib_arm64.a sdklib_armv7s.a sdklib_armv7.a
lipo -create -output sdklib.a sdklib_arm64.a sdklib_armv7s.a sdklib_armv7.a
And my makefile.iphone is :
Compiler to use. Can be g++ or clang++
CXX = clang
all: sdklib_$(TARGET).a
TMPDIR=tmp_$(TARGET)
PLATFORM_BASE=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk
COMMON_FLAGS = -Isrc/main/cpp -Isrc/main/objc -I../api/tmp_$(TARGET)/src/main/proto -I../protobuf/src/main/cpp
-Wno-unused-command-line-argument
-arch $(TARGET)
-emit-llvm
-mios-simulator-version-min=6.1
-fobjc-abi-version=2
-isysroot $(PLATFORM_BASE)
-stdlib=libc++
-pthread -DHAVE_PTHREAD -DGOOGLE_PROTOBUF_NO_RTTI -fPIC -fno-rtti
-fobjc-arc
-framework AdSupport
-framework AppTrackingTransparency
-framework CoreLocation
-framework CoreMotion
-framework Foundation
-framework UIKit
-framework SystemConfiguration
-Wall -Wno-deprecated-declarations -Werror
OBJC_FLAGS = $(COMMON_FLAGS)
CPP_FLAGS = $(COMMON_FLAGS) -std=c++11
Compiler flags to generate dependencies (.newdep) files.
.newdep files are then renames as .dep
DEPFLAGS = -MT $@ -MMD -MP -MF $(TMPDIR)/$*.newdep
CC_COMPILE = $(CXX) $(DEPFLAGS) $(CPP_FLAGS) -c
M_COMPILE = $(CXX) $(DEPFLAGS) $(OBJC_FLAGS) -c
MM_COMPILE = $(CXX) $(DEPFLAGS) $(CPP_FLAGS) -c
After compilation rename the .newdep files. This avoid a failing compilation to
corrupt the dependency stuff.
POSTCOMPILE = mv -f $(TMPDIR)/$.newdep $(TMPDIR)/$.dep
LDFLAGS_COMMON := -lprotobuf -lstdc++ -lpthread
Linker flags. On server use the server lib with all protos, on phone use only phone visible ones.
LDFLAGS_LIB := ../api/libapi.a $(LDFLAGS_COMMON)
Shell commands to build tmp directory tree as a clone of sources tree.
$(shell find src/main/cpp -type d | grep -v $(TMPDIR) | xargs -I {} mkdir -p $(TMPDIR)/{})
$(shell find src/main/objc -type d | grep -v $(TMPDIR) | xargs -I {} mkdir -p $(TMPDIR)/{})
Rules to compile
$(TMPDIR)/%.o: %.cc
$(TMPDIR)/%.o: %.cc
$(CC_COMPILE) $(OUTPUT_OPTION) $<
$(POSTCOMPILE)
$(TMPDIR)/%.o: %.m
$(TMPDIR)/%.o: %.m
$(M_COMPILE) $(OUTPUT_OPTION) $<
$(POSTCOMPILE)
$(TMPDIR)/%.o: %.mm
$(TMPDIR)/%.o: %.mm
$(MM_COMPILE) $(OUTPUT_OPTION) $<
$(POSTCOMPILE)
Include all dependency files.
-include $(shell find $(TMPDIR) -name '*.dep')
Scan folder for cc files.
CC_SRC = $(shell find src/main/cpp -name '.cc')
M_SRC = $(shell find src/main/objc -name '.m')
MM_SRC = $(shell find src/main/objc -name '*.mm')
Generate .o names for all .cc, .mm and .m files.
OBJS=$(addprefix $(TMPDIR)/,$(CC_SRC:.cc=.o)) $(addprefix $(TMPDIR)/,$(MM_SRC:.mm=.o)) $(addprefix $(TMPDIR)/,$(M_SRC:.m=.o))
Methods to remove items from a list if they match or does not match a string.
FILTER_OUT = $(foreach v,$(2),$(if $(findstring $(1),$(v)),,$(v)))
FILTER_OUT_NOT = $(foreach v,$(2),$(if $(findstring $(1),$(v)),$(v),))
SDKLIB_OBJS = $(call FILTER_OUT,_test., $(call FILTER_OUT,_tool., $(OBJS)))
Static target for tests.
sdklib_$(TARGET).a: $(SDKLIB_OBJS)
ar rcs sdklib_$(TARGET).a $(SDKLIB_OBJS)
clean:
rm -f sdklib_$(TARGET).a
rm -rf $(TMPDIR)