Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ python tools/check_release.py --libxr ../libxr --xrobot . \

```json
{
"xr-syntax": "0.2.0",
"xr-syntax": "0.3.0",
"repositories": {
"libxr": {"commit": "<40-hex>"},
"xrobot": {"commit": "<40-hex>", "version": "1.0.0"},
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies = [
"pyyaml",
"requests",
"ruamel.yaml>=0.17.21",
"xr-syntax==0.2.0"
"xr-syntax==0.3.0"
]

classifiers = [
Expand Down
373 changes: 0 additions & 373 deletions src/xrobot/cpp_layout.py

This file was deleted.

43 changes: 30 additions & 13 deletions src/xrobot/generate_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
import re
from pathlib import Path

from xr_syntax.cpp import CppFileBuilder
from xr_syntax.format import (
INDENT,
layout_call,
layout_declaration,
layout_macro,
layout_value,
sort_includes,
wrap_comment,
)
from xr_syntax.i18n import tr

from xrobot.config import (
Expand All @@ -35,15 +45,6 @@
template_bindings,
type_shape,
)
from xrobot.cpp_layout import (
INDENT,
layout_call,
layout_declaration,
layout_macro,
layout_value,
sort_includes,
wrap_comment,
)
from xrobot.module_parser import discover_modules, module_interface, select_module
from xrobot.project import Project, atomic_write, header_banner, header_relative
from xrobot.source_syntax import (
Expand Down Expand Up @@ -934,17 +935,33 @@ def _monitor_checks(names: list[str]) -> list[str]:

@staticmethod
def _numbered(lines: list, back: object, header_name: str | None) -> str:
"""把占位换成指回头文件自身的 #line,并拼成文本。
Replace the placeholders with #line directives that point back into the header
itself, and join the text.
"""把占位换成指回头文件自身的 #line,再交给 CppFileBuilder 拼成整份文件。
Replace the placeholders with #line directives that point back into the header itself and
hand the lines to CppFileBuilder.

#line 的编号只由它之前已经输出的行数决定,因此先算出全部行再拼装。
The number of a #line directive depends only on how many lines were emitted before it, so
every line is settled before the file is assembled.

最后一行是空行时,它就是文件末尾的那个换行,不再补第二个;否则补一个换行,让文件以换行结束。
An empty last line is the newline at the end of the file and gets no second one; otherwise
one is added, so the file ends with a newline.
"""
result, number = [], 1 # 下一行输出的行号 / line number of the next emitted line
for line in lines:
if line is back:
line = f'#line {number + 1} "{_c_string(header_name)}"'
result.append(line)
number += line.count("\n") + 1
return "\n".join(result) + ("" if result and result[-1] == "" else "\n")
if result and result[-1] == "":
# build() 逐片段原样拼接:末尾的空片段是一个空行,比 "\n".join(lines) 多出一个换行。
# build() joins the chunks as written, so a trailing empty chunk is one blank line more
# than "\n".join(lines) gives.
result.pop()
builder = CppFileBuilder()
for line in result:
builder.raw(line)
return builder.build(require_clean=True).render()


def _c_string(path: str) -> str:
Expand Down
19 changes: 14 additions & 5 deletions src/xrobot/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from urllib.request import url2pathname

import yaml
from xr_syntax.cmake import CMakeFileBuilder
from xr_syntax.i18n import tr

from xrobot.git import checkout_state, git, head_commit, origin_url
Expand Down Expand Up @@ -1246,18 +1247,26 @@ def write_cmake(modules_dir: Path, records: dict) -> None:
"""写出 Modules/CMakeLists.txt:模块自带 CMakeLists.txt 时 include 它,否则加 include 目录。
Write Modules/CMakeLists.txt: include a Module's own CMakeLists.txt, else add its
folder to the include path.

文件由 CMakeFileBuilder 生成,它在 build() 时把整份源码解析一次,因此写出的 CMake 一定是
xr-syntax 自己能读的。
CMakeFileBuilder writes the file and parses the whole source once in build(), so the CMake it
writes is always something xr-syntax can read back.
"""
lines = ["# Generated by `xrobot setup` from xrobot.lock; do not edit or commit.", ""]
builder = CMakeFileBuilder()
builder.comment("Generated by `xrobot setup` from xrobot.lock; do not edit or commit.")
builder.raw("")
for identity in sorted(records):
validate_id(identity)
folder = Path(modules_dir) / identity
if (folder / "CMakeLists.txt").exists():
lines.append(f'include("${{CMAKE_CURRENT_LIST_DIR}}/{identity}/CMakeLists.txt")')
builder.command("include", [f'"${{CMAKE_CURRENT_LIST_DIR}}/{identity}/CMakeLists.txt"'])
else:
lines.append(
f'target_include_directories(xr PUBLIC "${{CMAKE_CURRENT_LIST_DIR}}/{identity}")'
builder.command(
"target_include_directories",
["xr", "PUBLIC", f'"${{CMAKE_CURRENT_LIST_DIR}}/{identity}"'],
)
atomic_write(Path(modules_dir) / "CMakeLists.txt", "\n".join(lines) + "\n")
atomic_write(Path(modules_dir) / "CMakeLists.txt", builder.build(require_clean=True).render())


def _load_lock(lock_path: Path) -> dict:
Expand Down
34 changes: 17 additions & 17 deletions src/xrobot/module_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pathlib import Path

import yaml
from xr_syntax.cpp import CppFileBuilder
from xr_syntax.i18n import tr

from xrobot.config import identifier_problem
Expand Down Expand Up @@ -119,29 +120,28 @@ def _header(
sort_keys=False,
allow_unicode=True,
).rstrip()
lines = [
"#pragma once",
"",
"// clang-format off",
"/* === MODULE MANIFEST V2 ===",
manifest,
"=== END MANIFEST === */",
"// clang-format on",
"",
*_include_lines(constructors + templates, includes),
"",
]
builder = CppFileBuilder(header=True)
builder.raw("")
# manifest 由 clang-format 关闭保护,它的排版由 yaml.safe_dump 决定。
# The manifest is protected by clang-format off; yaml.safe_dump decides its layout.
builder.format_region(
"\n/* === MODULE MANIFEST V2 ===\n" + manifest + "\n=== END MANIFEST === */\n"
)
builder.raw("")
for line in _include_lines(constructors + templates, includes):
builder.raw(line)
builder.raw("")
if templates:
lines.append(f"template <{', '.join(templates)}>")
lines += [
builder.raw(f"template <{', '.join(templates)}>")
for line in (
f"class {class_name}",
"{",
" public:",
f" {class_name}({', '.join(constructors)}) {{}}",
"};",
"",
]
return "\n".join(lines)
):
builder.raw(line)
return builder.build(require_clean=True).render()


def _interface(text: str, class_name: str, constructors: list[str], templates: list[str]) -> dict:
Expand Down
3 changes: 1 addition & 2 deletions src/xrobot/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
from typing import NamedTuple

from xr_syntax.cpp import identifier_occurrences
from xr_syntax.format import wrap_comment
from xr_syntax.i18n import tr

from xrobot.cpp_layout import wrap_comment

MODULES_YAML = Path("Modules/modules.yaml")
SOURCE_SUFFIXES = (".c", ".cc", ".cpp", ".cxx")
HEADER_LINE = re.compile(r'^// xrobot: (config|entry|depends) "([^"]+)"$')
Expand Down
Loading
Loading