Add ability to check specific headers like autoconf

This commit is contained in:
Christophe de Dinechin 2017-05-02 11:08:14 +02:00
parent cbae7427e8
commit 6fe7280a78
3 changed files with 40 additions and 3 deletions

View File

@ -28,6 +28,14 @@ SOURCES=hello.cpp
# Define the product of the build (.exe will be removed for Unix builds)
PRODUCTS=hello.exe
# Define configuration options
CONFIG= HAVE_<stdio.h> \
HAVE_<unistd.h> \
HAVE_<nonexistent.h> \
HAVE_<sys/time.h> \
HAVE_<sys/improbable.h> \
HAVE_<iostream>
# Define what to test
TESTS=product count-characters
@ -39,3 +47,4 @@ include $(BUILD)rules.mk
count-characters.runtest:
@echo Output has `$(OBJPRODUCTS) | wc -c` characters, should be 35

View File

@ -1,5 +1,14 @@
#include "build-config.h"
#include <iostream>
#if HAVE_sys_improbable_h
#warning "Strange header present"
#endif
#if !HAVE_iostream
#warning "We expect to have iostream"
#endif
int main()
{
std::cout << "You successfully built using build\n";

View File

@ -150,7 +150,7 @@ help:
# Internal targets
#------------------------------------------------------------------------------
build: hello libraries recurse prebuild objects product postbuild goodbye
build: hello config libraries recurse prebuild objects product postbuild goodbye
hello:
@$(INFO) "[BEGIN]" $(TARGET) $(BUILDENV) in $(PRETTY_DIR)
@ -167,6 +167,7 @@ product:$(OBJPRODUCTS)
objects:$(OBJDIR:%=%/.mkdir) $(OBJECTS)
# "Hooks" for pre and post build steps
config: $(CONFIG:%=build-config.h)
prebuild:
postbuild:
@ -281,11 +282,12 @@ PRINT_PCT= $(shell printf "%3d%%" $$(( ($(BUILD_HIGH) - $(BUILD_LOW)) * $(BUILD_
PRINT_COMMAND= @
PRINT_COMPILE= $(PRINT_COMMAND) $(INFO) "[COMPILE$(PRINT_COUNT)] " $<;
PRINT_BUILD= $(PRINT_COMMAND) $(INFO) "[BUILD]" $(shell basename $@);
PRINT_GENERATE= $(PRINT_COMMAND) $(INFO) "[GENERATE]" $(shell basename $@);
PRINT_GENERATE= $(PRINT_COMMAND) $(INFO) "[GENERATE]" "$(shell basename "$@")";
PRINT_INSTALL= $(PRINT_COMMAND) $(INFO) "[INSTALL] " $(*F) in $(<D);
PRINT_COPY= $(PRINT_COMMAND) $(INFO) "[COPY]" $< '=>' $@ ;
PRINT_DEPEND= $(PRINT_COMMAND) $(INFO) "[DEPEND] " $< ;
PRINT_TEST= $(PRINT_COMMAND) $(INFO) "[TEST] " $(@:.runtest=) ;
PRINT_TEST= $(PRINT_COMMAND) $(INFO) "[TEST]" $(@:.runtest=) ;
PRINT_CONFIG= $(PRINT_COMMAND) $(INFO) "[CONFIG]" "$*" ;
logs.mkdir: $(dir $(BUILD_LOG))/.mkdir $(dir $(BUILD_SAVED_LOG))/.mkdir
%/.mkdir:
@ -362,6 +364,23 @@ ifeq ($(MAKECMDGOALS),build)
endif
#------------------------------------------------------------------------------
# Configuration rules
#------------------------------------------------------------------------------
build-config.h: $(CONFIG:%=$(OBJDIR)/%)
$(PRINT_GENERATE) cat $(CONFIG:%="$(OBJDIR)/%") > $@
$(OBJDIR)/HAVE_<%.h>: $(OBJDIR)/CONFIG_HAVE_%.c
$(PRINT_CONFIG) mkdir -p "$$(dirname "$@")" ; echo '#define HAVE_$*_h' $$($(CC) -c "$<" -o "$<".o > "$<".err 2>&1 && echo 1 || echo 0) | sed -e 's|[./\\]|_|g' > "$@"
$(OBJDIR)/HAVE_<%>: $(OBJDIR)/CONFIG_HAVE_%.cpp
$(PRINT_CONFIG) mkdir -p "$$(dirname "$@")" ; echo '#define HAVE_$*' $$($(CXX) -c "$<" -o "$<".o > "$<".err 2>&1 && echo 1 || echo 0) | sed -e 's|[./\\]|_|g' > "$@"
$(OBJDIR)/CONFIG_HAVE_%.c: $(OBJDIR)/.mkdir
$(PRINT_GENERATE) mkdir -p "$$(dirname "$@")" ; echo '#include' "<$*.h>" > "$@"
$(OBJDIR)/CONFIG_HAVE_%.cpp: $(OBJDIR)/.mkdir
$(PRINT_GENERATE) mkdir -p "$$(dirname "$@")" ; echo '#include' "<$*>" > "$@"
.PRECIOUS: $(OBJDIR)/CONFIG_HAVE_%.c
#------------------------------------------------------------------------------
# Makefile optimization tricks
#------------------------------------------------------------------------------