2

I am using generated files in my cmake project. I have a script that generates source code from some input, and the same script can also validate the input, if given a different command line argument, like this:

generate_code.py --generate

generate_code.py --validate

The --validate mode will print errors to stdout if the input is not valid, and I would like to have those errors visible. My current setup is to run with --validate in an execute_process and with --generate in an add_custom_command. This works, but doesn't print the user-friendly error messages from the --validate mode when the input changes, because the execute_process doesn't run again.

What would be the best way to surface these errors? If I could force execute_process to always run, that would work, or if I could capture stdout when --generate fails and surface that, I could modify the --generate option to print friendly error messages, and then display them. How can I do one or both of those?

Drew
  • 12,578
  • 11
  • 58
  • 98

1 Answers1

1

Commands in add_custom_target statements are re-run every time the build is executed as long as you pass the ALL argument. This may be what you are looking for.

Alternatively, you could make the inputs to generate_code.py be dependencies of the custom command (if they aren't already), and have the custom command execute generate_code.py --validate before generate_code.py --generate. That way it would get revalidated when it needs to be rebuilt, and not at any other time.

  • That seems promising. Only revalidating when I need to rebuild would be great. Do you know how I can capture the `stdout` of the custom command so I can display it when validation fails? – Drew Mar 31 '17 at 07:49
  • I think that you are getting confused between commands run at **CMake-generate** time and commands run at **build time**. You can capture the stdout of commands run by `execute_process` because CMake runs those commands when you run the CMake script. You cannot capture output from `add_custom_command` and `add_custom_target` because those commands are run by `make` at build time. If you really need to capture that output, what you _could_ do is write a CMake script that uses `execute_process` and invoke it by using `add_custom_command` to execute `cmake -P` – MultipleMonomials Apr 01 '17 at 01:31
  • However, what I'd recommend would be to have `generate_code.py --validate` return an error exit code when validation fails and print the results to the screen. This way, `make` would see the fault code and stop the build, and you would be able to see the error message that was printed. – MultipleMonomials Apr 01 '17 at 01:33