Skip to content
Open
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
51 changes: 48 additions & 3 deletions bazel_ros2_rules/lib/private/cmake_tools/file_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
from pathlib import Path
import subprocess
import sys
import threading
from typing import List


Expand Down Expand Up @@ -111,7 +113,50 @@ def __init__(self, codemodel_path: Path):
self.targets.append(Target(target_file))


def get_cmake_codemodel(project_path: Path, build_path: Path) -> CodeModel:
class Subprocess:
"""Subprocess wrapper that prints subprocess logs with prefix"""

def __init__(self, prefix: str):
self.prefix = ('[' + prefix + '] ' if prefix else '').encode('utf-8')
self.proc = None
self.threads = []

def _forward_with_prefix(self, in_stream, out_stream):
for line in iter(in_stream.readline, b''):
out_stream.write(self.prefix + line)
out_stream.flush()
in_stream.close()

def popen(self, *args, **kwargs):
kwargs.update(
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
bufsize=0,
)
self.proc = subprocess.Popen(*args, **kwargs)
self.threads = [
threading.Thread(target=self._forward_with_prefix,
args=(self.proc.stdout, sys.stdout.buffer)),
threading.Thread(target=self._forward_with_prefix,
args=(self.proc.stderr, sys.stderr.buffer)),
]
for th in self.threads:
th.start()

def wait(self) -> int:
if self.proc is None:
raise RuntimeError('Process is not started')
returncode = self.proc.wait()
for th in self.threads:
th.join()
return returncode

def run(self, *args, **kwargs) -> int:
self.popen(*args, **kwargs)
return self.wait()


def get_cmake_codemodel(project_path: Path, build_path: Path, label: str = '') -> CodeModel:
"""
Use the cmake-file-api to get information about the code model of a CMake
project.
Expand All @@ -132,8 +177,8 @@ def get_cmake_codemodel(project_path: Path, build_path: Path) -> CodeModel:
query_path.joinpath('codemodel-v2').touch()

args = ['cmake', str(project_path)]
result = subprocess.run(args, cwd=str(build_path))
if result.returncode != 0:
returncode = Subprocess(prefix=label).run(args, cwd=str(build_path))
if returncode != 0:
raise RuntimeError("CMake error occurred on project. See build logs")

# There should only be one file, but just in case the latest is the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def collect_ament_cmake_package_properties(name, metadata):
build_path = project_path.joinpath('build')
build_path.mkdir()
try:
codemodel = get_cmake_codemodel(project_path, build_path)
codemodel = get_cmake_codemodel(project_path, build_path, name)
except RuntimeError as e:
message = f"Error occured while generating build for {name}"
raise RuntimeError(message)
Expand Down