Markus Elfring - 2018-02-25

Would you like to integrate the following adjustment into your content management system?

diff --git a/GNUmakefile b/GNUmakefile
index 40cbe6e2..5dc845ae 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -8,17 +8,35 @@ $(info $(shell echo '     GEN      'version.h))
 $(shell echo '#define SPARSE_VERSION "$(SPARSE_VERSION)"' > version.h)
 endif

+ifeq ($(strip $(srcdir)),)
+   SOURCE_DIR::=
+   SMATCH_SOURCE_DATA_DIR::=smatch_data/
+   SMATCH_SOURCE_SCRIPT_DIR::=smatch_scripts/
+   SMATCH_SOURCE_MANUAL_DIR::=
+   SMATCH_INC::=
+else
+   VPATH::=$(srcdir)
+   VERSION_CFLAGS::=-I.
+   SOURCE_DIR::=$(srcdir)/
+   SMATCH_SOURCE_DATA_DIR::=$(SOURCE_DIR)smatch_data/
+   SMATCH_SOURCE_SCRIPT_DIR::=$(SOURCE_DIR)smatch_scripts/
+   SMATCH_SOURCE_MANUAL_DIR::=$(SOURCE_DIR)
+   SMATCH_INC::=-I$(srcdir)
+endif
+
 OS = linux

 ifeq ($(CC),"")
 CC = gcc
 endif
 CFLAGS = -O2 -finline-functions -fno-strict-aliasing -g
-CFLAGS += -Wall -Wwrite-strings -Wno-switch
+CFLAGS += -Wall -Wwrite-strings -Wno-switch $(VERSION_CFLAGS)
 LDFLAGS += -g -lm -lsqlite3 -lssl -lcrypto
 LD = gcc
 AR = ar
 PKG_CONFIG = pkg-config
+RM_F::=rm -f
+TOUCH::=touch

 ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS)
 #
@@ -28,9 +46,9 @@ ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS)
 #

 HAVE_LIBXML:=$(shell $(PKG_CONFIG) --exists libxml-2.0 2>/dev/null && echo 'yes')
-HAVE_GCC_DEP:=$(shell touch .gcc-test.c &&                 \
+HAVE_GCC_DEP:=$(shell $(TOUCH) .gcc-test.c &&              \
        $(CC) -c -Wp,-MD,.gcc-test.d .gcc-test.c 2>/dev/null && \
-       echo 'yes'; rm -f .gcc-test.d .gcc-test.o .gcc-test.c)
+       echo 'yes'; $(RM_F) .gcc-test.d .gcc-test.o .gcc-test.c)
 HAVE_GTK2:=$(shell $(PKG_CONFIG) --exists gtk+-2.0 2>/dev/null && echo 'yes')
 LLVM_CONFIG:=llvm-config
 HAVE_LLVM:=$(shell $(LLVM_CONFIG) --version >/dev/null 2>&1 && echo 'yes')
@@ -81,37 +99,61 @@ SMATCH_FILES=smatch_flow.o smatch_conditions.o smatch_slist.o smatch_states.o \
    smatch_mtag_map.o smatch_mtag_data.o \
    smatch_param_to_mtag_data.o

