Skip to content

Commit

Permalink
mk: more build system work
Browse files Browse the repository at this point in the history
  • Loading branch information
imaami committed Jul 11, 2024
1 parent 8096e18 commit b9d9a4d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
/*.creator.user
/.qtc_clangd
/.vscode
/build
/src/test
cc-id-macros.h
41 changes: 0 additions & 41 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,6 @@ override __libcanth_common_mk_included__ := 1

include $(lastword $(MAKEFILE_LIST:common.mk=mk/cc.mk))

$(call arg_var,O,$(THIS_DIR))
$(call arg_var,PKG_CONFIG,pkg-config)

override O := $(O:/=)/

ifneq (1,$(strip $V))
override undefine V
endif

ifdef V
override undefine Q
override undefine nop
else
ifneq (1,$(strip $Q))
override undefine Q
endif
override nop := @:
endif

ifeq (,$Q$V)
override Y_ := (╯°□°)╯︵ ┻━┻
override Y__ = $(call SGR,38;5;119, $(Y_))
override SGR = $(shell printf '\e[$1m%s\e[m' '$2')
override msg = $(info $($1_pfx)$2)

CLEAN_pfx = $(eval CLEAN_pfx := $$(call SGR,0;35,CLEAN ))$(CLEAN_pfx)
COMPILE_pfx = $(eval COMPILE_pfx := $$(call SGR,0;36,COMPILE ))$(COMPILE_pfx)
INSTALL_pfx = $(eval INSTALL_pfx := $$(call SGR,1;36,INSTALL ))$(INSTALL_pfx)
LINK_pfx = $(eval LINK_pfx := $$(call SGR,1;34,LINK ))$(LINK_pfx)
STRIP_pfx = $(eval STRIP_pfx := $$(call SGR,0;33,STRIP ))$(STRIP_pfx)
SYMLINK_pfx = $(eval SYMLINK_pfx := $$(call SGR,0;32,SYMLINK ))$(SYMLINK_pfx)
YEET_pfx = $(eval YEET_pfx := $$(call SGR,38;5;191,YEET))$(YEET_pfx)
else
override undefine Y__
override undefine msg
endif

ifndef V
override Q := @
endif

$(eval yeet:; $$(call msg,YEET,$$(Y__))@:)
.PHONY: yeet

Expand Down
54 changes: 54 additions & 0 deletions mk/arg.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
ifndef __libcanth_arg_mk_included__
override __libcanth_arg_mk_included__ := 1

include $(lastword $(MAKEFILE_LIST:arg.mk=internal.mk))

override __default_O = $(THIS_DIR)

$(call arg_var,DEBUG)
$(call arg_var,O)
$(call arg_var,PKG_CONFIG,pkg-config)
$(call arg_var,Q)
$(call arg_var,V)

$(eval override O=$$(eval override O:=$$$$(patsubst\
//,/,$$$$(or $$$$(realpath $(value O)),$$$$(error \
Output directory "$(value O)" does not exist))/))$$O)

ifneq (1,$(strip $V))
override undefine V
endif

ifdef V
override undefine Q
override undefine nop
else
ifneq (1,$(strip $Q))
override undefine Q
endif
override nop := @:
endif

ifeq (,$(strip $Q$V))
override Y_ := (╯°□°)╯︵ ┻━┻
override Y__ = $(call SGR,38;5;119, $(Y_))
override SGR = $(shell printf '\e[$1m%s\e[m' '$2')
override msg = $(info $($1_pfx)$2)

override CLEAN_pfx = $(eval override CLEAN_pfx := $$(call SGR,0;35,CLEAN ))$(CLEAN_pfx)
override COMPILE_pfx = $(eval override COMPILE_pfx := $$(call SGR,0;36,COMPILE ))$(COMPILE_pfx)
override INSTALL_pfx = $(eval override INSTALL_pfx := $$(call SGR,1;36,INSTALL ))$(INSTALL_pfx)
override LINK_pfx = $(eval override LINK_pfx := $$(call SGR,1;34,LINK ))$(LINK_pfx)
override STRIP_pfx = $(eval override STRIP_pfx := $$(call SGR,0;33,STRIP ))$(STRIP_pfx)
override SYMLINK_pfx = $(eval override SYMLINK_pfx := $$(call SGR,0;32,SYMLINK ))$(SYMLINK_pfx)
override YEET_pfx = $(eval override YEET_pfx := $$(call SGR,38;5;191,YEET))$(YEET_pfx)
else
override undefine Y__
override undefine msg
endif

ifndef V
override Q := @
endif

endif
7 changes: 3 additions & 4 deletions mk/cc.mk
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
ifndef __libcanth_cc_mk_included__
override __libcanth_cc_mk_included__ := 1

include $(lastword $(MAKEFILE_LIST:cc.mk=internal.mk))
include $(lastword $(MAKEFILE_LIST:cc.mk=arg.mk))

$(call arg_var,CC, gcc)
$(call arg_var,CXX, g++)
$(call arg_var,CC,gcc)
$(call arg_var,CXX,g++)

$(call import-macros, \
cc-id-macros, \
Expand Down Expand Up @@ -44,7 +44,6 @@ override __default_CXXFLAGS = $(__default_CFLAGS)
override __default_USE_CLANG = $(__clang__)

$(call arg_var,CPU)
$(call arg_var,DEBUG)
$(call arg_var,ARCH,native)
$(call arg_var,TUNE,native)
$(call arg_var,CSTD)
Expand Down
38 changes: 34 additions & 4 deletions mk/internal.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
ifndef __libcanth_internal_mk_included__
override __libcanth_internal_mk_included__ := 1

ifneq (,$(filter command file override,$(firstword $(origin DEBUG_MK))))
ifneq (1,$(strip $(DEBUG_MK)))
override undefine DEBUG_MK
endif
else ifneq (undefined,$(origin DEBUG_MK))
override undefine DEBUG_MK
endif

override define n:=


Expand All @@ -20,31 +28,51 @@ override sfx-if = $(if $1,$1$2)
override arg_var = $(call arg_var_,$(strip $(value 1)),$(strip $(value 2)))
override define arg_var_
$(eval
ifeq (,$$(filter file override undefined,$$(firstword $$(origin __default_$1))))
# Unset the __default variable if it was set outside the makefile.
$$(if $$(DEBUG_MK),$$(info override undefine __default_$1 [$$(origin __default_$1)]))
override undefine __default_$1
endif

ifeq (environment,$$(firstword $$(origin $1)))
# Unset the target variable if it originates from the environment.
$$(if $$(DEBUG_MK),$$(info override undefine $1 [$$(origin $1)]))
override undefine $1
else ifeq (default,$$(origin $1))
# The target variable is a make builtin default.
ifdef 2
# Explicit fallback takes precedence over a builtin default value.
$$(if $$(DEBUG_MK),$$(info override undefine $1 [default]))
override undefine $1
else ifneq (undefined,$$(origin __default_$1))
# The __default variable takes precedence over the builtin default.
$$(if $$(DEBUG_MK),$$(info override undefine $1 [$$(origin $1)]))
override undefine $1
endif
else ifneq (undefined,$$(origin $1))
# The target variable was set in the makefile or on the command line.
# Unset if it's empty, as we only accept nonempty argument variables
# from the user. To allow empty command line variables, __default_$1
# must be defined as empty (not undefined), and there must not be an
# explicit fallback.
ifeq (simple,$$(flavor $1))
ifeq (,$$(strip $$($1)))
$$(if $$(DEBUG_MK),$$(info override undefine $1 [$$(origin $1)]))
override undefine $1
endif
else ifeq (,$$(strip $$(value $1)))
$$(if $$(DEBUG_MK),$$(info override undefine $1 [$$(origin $1)]))
override undefine $1
endif
endif

ifeq (undefined,$$(origin $1))
# The target variable is undefined, i.e. we can modify it.
ifdef 2
$$(if $$(DEBUG_MK),$$(info override $1 = $$$$(eval override $1 := $$(value 2))$$$$($1)))
override $1 = $$(eval override $1 := $(value 2))$$($1)
else ifneq (undefined,$$(origin __default_$1))
$$(if $$(DEBUG_MK),$$(info override $1 = $$$$(eval override $1 := $$(value __default_$1))$$$$($1)))
override $1 = $$(eval override $1 := $$(value __default_$1))$$($1)
endif
endif
Expand Down Expand Up @@ -123,9 +151,11 @@ endef

# Generate rules and dependencies.
override define target_rules
$(eval override .O = $$(eval override .O := $$$$(shell \
realpath --relative-to='$$(THIS_DIR)' '$$O'))$$(eval \
override .O := $$$$(if $$$$(.O:.=),$$$$(.O)/))$$(.O))
$(eval override .O = $$(eval override .O :=$$$$(shell bash -c ' \
a=$$$$$$$$(realpath "$$$$O"); \
b=$$$$$$$$(realpath --relative-to="$$$$(THIS_DIR)" "$$$$O"); \
(( $$$$$$$${#a} > $$$$$$$${#b} )) || b="$$$$$$$${a%/}"; printf "%s/" \
"$$$$$$$$b"'))$$(if $$(.O:./=),,$$(eval override .O:=))$$(.O))
$(eval override undefine OBJ)
$(foreach t,$1,$(eval $t:| $$O$t) \
$(eval override OBJ_$t := $$(SRC_$t:%=$$O%.o)) \
Expand Down

0 comments on commit b9d9a4d

Please sign in to comment.