Skip to content

Commit

Permalink
Avoid re-copying include headers (#549)
Browse files Browse the repository at this point in the history
This adds a `.stamp` file to the build directory that tracks when all
headers have been copied over to the sysroot `include` directory.
Previously, the `include_dirs` target was phony, which `make` will run
every time, regardless of whether it is needed. This was part of the
problem with all of libc being rebuilt at each `make` invocation.
  • Loading branch information
abrown authored Nov 20, 2024
1 parent 913e58e commit c47daaf
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ ifeq ($(WASI_SNAPSHOT), p2)
TARGET_TRIPLE = wasm32-wasip2
endif

# These artifacts are "stamps" that we use to mark that some task (e.g., copying
# files) has been completed.
INCLUDE_DIRS := $(OBJDIR)/copy-include-headers.stamp

BUILTINS_LIB ?= $(shell ${CC} ${CFLAGS} --print-libgcc-file-name)

# These variables describe the locations of various files and directories in
Expand Down Expand Up @@ -735,31 +739,31 @@ $(LIBSETJMP_OBJS) $(LIBSETJMP_SO_OBJS): CFLAGS += \
$(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_SO_OBJS): CFLAGS += \
-D_WASI_EMULATED_SIGNAL

$(OBJDIR)/%.long-double.pic.o: %.c include_dirs
$(OBJDIR)/%.long-double.pic.o: %.c $(INCLUDE_DIRS)
@mkdir -p "$(@D)"
$(CC) $(CFLAGS) -MD -MP -o $@ -c $<

$(OBJDIR)/wasip2_component_type.pic.o $(OBJDIR)/wasip2_component_type.o: $(LIBC_BOTTOM_HALF_SOURCES)/wasip2_component_type.o
@mkdir -p "$(@D)"
cp $< $@

$(OBJDIR)/%.pic.o: %.c include_dirs
$(OBJDIR)/%.pic.o: %.c $(INCLUDE_DIRS)
@mkdir -p "$(@D)"
$(CC) $(CFLAGS) -MD -MP -o $@ -c $<

$(OBJDIR)/%.long-double.o: %.c include_dirs
$(OBJDIR)/%.long-double.o: %.c $(INCLUDE_DIRS)
@mkdir -p "$(@D)"
$(CC) $(CFLAGS) -MD -MP -o $@ -c $<

$(OBJDIR)/%.no-floating-point.o: %.c include_dirs
$(OBJDIR)/%.no-floating-point.o: %.c $(INCLUDE_DIRS)
@mkdir -p "$(@D)"
$(CC) $(CFLAGS) -MD -MP -o $@ -c $<

$(OBJDIR)/%.o: %.c include_dirs
$(OBJDIR)/%.o: %.c $(INCLUDE_DIRS)
@mkdir -p "$(@D)"
$(CC) $(CFLAGS) -MD -MP -o $@ -c $<

$(OBJDIR)/%.o: %.s include_dirs
$(OBJDIR)/%.o: %.s $(INCLUDE_DIRS)
@mkdir -p "$(@D)"
$(CC) $(ASMFLAGS) -o $@ -c $<

Expand Down Expand Up @@ -807,7 +811,10 @@ $(LIBWASI_EMULATED_PTHREAD_OBJS) $(LIBWASI_EMULATED_PTHREAD_SO_OBJS): CFLAGS +=
$(EMMALLOC_OBJS): CFLAGS += \
-fno-strict-aliasing

include_dirs:
ALL_POSSIBLE_HEADERS += $(shell find $(LIBC_TOP_HALF_MUSL_DIR) -name \*.h)
ALL_POSSIBLE_HEADERS += $(shell find $(LIBC_BOTTOM_HALF_HEADERS_PUBLIC) -name \*.h)
ALL_POSSIBLE_HEADERS += $(shell find $(MUSL_FTS_SRC_DIR) -name \*.h)
$(INCLUDE_DIRS): $(ALL_POSSIBLE_HEADERS)
#
# Install the include files.
#
Expand All @@ -823,10 +830,12 @@ include_dirs:

# Copy in the bulk of musl's public header files.
cp -r "$(LIBC_TOP_HALF_MUSL_INC)"/* "$(SYSROOT_INC)"

# Copy in the musl's "bits" header files.
cp -r "$(LIBC_TOP_HALF_MUSL_DIR)"/arch/generic/bits/* "$(SYSROOT_INC)/bits"
cp -r "$(LIBC_TOP_HALF_MUSL_DIR)"/arch/wasm32/bits/* "$(SYSROOT_INC)/bits"

# Copy in the fts header files.
cp "$(MUSL_FTS_SRC_DIR)/fts.h" "$(SYSROOT_INC)/fts.h"

# Remove selected header files.
Expand All @@ -836,7 +845,11 @@ ifeq ($(WASI_SNAPSHOT), p2)
> "$(SYSROOT_INC)/__wasi_snapshot.h"
endif

startup_files: include_dirs $(LIBC_BOTTOM_HALF_CRT_OBJS)
# Stamp the include installation.
@mkdir -p $(@D)
touch $@

startup_files: $(INCLUDE_DIRS) $(LIBC_BOTTOM_HALF_CRT_OBJS)
#
# Install the startup files (crt1.o etc).
#
Expand All @@ -861,7 +874,7 @@ LIBC_SO += \
endif
endif

libc_so: include_dirs $(LIBC_SO)
libc_so: $(INCLUDE_DIRS) $(LIBC_SO)

STATIC_LIBS = \
$(SYSROOT_LIB)/libc.a \
Expand All @@ -881,7 +894,7 @@ STATIC_LIBS += \
$(SYSROOT_LIB)/libsetjmp.a
endif

libc: include_dirs $(STATIC_LIBS)
libc: $(INCLUDE_DIRS) $(STATIC_LIBS)

DUMMY := m rt pthread crypt util xnet resolv
DUMMY_LIBS := $(patsubst %,$(SYSROOT_LIB)/lib%.a,$(DUMMY))
Expand Down Expand Up @@ -1101,4 +1114,4 @@ clean:
$(RM) -r "$(OBJDIR)"
$(RM) -r "$(SYSROOT)"

.PHONY: default startup_files libc libc_so finish install include_dirs clean check-symbols bindings
.PHONY: default startup_files libc libc_so finish install clean check-symbols bindings

0 comments on commit c47daaf

Please sign in to comment.