]> git.saurik.com Git - apple/xnu.git/blob - libkern/kxld/Makefile
xnu-2050.7.9.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 i386 -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_INSTALLNAME=/usr/lib/system/$(LIBKXLD_DYLIB)
49 LIBKXLDOBJ_DYLIB=$(OBJROOT)/$(LIBKXLD_DYLIB)
50 LIBKXLDOBJ_ARCHIVE=$(OBJROOT)/$(LIBKXLD_ARCHIVE)
51 LIBKXLDSYM_DYLIB=$(SYMROOT)/$(LIBKXLD_DYLIB)
52 LIBKXLDSYM_ARCHIVE=$(SYMROOT)/$(LIBKXLD_ARCHIVE)
53 LIBKXLDDST_DYLIB=$(DYLIBDST)/$(LIBKXLD_DYLIB)
54 LIBKXLDDST_ARCHIVE=$(ARCHIVEDST)/$(LIBKXLD_ARCHIVE)
55 TESTSRC=$(SRCROOT)/tests
56 TESTDST=./BUILD/tests
57
58 # Flags
59 SDKROOT ?= /
60 CFLAGS=-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
65 LDFLAGS=$(ARCHS) -dynamiclib -install_name $(LIBKXLD_INSTALLNAME) \
66 -compatibility_version $(COMPATIBILITY_VERSION) \
67 -current_version $(CURRENT_VERSION) -lstdc++
68 INCLUDES=-I$(HDRSRC) -I$(EXTHDRSRC)
69
70 ifneq ($(SDKROOT),/)
71 CFLAGS += -isysroot $(SDKROOT)
72 LDFLAGS += -isysroot $(SDKROOT)
73 endif
74
75 # Tools
76 CC = xcrun -sdk $(SDKROOT) clang
77 CLANG_ANALYZER = clang --analyze
78 LIBTOOL = xcrun -sdk $(SDKROOT) libtool
79 STRIP = xcrun -sdk $(SDKROOT) strip
80
81 # Files
82 HDR_NAMES=kxld.h kxld_types.h WKdm.h
83 OBJ_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
88 HDRS=$(addprefix $(HDRSRC)/, $(HDR_NAMES))
89 OBJS=$(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
100 SRCROOTESC=$(subst /,\/,$(SRCROOT))
101 OBJROOTESC=$(subst /,\/,$(OBJROOT))
102 SEDOBJS=sed -E 's/(^[a-z_]+)\.o/$(OBJROOTESC)\/\1\.o $(OBJROOTESC)\/\1\.d/'
103 SEDSRCS=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
109 release: OPTIM=-Os -dynamic
110 release: build
111
112 debug: OPTIM=-O0 -DDEBUG -dynamic
113 debug: build
114
115 profile: OPTIM=-Os -pg -dynamic
116 profile: build
117
118 tests: OPTIM=-O0 -DDEBUG
119 tests: kxld_array_test kxld_dict_test copyrighttest
120
121 build: $(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
137 installhdrs:
138 @mkdir -p $(HDRDST)
139 install -o 0 -g 0 -c -m 444 $(HDRS) $(HDRDST)
140
141 install: 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
153 KEXTCOPYOBJS=$(OBJROOT)/kextcopyright.o $(OBJROOT)/kxld_copyright.o $(OBJROOT)/kxld_util.o
154 kextcopyright: $(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
160 TESTOBJS=$(OBJROOT)/kxld_test.o $(OBJROOT)/kxld_util.o
161
162 ARRAYOBJS=$(OBJROOT)/kxld_array_test.o $(OBJROOT)/kxld_array.o $(TESTOBJS)
163 kxld_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
169 DICTOBJS=$(OBJROOT)/kxld_dict_test.o $(OBJROOT)/kxld_dict.o $(OBJROOT)/kxld_array.o $(TESTOBJS)
170 kxld_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
176 COPYTESTOBJS=$(OBJROOT)/kxld_copyright.o $(OBJROOT)/kxld_util.o
177 copyrighttest: OPTIM+=-DTEST
178 copyrighttest: $(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
184 analyze:
185 @$(CLANG_ANALYZER) *.c
186 @$(CLANG_ANALYZER) -I. tests/*.c
187 @rm -f *.plist
188
189 clean:
190 @rm -rf $(OBJROOT)/*
191
192 fullclean:
193 ifdef ROOT
194 @rm -rf $(ROOT)
195 else
196 @rm -rf $(OBJROOT) $(DSTROOT) $(SYMROOT)
197 endif
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