Skip to content

Commit

Permalink
Add more checks for unsupported compiler features
Browse files Browse the repository at this point in the history
- Dependecy generation (-MM -MT / -MF)
- C standard (--std=gnu89)
- Full program optimization (-flto=auto)
- Position independent executable (-pie)

All the above enables building with tinyc compiler by e.g.
CC=tcc CFLAGS="-Dinline=''" make
Note that the above requires tcc built from git because
tcc versions 0.9.27 and below can not grok the regex library
  • Loading branch information
bbonev committed Feb 27, 2024
1 parent e69c54a commit b210ec5
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ NEEDLRT:=$(shell if $(CC) -E glibcvertest.h -o -|grep IOTOP_NEED_LRT|grep -q yes
HAVESREA:=$(shell if $(CC) -mno-stackrealign -xc -c /dev/null -o /dev/null >/dev/null 2>/dev/null;then echo yes;else echo no;fi)
# old comiplers do not have -Wdate-time
HAVEWDTI:=$(shell if $(CC) -Wdate-time -xc -c /dev/null -o /dev/null >/dev/null 2>/dev/null;then echo yes;else echo no;fi)
# old compilers can not generate dependecies
HAVEDEPS:=$(shell if $(CC) -MM -MT /dev/null -MF /dev/null /dev/null >/dev/null 2>/dev/null;then echo yes;else echo no;fi)
# old compilers do not understand C standard
HAVECSTD:=$(shell if $(CC) --std=gnu89 -xc -c /dev/null -o /dev/null >/dev/null 2>/dev/null;then echo yes;else echo no;fi)
# old compilers do not understand -flto=auto
HAVEFLTA:=$(shell if $(CC) -flto=auto -xc -c /dev/null -o /dev/null >/dev/null 2>/dev/null;then echo yes;else echo no;fi)
# old compilers do not understand -flto at all
HAVEFLTO:=$(shell if $(CC) -flto -xc -c /dev/null -o /dev/null >/dev/null 2>/dev/null;then echo yes;else echo no;fi)
# old compilers do not understand -pie; clang yields error when stderr is redirected :(
HAVELPIE:=$(shell if $(CC) -Wno-unused-command-line-argument -pie -xc -c /dev/null -o /dev/null >/dev/null 2>/dev/null;then echo yes;else echo no;fi)

MYCFLAGS:=$(CPPFLAGS) $(CFLAGS) $(NCCC) -Wall -Wextra -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 --std=gnu89 -fPIE
MYLIBS:=$(NCLD) $(LIBS)
Expand All @@ -72,6 +82,31 @@ MYCFLAGS:=$(filter-out -Wdate-time,$(MYCFLAGS))
MYLDFLAGS:=$(filter-out -Wdate-time,$(MYLDFLAGS))
endif

ifeq ("$(HAVECSTD)","no")
MYCFLAGS:=$(filter-out --std=gnu89,$(MYCFLAGS))
MYLDFLAGS:=$(filter-out --std=gnu89,$(MYLDFLAGS))
MYCFLAGS:=$(filter-out -D_FORTIFY_SOURCE=2,$(MYCFLAGS))
MYLDFLAGS:=$(filter-out -D_FORTIFY_SOURCE=2,$(MYLDFLAGS))
endif

ifeq ("$(HAVEFLTA)","no")
ifndef NO_FLTO
ifeq ("$(HAVEFLTA)","no")
MYCFLAGS:=$(filter-out -flto=auto,$(MYCFLAGS))
MYLDFLAGS:=$(filter-out -flto=auto,$(MYLDFLAGS))
else
MYCFLAGS:=$(filter-out -flto=auto,$(MYCFLAGS))
MYLDFLAGS:=$(filter-out -flto=auto,$(MYLDFLAGS))
MYCFLAGS+=-flto
MYLDFLAGS+=-flto
endif
endif
endif

ifeq ("$(HAVELPIE)","no")
MYLDFLAGS:=$(filter-out -pie,$(MYLDFLAGS))
endif

ifeq ("$(NEEDLRT)","need")
MYLDFLAGS+=-lrt
endif
Expand All @@ -84,15 +119,21 @@ Q:=@
E:=@echo
endif

ifeq ("$(HAVEDEPS)","no")
NDEP:=@true
else
NDEP:=
endif

all: $(TARGET)

$(TARGET): $(OBJS)
$(E) LD $@
$(Q)$(CC) -o $@ $(MYLDFLAGS) $^ $(MYLIBS)

bld/%.o: src/%.c bld/.mkdir
$(E) DE $@
$(Q)$(CC) $(MYCFLAGS) -MM -MT $@ -MF $(patsubst %.o,%.d,$@) $<
$(NDEP) $(E) DE $@
$(NDEP) $(Q)$(CC) $(MYCFLAGS) -MM -MT $@ -MF $(patsubst %.o,%.d,$@) $<
$(E) CC $@
$(Q)$(CC) $(MYCFLAGS) -c -o $@ $<

Expand Down Expand Up @@ -144,6 +185,11 @@ pv:
@echo NEEDLRT: $(NEEDLRT)
@echo HAVESREA: $(HAVESREA)
@echo HAVEWDTI: $(HAVEWDTI)
@echo HAVEDEPS: $(HAVEDEPS)
@echo HAVECSTD: $(HAVECSTD)
@echo HAVEFLTA: $(HAVEFLTA)
@echo HAVEFLTO: $(HAVEFLTO)
@echo HAVELPIE: $(HAVELPIE)
@echo MYCFLAGS: $(MYCFLAGS)
@echo MYLDFLAGS: $(MYLDFLAGS)
@echo MYLIBS: $(MYLIBS)
Expand Down

0 comments on commit b210ec5

Please sign in to comment.