diff --git a/Makefile.include b/Makefile.include index aa68fdddc1..9ddde728c7 100644 --- a/Makefile.include +++ b/Makefile.include @@ -89,6 +89,7 @@ CLEAN = $(filter clean, $(MAKECMDGOALS)) # include makefiles utils tools include $(RIOTMAKE)/utils/variables.mk +include $(RIOTMAKE)/utils/strings.mk # get host operating system OS := $(shell uname) diff --git a/makefiles/utils/strings.mk b/makefiles/utils/strings.mk new file mode 100644 index 0000000000..0edb0ce85e --- /dev/null +++ b/makefiles/utils/strings.mk @@ -0,0 +1,8 @@ +# Make only version of string functions +# +# This replaces the pattern of using ':= $(shell echo $(var) | tr 'a-z-' 'A-Z_)' +# On local tests the make version was ~100 times faster than the shell one + +lowercase = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1)))))))))))))))))))))))))) +uppercase = $(subst a,A,$(subst b,B,$(subst c,C,$(subst d,D,$(subst e,E,$(subst f,F,$(subst g,G,$(subst h,H,$(subst i,I,$(subst j,J,$(subst k,K,$(subst l,L,$(subst m,M,$(subst n,N,$(subst o,O,$(subst p,P,$(subst q,Q,$(subst r,R,$(subst s,S,$(subst t,T,$(subst u,U,$(subst v,V,$(subst w,W,$(subst x,X,$(subst y,Y,$(subst z,Z,$1)))))))))))))))))))))))))) +uppercase_and_underscore = $(call uppercase,$(subst -,_,$1)) diff --git a/makefiles/utils/test-strings.mk b/makefiles/utils/test-strings.mk new file mode 100644 index 0000000000..e1e2059c35 --- /dev/null +++ b/makefiles/utils/test-strings.mk @@ -0,0 +1,14 @@ +include strings.mk + +STRING_LOWER = abcdefghijklmnopqrstuvwxyz-123456789 +STRING_UPPER = ABCDEFGHIJKLMNOPQRSTUVWXYZ-123456789 +STRING_MACRO = ABCDEFGHIJKLMNOPQRSTUVWXYZ_123456789 + +test-lowercase: + $(Q)bash -c 'test "$(STRING_LOWER)" = "$(call lowercase,$(STRING_UPPER))" || { echo ERROR: "$(STRING_LOWER)" != "$(call lowercase,$(STRING_UPPER))"; exit 1; }' + +test-uppercase: + $(Q)bash -c 'test "$(STRING_UPPER)" = "$(call uppercase,$(STRING_LOWER))" || { echo ERROR: "$(STRING_UPPER)" != "$(call uppercase,$(STRING_LOWER))"; exit 1; }' + +test-uppercase_and_underscore: + $(Q)bash -c 'test "$(STRING_MACRO)" = "$(call uppercase_and_underscore,$(STRING_LOWER))" || { echo ERROR: "$(STRING_MACRO)" != "$(call uppercase_and_underscore,$(STRING_LOWER))"; exit 1; }' diff --git a/tests/build_system_utils/Makefile b/tests/build_system_utils/Makefile index dc3afac78b..2b0157e028 100644 --- a/tests/build_system_utils/Makefile +++ b/tests/build_system_utils/Makefile @@ -19,6 +19,9 @@ MAKEFILES_UTILS = $(RIOTMAKE)/utils COMPILE_TESTS += test-ensure_value test-ensure_value-negative COMPILE_TESTS += test-exported-variables COMPILE_TESTS += test-memoized-variables +COMPILE_TESTS += test-lowercase +COMPILE_TESTS += test-uppercase +COMPILE_TESTS += test-uppercase_and_underscore # Tests will be run both in the host machine and in `docker` all: build-system-utils-tests @@ -39,3 +42,10 @@ test-exported-variables: test-memoized-variables: $(Q)$(call command_should_succeed,"$(MAKE)" -C $(MAKEFILES_UTILS) -f test-variables.mk test-memoized-variables) + +test-lowercase: + $(Q)$(call command_should_succeed,"$(MAKE)" -C $(MAKEFILES_UTILS) -f test-strings.mk test-lowercase) +test-uppercase: + $(Q)$(call command_should_succeed,"$(MAKE)" -C $(MAKEFILES_UTILS) -f test-strings.mk test-uppercase) +test-uppercase_and_underscore: + $(Q)$(call command_should_succeed,"$(MAKE)" -C $(MAKEFILES_UTILS) -f test-strings.mk test-uppercase_and_underscore)