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