Revision 94ad5427ad1ecd9480e41418ccfff21169486524 authored by Steven Johnson on 13 March 2023, 18:16:04 UTC, committed by Steven Johnson on 13 March 2023, 18:16:04 UTC
- printer.h uses `uint64_t`, so it needs to include something that ensures that type is defined. `HalideRuntime.h` is probably the right choice (since it always transitively includes `runtime_internal.h` when compiling runtime.

- HalideBuffer.h should completely elide itself when `COMPILING_HALIDE_RUNTIME` is defined, for a subtle reason: some compilation environments require that all .h files can be compiled 'standalone' -- ie, they include all prerequisites and can be included in any order. As it turns out, HalideBuffer.h has a previously unnoticed glitch that is now being caught by this:
    - It includes both `<cstring>` and `HalideRuntime.h`
    - but when `COMPILING_HALIDE_RUNTIME` is defined, `HalideRuntime.h` includes `runtime_internal.h`, which assumes that no std headers are included
    - as it happens, `runtime_internal.h` defines `strstr()` and `strchr()` in a way that doesn't match the std headers (they both return non-const `char *`, perversely enough) and we get a compile error for mismatched function prototypes

I think the neatest solution here is to just skip the entire contents of `HalideBuffer.h` in this situation, since none of its contents should ever be used inside the Halide runtime. (I could work around this situation on the google side by loosening the 'must be able to compile on its own' requirement in this case, but IMHO it's a good check and one that is worthy of keeping.)

The fact that our function prototypes are a mismatch for the 'correct' ones could be debated; IMHO our 'wrong' definitions are safer that the std and should be kept.
1 parent 78097a7
Raw File
rungen_test.cpp
#include <iostream>

#include "HalideRuntime.h"
#include "RunGen.h"

#include "example.h"

using namespace Halide::RunGen;
using namespace Halide::Runtime;

void check(bool b, const char *msg = "Failure!") {
    if (!b) {
        std::cerr << msg << "\n";
        exit(1);
    }
}

extern "C" void halide_register_argv_and_metadata(
    int (*filter_argv_call)(void **),
    const struct halide_filter_metadata_t *filter_metadata,
    const char *const *extra_key_value_pairs) {

    check(filter_argv_call == example_argv);
    check(filter_metadata == example_metadata());
    check(extra_key_value_pairs == nullptr);
}

namespace {

std::ostream *capture_cout = nullptr;
std::ostream *capture_cerr = nullptr;

bool log_info = false;
bool log_warn = true;

void do_log_cout(const std::string &s) {
    *capture_cout << s;
}

void do_log_cerr(const std::string &s) {
    *capture_cerr << s;
}

void do_log_info(const std::string &s) {
    if (log_info) {
        do_log_cerr(s);
    }
}

void do_log_warn(const std::string &s) {
    if (log_warn) {
        do_log_cerr("Warning: " + s);
    }
}

void do_log_fail(const std::string &s) {
    do_log_cerr(s);
    abort();
}

}  // namespace

namespace Halide {
namespace RunGen {

Logger log() {
    return {do_log_cout, do_log_info, do_log_warn, do_log_fail};
}

}  // namespace RunGen
}  // namespace Halide

int main(int argc, char **argv) {
    RunGen r(example_argv, example_metadata());

    check(r.get_halide_argv_call() == example_argv);
    check(r.get_halide_metadata() == example_metadata());

    {
        std::ostringstream out, err;
        capture_cout = &out;
        capture_cerr = &err;

        r.describe();

        check(err.str() == "");

        const char *expected_out = R"DESC(Filter name: "example"
  Input "runtime_factor" is of type float32
  Output "output" is of type Buffer<int32> with 3 dimensions
)DESC";
        check(out.str() == expected_out);
    }

    // TODO: add more here; all this does is verify that we can instantiate correctly
    // and that 'describe' parses the metadata as expected.

    std::cout << "Success!\n";
    return 0;
}
back to top