kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

bootstrap.mk (5332B)


      1 # mk/bootstrap.mk
      2 # ===========================================================================
      3 # Three-stage self-build verification. Included after mk/rt.mk because it
      4 # references RT_BUILD_DIR / RT_DEFAULT_VARIANTS.
      5 #
      6 # Build kit with the host compiler as stage 1, then rebuild it twice through
      7 # the normal Makefile using kit's busybox-style cc/ar/ld symlinks. Stages 2
      8 # and 3 must be bitwise identical.
      9 
     10 .PHONY: bootstrap bootstrap-debug bootstrap-release _do-bootstrap
     11 
     12 BOOTSTRAP_DIR = $(BUILD_DIR)/bootstrap
     13 BOOTSTRAP_STAGE1_DIR = $(BOOTSTRAP_DIR)/stage1
     14 BOOTSTRAP_STAGE2_DIR = $(BOOTSTRAP_DIR)/stage2
     15 BOOTSTRAP_STAGE3_DIR = $(BOOTSTRAP_DIR)/stage3
     16 BOOTSTRAP_STAGE1_BIN = $(BOOTSTRAP_STAGE1_DIR)/kit
     17 BOOTSTRAP_STAGE2_BIN = $(BOOTSTRAP_STAGE2_DIR)/kit
     18 BOOTSTRAP_STAGE3_BIN = $(BOOTSTRAP_STAGE3_DIR)/kit
     19 BOOTSTRAP_TOOLS = cc ld ar ranlib as
     20 BOOTSTRAP_HOST_MODE_CFLAGS =
     21 BOOTSTRAP_HOST_MODE_LDFLAGS =
     22 
     23 # When kit itself is the compiler (stages 2/3), drive its hosted profile with a
     24 # plain -lc on both the compile and link steps, on every host. -lc makes kit
     25 # resolve the hosted libc and add its include + library dirs (the system
     26 # headers + libc search path), which is exactly what stages 2/3 need. This
     27 # replaces the host CC's HOST_SYSROOT_{C,LD}FLAGS (e.g. Darwin's -isysroot): kit
     28 # discovers the SDK / sysroot via its own hosted profile + host probe rather
     29 # than an -isysroot it doesn't interpret the same way. Stage1 is built by the
     30 # host CC (the outer make) and keeps env.mk's HOST_SYSROOT_* untouched.
     31 BOOTSTRAP_KIT_TOOLCHAIN_FLAGS = HOST_SYSROOT_CFLAGS=-lc HOST_SYSROOT_LDFLAGS=-lc
     32 
     33 # Portable SHA-256 display: macOS uses shasum, FreeBSD/Linux have sha256sum.
     34 BOOTSTRAP_SHASUM = shasum -a 256
     35 ifeq ($(HOST_OS),freebsd)
     36 BOOTSTRAP_SHASUM = sha256sum
     37 endif
     38 ifeq ($(HOST_OS),linux)
     39 BOOTSTRAP_SHASUM = sha256sum
     40 endif
     41 BOOTSTRAP_STAMP = $(BOOTSTRAP_DIR)/.stamp
     42 BOOTSTRAP_RT_LIBS = $(addprefix $(RT_BUILD_DIR)/,$(addsuffix /libkit_rt.a,$(RT_DEFAULT_VARIANTS)))
     43 BOOTSTRAP_MAKEFILES = Makefile mk/env.mk mk/config.mk mk/flags.mk \
     44                       mk/lib_srcs.mk mk/driver_srcs.mk mk/rt.mk \
     45                       mk/bootstrap.mk
     46 
     47 # BOOTSTRAP_SEED: a pre-built stage1 kit. Empty (default) = the normal native
     48 # flow where stage1 is the host $(BIN). When set (a path), stage1 is copied from
     49 # the seed and the host $(BIN) / outer rt are NOT prerequisites — this is how a
     50 # foreign-arch kit cross-compiled on the host (e.g. riscv64 via clang) bootstraps
     51 # itself: stages 2/3 (and their rt) run the seed under emulation in a container,
     52 # but the expensive stage1 was built natively on the host. Stages 2/3 are still
     53 # byte-identical because the seed and stage2 are the same kit source, hence
     54 # functionally identical compilers. Driven by scripts/linux_bootstrap.sh.
     55 BOOTSTRAP_SEED ?=
     56 BOOTSTRAP_STAGE1_SRC = $(BIN)
     57 BOOTSTRAP_DEPS = $(BIN) $(BOOTSTRAP_RT_LIBS) $(BOOTSTRAP_MAKEFILES)
     58 ifneq ($(BOOTSTRAP_SEED),)
     59 BOOTSTRAP_STAGE1_SRC = $(BOOTSTRAP_SEED)
     60 BOOTSTRAP_DEPS = $(BOOTSTRAP_SEED) $(BOOTSTRAP_MAKEFILES)
     61 endif
     62 
     63 ifeq ($(RELEASE),1)
     64 $(BOOTSTRAP_STAMP): HOST_OPTFLAGS = -O1
     65 $(BOOTSTRAP_STAMP): BOOTSTRAP_HOST_MODE_CFLAGS = $(HOST_MODE_CFLAGS)
     66 $(BOOTSTRAP_STAMP): BOOTSTRAP_HOST_MODE_LDFLAGS = -Wl,--gc-sections -Wl,-S
     67 endif
     68 
     69 bootstrap: bootstrap-debug bootstrap-release
     70 
     71 bootstrap-debug:
     72 	$(MAKE) RELEASE=0 BUILD_DIR='$(BUILD_DIR)/debug' _do-bootstrap
     73 
     74 bootstrap-release:
     75 	$(MAKE) RELEASE=1 BUILD_DIR='$(BUILD_DIR)/release' _do-bootstrap
     76 
     77 _do-bootstrap: $(BOOTSTRAP_STAMP)
     78 
     79 # Cross-OS self-host (kit built to run on linux/freebsd/windows, then compiling +
     80 # running a program there) is now driven by `make test-selfhost TARGET=…`
     81 # (engine: scripts/selfhost.sh), which calls scripts/{linux,freebsd}_bootstrap.sh
     82 # and scripts/windows_cross.sh per target. See doc/plan/PORT.md. `make bootstrap`
     83 # here remains the native (host-arch) 3-stage byte-identical self-reproduction.
     84 
     85 $(BOOTSTRAP_STAMP): $(BOOTSTRAP_DEPS)
     86 	rm -rf $(BOOTSTRAP_DIR)
     87 	@mkdir -p $(BOOTSTRAP_STAGE1_DIR)
     88 	cp $(BOOTSTRAP_STAGE1_SRC) $(BOOTSTRAP_STAGE1_BIN)
     89 	# The copied stage1 binary no longer sits beside $(BUILD_DIR)/support. Give
     90 	# it the same self-contained support/rt layout as every normal bin target.
     91 	$(MAKE) $(BOOTSTRAP_STAGE1_DIR)/support/rt
     92 	@for tool in $(BOOTSTRAP_TOOLS); do ln -sf kit "$(BOOTSTRAP_STAGE1_DIR)/$$tool"; done
     93 	$(MAKE) lib bin \
     94 	    BUILD_DIR='$(abspath $(BOOTSTRAP_STAGE2_DIR))' \
     95 	    RELEASE='$(RELEASE)' \
     96 	    HOST_OPTFLAGS='$(HOST_OPTFLAGS)' \
     97 	    HOST_MODE_CFLAGS='$(BOOTSTRAP_HOST_MODE_CFLAGS)' \
     98 	    HOST_MODE_LDFLAGS='$(BOOTSTRAP_HOST_MODE_LDFLAGS)' \
     99 	    CC='$(abspath $(BOOTSTRAP_STAGE1_DIR))/cc' \
    100 	    AR='$(abspath $(BOOTSTRAP_STAGE1_DIR))/ar' \
    101 	    $(BOOTSTRAP_KIT_TOOLCHAIN_FLAGS)
    102 	@for tool in $(BOOTSTRAP_TOOLS); do ln -sf kit "$(BOOTSTRAP_STAGE2_DIR)/$$tool"; done
    103 	$(MAKE) lib bin \
    104 	    BUILD_DIR='$(abspath $(BOOTSTRAP_STAGE3_DIR))' \
    105 	    RELEASE='$(RELEASE)' \
    106 	    HOST_OPTFLAGS='$(HOST_OPTFLAGS)' \
    107 	    HOST_MODE_CFLAGS='$(BOOTSTRAP_HOST_MODE_CFLAGS)' \
    108 	    HOST_MODE_LDFLAGS='$(BOOTSTRAP_HOST_MODE_LDFLAGS)' \
    109 	    CC='$(abspath $(BOOTSTRAP_STAGE2_DIR))/cc' \
    110 	    AR='$(abspath $(BOOTSTRAP_STAGE2_DIR))/ar' \
    111 	    $(BOOTSTRAP_KIT_TOOLCHAIN_FLAGS)
    112 	cmp $(BOOTSTRAP_STAGE2_BIN) $(BOOTSTRAP_STAGE3_BIN)
    113 	$(BOOTSTRAP_SHASUM) $(BOOTSTRAP_STAGE2_BIN) $(BOOTSTRAP_STAGE3_BIN)
    114 	@touch $@