Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# -*- mode: makefile;-*-
#
# Copyright (C) 1999-2020 Apple Inc. All rights reserved.
#
# MakeInc.kernel augments the single-architecture
# recursive build system with rules specific
# to assembling and linking a kernel.
#

#
# Validate configuration options
#
ifeq ($(filter $(CURRENT_ARCH_CONFIG),$(SUPPORTED_ARCH_CONFIGS)),)
$(error Unsupported CURRENT_ARCH_CONFIG $(CURRENT_ARCH_CONFIG))
endif

ifeq ($(filter $(CURRENT_KERNEL_CONFIG),$(SUPPORTED_KERNEL_CONFIGS)),)
$(error Unsupported CURRENT_KERNEL_CONFIG $(CURRENT_KERNEL_CONFIG))
endif

ifeq ($(filter $(CURRENT_MACHINE_CONFIG),$(SUPPORTED_$(CURRENT_ARCH_CONFIG)_MACHINE_CONFIGS)),)
$(error Unsupported CURRENT_MACHINE_CONFIG $(CURRENT_MACHINE_CONFIG))
endif

ifeq ($(filter $(PLATFORM),$(SUPPORTED_PLATFORMS)),)
$(error Unsupported PLATFORM $(PLATFORM))
endif

# Generate xnu version file early (from build environment or SDK/KDK)
do_build_setup:: $(XNU_VERSION)

ifeq ($(BUILD_STATIC_LINK),1)
ifeq ($(USE_LTO),1)
# <rdar://problem/46252406>
# To run LTO in the xnu project while linking the final result in KCB, without losing debugging info,
# run ld -r on only the LTO bitcode object files to produce one mach-o for KCB to use, which is added
# to the static link archive, along with the non-LTO objects (not linked, since ld -r on mach-o objects
# does not preserve DWARF.)
PRE_LTO=1
endif
endif

#
# Rules for the highly parallel "build" phase, where each build configuration
# writes into their own $(TARGET) independent of other build configs
#
# There are 5 primary build outputs:
# 1) $(KERNEL_FILE_NAME).unstripped    (raw linked kernel, unstripped)
# 2) $(KERNEL_FILE_NAME)               (stripped kernel, with optional CTF data)
# 3) $(KERNEL_FILE_NAME).dSYM          (dSYM)
# 4) $(KERNEL_FILE_NAME).link          (bits for static linking)
# 5) lib$(KERNEL_FILE_NAME).a          (static archive for testing)

ifeq ($(BUILD_STATIC_LINK),1)
ifeq ($(BUILD_XNU_LIBRARY),1)

KERNEL_STATIC_LINK_TARGETS = \
	$(TARGET)/lib$(KERNEL_FILE_NAME).a
KERNEL_STATIC_LINK_DST = \
	$(DSTROOT)/$(INSTALL_KERNEL_DIR)/lib$(KERNEL_FILE_NAME).a

else

KERNEL_STATIC_LINK_TARGETS = \
	$(TARGET)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).a

KERNEL_STATIC_LINK_DST = \
			$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).a             \
			$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).linkarguments \
			$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).linkarchives  \
			$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).exp           \
			$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).alias.exp     \
			$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/lldbmacros                   \
			$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/$(KERNEL_LLDBBOOTSTRAP_NAME)

endif
endif

do_build_all:: do_build_kernel

.PHONY: do_build_kernel

ifeq ($(BUILD_XNU_LIBRARY),1)
do_build_kernel: $(KERNEL_STATIC_LINK_TARGETS)

else

do_build_kernel: $(TARGET)/$(KERNEL_FILE_NAME) $(TARGET)/$(KERNEL_FILE_NAME).unstripped $(KERNEL_STATIC_LINK_TARGETS)
	@:

ifeq ($(BUILD_DSYM),1)
do_build_all:: do_build_kernel_dSYM
endif

.PHONY: do_build_kernel_dSYM

do_build_kernel_dSYM: $(TARGET)/$(KERNEL_FILE_NAME).dSYM
	@:

endif

do_build_all:: do_build_compile_commands_json

.PHONY: do_build_compile_commands_json

do_build_compile_commands_json: $(TARGET)/compile_commands.json
	@:

