[PATCH] D110019: [gn build] improve write_cmake_config to be truthy and exception friendly
yaozhongxiao via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 18 01:41:01 PDT 2021
zhongxiao.yzx created this revision.
zhongxiao.yzx added reviewers: thakis, mantognini, alexfh.
Herald added a subscriber: mgorny.
zhongxiao.yzx requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
The rule of python for strings is that an empty string is
considered False, a non-empty string is considered True.
It means '0','false' and many other string value will be
treat "true" which should be "false". "distutils.util.strtobool"
can help use to solve the problem to much extent.
Besides, in order to substiute #cmakedefine var with value,
it needs to access "values[key]" which will raises a "KeyError"
exception if searching for a key that does not exist. "values.get(key)"
can help use to avoid "KeyError" exception in a simply way.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D110019
Files:
llvm/utils/gn/build/write_cmake_config.py
Index: llvm/utils/gn/build/write_cmake_config.py
===================================================================
--- llvm/utils/gn/build/write_cmake_config.py
+++ llvm/utils/gn/build/write_cmake_config.py
@@ -38,7 +38,7 @@
import os
import re
import sys
-
+from distutils.util import strtobool
def main():
parser = argparse.ArgumentParser(
@@ -69,16 +69,17 @@
def repl(m):
key = m.group(1) or m.group(2)
unused_values.discard(key)
- return values[key]
+ return values.get(key)
in_line = var_re.sub(repl, in_line)
if in_line.startswith('#cmakedefine01 '):
_, var = in_line.split()
- if values[var] == '0':
- print('error: "%s=0" used with #cmakedefine01 %s' % (var, var))
- print(" '0' evaluates as truthy with #cmakedefine01")
- print(' use "%s=" instead' % var)
- return 1
- in_line = '#define %s %d\n' % (var, 1 if values[var] else 0)
+ bool_value = values.get(var)
+ try:
+ bool_value = strtobool(bool_value)
+ except:
+ bool_value = None
+ pass
+ in_line = '#define %s %d\n' % (var, 1 if bool_value else 0)
unused_values.discard(var)
elif in_line.startswith('#cmakedefine '):
_, var = in_line.split(None, 1)
@@ -88,7 +89,7 @@
except:
var = var.rstrip()
in_line = '#define %s\n' % var
- if not values[var]:
+ if not values.get(var):
in_line = '/* #undef %s */\n' % var
unused_values.discard(var)
out_lines.append(in_line)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110019.373396.patch
Type: text/x-patch
Size: 1754 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210918/9b676725/attachment.bin>
More information about the llvm-commits
mailing list