-SMATCH_CHECKS=$(shell ls check_*.c | sed -e 's/\.c/.o/')
-SMATCH_DATA=smatch_data/kernel.allocation_funcs \
-   smatch_data/kernel.frees_argument smatch_data/kernel.puts_argument \
-   smatch_data/kernel.dev_queue_xmit smatch_data/kernel.returns_err_ptr \
-   smatch_data/kernel.dma_funcs smatch_data/kernel.returns_held_funcs \
-   smatch_data/kernel.no_return_funcs
-
-SMATCH_SCRIPTS=smatch_scripts/add_gfp_to_allocations.sh \
-   smatch_scripts/build_kernel_data.sh \
-   smatch_scripts/call_tree.pl smatch_scripts/filter_kernel_deref_check.sh \
-   smatch_scripts/find_expanded_holes.pl smatch_scripts/find_null_params.sh \
-   smatch_scripts/follow_params.pl smatch_scripts/gen_allocation_list.sh \
-   smatch_scripts/gen_bit_shifters.sh smatch_scripts/gen_dma_funcs.sh \
-   smatch_scripts/generisize.pl smatch_scripts/gen_err_ptr_list.sh \
-   smatch_scripts/gen_expects_err_ptr.sh smatch_scripts/gen_frees_list.sh \
-   smatch_scripts/gen_gfp_flags.sh smatch_scripts/gen_no_return_funcs.sh \
-   smatch_scripts/gen_puts_list.sh smatch_scripts/gen_returns_held.sh \
-   smatch_scripts/gen_rosenberg_funcs.sh smatch_scripts/gen_sizeof_param.sh \
-   smatch_scripts/gen_unwind_functions.sh smatch_scripts/kchecker \
-   smatch_scripts/kpatch.sh smatch_scripts/new_bugs.sh \
-   smatch_scripts/show_errs.sh smatch_scripts/show_ifs.sh \
-   smatch_scripts/show_unreachable.sh smatch_scripts/strip_whitespace.pl \
-   smatch_scripts/summarize_errs.sh smatch_scripts/test_kernel.sh \
-   smatch_scripts/trace_params.pl smatch_scripts/unlocked_paths.pl \
-   smatch_scripts/whitespace_only.sh smatch_scripts/wine_checker.sh \
+ifeq ($(strip $(srcdir)),)
+   SMATCH_CHECKS::=$(patsubst %c,%o,$(wildcard check_*.c))
+else
+   SMATCH_CHECKS::=$(patsubst %c,%o,$(notdir $(wildcard $(SOURCE_DIR)check_*.c)))
+endif
+
+SMATCH_DATA::=$(foreach f,kernel.allocation_funcs \
+kernel.dev_queue_xmit \
+kernel.dma_funcs \
+kernel.frees_argument \
+kernel.puts_argument \
+kernel.no_return_funcs \
+kernel.returns_err_ptr \
+kernel.returns_held_funcs,$(SMATCH_SOURCE_DATA_DIR)$(f))
+
+SMATCH_SCRIPTS::=$(foreach f,add_gfp_to_allocations.sh \
+build_kernel_data.sh \
+call_tree.pl \
+filter_kernel_deref_check.sh \
+find_expanded_holes.pl \
+find_null_params.sh \
+follow_params.pl \
+gen_allocation_list.sh \
+gen_bit_shifters.sh \
+gen_dma_funcs.sh \
+gen_err_ptr_list.sh \
+gen_expects_err_ptr.sh \
+gen_frees_list.sh \
+gen_gfp_flags.sh \
+gen_no_return_funcs.sh \
+gen_puts_list.sh \
+gen_returns_held.sh \
+gen_rosenberg_funcs.sh \
+gen_sizeof_param.sh \
+gen_unwind_functions.sh \
+generisize.pl \
+kchecker \
+kpatch.sh \
+new_bugs.sh \
+show_errs.sh \
+show_ifs.sh \
+show_unreachable.sh \
+strip_whitespace.pl \
+summarize_errs.sh \
+test_kernel.sh \
+trace_params.pl \
+unlocked_paths.pl \
+whitespace_only.sh \
+wine_checker.sh,$(SMATCH_SOURCE_SCRIPT_DIR)$(f))

 PROGRAMS=test-lexing test-parsing obfuscate compile graph sparse \
     test-linearize example test-unssa test-dissect ctags
 INST_PROGRAMS=smatch cgcc

-INST_MAN1=sparse.1 cgcc.1
+INST_MAN1::=$(foreach f,cgcc.1 sparse.1,$(SMATCH_SOURCE_MANUAL_DIR)$(f))

 ifeq ($(HAVE_LIBXML),yes)
 PROGRAMS+=c2xml
@@ -153,9 +195,22 @@ sparse-llvm_EXTRA_OBJS := $(LLVM_LIBS) $(LLVM_LDFLAGS)
 endif
 endif

-LIB_H=    token.h parse.h lib.h symbol.h scope.h expression.h target.h \
-     linearize.h bitmap.h ident-list.h compat.h flow.h allocate.h \
-     storage.h ptrlist.h dissect.h
+LIB_H::=$(foreach f,allocate.h \
+bitmap.h \
+compat.h \
+dissect.h \
+expression.h \
+flow.h \
+ident-list.h \
+lib.h \
+linearize.h \
+parse.h \
+ptrlist.h \
+scope.h \
+storage.h \
+symbol.h \
+target.h \
+token.h,$(SOURCE_DIR)$(f))

 LIB_OBJS= target.o parse.o tokenize.o pre-process.o symbol.o lib.o scope.o \
      expression.o show-parse.o evaluate.o expand.o inline.o linearize.o \
@@ -228,8 +283,8 @@ install: all-installable
    $(foreach f,$(SMATCH_DATA),$(call INSTALL_FILE,$f,$(SMATCHDATADIR)))
    $(foreach f,$(SMATCH_SCRIPTS),$(call INSTALL_EXEC,$f,$(SMATCHDATADIR)))

-sparse.pc: sparse.pc.in
-   $(QUIET_GEN)sed $(SED_PC_CMD) sparse.pc.in > sparse.pc
+sparse.pc: $(SOURCE_DIR)sparse.pc.in
+   $(QUIET_GEN)sed $(SED_PC_CMD) $(SOURCE_DIR)sparse.pc.in > sparse.pc

 compile_EXTRA_DEPS = compile-i386.o
