Makefile.rules: Avoid redundant .d generation (make restart) and inline archive rule to the only test

Take an example when `CXX_SOURCES` is main.cpp.

main.d is an included file. make will rebuild main.d, re-executes itself [1] to read
in the new main.d file, then rebuild main.o, finally link main.o into a.out.
main.cpp is parsed twice in this process.

This patch merges .d generation into .o generation [2], writes explicit rules
for .c/.m and deletes suffix rules for %.m and %.o. Since a target can be
satisfied by either of .c/.cpp/.m/.mm, we use multiple pattern rules. The
rule with the prerequisite (with VPATH considered) satisfied is used [3].

Since suffix rules are disabled, the implicit rule for archive member targets is
no long available [4]. Rewrite, simplify the archive rule and inline it into the
only test `test/API/functionalities/archives/Makefile`.

[1]: https://www.gnu.org/software/make/manual/html_node/Remaking-Makefiles.html
[2]: http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
[3]: https://www.gnu.org/software/make/manual/html_node/Pattern-Match.html
[4]: https://www.gnu.org/software/make/manual/html_node/Archive-Update.html

ObjC/ObjCXX tests only run on macOS. I don't have testing environment.  Hope
someone can do it for me.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D94890
This commit is contained in:
Fangrui Song 2021-01-20 14:22:33 -08:00
parent 555a395f2c
commit 6afdf13ae4
2 changed files with 36 additions and 92 deletions

View File

