5

I'm using protocol buffers for data serialization in my C++ application. I would like to add the invokation of the protoc code generator in my premake build script (thus ensure the up-to-date state of the generated classes and avoid the need to store generated source under version control).

Even their FAQ has a question and answer about this, but the answer is very incomplete for me. Having the ability to call any lua function is great, but where exactly do I put that call? I need to run the protoc compiler before building either the application or the unit tests.

Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180

2 Answers2

5

You can certainly call outside code from Premake scripts. But remember: Premake scripts are used to generate build files: Makefiles, C++ projects, etc. The Premake script is run before building the project.

If you want this preprocess to be run outside of the actual build files (and not by make, VC++, Code::Blocks, etc), then it's easy. Lua's os.execute will execute a command-line.

Premake scripts are still Lua scripts. All of the Premake commands are just Lua calls into functions that Premake defines. Premake executes the scripts, then uses the data from them to generate the build files. So all of your Lua code is run during the execution of the script. Where you put this command in your script is irrelevant; wherever it is, it will execute before your build files are generated.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Thanks for shedding light on it. I remembered the statement that the premake script is actually a lua script, but I didn't comprehend its consequences. – Tamás Szelei Aug 02 '11 at 22:10
  • In my own experience, this can be tricky, especially when you don't know ahead of time what OS the developer will be using your package in. Fortunately, the command you would have used for a _build time processing of a proto file_ is the same command you would use for _premake time processing of a proto file_. The tricky part is you aren't in the right context to use the `files` block, so you must use `os.matchfiles` to find you protos, build the command for each file before giving it to `os.execute`. And I strongly recommend using `pcall` so you can catch OS exceptions that might happen. – Jesse Chisholm Mar 19 '18 at 18:28
2

And if you want to run the protoc step during the build (from VC++, makefile, etc.) you can set up a prebuild command. See http://industriousone.com/prebuildcommands for more info and an example.

J. Perkins
  • 4,156
  • 2
  • 34
  • 48