GNU Make Random Number and String Length Using Python
SummaryIn GNU make, there appears no built-in function to return a random number, nor a function to return the length of a string. This utility gives ways to do both using Python.
OverviewAndroid building system is based on GNU make. However, GNU make does not appear to have function to
- Return a random number
- Return the length of a text string
- Python makeutil.py - returns a random number
- Python makeutil.py string - return the length of the input string
How to Use the Script
Once makeutil.py is copied into the Makefile folder, you can use GNU make's shell command to set the output into a variable, for example,
- $(shell python makeutil.py) will return a random number.
- $(shell python makeutil.py "text string") will return the length of the input string (11 in this case).
File Listings
makeutil.py
import random import sys if len(sys.argv) > 1 : # return length of input string sys.stdout.write(str(len(sys.argv[1]))) else : # return a random number sys.stdout.write(str(random.randint(0,4294967295)))Makefile
# Ways of generate random numbers # # 1. Using a python script randnum := $(shell python makeutil.py) # # 2. using shell date (not really random, but would be unique during program execution randnum_bydate := $(shell date +%H%M%S%N) # $(info pre-assigned random number=$(randnum)) $(info pre-assigned randnum_bydate=$(randnum_bydate)) # $(info length of $(randnum_bydate) = $(shell python makeutil.py "$(randnum_bydate)")) # all : Makefile $(eval runtime_number := $(shell python makeutil.py)) $(eval runtime_randnum_bydate := $(shell date +%H%M%S%N)) @echo runtime_number=$(runtime_number) @echo runtime_randnum_bydate=$(runtime_randnum_bydate) @echo length of $(runtime_number) = $(shell python makeutil.py $(runtime_number)) $(info end of script)