| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
import sys |
| 5 |
import os |
| 6 |
import re |
| 7 |
|
| 8 |
|
| 9 |
# Define regexes for matches |
| 10 |
|
| 11 |
commandCallRegex = \ |
| 12 |
re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*\(') |
| 13 |
commandCallWithSpacesRegex = \ |
| 14 |
re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*[\s]*\(') |
| 15 |
macroDefRegex = \ |
| 16 |
re.compile(r'[^a-zA-Z_][mM][aA][cC][rR][oO]\([\s]*[a-zA-Z_][a-zA-Z0-9_]*') |
| 17 |
functionDefRegex = \ |
| 18 |
re.compile(r'[^a-zA-Z_][fF][uU][nN][cC][tT][iI][oO][nN]\([\s]*[a-zA-Z_][a-zA-Z0-9_]*') |
| 19 |
|
| 20 |
specialCommandNamesList = [ |
| 21 |
r'[sS][eE][tT]', |
| 22 |
r'[iI][fF]', |
| 23 |
r'[eE][lL][sS][eE][iI][fF]', |
| 24 |
r'[fF][oO][rR][eE][aA][cC][hH]', |
| 25 |
r'[wW][hH][iI][lL][eE]', |
| 26 |
] |
| 27 |
specialCommandRegexList = [] |
| 28 |
for specialCommandName in specialCommandNamesList: |
| 29 |
specialCommandRegexList.append( |
| 30 |
re.compile(specialCommandName+r'[\s]*\(') ) |
| 31 |
|
| 32 |
andLogicalRegex = re.compile(r'AND[\s]*\(', re.DOTALL) |
| 33 |
orLogicalRegex = re.compile(r'OR[\s]*\(', re.DOTALL) |
| 34 |
notLogicalRegex = re.compile(r'NOT[\s]*\(', re.DOTALL) |
| 35 |
|
| 36 |
|
| 37 |
# Lower case CMake command calls and macro and function names. |
| 38 |
# |
| 39 |
def makeCmndsLowerCaseInCMakeStr(cmakeCodeStrIn): |
| 40 |
cmakeCodeStrOut = cmakeCodeStrIn |
| 41 |
cmakeCodeStrOut = makeRegexMatchesLowerCaseInCMakeStr(cmakeCodeStrOut, |
| 42 |
commandCallWithSpacesRegex) |
| 43 |
cmakeCodeStrOut = makeRegexMatchesLowerCaseInCMakeStr(cmakeCodeStrOut, |
| 44 |
macroDefRegex) |
| 45 |
cmakeCodeStrOut = makeRegexMatchesLowerCaseInCMakeStr(cmakeCodeStrOut, |
| 46 |
functionDefRegex) |
| 47 |
return cmakeCodeStrOut |
| 48 |
|
| 49 |
|
| 50 |
# Make regex pattern matches lower cases. |
| 51 |
# |
| 52 |
def makeRegexMatchesLowerCaseInCMakeStr(cmakeCodeStrIn, regex): |
| 53 |
cmakeCodeStrOut = "" |
| 54 |
cmakeCodeStrInLen = len(cmakeCodeStrIn) |
| 55 |
cmakeCodeStrStartSearchIdx = 0 |
| 56 |
while cmakeCodeStrStartSearchIdx < cmakeCodeStrInLen: |
| 57 |
m = regex.search(cmakeCodeStrIn[cmakeCodeStrStartSearchIdx:]) |
| 58 |
if m: |
| 59 |
#print("") |
| 60 |
#print("m.group() = '"+str(m.group())+"'") |
| 61 |
# Get the substr for the match |
| 62 |
cmakeCodeStrMatchStartIdx = cmakeCodeStrStartSearchIdx + m.start() |
| 63 |
cmakeCodeStrMatchEndIdx = cmakeCodeStrStartSearchIdx + m.end() |
| 64 |
commandCallMatchStr = \ |
| 65 |
cmakeCodeStrIn[cmakeCodeStrMatchStartIdx:cmakeCodeStrMatchEndIdx] |
| 66 |
assert m.group() == commandCallMatchStr |
| 67 |
# Lower case the matching command call and update the output string |
| 68 |
# since last match |
| 69 |
cmakeCodeStrOut += \ |
| 70 |
cmakeCodeStrIn[cmakeCodeStrStartSearchIdx:cmakeCodeStrMatchStartIdx] |
| 71 |
cmakeCodeStrOut += conditionalLowerCaseCMakeGroup(commandCallMatchStr) |
| 72 |
# Update indexes for next search |
| 73 |
cmakeCodeStrStartSearchIdx += m.end() |
| 74 |
else: |
| 75 |
# No more matches so copy the rest of the string |
| 76 |
cmakeCodeStrOut += \ |
| 77 |
cmakeCodeStrIn[cmakeCodeStrStartSearchIdx:] |
| 78 |
break |
| 79 |
|
| 80 |
return cmakeCodeStrOut |
| 81 |
|
| 82 |
|
| 83 |
def conditionalLowerCaseCMakeGroup(cmakeMatchGroup): |
| 84 |
cmakeMatchGroupNewCase = None |
| 85 |
#print("") |
| 86 |
#print("cmakeMatchGroup = '"+cmakeMatchGroup+"'") |
| 87 |
if isConditionalCMakeGroup(cmakeMatchGroup): |
| 88 |
#print("Is a conditional expression") |
| 89 |
cmakeMatchGroupNewCase = cmakeMatchGroup |
| 90 |
elif isSpecialCommandNameMatch(cmakeMatchGroup): |
| 91 |
#print("Is special command name!") |
| 92 |
cmakeMatchGroupNewCase = cmakeMatchGroup.lower() |
| 93 |
elif isMacroOrFunctionGroup(cmakeMatchGroup): |
| 94 |
cmakeMatchGroupNewCase = cmakeMatchGroup.lower() |
| 95 |
elif commandCallRegex.match(cmakeMatchGroup): |
| 96 |
#print("Is standard command call!") |
| 97 |
cmakeMatchGroupNewCase = cmakeMatchGroup.lower() |
| 98 |
else: |
| 99 |
#print("Does not match known cases so don't lower case!") |
| 100 |
cmakeMatchGroupNewCase = cmakeMatchGroup |
| 101 |
#print("cmakeMatchGroupNewCase = '"+cmakeMatchGroupNewCase+"'") |
| 102 |
return cmakeMatchGroupNewCase |
| 103 |
|
| 104 |
|
| 105 |
def isConditionalCMakeGroup(cmakeMatchGroup): |
| 106 |
if andLogicalRegex.match(cmakeMatchGroup) \ |
| 107 |
or orLogicalRegex.match(cmakeMatchGroup) \ |
| 108 |
or notLogicalRegex.match(cmakeMatchGroup) \ |
| 109 |
: |
| 110 |
return True |
| 111 |
return False |
| 112 |
|
| 113 |
|
| 114 |
def isSpecialCommandNameMatch(cmakeMatchGroup): |
| 115 |
for specalCommandRegex in specialCommandRegexList: |
| 116 |
#print("specalCommandRegex.pattern = '"+specalCommandRegex.pattern+"'") |
| 117 |
m = specalCommandRegex.match(cmakeMatchGroup) |
| 118 |
if m: |
| 119 |
#print("MATCH!") |
| 120 |
return True |
| 121 |
return False |
| 122 |
|
| 123 |
|
| 124 |
def isMacroOrFunctionGroup(cmakeMatchGroup): |
| 125 |
if macroDefRegex.match(cmakeMatchGroup): |
| 126 |
return True |
| 127 |
if functionDefRegex.match(cmakeMatchGroup): |
| 128 |
return True |
| 129 |
return False |
| 130 |
|
| 131 |
|
| 132 |
# |
| 133 |
# Helper functions for main() |
| 134 |
# |
| 135 |
|
| 136 |
|
| 137 |
usageHelp = r""" |
| 138 |
|
| 139 |
Convert cmake command calls to lower case and macro and function names in |
| 140 |
definitions to lower case in a given file. |
| 141 |
|
| 142 |
The replacements are somewhat conservative in order not to make too many |
| 143 |
replacements in non-CMake code. That is, in general, any text of the form: |
| 144 |
|
| 145 |
<identifier>( |
| 146 |
|
| 147 |
will result in '<identifier>' being lower-cased. However, this does not apply |
| 148 |
for logical operators 'AND, 'OR', and 'NOT'. |
| 149 |
|
| 150 |
Also, certain common CMake commands that often have spaces between |
| 151 |
'<identifier>' and '(' like: |
| 152 |
|
| 153 |
SET (...) |
| 154 |
IF (...) |
| 155 |
ELSEIF (...) |
| 156 |
FOREACH (...) |
| 157 |
WHILE (...) |
| 158 |
|
| 159 |
will be lower-cased as well. (These are less likely to match in regular |
| 160 |
text.) |
| 161 |
""" |
| 162 |
|
| 163 |
|
| 164 |
def getCmndLineOptions(): |
| 165 |
from argparse import ArgumentParser, RawDescriptionHelpFormatter |
| 166 |
clp = ArgumentParser(description=usageHelp, |
| 167 |
formatter_class=RawDescriptionHelpFormatter) |
| 168 |
clp.add_argument("file", |
| 169 |
help="The file to lower-case the CMake commands within." ) |
| 170 |
options = clp.parse_args(sys.argv[1:]) |
| 171 |
return options |
| 172 |
|
| 173 |
|
| 174 |
def validateCmndLineOptions(inOptions): |
| 175 |
if not os.path.exists(inOptions.file): |
| 176 |
print("Error, the file '"+inOptions.file+"' does not exist!") |
| 177 |
sys.exit(1) |
| 178 |
|
| 179 |
|
| 180 |
def lowerCaseCMakeCodeInFile(inOptions): |
| 181 |
with open(inOptions.file, 'r') as fileHandle: |
| 182 |
cmakeStrOrig = fileHandle.read() |
| 183 |
cmakeStrLowerCased = makeCmndsLowerCaseInCMakeStr(cmakeStrOrig) |
| 184 |
with open(inOptions.file, 'w') as fileHandle: |
| 185 |
fileHandle.write(cmakeStrLowerCased) |
| 186 |
|
| 187 |
# |
| 188 |
# main() |
| 189 |
# |
| 190 |
|
| 191 |
|
| 192 |
if __name__ == '__main__': |
| 193 |
inOptions = getCmndLineOptions() |
| 194 |
validateCmndLineOptions(inOptions) |
| 195 |
lowerCaseCMakeCodeInFile(inOptions) |