1################################################################################ 2# Note: this makefile is used only for the libkxld build alias. It is not part 3# of the regular kernel build process. 4################################################################################ 5 6# Version 7COMPATIBILITY_VERSION=1 8CURRENT_VERSION=1 9 10# Paths 11ifndef DSTROOT 12DSTROOT=./BUILD/dst 13endif 14ifndef OBJROOT 15OBJROOT=./BUILD/obj 16endif 17ifndef SYMROOT 18SYMROOT=./BUILD/sym 19endif 20ifdef SRCROOT 21EXTHDRSRC=$(SRCROOT)/EXTERNAL_HEADERS 22HDRSRC=$(SRCROOT)/libkern/libkern 23OBJSRC=$(SRCROOT)/libkern/kxld 24else 25SRCROOT=. 26EXTHDRSRC=$(SRCROOT)/../../EXTERNAL_HEADERS 27HDRSRC=$(SRCROOT)/../libkern 28OBJSRC=$(SRCROOT) 29ROOT=BUILD 30endif 31ifdef RC_CFLAGS 32ARCHS=$(addprefix -arch , $(RC_ARCHS)) 33else 34ARCHS=-arch i386 -arch x86_64 35RC_CFLAGS=$(ARCHS) -pipe 36endif 37ifdef INSTALL_LOCATION 38override DSTROOT := $(DSTROOT)/$(INSTALL_LOCATION) 39endif 40 41PRODUCT_TYPE ?= DYLIB 42 43HDRDST=$(DSTROOT)/usr/local/include 44DYLIBDST=$(DSTROOT)/usr/lib/system 45ARCHIVEDST=$(DSTROOT)/usr/local/lib 46LIBKXLD_DYLIB=libkxld.dylib 47LIBKXLD_ARCHIVE=libkxld.a 48LIBKXLD_INSTALLNAME=/usr/lib/system/$(LIBKXLD_DYLIB) 49LIBKXLDOBJ_DYLIB=$(OBJROOT)/$(LIBKXLD_DYLIB) 50LIBKXLDOBJ_ARCHIVE=$(OBJROOT)/$(LIBKXLD_ARCHIVE) 51LIBKXLDSYM_DYLIB=$(SYMROOT)/$(LIBKXLD_DYLIB) 52LIBKXLDSYM_ARCHIVE=$(SYMROOT)/$(LIBKXLD_ARCHIVE) 53LIBKXLDDST_DYLIB=$(DYLIBDST)/$(LIBKXLD_DYLIB) 54LIBKXLDDST_ARCHIVE=$(ARCHIVEDST)/$(LIBKXLD_ARCHIVE) 55TESTSRC=$(SRCROOT)/tests 56TESTDST=./BUILD/tests 57 58# Flags 59SDKROOT ?= / 60CFLAGS=-std=c99 -Wall -Wextra -Werror -pedantic -Wformat=2 -Wcast-align \ 61 -Wwrite-strings -Wshorten-64-to-32 -Wshadow -Winit-self -Wpointer-arith \ 62 -Wno-format-y2k -W -Wstrict-prototypes -Wmissing-prototypes -Wreturn-type \ 63 -Wcast-qual -Wwrite-strings -Wswitch -Wcast-align -Wbad-function-cast \ 64 -Wchar-subscripts -Winline -Wnested-externs -Wredundant-decls -g 65LDFLAGS=$(ARCHS) -dynamiclib -install_name $(LIBKXLD_INSTALLNAME) \ 66 -compatibility_version $(COMPATIBILITY_VERSION) \ 67 -current_version $(CURRENT_VERSION) -lstdc++ 68INCLUDES=-I$(HDRSRC) -I$(EXTHDRSRC) 69 70ifneq ($(SDKROOT),/) 71 CFLAGS += -isysroot $(SDKROOT) 72 LDFLAGS += -isysroot $(SDKROOT) 73endif 74 75# Tools 76CC = xcrun -sdk $(SDKROOT) clang 77CLANG_ANALYZER = clang --analyze 78LIBTOOL = xcrun -sdk $(SDKROOT) libtool 79STRIP = xcrun -sdk $(SDKROOT) strip 80 81# Files 82HDR_NAMES=kxld.h kxld_types.h WKdm.h 83OBJ_NAMES=kxld.o kxld_array.o kxld_copyright.o kxld_demangle.o kxld_dict.o \ 84 kxld_kext.o kxld_object.o kxld_reloc.o kxld_sect.o kxld_seg.o \ 85 kxld_srcversion.o kxld_sym.o kxld_symtab.o kxld_util.o kxld_uuid.o \ 86 kxld_vtable.o kxld_versionmin.o WKdmCompress.o WKdmDecompress.o 87 88HDRS=$(addprefix $(HDRSRC)/, $(HDR_NAMES)) 89OBJS=$(addprefix $(OBJROOT)/, $(OBJ_NAMES)) 90 91$(shell [ -d $(OBJROOT) ] || mkdir -p $(OBJROOT)) 92 93# Implicit rules 94%.o : %.c 95$(OBJROOT)/%.o : $(OBJSRC)/%.c 96 $(CC) $(RC_CFLAGS) $(CFLAGS) $(OPTIM) $(INCLUDES) -c $< -o $@ 97$(OBJROOT)/%.o : $(TESTSRC)/%.c 98 $(CC) $(RC_CFLAGS) $(CFLAGS) -O0 -DDEBUG $(INCLUDES) -I $(SRCROOT) -c $< -o $@ 99 100SRCROOTESC=$(subst /,\/,$(SRCROOT)) 101OBJROOTESC=$(subst /,\/,$(OBJROOT)) 102SEDOBJS=sed -E 's/(^[a-z_]+)\.o/$(OBJROOTESC)\/\1\.o $(OBJROOTESC)\/\1\.d/' 103SEDSRCS=sed -E 's/ ([a-z_]+\.[ch])/ $(SRCROOTESC)\/\1/g' 104$(OBJROOT)/%.d: $(OBJSRC)/%.c 105 @set -e; rm -f $@; \ 106 $(CC) $(INCLUDES) -MM $< | $(SEDOBJS) | $(SEDSRCS) > $@; 107 108# Rules 109release: OPTIM=-Os -dynamic 110release: build 111 112debug: OPTIM=-O0 -DDEBUG -dynamic 113debug: build 114 115profile: OPTIM=-Os -pg -dynamic 116profile: build 117 118tests: OPTIM=-O0 -DDEBUG 119tests: kxld_array_test kxld_dict_test copyrighttest 120 121build: $(LIBKXLDSYM_$(PRODUCT_TYPE)) 122 123$(LIBKXLDSYM_DYLIB): $(LIBKXLDOBJ_DYLIB) 124 @mkdir -p $(SYMROOT) 125 install -c -m 644 $< $@ 126 127$(LIBKXLDSYM_ARCHIVE): $(LIBKXLDOBJ_ARCHIVE) 128 @mkdir -p $(SYMROOT) 129 install -c -m 644 $< $@ 130 131$(LIBKXLDOBJ_DYLIB): $(OBJS) 132 $(CC) $(LDFLAGS) -o $@ $^ 133 134$(LIBKXLDOBJ_ARCHIVE): $(OBJS) 135 $(LIBTOOL) -static -o $@ $^ 136 137installhdrs: 138 @mkdir -p $(HDRDST) 139 install -o 0 -g 0 -c -m 444 $(HDRS) $(HDRDST) 140 141install: release installhdrs $(LIBKXLDDST_$(PRODUCT_TYPE)) 142 143$(LIBKXLDDST_DYLIB): $(LIBKXLDSYM_DYLIB) 144 @mkdir -p $(DYLIBDST) 145 install -o 0 -g 0 -c -m 555 $< $@ 146 $(STRIP) -S -x $@ 147 148$(LIBKXLDDST_ARCHIVE): $(LIBKXLDSYM_ARCHIVE) 149 @mkdir -p $(ARCHIVEDST) 150 install -o 0 -g 0 -c -m 555 $< $@ 151 152 153KEXTCOPYOBJS=$(OBJROOT)/kextcopyright.o $(OBJROOT)/kxld_copyright.o $(OBJROOT)/kxld_util.o 154kextcopyright: $(TESTDST)/kextcopyright 155$(TESTDST)/kextcopyright: $(KEXTCOPYOBJS) 156 @mkdir -p $(TESTDST) 157 $(CC) $(ARCHS) $(KEXTCOPYOBJS) -framework CoreFoundation -o $(OBJROOT)/kextcopyright 158 install -c -m 755 $(OBJROOT)/kextcopyright $@ 159 160TESTOBJS=$(OBJROOT)/kxld_test.o $(OBJROOT)/kxld_util.o 161 162ARRAYOBJS=$(OBJROOT)/kxld_array_test.o $(OBJROOT)/kxld_array.o $(TESTOBJS) 163kxld_array_test: $(TESTDST)/kxld_array_test 164$(TESTDST)/kxld_array_test: $(ARRAYOBJS) 165 @mkdir -p $(TESTDST) 166 $(CC) $(ARCHS) $(ARRAYOBJS) -o $(OBJROOT)/kxld_array_test 167 install -c -m 755 $(OBJROOT)/kxld_array_test $@ 168 169DICTOBJS=$(OBJROOT)/kxld_dict_test.o $(OBJROOT)/kxld_dict.o $(OBJROOT)/kxld_array.o $(TESTOBJS) 170kxld_dict_test: $(TESTDST)/kxld_dict_test 171$(TESTDST)/kxld_dict_test: $(DICTOBJS) 172 @mkdir -p $(TESTDST) 173 $(CC) $(ARCHS) $(DICTOBJS) -o $(OBJROOT)/kxld_dict_test 174 install -c -m 755 $(OBJROOT)/kxld_dict_test $@ 175 176COPYTESTOBJS=$(OBJROOT)/kxld_copyright.o $(OBJROOT)/kxld_util.o 177copyrighttest: OPTIM+=-DTEST 178copyrighttest: $(TESTDST)/copyrighttest 179$(TESTDST)/copyrighttest: $(COPYTESTOBJS) 180 @mkdir -p $(TESTDST) 181 $(CC) $(ARCHS) $(COPYTESTOBJS) -framework CoreFoundation -framework IOKit -o $(OBJROOT)/copyrighttest 182 install -c -m 755 $(OBJROOT)/copyrighttest $@ 183 184analyze: 185 @$(CLANG_ANALYZER) *.c 186 @$(CLANG_ANALYZER) -I. tests/*.c 187 @rm -f *.plist 188 189clean: 190 @rm -rf $(OBJROOT)/* 191 192fullclean: 193ifdef ROOT 194 @rm -rf $(ROOT) 195else 196 @rm -rf $(OBJROOT) $(DSTROOT) $(SYMROOT) 197endif 198 199# Automatically build dependency information when .c or .h files change based 200# on implicit rule for .d:.c 201-include $(OBJS:.o=.d) 202 203