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