Update script that generates CMake files.

This commit is contained in:
Ryan Pavlik 2020-05-06 11:30:10 -05:00
parent f3c6608cc6
commit 8376152d94

View File

@ -1,4 +1,9 @@
#!/usr/bin/env python3
# Copyright 2019-2020, Collabora, Ltd.
# SPDX-License-Identifier: BSL-1.0
# Be sure to install cmake-format first to get best results:
# pip3 install cmake-format
from pathlib import Path
@ -6,15 +11,11 @@ from jinja2 import Environment, FileSystemLoader
from jinja2.utils import Markup
try:
# You need to add:
#
# from .configuration import Configuration
# from .__main__ import process_file
#
# to your [~/.local/lib/python3.7/site-packages/]cmake_format/__init__.py
from cmake_format import process_file as cf_process_file, Configuration as CF_Configuration
from cmake_format.__main__ import process_file as cf_process_file
from cmake_format.configuration import Configuration as CF_Configuration
HAVE_CMAKE_FORMAT = True
except:
print("cmake-format not found... generated files might look gross. Do pip3 install cmake-format")
HAVE_CMAKE_FORMAT = False
@ -25,7 +26,8 @@ class CMaker:
self.env = Environment(keep_trailing_newline=True,
autoescape=False,
loader=FileSystemLoader([str(self.template_dir)]))
self.template = self.env.get_template('CMakeLists.template.cmake')
self.default_template_name = 'CMakeLists.template.cmake'
self.template = self.env.get_template(self.default_template_name)
self.format_config = format_config
def handle_dir(self, d, recurse=True):
@ -56,17 +58,28 @@ class CMaker:
data["ui"] = ui
data["xml"] = list(sorted(x.relative_to(d) for x in glob("*.xml")))
assert(len(data['xml']) in (0, 1))
if len(data['xml']) not in (0, 1):
print(data["xml"])
raise RuntimeError("Expected 0 or 1 xml files for plugin %s, got %d" % (
data["name"], len(data['xml'])))
custom_template_name = d.name + '.cmake'
if (self.template_dir / custom_template_name).exists():
print(d, "has a custom template")
template = self.env.get_template(custom_template_name)
data["template"] = "{} (custom for this directory)".format(
custom_template_name)
else:
template = self.template
data["template"] = "{} (shared with all other directories)".format(
self.default_template_name)
data["assumed_custom_template_name"] = custom_template_name
output = template.render(data)
if self.format_config:
output = cf_process_file(self.format_config, output)
# Handle both original 0.6.0 and newer - tested with 0.6.10dev3
if isinstance(output, tuple):
output = output[0]
with open(d / 'CMakeLists.txt', 'w', encoding='utf-8') as fp:
fp.write(output)
@ -78,13 +91,13 @@ class CMaker:
self.root / 'meshlabplugins',
self.root / 'plugins_experimental',
self.root / 'plugins_unsupported',
self.root / 'sampleplugins'
# self.root / 'sampleplugins'
)
for plugins_dir in plugins_dirs:
if not plugins_dir.exists():
continue
for d in plugins_dir.iterdir():
if d.is_dir() and d.name != "fgt":
if d.is_dir() and d.name not in ("fgt", "external", "shaders"):
self.handle_dir(d)