.LDFLAGS: ALWAYS
	$(_v)$(REPLACECONTENTS) $@ $(LD) $(LDFLAGS_KERNEL) $(LDFLAGS_KERNEL_ONLY) $(LD_KERNEL_LIBS)
.CFLAGS: ALWAYS
	$(_v)$(REPLACECONTENTS) $@ $(KCC) $(CFLAGS) $(INCFLAGS)


#
# CTF generation requires the following files:

#     kernel.unstripped.noctf  Unstripped kernel with no CTF
#     kernel.ctf               CTF from the dSYM associated with kernel.unstripped.noctf
#     kernel.unstripped        kernel.unstripped.noctf merged with kernel.ctf (Debug Rich Executable)
#     kernel                   Stripped kernel.unstripped.noctf merged with kernel.ctf (Final Kernel)

# Note: "kernel" here is a placeholder for the actual name of the kernel which
# varies.
#


# Convert DWARF to CTF
$(TARGET)/$(KERNEL_FILE_NAME).ctf: $(TARGET)/$(KERNEL_FILE_NAME).dSYM $(TARGET)/$(KERNEL_FILE_NAME).unstripped.noctf
ifeq ($(DO_CTFMERGE),1)
	@$(LOG_CTFCONVERT) "$(@F)"
	$(_v)$(CTFCONVERT) -c -l xnu -u xnu -o $@ $(TARGET)/$(KERNEL_FILE_NAME).dSYM/Contents/Resources/DWARF/$(KERNEL_FILE_NAME)
endif

# Strip the kernel and merge in the CTF
$(TARGET)/$(KERNEL_FILE_NAME): $(TARGET)/$(KERNEL_FILE_NAME).unstripped.noctf $(TARGET)/$(KERNEL_FILE_NAME).dSYM $(TARGET)/$(KERNEL_FILE_NAME).ctf
	@$(LOG_STRIP) "$(@F)"
	$(_v)$(STRIP) $(STRIP_FLAGS) $< -o $@
	@echo "built kernel at $@"
	$(_v)$(RM) $@.ctfdata
ifeq ($(DO_CTFMERGE),1)
	@$(LOG_CTFMERGE) "$(@F)"
	$(_v)$(CTFMERGE) -l xnu -o $@ -Z $@.ctfdata $@.ctf
	$(_v)if [ -s $@.ctfdata ]; then                          \
		$(LOG_CTFINSERT) "$(@F)";                            \
		$(CTFINSERT) $@	$(ARCH_FLAGS_$(CURRENT_ARCH_CONFIG)) \
			     $@.ctfdata -o $@;                           \
	else                                                     \
	    exit 1;                                              \
	fi;
	-$(_v)$(CTFDUMP) -S $@ $(_vstdout) $(_vstderr)
endif
	$(_v)$(LN) $(call function_convert_build_config_to_objdir,$(CURRENT_BUILD_CONFIG))/$(KERNEL_FILE_NAME) $(OBJROOT)/$(KERNEL_FILE_NAME)

# Merge the CTF into the unstripped kernel (Debug Rich Executable)
$(TARGET)/$(KERNEL_FILE_NAME).unstripped: $(TARGET)/$(KERNEL_FILE_NAME).ctf $(TARGET)/$(KERNEL_FILE_NAME).unstripped.noctf
	$(_v)$(CP) $(TARGET)/$(KERNEL_FILE_NAME).unstripped.noctf $(TARGET)/$(KERNEL_FILE_NAME).unstripped
	$(_v)$(RM) $@.ctfdata
ifeq ($(DO_CTFMERGE),1)
	@$(LOG_CTFMERGE) "$(@F)"
	$(_v)$(CTFMERGE) -l xnu -o $@ -Z $@.ctfdata $(TARGET)/$(KERNEL_FILE_NAME).ctf
	$(_v)if [ -s $@.ctfdata ]; then                          \
		$(LOG_CTFINSERT) "$(@F)";                            \
		$(CTFINSERT) $@	$(ARCH_FLAGS_$(CURRENT_ARCH_CONFIG)) \
			     $@.ctfdata -o $@;                           \
	else                                                     \
	    exit 1;                                              \
	fi;
	-$(_v)$(CTFDUMP) -S $@ $(_vstdout) $(_vstderr)
endif

