Skip to content

Commit

Permalink
Add a StaticLibrary() helper to Protoc.
Browse files Browse the repository at this point in the history
This also fixes a bug where protoc couldn't be invoked multiple times on
the same inputs, because dependency files weren't shared outputs.

It's a little gross to use shared outputs here, but the alternative is
putting every .d file in its own subdir, which is even grosser.

Usage:

    cxx = builder.DetectCxx()
    protoc = builder.DetectProtoc()
    out = protoc.StaticLibrary('protos', builder, cxx, ['blah.proto'])

    other_binary = builder.Library('mylibrary')
    other_binary.compiler.sourcedeps += out.headers
    other_binary.compiler.linkflags += [out.lib.binary]
    ...
  • Loading branch information
dvander committed Nov 9, 2023
1 parent 61c1e99 commit 6a319f7
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions ambuild2/frontend/v2_2/tools/protoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,18 @@ def AddSource(self, source_path):
gen_info['sources'] += gen_source_names
gen_info['headers'] += gen_header_names

gen_file_list += ['{}.d'.format(source_name)]
dep_file = '{}.d'.format(source_name)
argv = self.argv + [
'--dependency_out={}'.format(gen_file_list[-1]),
'--dependency_out={}'.format(dep_file),
source_path,
]

gen_entries = self.builder.AddCommand(inputs = [source_path],
argv = argv,
outputs = gen_file_list,
dep_type = 'md',
dep_file = gen_file_list[-1])
dep_file = dep_file,
shared_outputs = [dep_file])

# Translate the list of generated output entries.
cursor = 0
Expand All @@ -112,7 +113,12 @@ def AddSource(self, source_path):
self.gen_map[language].setdefault('headers', []).extend(gen_headers)

# Should be one entry remaining, for the .d file.
assert (cursor == len(gen_entries) - 1)
assert (cursor == len(gen_entries))

class ProtocCppNode(object):
def __init__(self, lib, headers):
self.lib = lib
self.headers = headers

class Protoc(object):
def __init__(self, path, name, version):
Expand All @@ -131,7 +137,7 @@ def clone(self):

# Each output entry is either a language, or a tuple of (language, folder_entry).
def Generate(self, builder, sources, outputs, includes = []):
runner = ProtocRunner(self, builder, includes)
runner = ProtocRunner(self, builder, includes + ['.'])

if not outputs:
raise Exception('No output languages were specified')
Expand All @@ -152,6 +158,16 @@ def Generate(self, builder, sources, outputs, includes = []):

return runner.gen_map

def StaticLibrary(self, name, builder, cxx, sources, includes = []):
gen_map = self.Generate(builder, sources, ['cpp'], includes)
binary = cxx.StaticLibrary(name)
binary.compiler.sourcedeps += gen_map['cpp']['sources']
binary.compiler.sourcedeps += gen_map['cpp']['headers']
for source in gen_map['cpp']['sources']:
binary.sources += [os.path.join(builder.buildPath, source.path)]
out = builder.Add(binary)
return ProtocCppNode(out, gen_map['cpp']['headers'])

def DetectProtoc(**kwargs):
path = kwargs.pop('path', None)
if len(kwargs):
Expand Down

0 comments on commit 6a319f7

Please sign in to comment.