cmEngine  0.1.1
A library for interpreting CMake code.
echoCommand.c

This example demonstrates a minimalistic way of defining a custom CMake command.

#include <cmEngine/cmArgument.h>
#include <cmEngine/cmEngine.h>
#include <cmEngine/cmFunction.h>
#include <cmEngine/cmFunctionContext.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
callEchoCommand(void* custom_data, struct cmFunctionContext* function_context)
{
(void)custom_data;
for (;;) {
const struct cmArgument* argument = cmPopFunctionArgument(function_context);
if (argument == NULL) {
/* end of arguments */
break;
}
/* Note that a null-terminated string
* is returned here. */
const char* content = cmGetArgumentContent(argument);
/* Even though the string that is returned
* is null terminated, we can get the size of
* the string (not including the null terminator)
* by calling this function. */
size_t content_size = cmGetArgumentSize(argument);
fwrite(content, 1, content_size, stdout);
fputc(' ', stdout);
}
}
static void
defineEchoCommand(struct cmEngine* engine)
{
struct cmFunction echo_function;
/* We only want to implement one callback
* function, so well zero the rest of the function. */
memset(&echo_function, 0, sizeof(echo_function));
echo_function.call = callEchoCommand;
cmDefineFunction(engine, "echo", &echo_function);
}
int
main(int argc, const char** argv)
{
struct cmEngine* engine = cmInit(NULL);
defineEchoCommand(engine);
for (int i = 1; i < argc; i++) {
int err = cmOpen(engine, argv[i]);
if (err != 0) {
return EXIT_FAILURE;
}
}
cmDone(engine);
return EXIT_SUCCESS;
}