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