Skip to content

Commit

Permalink
fix: Fixed has target check taking ages for large projects
Browse files Browse the repository at this point in the history
The old implementation took up to 30 seconds for packages,
were a lot of targets existed. This is a common case for
interface packages containing a lot of messages.

Signed-off-by: Janosch Machowinski <J.Machowinski@cellumation.com>
  • Loading branch information
Janosch Machowinski committed Jun 12, 2024
1 parent 1509c77 commit 65b9d43
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion colcon_cmake/task/cmake/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2016-2018 Dirk Thomas
# Licensed under the Apache License, Version 2.0

import asyncio
import os
import re
import shutil
Expand Down Expand Up @@ -54,7 +55,7 @@ async def has_target(path, target):
"""
generator = get_generator(path)
if 'Unix Makefiles' in generator:
return target in await get_makefile_targets(path)
return await get_makefile_has_targets(path, target)
if 'Ninja' in generator:
return target in get_ninja_targets(path)
if 'Visual Studio' in generator:
Expand All @@ -65,6 +66,21 @@ async def has_target(path, target):
"'has_target' not implemented for CMake generator '{generator}'" \
.format_map(locals())

async def get_makefile_has_targets(path, target):
"""
Check if a `Makefile` has a specific target.
:param str path: The path of the directory contain the Makefile
:param str target: The name of the target
:rtype: bool
"""
proc= await asyncio.create_subprocess_exec(CMAKE_EXECUTABLE, '--build', path, '--', '-q', target,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.PIPE)

stdout, stderr = await proc.communicate()

return stderr is None or not "No rule to make target" in stderr.decode()

async def get_makefile_targets(path):
"""
Expand Down

0 comments on commit 65b9d43

Please sign in to comment.