Skip to content

Instantly share code, notes, and snippets.

@OrangeTide
Created March 30, 2019 14:09
Show Gist options
  • Save OrangeTide/96be43e35abe1bfea793307b42e872b8 to your computer and use it in GitHub Desktop.
Save OrangeTide/96be43e35abe1bfea793307b42e872b8 to your computer and use it in GitHub Desktop.
Sample build system
TYPE=executable
PROGRAM=demo1
SOURCES=demo1.c
PACKAGES=m tinfo
# Default options
CFLAGS = -Wall -W -g -O
# CFLAGS += -D_GNU_SOURCE
# OS Detection
ifeq ($(OS),Windows_NT)
X=.exe
RM=del
else
X=
endif
# Package generators
## package-system parameters: (name,cflags,ldflags)
define package-system =
PKG.$(1).CFLAGS = $(2)
PKG.$(1).LDFLAGS = $(3)
endef
## get-package-cflags parameters: (names)
get-package-cflags = $(foreach pkg,$(1),$$(PKG.$(pkg).CFLAGS))
## get-package-ldflags parameters: (names)
get-package-ldflags = $(foreach pkg,$(1),$$(PKG.$(pkg).LDFLAGS))
# Packages (TODO: only run pkg-config if package is used)
$(eval $(call package-system,m,,-lm)) # math libray
$(eval $(call package-system,rt,,-lrt)) # rt for clock_gettime(), etc
$(eval $(call package-system,ncurses,$(shell pkg-config --cflags ncurses),$(shell pkg-config --libs ncurses))) # ncurses
# $(eval $(call package-system,libedit,$(shell pkg-config --cflags libedit),$(shell pkg-config --libs libedit))) # libedit
PKG.tinfo.CFLAGS = $(shell pkg-config --cflags tinfo)
PKG.tinfo.LDFLAGS = $(shell pkg-config --libs tinfo)
# Target generators
## target-exe parametes (target-name,source files,packages)
define target-exe =
EXE.$(1) = $(1)$X
SRCS.$(1) = $(2)
OBJS.$(1) = $$(SRCS.$(1):.c=.o)
ifneq (,$(3))
#OLD: $$(EXE.$(1)) $$(OBJS.$(1)) : CFLAGS += $(call get-package-cflags,$(3))
$$(EXE.$(1)) : CFLAGS += $(call get-package-cflags,$(3))
$$(EXE.$(1)) : LDFLAGS += $(call get-package-ldflags,$(3))
endif
$$(EXE.$(1)) : $$(OBJS.$(1)) ; $$(CC) -o $$@ $$(LDFLAGS) $$^ $$(LOADLIBES) $$(LD
LIBS)
all :: $$(EXE.$(1))
clean :: ; $(RM) $$(EXE.$(1)) $$(OBJS.$(1))
endef
# Default rules
all ::
clean ::
# Load Project definitions
## target-load parameters: (filename)
## description: helper for processing .target files
define target-load =
TYPE:=
PROGRAM:=
SOURCES:=
PACKAGES:=
$(eval include $(1))
ifeq ($(TYPE),executable)
$(call target-exe,$(PROGRAM),$(SOURCES),$(PACKAGES))
$($(PROGRAM)_EXE) : | $(1)
# TODO: add conditions for other types once we support them.
else
$$(error TYPE must be one of "executable", "static-library", "dll", or "module")
endif
TYPE:=
PROGRAM:=
SOURCES:=
PACKAGES:=
endef
# load all .target files in the current directory
$(foreach proj,$(wildcard *.target),$(eval $(call target-load,$(proj))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment