Welcome to Blogs @ Andrew Qu
Blog Index
All blogs
Search results

GNU Make Random Number and String Length Using Python


In 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.


Android building system is based on GNU make. However, GNU make does not appear to have function to

  1. Return a random number
  2. Return the length of a text string
The python script, makeutil.py has 2 functions:
  1. Python makeutil.py - returns a random number
  2. 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).
A sample Makefile can be download that contains more usage examples.

File Listings

import random
import sys
if len(sys.argv) > 1 : # return length of input string
else : # return a random number
# 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)
Ads from Google
Dr Li Anchor Profi
Engineering anchorage plate design system
©Andrew Qu, 2014. All rights reserved. Code snippets may be used "AS IS" without any kind of warranty. DIY tips may be followed at your own risk.