$(TARGET)/$(KERNEL_FILE_NAME).dSYM: $(TARGET)/$(KERNEL_FILE_NAME).unstripped.noctf
ifeq ($(BUILD_DSYM),1)
	@$(LOG_DSYMUTIL) "$(@F)"
	$(_v)$(BASH) -c "$(DSYMUTIL) $(DSYMUTIL_FLAGS) $< -o $@ $(_vstdout) 2> >(grep -v '^warning:.*could not find object file symbol for symbol' 1>&2)"
	$(_v)$(MV) $@/$(DSYMDWARFDIR)/$(KERNEL_FILE_NAME).unstripped.noctf $@/$(DSYMDWARFDIR)/$(KERNEL_FILE_NAME)
else
	$(_v)$(MKDIR) $@
endif
	$(_v)$(TOUCH) $@

ifeq ($(BUILD_XNU_LIBRARY),1)
$(TARGET)/lib$(KERNEL_FILE_NAME).a: $(addprefix $(TARGET)/,$(foreach component,$(COMPONENT_LIST),$(component)/$(CURRENT_KERNEL_CONFIG)/$(component).libfilelist)) nonlto.o version.o .LDFLAGS $(filter %/MakeInc.kernel,$(MAKEFILE_LIST))
	@$(LOG_LIBTOOL) "$(@F)"
	$(_v)$(CAT) $(filter %.libfilelist,$+) < /dev/null > link.filelist
	$(_v)$(LIBTOOL) -static -csD -filelist link.filelist -o $@
	$(_v)$(LN) $(call function_convert_build_config_to_objdir,$(CURRENT_BUILD_CONFIG))/lib$(KERNEL_FILE_NAME).a $(OBJROOT)/lib$(KERNEL_FILE_NAME).a
endif

