aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild
diff options
context:
space:
mode:
authorDaniel Mensinger <daniel@mensinger-ka.de>2019-03-04 12:58:35 +0100
committerJussi Pakkanen <jpakkane@gmail.com>2019-03-04 13:58:35 +0200
commit760d1bff9cf7db9886aedb02226e5a7105d5d454 (patch)
tree96ce685a101fdc479a77e19698f3244a8ff1d5ce /mesonbuild
parent81f0eef2df495872af38a04a4fd792a2b9058c6e (diff)
downloadmeson-760d1bff9cf7db9886aedb02226e5a7105d5d454.zip
meson-760d1bff9cf7db9886aedb02226e5a7105d5d454.tar.gz
meson-760d1bff9cf7db9886aedb02226e5a7105d5d454.tar.bz2
rewriter: Sort source files (#5010)
* rewriter: Sort source files * rewriter: Natural sorting * rewriter: Fix flake8 * rewriter: Fixed sorting * rewriter: Make sorting key more readable * rewriter: Even simpler key
Diffstat (limited to 'mesonbuild')
-rw-r--r--mesonbuild/rewriter.py34
1 files changed, 25 insertions, 9 deletions
diff --git a/mesonbuild/rewriter.py b/mesonbuild/rewriter.py
index c997434..2619aae 100644
--- a/mesonbuild/rewriter.py
+++ b/mesonbuild/rewriter.py
@@ -565,6 +565,8 @@ class Rewriter:
args = n.arguments
return args
+ to_sort_nodes = []
+
if cmd['operation'] == 'src_add':
node = None
if target['sources']:
@@ -592,14 +594,17 @@ class Rewriter:
to_append += [StringNode(token)]
# Append to the AST at the right place
- if isinstance(node, FunctionNode):
- node.args.arguments += to_append
- elif isinstance(node, ArrayNode):
- node.args.arguments += to_append
+ arg_node = None
+ if isinstance(node, (FunctionNode, ArrayNode)):
+ arg_node = node.args
elif isinstance(node, ArgumentNode):
- node.arguments += to_append
+ arg_node = node
+ assert(arg_node is not None)
+ arg_node.arguments += to_append
# Mark the node as modified
+ if arg_node not in to_sort_nodes and not isinstance(node, FunctionNode):
+ to_sort_nodes += [arg_node]
if node not in self.modefied_nodes:
self.modefied_nodes += [node]
@@ -622,11 +627,9 @@ class Rewriter:
# Remove the found string node from the argument list
arg_node = None
- if isinstance(root, FunctionNode):
- arg_node = root.args
- if isinstance(root, ArrayNode):
+ if isinstance(root, (FunctionNode, ArrayNode)):
arg_node = root.args
- if isinstance(root, ArgumentNode):
+ elif isinstance(root, ArgumentNode):
arg_node = root
assert(arg_node is not None)
mlog.log(' -- Removing source', mlog.green(i), 'from',
@@ -634,6 +637,8 @@ class Rewriter:
arg_node.arguments.remove(string_node)
# Mark the node as modified
+ if arg_node not in to_sort_nodes and not isinstance(root, FunctionNode):
+ to_sort_nodes += [arg_node]
if root not in self.modefied_nodes:
self.modefied_nodes += [root]
@@ -685,6 +690,17 @@ class Rewriter:
}
self.add_info('target', target['id'], test_data)
+ # Sort files
+ for i in to_sort_nodes:
+ convert = lambda text: int(text) if text.isdigit() else text.lower()
+ alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
+ path_sorter = lambda key: ([(key.count('/') <= idx, alphanum_key(x)) for idx, x in enumerate(key.split('/'))])
+
+ unknown = [x for x in i.arguments if not isinstance(x, StringNode)]
+ sources = [x for x in i.arguments if isinstance(x, StringNode)]
+ sources = sorted(sources, key=lambda x: path_sorter(x.value))
+ i.arguments = unknown + sources
+
def process(self, cmd):
if 'type' not in cmd:
raise RewriterException('Command has no key "type"')