Summary question: How do you write a Premake command that can take multiple inputs and generate multiple outputs?
I am trying to use a Premake custom build command for a program which generates code that takes multiple inputs and creates multiple outputs. However, I can't figure out the Premake to take multiple inputs.
The Protocol Buffer compiler protoc is one such program that operates in this way that I will use for demonstration. This script works for taking all of the .proto files and compiling them individually:
workspace "CustomBuildCommandTesting"
configurations { "Debug", "Release" }
location "build"
project "custom-buildcommand-test"
kind "ConsoleApp"
language "C++"
targetdir "build/%{cfg.buildcfg}/bin"
files { "src/**.cpp", "src/**.proto" }
includedirs { "%{cfg.objdir}/generated" }
-- Using protoc for demo, but the question applies to anything with multiple inputs
filter 'files:**.proto'
buildcommands {
'mkdir -p "%{cfg.objdir}/generated"',
'protoc --proto_path=../src --cpp_out="%{cfg.objdir}/generated" "%{file.relpath}"',
}
buildoutputs {
'%{cfg.objdir}/generated/%{file.basename}.pb.h',
'%{cfg.objdir}/generated/%{file.basename}.pb.cc',
}
This creates rules in Make that look like this (cleaned a bit for clarity):
obj/Debug/generated/foo.pb.h: ../src/foo.proto
mkdir -p "obj/Release/generated"
protoc --proto_path=../src --cpp_out="obj/Release/generated" "../src/foo.proto"
obj/Debug/generated/bar.pb.h: ../src/bar.proto
mkdir -p "obj/Debug/generated"
protoc --proto_path=../src --cpp_out="obj/Debug/generated" "../src/bar.proto"
As an aside, there is no mention of the .pb.cc files that get generated anywhere, which is not ideal. It seems like that should get picked up from buildoutputs, but I must be missing how to make that happen.
The real problem here is this isn't the way you're supposed to use protoc. For protoc, you want to use all of the inputs to generate all of the outputs in one pass. A more-correct rule in Make would look like this:
obj/Debug/generated/bar.pb.h \
obj/Debug/generated/bar.pb.cc \
obj/Debug/generated/foo.pb.h \
obj/Debug/generated/foo.pb.cc \
: \
../src/bar.proto \
../src/foo.proto
mkdir -p "obj/Debug/generated"
protoc --proto_path=../src --cpp_out="obj/Debug/generated" $^
The core question: How do you write a Premake command that can take multiple inputs and generate multiple outputs?
For completeness, here's the full demo source tree.
.
├── premake5.lua
└── src
├── bar.proto
├── foo.proto
└── main.cpp
src/foo.proto:
syntax = "proto3";
message Foo {
uint64 value = 1;
}
src/bar.proto:
syntax = "proto3";
import "foo.proto";
message Bar {
Foo foo = 1;
}
src/main.cpp:
#include "bar.pb.h"
int main()
{
}
As of 2023-11-15, this seems to be something Premake does not really do elegantly, but I opened issue 2160 so this might change in the future. For now, here's a workaround.
The workaround here is to build up the list of inputs and outputs manually, then pass them to
buildinputsandbuildoutputsby hand. The trick when setting thefilteris to just pick one of the.protoinput files; it does not matter which one, since they're all inbuildinputsanyway.This all works. In Make, there is a
foo.pb.hrule that looks like this:Which is linked to the other generated files by a rule without a recipe:
If a non-generated file needs
bar.pb.h(asmain.cppdoes here), the Makefile kind-of knows that it needsfoo.pb.hto get it and picks it up properly as a side-effect. The "correct" solution is grouped targets, but this one works Good Enough.