$(TARGET)/$(KERNEL_FILE_NAME).unstripped.noctf: $(addprefix $(TARGET)/,$(foreach component,$(COMPONENT_LIST),$(component)/$(CURRENT_KERNEL_CONFIG)/$(component).filelist)) lastkerneldataconst.o lastkernelconstructor.o nonlto.o version.o $(LDFILES_KERNEL_ONLY) .LDFLAGS $(filter %/MakeInc.kernel,$(MAKEFILE_LIST))
ifeq ($(PRE_LTO),1)
	@$(LOG_LTO) "$(@F)"
	$(_v)$(RM) ltolink.filelist
	$(_v)$(RM) nonltolink.filelist
	$(_v)$(RM) -r $(TARGET)/justlto.o
	$(_v)files="$$($(CAT) $(filter %.filelist,$+)) $(filter %.o,$+)"; \
	for ofile in $$files; \
	do \
		hdr=$$(od -An -N 4 -t x4 $$ofile); \
		if [ $$hdr = "0b17c0de" ]; \
			then \
				if [ -z "$$lto" ]; \
					then \
						lto="$$ofile"; \
					else \
						lto="$$(printf '%s\n%s' "$$lto" "$$ofile")"; \
				fi; \
			else \
				if [ -z "$$nonlto" ]; \
					then \
						nonlto="$$ofile"; \
					else \
						nonlto="$$(printf '%s\n%s' "$$nonlto" "$$ofile")"; \
				fi; \
			fi; \
	done; \
	printf '%s\n' "$$lto" >ltolink.filelist; \
	printf '%s\n' "$$nonlto" >nonltolink.filelist
	@$(LOG_LD) "$(@F)"
	$(_v)if [ -s ltolink.filelist ]; \
	then \
		$(LD) $($(addsuffix $(CURRENT_ARCH_CONFIG),ARCH_FLAGS_)) -r nonlto.o -filelist ltolink.filelist $(LDFLAGS_KERNEL_LTO) -Wl,-object_path_lto,$(TARGET)/justlto.o -o $(TARGET)/justlto.tmp.o && \
		if test -d $(TARGET)/justlto.o; \
		then \
		    $(LD) $(LDFLAGS_KERNEL) $(LDFLAGS_KERNEL_ONLY) -filelist nonltolink.filelist $(TARGET)/justlto.o/*.o $(LDFLAGS_KERNEL_STRIP_LTO) -o $@ $(LD_KERNEL_LIBS) $(LD_KERNEL_ARCHIVES); \
		else \
		    $(LD) $(LDFLAGS_KERNEL) $(LDFLAGS_KERNEL_ONLY) -filelist nonltolink.filelist $(TARGET)/justlto.o $(LDFLAGS_KERNEL_STRIP_LTO) -o $@ $(LD_KERNEL_LIBS) $(LD_KERNEL_ARCHIVES); \
		fi; \
	else \
		$(LD) $(LDFLAGS_KERNEL) $(LDFLAGS_KERNEL_ONLY) -filelist nonltolink.filelist -o $@ $(LD_KERNEL_LIBS) $(LD_KERNEL_ARCHIVES); \
	fi
else
	@$(LOG_LD) "$(@F)"
	$(_v)$(CAT) $(filter %.filelist,$+) < /dev/null > link.filelist
	$(_v)$(LD) $(LDFLAGS_KERNEL) $(LDFLAGS_KERNEL_ONLY) -filelist link.filelist $(filter %.o,$+) -o $@ $(LD_KERNEL_LIBS) $(LD_KERNEL_ARCHIVES)
endif

$(TARGET)/compile_commands.json: $(addprefix $(TARGET)/,$(foreach component,$(COMPONENT_LIST),$(component)/$(CURRENT_KERNEL_CONFIG)/$(component).filelist))
	$(_v)files="$$(sed -e 's/$$/.json/' $(filter %.filelist,$+))"; \
	sed -e '1s/^/[\'$$'\n''/' -e '$$s/,$$/\'$$'\n'']/' $$files > compile_commands.json

# for now, rename LASTDATA_CONST to LAST on static kernel cache builds
EXTRA_KC_LINKARGS = -Wl,-rename_segment,__LASTDATA_CONST,__LAST

$(TARGET)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).a:   $(TARGET)/$(KERNEL_FILE_NAME).unstripped .LDFLAGS $(filter %/MakeInc.kernel,$(MAKEFILE_LIST))
	@$(LOG_LIBTOOL) "$(@F)"
	$(_v)$(MKDIR) $(dir $@)
ifeq ($(PRE_LTO),1)
	$(_v)if [ -d $(TARGET)/justlto.o ]; \
	then \
	    $(LIBTOOL) -ca $(TARGET)/justlto.o/*.o -filelist nonltolink.filelist -o $@; \
	else \
	    $(LIBTOOL) -ca $(TARGET)/justlto.o -filelist nonltolink.filelist -o $@; \
	fi
else
	$(_v)$(LIBTOOL) -ca -filelist link.filelist version.o lastkerneldataconst.o lastkernelconstructor.o -o $@
endif
	$(_v)cp $(TARGET)/all-kpi.exp $(TARGET)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).exp
	$(_v)cp $(TARGET)/all-alias.exp $(TARGET)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).alias.exp
	$(_v)echo "$(LD_KERNEL_ARCHIVES)" >$(TARGET)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).linkarchives
	$(_v)echo "$(LDFLAGS_KERNEL) $(LD_KERNEL_LIBS) $(EXTRA_KC_LINKARGS)" >$(TARGET)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).linkarguments
	$(_v)$(LN) $(call function_convert_build_config_to_objdir,$(CURRENT_BUILD_CONFIG))/$(KERNEL_FILE_NAME).link $(OBJROOT)/$(KERNEL_FILE_NAME).link

nonlto.o: .CFLAGS $(filter %/MakeInc.kernel,$(MAKEFILE_LIST))
nonlto.o:   $(SRCROOT)/libsa/nonlto.c
	${C_RULE_0}
	${C_RULE_1A}$< $(CFLAGS_NOLTO_FLAG)
	${C_RULE_2}

-include version.d
version.o: .CFLAGS $(filter %/MakeInc.kernel,$(MAKEFILE_LIST))
version.o: $(OBJPATH)/version.c
	${C_RULE_0}
	${C_RULE_1A}$<
	${C_RULE_2}
	${C_RULE_4}

$(OBJPATH)/version.c: $(SRCROOT)/config/version.c.template $(NEWVERS) $(XNU_VERSION)
	$(_v)$(CP) $< $@
	$(_v)$(NEWVERS) $@ > /dev/null;

# Generate xnuVersion file from B&I env var or the System.kext/Info.plist
# found in the SDK or KDK. Only update the file if the version number has
# actually changed to avoid rebuilding dependent source files unnecessarily.
ifeq ($(origin RC_DARWIN_KERNEL_VERSION), undefined)
SDK_SYSTEM_KEXT_INFO_PLIST := $(firstword $(wildcard $(addsuffix $(SYSTEM_KEXT_INFO_PLIST),$(SDKROOT_RESOLVED) $(KDKROOT))))
ifneq ($(SDK_SYSTEM_KEXT_INFO_PLIST),)
RC_DARWIN_KERNEL_VERSION := $(strip $(shell $(PLUTIL) -extract CFBundleVersion raw -n $(SDK_SYSTEM_KEXT_INFO_PLIST)))
endif
endif
ifeq ($(RC_DARWIN_KERNEL_VERSION),)
$(error Could not determine xnu version from SDK or KDK! Set RC_DARWIN_KERNEL_VERSION environment variable.)
endif
EXISTING_KERNEL_VERSION := $(strip $(shell test -r $(XNU_VERSION) && head -1 $(XNU_VERSION)))
ifneq ($(EXISTING_KERNEL_VERSION), $(RC_DARWIN_KERNEL_VERSION))
$(XNU_VERSION): ALWAYS
	@$(LOG_GENERATE) "$(notdir $@)$(Color0) with version \"$(ColorF)$(RC_DARWIN_KERNEL_VERSION)$(Color0)\""
	$(_v)printf > $@ '%s\n' "$(RC_DARWIN_KERNEL_VERSION)"
else ifeq ($(VERBOSE),YES)
$(XNU_VERSION): ALWAYS
	@echo "xnuVersion unchanged: checked for \"$(RC_DARWIN_KERNEL_VERSION)\" in $@"
endif

-include lastkerneldataconst.d
lastkerneldataconst.o: .CFLAGS $(filter %/MakeInc.kernel,$(MAKEFILE_LIST))
lastkerneldataconst.o: $(SRCROOT)/libsa/lastkerneldataconst.c
	${C_RULE_0}
	${C_RULE_1A}$<
	${C_RULE_2}


lastkernelconstructor.o_CFLAGS_RM = -fprofile-instr-generate -fcoverage-mapping
# the LAST segment is mapped read-only on arm, so if we include llvm profiling
# here it will segfault the kernel.  (see arm_vm_init.c) We don't currently have
# a way of retrieving these counters from LAST anyway, so there's no harm in just
# disabling them.

LAST_FILES=lastkernelconstructor.o
-include lastkernelconstructor.d
lastkernelconstructor.o: .CFLAGS $(filter %/MakeInc.kernel,$(MAKEFILE_LIST))
lastkernelconstructor.o: $(SRCROOT)/libsa/lastkernelconstructor.c
	${C_RULE_0}
	${C_RULE_1A}$< $(CFLAGS_NOLTO_FLAG)
	${C_RULE_2}
	${C_RULE_3}
	${C_RULE_4}
	$(_v)for last_file in ${LAST_FILES};				\
	do							\
		$(SEG_HACK) -s __DATA -n __LASTDATA_CONST -o $${last_file}__ $${last_file} || exit 1; \
		mv $${last_file}__ $${last_file} || exit 1;		\
	done
EXTRA_KC_LINKARGS = -Wl,-rename_segment,__LASTDATA_CONST,__LAST

#
# Install rules. Each build config is classified as "primary" (the first
# config for an architecture) or "non-primary". Primary build configs
# have the semantic of competing to *combine* single-architecture
# files into a multi-architecture output in the DSTROOT, like
# $(DSTROOT)/$(KERNEL_FILE_NAME), and consequently each primary build config
# has its install target run serially with respect to other primary
# build configs. Non-primary build configs will never compete for
# files in the DSTROOT or SYMROOT, and can be installed in parallel
# with other non-primary configs (and even primary configs)
#

do_build_install_primary:: do_install_machine_specific_kernel
ifeq ($(BUILD_DSYM),1)
do_build_install_primary:: do_install_machine_specific_kernel_dSYM
endif

do_build_install_non_primary:: do_install_machine_specific_kernel
ifeq ($(BUILD_DSYM),1)
do_build_install_non_primary:: do_install_machine_specific_kernel_dSYM
endif

ifeq ($(BUILD_DSYM),1)
ifeq ($(INSTALL_KERNEL_SYM_TO_KDK),1)
do_build_install_primary:: do_install_machine_specific_KDK_dSYM
do_build_install_non_primary:: do_install_machine_specific_KDK_dSYM
endif
endif

ifneq ($(BUILD_XNU_LIBRARY),1)
ifeq ($(INSTALL_XNU_DEBUG_FILES),1)
do_build_install_primary:: do_install_xnu_debug_files
endif

.PHONY: do_install_xnu_debug_files

do_install_xnu_debug_files:	$(DSTROOT)/$(DEVELOPER_EXTRAS_DIR)/README.DEBUG-kernel.txt
	@:
endif

#
# If the timestamp indicates the DSTROOT kernel is out of
# date, start over. Normal dependencies don't work because we can have
# ( BUILDA, BUILDB, INSTALLB, INSTALLA ) in which case at INSTALLA time
# the timestamps would $(DSTROOT)/$(KERNEL_FILE_NAME) is not out of date compared
# to BUILDA. So we maintain a separate file at the time make(1)
# was run and use it to determine what actions to take
#

$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME): $(TARGET)/$(KERNEL_FILE_NAME) ALWAYS
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALL) "$(@F)$(Color0) ($(ColorLF)$(CURRENT_ARCH_CONFIG_LC)$(Color0))"
    # -nt and -ot are evaluated differently by bash, dash, and zsh (and are not part of the POSIX specification).
    # Explicitly specify what should happen when the right hand file doesn't exist.
	$(_v)if [ $(OBJROOT)/.mach_kernel.timestamp -nt $@ -o \( -e $(OBJROOT)/.mach_kernel.timestamp -a \! -e $@ \) ]; then	\
		$(INSTALL) $(EXEC_INSTALL_FLAGS) $< $@;			\
		cmdstatus=$$?;						\
	else								\
		$(LIPO) -create $@ $< -output $@;			\
		cmdstatus=$$?;						\
	fi;								\
	exit $$cmdstatus

ifeq ($(BUILD_STATIC_LINK),1)
ifeq ($(BUILD_XNU_LIBRARY),1)

$(DSTROOT)/$(INSTALL_KERNEL_DIR)/lib$(KERNEL_FILE_NAME).a: $(TARGET)/lib$(KERNEL_FILE_NAME).a ALWAYS
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALL) "$(@F)"
	$(_v)$(INSTALL) $(INSTALL_FLAGS) $< $@

else
$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).a: $(TARGET)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).a ALWAYS
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALL) "$(@F)"
	$(_v)$(INSTALL) $(INSTALL_FLAGS) $< $@

$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).linkarguments: $(TARGET)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).linkarguments ALWAYS
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALL) "$(@F)"
	$(_v)$(INSTALL) $(INSTALL_FLAGS) $< $@

$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).linkarchives: $(TARGET)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).linkarchives ALWAYS
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALL) "$(@F)"
	$(_v)$(INSTALL) $(INSTALL_FLAGS) $< $@

$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).exp: $(TARGET)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).exp ALWAYS
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALL) "$(@F)"
	$(_v)$(INSTALL) $(INSTALL_FLAGS) $< $@

$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).alias.exp: $(TARGET)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).alias.exp ALWAYS
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALL) "$(@F)"
	$(_v)$(INSTALL) $(INSTALL_FLAGS) $< $@
endif

# BUILD_STATIC_LINK
endif

$(SYMROOT)/$(KERNEL_FILE_NAME): $(TARGET)/$(KERNEL_FILE_NAME).unstripped ALWAYS
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALLSYM) "$(@F)$(Color0) ($(ColorLF)$(CURRENT_ARCH_CONFIG_LC)$(Color0))"
    # -nt and -ot are evaluated differently by bash, dash, and zsh (and are not part of the POSIX specification).
    # Explicitly specify what should happen when the right hand file doesn't exist.
	$(_v)if [ $(OBJROOT)/.mach_kernel.timestamp -nt $@ -o \( -e $(OBJROOT)/.mach_kernel.timestamp -a \! -e $@ \) ]; then		\
		$(INSTALL) $(EXEC_INSTALL_FLAGS) $< $@;				\
		cmdstatus=$$?;							\
	else									\
		$(LIPO) -create $@ $< -output $@;				\
		cmdstatus=$$?;							\
	fi;									\
	exit $$cmdstatus


$(SYMROOT)/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/lldbmacros                                                                     \
$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/lldbmacros                      \
$(DSTROOT)/$(INSTALL_KERNEL_SYM_DIR)/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/lldbmacros:                                          \
$(TARGET)/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/lldbmacros
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALLMACROS) "$(@F)$(Color0) $(ColorLF)$(CURRENT_ARCH_CONFIG_LC)$(Color0))"
	$(_v)$(CP) -r $< $(dir $@)
	$(_v)$(TOUCH) $@

$(SYMROOT)/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/$(KERNEL_LLDBBOOTSTRAP_NAME)                                                   \
$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME).link/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/$(KERNEL_LLDBBOOTSTRAP_NAME)    \
$(DSTROOT)/$(INSTALL_KERNEL_SYM_DIR)/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/$(KERNEL_LLDBBOOTSTRAP_NAME):                        \
$(TARGET)/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/$(KERNEL_LLDBBOOTSTRAP_NAME)
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALLMACROS) "$(@F)$(Color0) ($(ColorLF)$(CURRENT_ARCH_CONFIG_LC)$(Color0))"
	$(_v)$(INSTALL) $(INSTALL_FLAGS) $< $@

$(DSTROOT)/$(DEVELOPER_EXTRAS_DIR)/README.DEBUG-kernel.txt: $(SRCROOT)/config/README.DEBUG-kernel.txt
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALL) "$(@F)"
	$(_v)$(INSTALL) $(INSTALL_FLAGS) $< $@

$(SYMROOT)/$(KERNEL_FILE_NAME).dSYM/$(DSYMINFODIR)/Info.plist $(DSTROOT)/$(INSTALL_KERNEL_SYM_DIR)/$(KERNEL_FILE_NAME).dSYM/$(DSYMINFODIR)/Info.plist: $(TARGET)/$(KERNEL_FILE_NAME).dSYM/$(DSYMINFODIR)/Info.plist
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALLSYM) "$(ColorL)dSYM$(Color0) $(ColorF)$(@F)$(Color0) ($(ColorLF)$(CURRENT_ARCH_CONFIG_LC)$(Color0))"
	$(_v)$(INSTALL) $(INSTALL_FLAGS) $< $@

$(SYMROOT)/$(KERNEL_FILE_NAME).dSYM/$(DSYMDWARFDIR)/$(KERNEL_FILE_NAME) $(DSTROOT)/$(INSTALL_KERNEL_SYM_DIR)/$(KERNEL_FILE_NAME).dSYM/$(DSYMDWARFDIR)/$(KERNEL_FILE_NAME): $(TARGET)/$(KERNEL_FILE_NAME).dSYM/$(DSYMDWARFDIR)/$(KERNEL_FILE_NAME) ALWAYS
	$(_v)$(MKDIR) $(dir $@)
	@$(LOG_INSTALLSYM) "$(ColorL)dSYM$(Color0) $(ColorF)$(@F).dSYM$(ColorF) ($(ColorLF)$(CURRENT_ARCH_CONFIG_LC)$(Color0))"
    # -nt and -ot are evaluated differently by bash, dash, and zsh (and are not part of the POSIX specification).
    # Explicitly specify what should happen when the right hand file doesn't exist.
	$(_v)if [ $(OBJROOT)/.mach_kernel.timestamp -nt $@ -o \( -e $(OBJROOT)/.mach_kernel.timestamp -a \! -e $@ \) ]; then			\
		$(INSTALL) $(EXEC_INSTALL_FLAGS) $< $@;					\
		cmdstatus=$$?;								\
	else										\
		$(LIPO) -create $@ $< -output $@;					\
		cmdstatus=$$?;								\
	fi;										\
	exit $$cmdstatus

.PHONY: do_install_machine_specific_kernel do_install_machine_specific_kernel_dSYM
.PHONY: do_install_machine_specific_KDK_dSYM

ifeq ($(BUILD_XNU_LIBRARY),1)

do_install_machine_specific_kernel: $(KERNEL_STATIC_LINK_DST)
	@:
do_install_machine_specific_kernel_dSYM:
	@:

else

do_install_machine_specific_kernel: $(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME)                \
			$(SYMROOT)/$(KERNEL_FILE_NAME)                                                              \
			$(KERNEL_STATIC_LINK_DST)
	@:

do_install_machine_specific_kernel_dSYM: \
			$(SYMROOT)/$(KERNEL_FILE_NAME).dSYM/$(DSYMINFODIR)/Info.plist \
			$(SYMROOT)/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/lldbmacros \
			$(SYMROOT)/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/$(KERNEL_LLDBBOOTSTRAP_NAME) \
			$(SYMROOT)/$(KERNEL_FILE_NAME).dSYM/$(DSYMDWARFDIR)/$(KERNEL_FILE_NAME)
	@:

do_install_machine_specific_KDK_dSYM: \
			$(DSTROOT)/$(INSTALL_KERNEL_SYM_DIR)/$(KERNEL_FILE_NAME).dSYM/$(DSYMINFODIR)/Info.plist \
			$(DSTROOT)/$(INSTALL_KERNEL_SYM_DIR)/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/lldbmacros \
			$(DSTROOT)/$(INSTALL_KERNEL_SYM_DIR)/$(KERNEL_FILE_NAME).dSYM/$(DSYMLLDBMACROSDIR)/$(KERNEL_LLDBBOOTSTRAP_NAME) \
			$(DSTROOT)/$(INSTALL_KERNEL_SYM_DIR)/$(KERNEL_FILE_NAME).dSYM/$(DSYMDWARFDIR)/$(KERNEL_FILE_NAME)
	@:

endif

# The $(RM) is needed so that the $(LN) doesn't dereference an existing
# symlink during incremental builds and create a new symlink inside
# the target of the existing symlink
ifneq ($(INSTALLHDRS_SKIP_HOST),YES)
do_installhdrs_mi:: $(DSTROOT)/$(KRESDIR)/Info.plist
	$(_v)$(MKDIR) $(DSTROOT)/$(KINCFRAME)
	$(_v)$(MKDIR) $(DSTROOT)/$(KPINCDIR)
	$(_v)$(MKDIR) $(DSTROOT)/$(KRESDIR)
	$(_v)$(MKDIR) $(DSTROOT)/$(KLIBCXXINCDIR)
	$(_v)$(RM) $(DSTROOT)/$(KINCFRAME)/Versions/Current
	$(_v)$(LN) $(KINCVERS) $(DSTROOT)/$(KINCFRAME)/Versions/Current
	$(_v)$(RM) $(DSTROOT)/$(KINCFRAME)/Headers
	$(_v)$(LN) Versions/Current/Headers			\
		   $(DSTROOT)/$(KINCFRAME)/Headers
	$(_v)$(RM) $(DSTROOT)/$(KINCFRAME)/PrivateHeaders
	$(_v)$(LN) Versions/Current/PrivateHeaders		\
		   $(DSTROOT)/$(KINCFRAME)/PrivateHeaders
	$(_v)$(RM) $(DSTROOT)/$(KINCFRAME)/Resources
	$(_v)$(LN) Versions/Current/Resources			\
		   $(DSTROOT)/$(KINCFRAME)/Resources
endif

ifneq ($(INSTALLHDRS_SKIP_HOST),YES)
$(DSTROOT)/$(KRESDIR)/Info.plist: $(SOURCE)/EXTERNAL_HEADERS/Info.plist $(NEWVERS) $(XNU_VERSION)
	$(_v)$(MKDIR) $(DSTROOT)/$(KRESDIR)
	$(_v)$(INSTALL) $(DATA_INSTALL_FLAGS) $< $@
	$(_v)$(NEWVERS) $@ $(_vstdout)
ifeq ($(USE_BINARY_PLIST),1)
	$(_v)$(PLUTIL) -convert binary1 -o $@ $@
endif
endif

$(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(ALIAS_FILE_NAME): ALWAYS
	@$(LOG_ALIAS) "$(@F)$(Color0) ($(ColorLF)$(CURRENT_ARCH_CONFIG_LC)$(Color0) $(ColorLF)$(CURRENT_MACHINE_CONFIG_LC)$(Color0) $(ColorLF)$(CURRENT_ALIAS_MACHINE_CONFIG_LC)$(Color0))"
	$(_v)$(INSTALL) $(EXEC_INSTALL_FLAGS) $(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(KERNEL_FILE_NAME) $@

install_alias: $(DSTROOT)/$(INSTALL_KERNEL_DIR)/$(ALIAS_FILE_NAME)

print_exports:
	$(_v)printenv | sort

# vim: set ft=make: