在Makefile中更改构建规则以构建多个文件类型

时间:2016-12-05 18:38:53

标签: c++ objective-c makefile clang

我想改变我的Makefile来构建cpp和mm文件,因为我刚刚开始在我的项目中添加Objective-C代码。这是我目前的Makefile:

# Name of the project being built
PROJECT := modpe

# Name of process the tweak is loaded into
PROCESS := minecraftpe

# Local IP Address of device to SSH into
DEVICE := ryans-ipod-touch.local

# Path of the SDK on MacOS
SDKPATH := /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk

# Build using libc++ and C++11 support
override CXXFLAGS += -stdlib=libc++ -std=c++11 -miphoneos-version-min=7.0 -isysroot $(SDKPATH)
override LDFLAGS += -stdlib=libc++ -std=c++11 -miphoneos-version-min=7.0 -isysroot $(SDKPATH)


# Names of the tweak library, substrate filter, and debian package
TWEAK := $(PROJECT).dylib
FILTER := $(PROJECT).plist
DEB := $(PROJECT).deb
# Directory for build products like executables, object files, and dependency files
BUILD := build
# Directory for debian package filesystem layout
LAYOUT := layout
# Directory for temporary staging of the debian package structure while creating the package
STAGING := $(BUILD)/deb
SUBSTRATE := $(STAGING)/Library/MobileSubstrate/DynamicLibraries
# Name of the unstripped version
UNSTRIPPED := $(BUILD)/$(TWEAK:.dylib=_unstripped.dylib)
# List of source files and their corresponding object file paths
SRCS := $(shell find . -type f -name '*.cpp')
OBJS := $(addprefix $(BUILD)/,$(SRCS:.cpp=.o))
# Supported architectures
ARCHS := arm64
ARCHFLAGS := $(addprefix -arch ,$(ARCHS))
# Frameworks for linking
FRAMEWORKS := CydiaSubstrate Forklift
override LDFLAGS += $(addprefix -framework ,$(FRAMEWORKS))

# Compiler and linker
CLANG := clang $(ARCHFLAGS)
CLANGXX := clang++ $(ARCHFLAGS)
CC := $(CLANG)
CXX := $(CLANGXX)
LD := $(CLANGXX)
STRIP := strip

# When invoked as "make VERBOSE=1", every command gets printed as it runs
ifdef VERBOSE
_v :=
else
_v := @
endif


### Build rules ###

all: $(TWEAK)

.PHONY: all

$(TWEAK): $(UNSTRIPPED)
    @echo 'Strip $@'
    $(_v)$(STRIP) -x -o $@ $<

%.dylib: $(OBJS)
    @echo 'Link $@'
    $(_v)$(LD) $(LDFLAGS) -dynamiclib -o $@ $^

%.cpp: $(BUILD)/%.d

$(BUILD)/%.o: %.cpp | $(BUILD)/.dir
    @echo 'Compile $@'
    $(_v)$(CXX) $(CXXFLAGS) -MD -MF $(BUILD)/$*.d -c -o $@ $<

.SECONDARY: $(BUILD)/.dir

-include $(BUILD)/*.d


### Package rules ###

package: $(DEB)

.PHONY: package

$(DEB): $(TWEAK) $(FILTER)
    @echo 'Stage $@'
    $(_v)rm -rf $(STAGING)
    $(_v)mkdir -p $(STAGING)
    $(_v)cp -R $(LAYOUT)/* $(STAGING)
    $(_v)mkdir -p $(SUBSTRATE)
    $(_v)cp $^ $(SUBSTRATE)
    $(_v)chown -R root:wheel $(STAGING)
    $(_v)chmod 0755 $(SUBSTRATE)/$(TWEAK)
    $(_v)chmod 0644 $(SUBSTRATE)/$(FILTER)
    @echo 'Package $@'
    $(_v)dpkg-deb -Zgzip -b $(STAGING) $@
    $(_v)rm -rf $(STAGING)


### Install rules ###

install: $(DEB)
    @echo 'Install $(DEB)'
    scp $(DEB) root@$(DEVICE):/var/tmp/
    @echo ‘Run dpkg -i /var/tmp/$(DEB) to install.’
    ssh root@$(DEVICE)

.PHONY: install

### Offline install rules ###

offline:
    @echo 'No WiFi to SSH: Using afc instead'
    $(_v)sudo afc/afcclient put ./modpe.deb /modpe.deb

.PHONY: offline

### Clean rules ###

clean:
    @echo 'Remove $(BUILD)'
    $(_v)rm -rf $(BUILD)

.PHONY: clean


### General rules ###

%/.dir:
    @echo 'Create directory $*/'
    $(_v)mkdir -p $* && touch $@

# deploys test script
deploy: $(OFFLINE)
    scp scripts/script.js root@$(DEVICE):/var/mobile/modpe/

.PHONY: deploy

到目前为止,我所知道的唯一改变的是SRCS系列:

SRCS := $(shell find -E . -regex “.*\.\(cpp\|mm\)”)

我的问题是,我还能做些什么来使用 .cpp源文件构建.mm源文件,而不是忽略它们?

1 个答案:

答案 0 :(得分:0)

您没有向我们提供足够的信息来重现问题或测试解决方案,因此您可能需要进行一些微调,但这是一种应该有效的方法。

您已修改SRCS := ...行。 验证确实有效后,修改OBJS := ...行:

OBJS := $(patsubst %, $(BUILD)/%.o,$(basename $(SRCS)))

然后一旦您确认其有效,添加新规则:

$(BUILD)/%.o: %.mm | $(BUILD)/.dir
    @echo 'Compile $@'
    $(_v)$(CXX) $(CXXFLAGS) -MD -MF $(BUILD)/$*.d -c -o $@ $<