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 ################################################################################
7 COMPATIBILITY_VERSION=1
21 EXTHDRSRC=$(SRCROOT)/EXTERNAL_HEADERS
22 HDRSRC=$(SRCROOT)/libkern/libkern
23 OBJSRC=$(SRCROOT)/libkern/kxld
26 EXTHDRSRC=$(SRCROOT)/../../EXTERNAL_HEADERS
27 HDRSRC=$(SRCROOT)/../libkern
32 ARCHS=$(addprefix -arch , $(RC_ARCHS))
35 RC_CFLAGS=$(ARCHS) -pipe
37 ifdef INSTALL_LOCATION
38 override DSTROOT := $(DSTROOT)/$(INSTALL_LOCATION)
43 HDRDST=$(DSTROOT)/usr/local/include
44 DYLIBDST=$(DSTROOT)/usr/lib/system
45 ARCHIVEDST=$(DSTROOT)/usr/local/lib
46 LIBKXLD_DYLIB=libkxld.dylib
47 LIBKXLD_ARCHIVE=libkxld.a
48 LIBKXLD_DSYM=$(LIBKXLD_DYLIB).dSYM
49 LIBKXLD_INSTALLNAME=/usr/lib/system/$(LIBKXLD_DYLIB)
50 LIBKXLDOBJ_DYLIB=$(OBJROOT)/$(LIBKXLD_DYLIB)
51 LIBKXLDOBJ_ARCHIVE=$(OBJROOT)/$(LIBKXLD_ARCHIVE)
52 LIBKXLDSYM_DYLIB=$(SYMROOT)/$(LIBKXLD_DYLIB)
53 LIBKXLDSYM_DYLIB_DSYM=$(SYMROOT)/$(LIBKXLD_DSYM)
54 LIBKXLDSYM_ARCHIVE=$(SYMROOT)/$(LIBKXLD_ARCHIVE)
55 LIBKXLDDST_DYLIB=$(DYLIBDST)/$(LIBKXLD_DYLIB)
56 LIBKXLDDST_ARCHIVE=$(ARCHIVEDST)/$(LIBKXLD_ARCHIVE)
57 TESTSRC=$(SRCROOT)/tests
62 SDK_DIR := $(shell xcodebuild -version -sdk $(SDKROOT) Path)
65 ifeq ($(strip $(SDK_DIR)),)
70 CFLAGS=-std=c99 -Wall -Wextra -Werror -pedantic -Wformat=2 -Wcast-align \
71 -Wwrite-strings -Wshorten-64-to-32 -Wshadow -Winit-self -Wpointer-arith \
72 -Wno-format-y2k -W -Wstrict-prototypes -Wmissing-prototypes -Wreturn-type \
73 -Wcast-qual -Wwrite-strings -Wswitch -Wcast-align -Wbad-function-cast \
74 -Wchar-subscripts -Winline -Wnested-externs -Wredundant-decls -g
75 LDFLAGS=$(ARCHS) -dynamiclib -install_name $(LIBKXLD_INSTALLNAME) \
76 -compatibility_version $(COMPATIBILITY_VERSION) \
77 -current_version $(CURRENT_VERSION) -lstdc++
78 INCLUDES=-I$(HDRSRC) -isystem $(EXTHDRSRC)
81 CFLAGS += -isysroot $(SDK_DIR)
82 LDFLAGS += -isysroot $(SDK_DIR)
86 CC = xcrun -sdk $(SDK_DIR) clang
87 CLANG_ANALYZER = clang --analyze
88 LIBTOOL = xcrun -sdk $(SDK_DIR) libtool
89 STRIP = xcrun -sdk $(SDK_DIR) strip
90 DSYMUTIL = xcrun -sdk $(SDK_DIR) dsymutil
93 HDR_NAMES=kxld.h kxld_types.h
94 OBJ_NAMES=kxld.o kxld_array.o kxld_copyright.o kxld_demangle.o kxld_dict.o \
95 kxld_kext.o kxld_object.o kxld_reloc.o kxld_sect.o kxld_seg.o \
96 kxld_srcversion.o kxld_sym.o kxld_symtab.o kxld_util.o kxld_uuid.o \
97 kxld_vtable.o kxld_versionmin.o
99 HDRS=$(addprefix $(HDRSRC)/, $(HDR_NAMES))
100 OBJS=$(addprefix $(OBJROOT)/, $(OBJ_NAMES))
102 $(shell [ -d $(OBJROOT) ] || mkdir -p $(OBJROOT))
106 $(OBJROOT)/%.o : $(OBJSRC)/%.c
107 $(CC) $(RC_CFLAGS) $(CFLAGS) $(DEFINES) $(OPTIM) $(INCLUDES) -c $< -o $@
108 $(OBJROOT)/%.o : $(TESTSRC)/%.c
109 $(CC) $(RC_CFLAGS) $(CFLAGS) $(DEFINES) -O0 -DDEBUG $(INCLUDES) -I$(SRCROOT) -c $< -o $@
111 SRCROOTESC=$(subst /,\/,$(SRCROOT))
112 OBJROOTESC=$(subst /,\/,$(OBJROOT))
113 SEDOBJS=sed -E 's/(^[a-z_]+)\.o/$(OBJROOTESC)\/\1\.o $(OBJROOTESC)\/\1\.d/'
114 SEDSRCS=sed -E 's/ ([a-z_]+\.[ch])/ $(SRCROOTESC)\/\1/g'
115 $(OBJROOT)/%.d: $(OBJSRC)/%.c
117 $(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -MM $< | $(SEDOBJS) | $(SEDSRCS) > $@;
120 release: OPTIM=-Os -dynamic
123 debug: OPTIM=-O0 -DDEBUG -dynamic
126 profile: OPTIM=-Os -pg -dynamic
129 tests: OPTIM=-O0 -DDEBUG
130 tests: kxld_array_test kxld_dict_test copyrighttest
132 build: $(LIBKXLDSYM_$(PRODUCT_TYPE))
134 $(LIBKXLDSYM_DYLIB): $(LIBKXLDOBJ_DYLIB)
136 install -c -m 644 $< $@
137 $(DSYMUTIL) $@ -o $(LIBKXLDSYM_DYLIB_DSYM)
139 $(LIBKXLDSYM_ARCHIVE): $(LIBKXLDOBJ_ARCHIVE)
141 install -c -m 644 $< $@
143 $(LIBKXLDOBJ_DYLIB): $(OBJS)
144 $(CC) $(LDFLAGS) -o $@ $^
146 $(LIBKXLDOBJ_ARCHIVE): $(OBJS)
147 $(LIBTOOL) -static -o $@ $^
151 install -o 0 -g 0 -c -m 444 $(HDRS) $(HDRDST)
153 install: release installhdrs $(LIBKXLDDST_$(PRODUCT_TYPE))
155 $(LIBKXLDDST_DYLIB): $(LIBKXLDSYM_DYLIB)
156 @mkdir -p $(DYLIBDST)
157 install -o 0 -g 0 -c -m 555 $< $@
160 $(LIBKXLDDST_ARCHIVE): $(LIBKXLDSYM_ARCHIVE)
161 @mkdir -p $(ARCHIVEDST)
162 install -o 0 -g 0 -c -m 555 $< $@
165 KEXTCOPYOBJS=$(OBJROOT)/kextcopyright.o $(OBJROOT)/kxld_copyright.o $(OBJROOT)/kxld_util.o
166 kextcopyright: $(TESTDST)/kextcopyright
167 $(TESTDST)/kextcopyright: $(KEXTCOPYOBJS)
169 $(CC) $(ARCHS) $(KEXTCOPYOBJS) -framework CoreFoundation -o $(OBJROOT)/kextcopyright
170 install -c -m 755 $(OBJROOT)/kextcopyright $@
172 TESTOBJS=$(OBJROOT)/kxld_test.o $(OBJROOT)/kxld_util.o
174 ARRAYOBJS=$(OBJROOT)/kxld_array_test.o $(OBJROOT)/kxld_array.o $(TESTOBJS)
175 kxld_array_test: $(TESTDST)/kxld_array_test
176 $(TESTDST)/kxld_array_test: $(ARRAYOBJS)
178 $(CC) $(ARCHS) $(ARRAYOBJS) -o $(OBJROOT)/kxld_array_test
179 install -c -m 755 $(OBJROOT)/kxld_array_test $@
181 DICTOBJS=$(OBJROOT)/kxld_dict_test.o $(OBJROOT)/kxld_dict.o $(OBJROOT)/kxld_array.o $(TESTOBJS)
182 kxld_dict_test: $(TESTDST)/kxld_dict_test
183 $(TESTDST)/kxld_dict_test: $(DICTOBJS)
185 $(CC) $(ARCHS) $(DICTOBJS) -o $(OBJROOT)/kxld_dict_test
186 install -c -m 755 $(OBJROOT)/kxld_dict_test $@
188 COPYTESTOBJS=$(OBJROOT)/kxld_copyright.o $(OBJROOT)/kxld_util.o
189 copyrighttest: OPTIM+=-DTEST
190 copyrighttest: $(TESTDST)/copyrighttest
191 $(TESTDST)/copyrighttest: $(COPYTESTOBJS)
193 $(CC) $(ARCHS) $(COPYTESTOBJS) -framework CoreFoundation -framework IOKit -o $(OBJROOT)/copyrighttest
194 install -c -m 755 $(OBJROOT)/copyrighttest $@
197 @$(CLANG_ANALYZER) $(CFLAGS) $(INCLUDES) $(filter-out WKdm%.c,$(wildcard *.c))
198 @$(CLANG_ANALYZER) $(CFLAGS) $(INCLUDES) -I$(SRCROOT) tests/*.c
208 @rm -rf $(OBJROOT) $(DSTROOT) $(SYMROOT)
211 # Automatically build dependency information when .c or .h files change based
212 # on implicit rule for .d:.c
213 -include $(OBJS:.o=.d)