@@ -247,28 +302,53 @@ $(LIB_FILE): $(LIB_OBJS)
 $(SLIB_FILE): $(LIB_OBJS)
    $(QUIET_LINK)$(CC) -Wl,-soname,$@ -shared -o $@ $(LIB_OBJS) $(LDFLAGS)

+ifdef USE_CHECK_LIST_LOCAL_H
+   LOCAL_HEADER_DIR::=$(SOURCE_DIR)
+else
+   LOCAL_HEADER_DIR::=
+   SMATCH_INC+=-I.
+
 check_list_local.h:
-   touch check_list_local.h
+   $(TOUCH) check_list_local.h
+endif

-smatch.o: smatch.c $(LIB_H) smatch.h check_list.h check_list_local.h
-   $(CC) -c smatch.c -DSMATCHDATADIR='"$(SMATCHDATADIR)"'
-$(SMATCH_CHECKS): smatch.h smatch_slist.h smatch_extra.h avl.h
+smatch.o: $(foreach f,smatch.c smatch.h check_list.h,$(SOURCE_DIR)$(f)) $(LIB_H) $(LOCAL_HEADER_DIR)check_list_local.h
+   $(CC) -c $(SOURCE_DIR)smatch.c -DSMATCHDATADIR='"$(SMATCHDATADIR)"' $(SMATCH_INC)
+$(SMATCH_CHECKS): $(foreach f,smatch.h smatch_slist.h smatch_extra.h avl.h,$(SOURCE_DIR)$(f))
 DEP_FILES := $(wildcard .*.o.d)
 $(if $(DEP_FILES),$(eval include $(DEP_FILES)))

-c2xml.o: c2xml.c $(LIB_H)
+c2xml.o: $(SOURCE_DIR)c2xml.c $(LIB_H)
    $(QUIET_CC)$(CC) `$(PKG_CONFIG) --cflags libxml-2.0` -o $@ -c $(ALL_CFLAGS) $<

-compat-linux.o: compat/strtold.c compat/mmap-blob.c $(LIB_H)
-compat-solaris.o: compat/mmap-blob.c $(LIB_H)
+compat-linux.o: $(SOURCE_DIR)compat/strtold.c $(SOURCE_DIR)compat/mmap-blob.c $(LIB_H)
+compat-solaris.o: $(SOURCE_DIR)compat/mmap-blob.c $(LIB_H)
 compat-mingw.o: $(LIB_H)
 compat-cygwin.o: $(LIB_H)

+clean:: clean-check
+   $(RM_F) cwchash/*.o cwchash/.*.d cwchash/tester
+
+ifneq ($(strip $(srcdir)),)
+HASHTABLE_C::=$(SOURCE_DIR)cwchash/hashtable.c
+MKDIR_P::=mkdir -p
+RMDIR::=rmdir
+
+.cwchash_dir_created.status:
+   $(MKDIR_P) cwchash && $(TOUCH) $@
+
+cwchash/hashtable.o: .cwchash_dir_created.status $(HASHTABLE_C)
+   $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $(HASHTABLE_C)
+
+clean::
+   $(RM_F) .cwchash_dir_created.status && $(RMDIR) cwchash
+endif
+
 %.o: %.c
    $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $<

-clean: clean-check
-   rm -f *.[oa] .*.d *.so cwchash/*.o cwchash/.*.d cwchash/tester \
+clean::
+   $(RM_F) *.[oa] .*.d *.so \
        $(PROGRAMS) $(SLIB_FILE) pre-process.h sparse.pc

 dist:
@@ -279,13 +359,14 @@ dist:
    git archive --format=tar --prefix=sparse-$(VERSION)/ HEAD^{tree} | gzip -9 > sparse-$(VERSION).tar.gz

 check: all
-   $(Q)cd validation && ./test-suite
+   $(Q)cd $(SOURCE_DIR)validation && ./test-suite

 clean-check:
-   find validation/ \( -name "*.c.output.expected" \
-                    -o -name "*.c.output.got" \
-                    -o -name "*.c.output.diff" \
-                    -o -name "*.c.error.expected" \
-                    -o -name "*.c.error.got" \
-                    -o -name "*.c.error.diff" \
-                    \) -exec rm {} \;
+   find $(SOURCE_DIR)validation/ \
+   \( -name '*.c.output.expected' \
+   -o -name '*.c.output.got' \
+   -o -name '*.c.output.diff' \
+   -o -name '*.c.error.expected' \
+   -o -name '*.c.error.got' \
+   -o -name '*.c.error.diff' \
+   \) -exec rm {} \;