Autoconf and automake are heavy duty tools that can do what you're describing very well, but there may be a simpler solution. To start, make sure you're using variables where appropriate:
%.o:%.c
$(CC) $(CFLAGS) $^ -o $@
program: $(OBJS)
$(LD) $(LDFLAGS) ...
And then populate the CC and LDFLAGS. You can require the user to override them on the command line as so:
make CC=/opt/toolchains/xxx/yyy/gcc
Notice that even if you define CC
in your makefile, the value you specified on the command line will take precedence. Alternatively, you can do an editable configure file, and have lines like this in your makefile:
CONFIG_FILE := .configure
TOOLCHAIN_DIR := $(sed -n "s/^\s*TOOLCHAIN:\s*\(\S*\).*$$/\1/p" < $(CONFIG_FILE))
LIB_DIRS := $(sed -n "s/^\s*LIBS:\s*\(\S*\).*$$/\1/p" < $(CONFIG_FILE))
CC:=$(TOOLCHAIN_DIR)/gcc
LDFLAGS += $(addprefix -L,$(LIB_DIRS))
And, on top of that you can create a configure
rule in your makefile to generate the .configure
file based on user input. If you need to go beyond that, you may want to consider autoconf.