@ -26,15 +26,13 @@
# SPLIT_DEBUG_SYMBOLS := YES
# CROSS_COMPILE :=
# USE_PRIVATE_MODULE_CACHE := YES
#
# And test/functionalities/archives/Makefile:
# MAKE_DSYM := NO
# ARCHIVE_NAME := libfoo.a
# ARCHIVE_C_SOURCES := a.c b.c
# Uncomment line below for debugging shell commands
# SHELL = /bin/sh -x
# Suppress built-in suffix rules. We explicitly define rules for %.o.
.SUFFIXES:
SRCDIR := $(shell dirname $(firstword $(MAKEFILE_LIST)))
BUILDDIR := $(shell pwd)
MAKEFILE_RULES := $(lastword $(MAKEFILE_LIST))
@ -477,42 +475,6 @@ ifneq "$(strip $(OBJCXX_SOURCES))" ""
endif
endif
#----------------------------------------------------------------------
# Check if we have any C source files for archive
#----------------------------------------------------------------------
ifneq "$(strip $(ARCHIVE_C_SOURCES))" ""
ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_C_SOURCES:.c=.o))
endif
#----------------------------------------------------------------------
# Check if we have any C++ source files for archive
#----------------------------------------------------------------------
ifneq "$(strip $(ARCHIVE_CXX_SOURCES))" ""
ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_CXX_SOURCES:.cpp=.o))
CXX = $(call cxx_compiler,$(CC))
LD = $(call cxx_linker,$(CC))
endif
#----------------------------------------------------------------------
# Check if we have any ObjC source files for archive
#----------------------------------------------------------------------
ifneq "$(strip $(ARCHIVE_OBJC_SOURCES))" ""
ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJC_SOURCES:.m=.o))
LDFLAGS +=-lobjc
endif
#----------------------------------------------------------------------
# Check if we have any ObjC++ source files for archive
#----------------------------------------------------------------------
ifneq "$(strip $(ARCHIVE_OBJCXX_SOURCES))" ""
ARCHIVE_OBJECTS +=$(strip $(ARCHIVE_OBJCXX_SOURCES:.mm=.o))
CXX = $(call cxx_compiler,$(CC))
LD = $(call cxx_linker,$(CC))
ifeq "$(findstring lobjc,$(LDFLAGS))" ""
LDFLAGS +=-lobjc
endif
endif
ifeq ($(findstring clang, $(CXX)), clang)
CXXFLAGS += --driver-mode=g++
endif
@ -534,8 +496,8 @@ endif
#----------------------------------------------------------------------
ifneq "$(DYLIB_NAME)" ""
ifeq "$(DYLIB_ONLY)" ""
$(EXE) : $(OBJECTS) $(ARCHIVE_NAME) $(DYLIB_FILENAME)
$(LD) $(OBJECTS) $(ARCHIVE_NAME) -L. -l$(DYLIB_NAME) $(LDFLAGS) -o "$(EXE)"
$(EXE) : $(OBJECTS) $(DYLIB_FILENAME)
$(LD) $(OBJECTS) -L. -l$(DYLIB_NAME) $(LDFLAGS) -o "$(EXE)"
ifneq "$(CODESIGN)" ""
$(CODESIGN) -s - "$(EXE)"
endif
@ -543,8 +505,8 @@ else
EXE = $(DYLIB_FILENAME)
endif
else
$(EXE) : $(OBJECTS) $(ARCHIVE_NAME)
$(LD) $(OBJECTS) $(LDFLAGS) $(ARCHIVE_NAME) -o "$(EXE)"
$(EXE) : $(OBJECTS)
$(LD) $(OBJECTS) $(LDFLAGS) -o "$(EXE)"
ifneq "$(CODESIGN)" ""
$(CODESIGN) -s - "$(EXE)"
endif
@ -566,19 +528,6 @@ ifeq "$(SPLIT_DEBUG_SYMBOLS)" "YES"
endif
endif
#----------------------------------------------------------------------
# Make the archive
#----------------------------------------------------------------------
ifneq "$(ARCHIVE_NAME)" ""
ifeq "$(OS)" "Darwin"
$(ARCHIVE_NAME) : $(ARCHIVE_OBJECTS)
$(AR) $(ARFLAGS) $(ARCHIVE_NAME) $(ARCHIVE_OBJECTS)
$(RM) $(ARCHIVE_OBJECTS)
else
$(ARCHIVE_NAME) : $(foreach ar_obj,$(ARCHIVE_OBJECTS),$(ARCHIVE_NAME)($(ar_obj)))
endif
endif
#----------------------------------------------------------------------
# Make the dylib
#----------------------------------------------------------------------
@ -628,12 +577,22 @@ endif
# Make the precompiled header and compile C++ sources against it
#----------------------------------------------------------------------
#ifneq "$(PCH_OUTPUT)" ""
ifneq "$(PCH_OUTPUT)" ""
$(PCH_OUTPUT) : $(PCH_CXX_SOURCE)
$(CXX) $(CXXFLAGS) -x c++-header -o $@ $<
%.o : %.cpp $(PCH_OUTPUT)
$(CXX) $(PCHFLAGS) $(CXXFLAGS) -c -o $@ $<
#endif
endif
%.o: %.c %.d
$(CC) $(CFLAGS) -MT $@ -MD -MP -MF $*.d -c -o $@ $<
%.o: %.cpp %.d $(PCH_OUTPUT)
$(CXX) $(PCHFLAGS) $(CXXFLAGS) -MT $@ -MD -MP -MF $*.d -c -o $@ $<
%.o: %.m %.d
$(CC) $(CFLAGS) -MT $@ -MD -MP -MF $*.d -c -o $@ $<
%.o: %.mm %.d
$(CXX) $(CXXFLAGS) -MT $@ -MD -MP -MF $*.d -c -o $@ $<
#----------------------------------------------------------------------
# Automatic variables based on items already entered. Below we create
@ -642,42 +601,20 @@ $(PCH_OUTPUT) : $(PCH_CXX_SOURCE)
# files by replacing all .c files with .d.
#----------------------------------------------------------------------
PREREQS := $(OBJECTS:.o=.d)
DWOS := $(OBJECTS:.o=.dwo) $(ARCHIVE_OBJECTS:.o=.dwo)
DWOS := $(OBJECTS:.o=.dwo)
ifneq "$(DYLIB_NAME)" ""
DYLIB_PREREQS := $(DYLIB_OBJECTS:.o=.d)
DYLIB_DWOS := $(DYLIB_OBJECTS:.o=.dwo)
endif
#----------------------------------------------------------------------
# Rule for Generating Prerequisites Automatically using .d files and
# the compiler -MM option. The -M option will list all system headers,
# and the -MM option will list all non-system dependencies.
#----------------------------------------------------------------------
%.d: %.c
$(CC) -M $(CFLAGS) $< -MF $@ -MT $@ -MT $*.o
%.d: %.cpp
@$(CXX) -M $(CXXFLAGS) $< -MF $@ -MT $@ -MT $*.o
%.d: %.m
@$(CC) -M $(CFLAGS) $< -MF $@ -MT $@ -MT $*.o
%.d: %.mm
@$(CXX) -M $(CXXFLAGS) $< -MF $@ -MT $@ -MT $*.o
# Don't error if a .d file is deleted.
$(PREREQS) $(DYLIB_PREREQS): ;
#----------------------------------------------------------------------
# Include all of the makefiles for each source file so we don't have
# to manually track all of the prerequisites for each source file.
#----------------------------------------------------------------------
sinclude $(PREREQS)
ifneq "$(DYLIB_NAME)" ""
sinclude $(DYLIB_PREREQS)
endif
# Define a suffix rule for .mm -> .o
.SUFFIXES: .mm .o
.mm.o:
$(CXX) $(CXXFLAGS) -c $<
include $(wildcard $(PREREQS) $(DYLIB_PREREQS))
.PHONY: clean
dsym: $(DSYM)

View File

@ -1,7 +1,14 @@
C_SOURCES := main.c
C_SOURCES := main.c a.c b.c
EXE := # Define a.out explicitly
MAKE_DSYM := NO
ARCHIVE_NAME := libfoo.a
ARCHIVE_C_SOURCES := a.c b.c
all: a.out
a.out: main.o libfoo.a
$(LD) $(LDFLAGS) $^ -o $@
libfoo.a: a.o b.o
$(AR) $(ARFLAGS) $@ $^
$(RM) $^
include Makefile.rules