From 2cc3c53cd29fed18e6262e2ee75f90fcb00dcb64 Mon Sep 17 00:00:00 2001 From: Yehia Hafez Date: Tue, 4 Jul 2023 22:12:38 -0500 Subject: [PATCH] Added makefile --- .gitignore | 4 + Makefile | 31 +++++ btrfs_filesystem/Makefile | 220 ++++++++++++++++++++++++++++++++++ btrfs_filesystem/Makefile.inc | 9 ++ mount/Makefile | 21 ++++ 5 files changed, 285 insertions(+) create mode 100644 Makefile create mode 100644 btrfs_filesystem/Makefile create mode 100644 btrfs_filesystem/Makefile.inc create mode 100644 mount/Makefile diff --git a/.gitignore b/.gitignore index 567609b..a0e0529 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ build/ +bin/ +.vscode/ +*.o +Info.plist~ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5dfe3df --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +# +# All-in-one Makefile +# + +MAKE=make +MV=mv +RM=rm +MKDIR=mkdir +OUT=bin + +all: debug + +debug: + $(RM) -rf $(OUT)/macos_btrfs.kext* $(OUT)/mount_btrfs* + $(MAKE) -C btrfs_filesystem $(TARGET) + $(MAKE) -C mount $(TARGET) + $(MKDIR) -p $(OUT) + $(MV) btrfs_filesystem/macos_btrfs.kext btrfs_filesystem/macos_btrfs.kext.dSYM $(OUT) + $(MV) mount/mount_btrfs $(OUT) + $(MV) mount/mount_btrfs.dSYM $(OUT) 2> /dev/null || true + +release: TARGET=release +release: debug + +clean: + $(RM) -rf $(OUT)/macos_btrfs.kext* $(OUT)/mount_btrfs* + $(MAKE) -C btrfs_filesystem clean + $(MAKE) -C mount clean + +.PHONY: all debug release clean + diff --git a/btrfs_filesystem/Makefile b/btrfs_filesystem/Makefile new file mode 100644 index 0000000..ef4c65c --- /dev/null +++ b/btrfs_filesystem/Makefile @@ -0,0 +1,220 @@ +# +# macOS generic kernel extension Makefile +# + +include Makefile.inc + +# +# Check mandatory vars +# +ifndef KEXTNAME +$(error KEXTNAME not defined) +endif + +ifndef KEXTVERSION +$(error KEXTVERSION not defined) +endif + +ifndef KEXTBUILD +# [assume] zero indicates no build number +KEXTBUILD:= 0 +endif + +ifndef BUNDLEDOMAIN +$(error BUNDLEDOMAIN not defined) +endif + + +# defaults +BUNDLEID?= $(BUNDLEDOMAIN).kext.$(KEXTNAME) +KEXTBUNDLE?= $(KEXTNAME).kext +KEXTMACHO?= $(KEXTNAME).out +ARCHFLAGS?= -arch x86_64 +PREFIX?= /Library/Extensions + +# +# Set default macOS SDK +# You may use +# sudo xcode-select -s /Applications/Xcode.app/Contents/Developer +# to switch to Xcode from Command Line Tools if cannot find any SDK +# +SDKROOT?= $(shell xcrun --sdk macosx --show-sdk-path) + +SDKFLAGS= -isysroot $(SDKROOT) +CC= $(shell xcrun -find -sdk $(SDKROOT) cc) +CODESIGN= $(shell xcrun -find -sdk $(SDKROOT) codesign) + +# +# Standard defines and includes for kernel extensions +# +# The __kext_makefile__ macro used to compatible with XCode +# Since XCode use intermediate objects which causes symbol duplicated +# +CPPFLAGS+= -DKERNEL \ + -DKERNEL_PRIVATE \ + -DDRIVER_PRIVATE \ + -DAPPLE \ + -DNeXT \ + -I$(SDKROOT)/System/Library/Frameworks/Kernel.framework/Headers \ + -I/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include \ + -D__kext_makefile__ + +# +# Convenience defines +# BUNDLEID macro will be used in KMOD_EXPLICIT_DECL +# +CPPFLAGS+= -DKEXTNAME_S=\"$(KEXTNAME)\" \ + -DKEXTVERSION_S=\"$(KEXTVERSION)\" \ + -DKEXTBUILD_S=\"$(KEXTBUILD)\" \ + -DBUNDLEID_S=\"$(BUNDLEID)\" \ + -DBUNDLEID=$(BUNDLEID) + +TIME_STAMP:= $(shell date +'%Y/%m/%d\ %H:%M:%S%z') +CPPFLAGS+= -D__TS__=\"$(TIME_STAMP)\" + +# +# C compiler flags +# +ifdef MACOSX_VERSION_MIN +CFLAGS+= -mmacosx-version-min=$(MACOSX_VERSION_MIN) +else +CFLAGS+= -mmacosx-version-min=10.15 +endif +CFLAGS+= $(SDKFLAGS) \ + $(ARCHFLAGS) \ + -x c \ + -std=c99 \ + -fno-common \ + -mkernel \ + -nostdinc \ + -fno-builtin + +# warnings +CFLAGS+= -Wall -Wextra + +# linker flags +ifdef MACOSX_VERSION_MIN +LDFLAGS+= -mmacosx-version-min=$(MACOSX_VERSION_MIN) +else +LDFLAGS+= -mmacosx-version-min=10.15 +endif +LDFLAGS+= $(SDKFLAGS) \ + $(ARCHFLAGS) \ + -nostdlib \ + -Xlinker -kext \ + -Xlinker -object_path_lto \ + -Xlinker -export_dynamic + +# libraries +LIBS+= -lkmod +#LIBS+= -lkmodc++ +LIBS+= -lcc_kext + +# kextlibs flags +KLFLAGS+= -xml -c -unsupported -undef-symbols + +# source, object files +SRCS:= $(wildcard *.c) +OBJS:= $(SRCS:.c=.o) + +# targets + +all: debug + +%.o: %.c $(HDRS) + $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< + +$(KEXTMACHO): $(OBJS) + $(CC) $(LDFLAGS) $(LIBS) -o $@ $^ + otool -h $@ + otool -l $@ | grep uuid + +Info.plist~: Info.plist.in + sed \ + -e 's/__KEXTNAME__/$(KEXTNAME)/g' \ + -e 's/__KEXTMACHO__/$(KEXTNAME)/g' \ + -e 's/__KEXTVERSION__/$(KEXTVERSION)/g' \ + -e 's/__KEXTBUILD__/$(KEXTBUILD)/g' \ + -e 's/__BUNDLEID__/$(BUNDLEID)/g' \ + -e 's/__OSBUILD__/$(shell /usr/bin/sw_vers -buildVersion)/g' \ + -e 's/__CLANGVER__/$(shell $(CC) -v 2>&1 | grep version)/g' \ + $^ > $@ + +$(KEXTBUNDLE): $(KEXTMACHO) Info.plist~ + mkdir -p $@/Contents/MacOS + mv $< $@/Contents/MacOS/$(KEXTNAME) + + # Clear placeholders(o.w. kextlibs cannot parse) + sed 's/__KEXTLIBS__//g' Info.plist~ > $@/Contents/Info.plist + awk '/__KEXTLIBS__/{system("kextlibs $(KLFLAGS) $@");next};1' Info.plist~ > $@/Contents/Info.plist~ + mv $@/Contents/Info.plist~ $@/Contents/Info.plist + +ifdef COMPATIBLE_VERSION + /usr/libexec/PlistBuddy -c 'Add :OSBundleCompatibleVersion string "$(COMPATIBLE_VERSION)"' $@/Contents/Info.plist +endif + +ifdef COPYRIGHT + /usr/libexec/PlistBuddy -c 'Add :NSHumanReadableCopyright string "$(COPYRIGHT)"' $@/Contents/Info.plist +endif + +ifdef SIGNCERT + $(CODESIGN) --force --timestamp=none --generate-entitlement-der -o runtime --sign $(SIGNCERT) $@ + /usr/libexec/PlistBuddy -c 'Add :CFBundleSignature string ????' $@/Contents/Info.plist +endif + + # Empty-dependency kext cannot be load so we add one if necessary + /usr/libexec/PlistBuddy -c 'Print OSBundleLibraries' $@/Contents/Info.plist &> /dev/null || \ + /usr/libexec/PlistBuddy -c 'Add :OSBundleLibraries:com.apple.kpi.bsd string "9.0.0"' $@/Contents/Info.plist + + touch $@ + + + /usr/libexec/PlistBuddy -c 'Add :OSBundleLibraries:com.apple.kext.triggers string "1.0.0d1"' $@/Contents/Info.plist + /usr/libexec/PlistBuddy -c 'Add :OSBundleLibraries:com.apple.kpi.iokit string "9.0.0"' $@/Contents/Info.plist + /usr/libexec/PlistBuddy -c 'Add :OSBundleLibraries:com.apple.kpi.libkern string "9.0.0"' $@/Contents/Info.plist + /usr/libexec/PlistBuddy -c 'Add :OSBundleLibraries:com.apple.kpi.mach string "9.0.0"' $@/Contents/Info.plist + /usr/libexec/PlistBuddy -c 'Add :OSBundleLibraries:com.apple.kpi.unsupported string "9.0.0"' $@/Contents/Info.plist + + + dsymutil $(ARCHFLAGS) -o $(KEXTNAME).kext.dSYM $@/Contents/MacOS/$(KEXTNAME) + +# see: https://www.gnu.org/software/make/manual/html_node/Target_002dspecific.html +# Those two flags must present at the same time o.w. debug symbol cannot be generated +debug: CPPFLAGS += -g -DDEBUG +debug: CFLAGS += -O0 +debug: $(KEXTBUNDLE) + +# see: https://stackoverflow.com/questions/15548023/clang-optimization-levels +release: CFLAGS += -O2 +release: $(KEXTBUNDLE) + +load: $(KEXTBUNDLE) + sudo chown -R root:wheel $< + sudo sync + sudo kextutil $< + # restore original owner:group + sudo chown -R '$(USER):$(shell id -gn)' $< + sudo dmesg | grep $(KEXTNAME) | tail -1 + +stat: + kextstat | grep $(KEXTNAME) + +unload: + sudo kextunload $(KEXTBUNDLE) + sudo dmesg | grep $(KEXTNAME) | tail -2 + +install: $(KEXTBUNDLE) uninstall + test -d "$(PREFIX)" + sudo cp -r $< "$(PREFIX)/$<" + sudo chown -R root:wheel "$(PREFIX)/$<" + +uninstall: + test -d "$(PREFIX)" + test -e "$(PREFIX)/$(KEXTBUNDLE)" && \ + sudo rm -rf "$(PREFIX)/$(KEXTBUNDLE)" || true + +clean: + rm -rf $(KEXTBUNDLE) $(KEXTBUNDLE).dSYM Info.plist~ $(OBJS) $(KEXTMACHO) + +.PHONY: all debug release load stat unload intall uninstall clean + diff --git a/btrfs_filesystem/Makefile.inc b/btrfs_filesystem/Makefile.inc new file mode 100644 index 0000000..7187d47 --- /dev/null +++ b/btrfs_filesystem/Makefile.inc @@ -0,0 +1,9 @@ +# +# Put user-defined variables here +# + +KEXTNAME=macos_btrfs +KEXTVERSION=0000.00.01 +KEXTBUILD=1 +BUNDLEDOMAIN=com.relalis + diff --git a/mount/Makefile b/mount/Makefile new file mode 100644 index 0000000..9d5a797 --- /dev/null +++ b/mount/Makefile @@ -0,0 +1,21 @@ +CC=gcc +CFLAGS=-std=c99 -Wall -Wextra +SOURCES= mount_btrfs.c +EXECUTABLE=mount_btrfs +RM=rm + +all: debug + +release: $(EXECUTABLE) + +debug: CFLAGS += -g -DDEBUG -lutil +debug: release + +$(EXECUTABLE): $(SOURCES) + $(CC) $(CFLAGS) $< -o $@ + +clean: + $(RM) -rf *.o $(EXECUTABLE) *.dSYM + +.PHONY: all debug release clean +