reorganize Makefile to support "least surprise" config/make semantics
authorRich Felker <dalias@aerifal.cx>
Tue, 1 May 2012 23:30:03 +0000 (19:30 -0400)
committerRich Felker <dalias@aerifal.cx>
Tue, 1 May 2012 23:30:03 +0000 (19:30 -0400)
the major change here is that CFLAGS is now a variable that can be
changed entirely under user control, without causing essential flags
to be lost. previously, "CFLAGS += ..." was valid in config.mak, but
using "CFLAGS = ..." in config.mak would have badly broken the build
process unless the user took care to copy the necessary flags out of
the main Makefile.

I have also added a distclean target that removes config.mak.

Makefile

index b419554..2265fce 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -21,10 +21,17 @@ OBJS = $(SRCS:.c=.o)
 LOBJS = $(OBJS:.o=.lo)
 GENH = include/bits/alltypes.h
 
 LOBJS = $(OBJS:.o=.lo)
 GENH = include/bits/alltypes.h
 
-CFLAGS  = -Os -nostdinc -ffreestanding -std=c99 -D_XOPEN_SOURCE=700 -pipe
-LDFLAGS = -nostdlib -shared -fPIC -Wl,-e,_start -Wl,-Bsymbolic-functions
-INC     = -I./src/internal -I./include -I./arch/$(ARCH)
-PIC     = -fPIC -O3
+LDFLAGS = 
+CPPFLAGS =
+CFLAGS = -Os -pipe
+CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc 
+
+CFLAGS_ALL = $(CFLAGS_C99FSE)
+CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I./src/internal -I./include -I./arch/$(ARCH)
+CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS)
+CFLAGS_ALL_STATIC = $(CFLAGS_ALL)
+CFLAGS_ALL_SHARED = $(CFLAGS_ALL) -fPIC -DSHARED -O3
+
 AR      = $(CROSS_COMPILE)ar
 RANLIB  = $(CROSS_COMPILE)ranlib
 
 AR      = $(CROSS_COMPILE)ar
 RANLIB  = $(CROSS_COMPILE)ranlib
 
@@ -33,10 +40,10 @@ ALL_INCLUDES = $(sort $(wildcard include/*.h include/*/*.h) $(GENH))
 EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
 EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
 CRT_LIBS = lib/crt1.o lib/crti.o lib/crtn.o
 EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv dl
 EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a)
 CRT_LIBS = lib/crt1.o lib/crti.o lib/crtn.o
-STATIC_LIBS = lib/libc.a $(EMPTY_LIBS)
+STATIC_LIBS = lib/libc.a
 SHARED_LIBS = lib/libc.so
 TOOL_LIBS = lib/musl-gcc.specs
 SHARED_LIBS = lib/libc.so
 TOOL_LIBS = lib/musl-gcc.specs
-ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(TOOL_LIBS)
+ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
 ALL_TOOLS = tools/musl-gcc
 
 LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH).so.1
 ALL_TOOLS = tools/musl-gcc
 
 LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH).so.1
@@ -56,6 +63,9 @@ clean:
        rm -f $(GENH) 
        rm -f include/bits
 
        rm -f $(GENH) 
        rm -f include/bits
 
+distclean: clean
+       rm -f config.mak
+
 include/bits:
        @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; }
        ln -sf ../arch/$(ARCH)/bits $@
 include/bits:
        @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; }
        ln -sf ../arch/$(ARCH)/bits $@
@@ -66,19 +76,21 @@ include/bits/alltypes.h: include/bits/alltypes.h.sh
        sh $< > $@
 
 %.o: $(ARCH)/%.s
        sh $< > $@
 
 %.o: $(ARCH)/%.s
-       $(CC) $(CFLAGS) $(INC) -c -o $@ $<
+       $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
 
 %.o: %.c $(GENH)
 
 %.o: %.c $(GENH)
-       $(CC) $(CFLAGS) $(INC) -c -o $@ $<
+       $(CC) $(CFLAGS_ALL_STATIC) -c -o $@ $<
 
 %.lo: $(ARCH)/%.s
 
 %.lo: $(ARCH)/%.s
-       $(CC) $(CFLAGS) $(INC) $(PIC) -c -o $@ $<
+       $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
 
 %.lo: %.c $(GENH)
 
 %.lo: %.c $(GENH)
-       $(CC) $(CFLAGS) $(INC) $(PIC) -c -o $@ $<
+       $(CC) $(CFLAGS_ALL_SHARED) -c -o $@ $<
 
 lib/libc.so: $(LOBJS)
 
 lib/libc.so: $(LOBJS)
-       $(CC) $(LDFLAGS) -Wl,-soname=libc.so -o $@ $(LOBJS) -lgcc
+       $(CC) $(CFLAGS_ALL_SHARED) $(LDFLAGS) -nostdlib -shared \
+       -Wl,-e,_start -Wl,-Bsymbolic-functions \
+       -Wl,-soname=libc.so -o $@ $(LOBJS) -lgcc
 
 lib/libc.a: $(OBJS)
        rm -f $@
 
 lib/libc.a: $(OBJS)
        rm -f $@