nanomq / NanoNNG

The NNG submodule of NanoMQ
MIT License
37 stars 16 forks source link

nanonng 0.21.9 build issue with MinGW-w64 MSVCRT #922

Open brechtsanders opened 3 months ago

brechtsanders commented 3 months ago

When building nanonng version 0.21.9 for Windows with an MSVCRT MinGW-w64 toolchain the build fails in src/platform/windows/win_clock.c because timespec_get() is not defined (it does exist in UCRT MinGW-w64).

The solution is to modify nni_time_get() in src/platform/windows/win_clock.c to only use the timespec_get() implementation when building against UCRT (using #ifdef _UCRT) and otherwise fall back to another implementation, for example the same fallback based in gettimeofday() used in src/platform/posix/posix_clock.c.

On a side note, the line int rv; in nni_time_get() in src/platform/windows/win_clock.c is unused.

I was able to build when after modifying nni_time_get() in src/platform/windows/win_clock.c as follows:

int
nni_time_get(uint64_t *seconds, uint32_t *nanoseconds)
{
#ifdef _UCRT
    struct timespec ts;
    if (timespec_get(&ts, TIME_UTC) == TIME_UTC) {
        *seconds     = ts.tv_sec;
        *nanoseconds = ts.tv_nsec;
        return (0);
    }
    return (nni_win_error(GetLastError()));
#else
    int rv;
    struct timeval tv;
    if ((rv = gettimeofday(&tv, NULL)) == 0) {
        *seconds     = tv.tv_sec;
        *nanoseconds = tv.tv_usec * 1000;
        return (0);
    }
    return (nni_plat_errno(errno));
#endif
}

Note that I haven't tested it yet, and maybe there is a better way (with a more granular clock) to do this.

JaylinYu commented 2 months ago

thats why i labled it as a pre-release, you know. Will discuss with Garrett on it.

JaylinYu commented 2 months ago

related to https://github.com/nanomsg/nng/pull/1826 you can try port the code if you have your own fork... seems like garrett not being happy with MinGW

brechtsanders commented 2 months ago

Sorry to hear that.

I'm offering the solution on a plate here . If it doesn't get incorporated I guess will just keep patching my builds.

Just trying to help - as an open source developer.

JaylinYu commented 2 months ago

Sorry to hear that.

I'm offering the solution on a plate here . If it doesn't get incorporated I guess will just keep patching my builds.

Just trying to help - as an open source developer.

But we definitely could figure it out in NanoNNG fork.

JaylinYu commented 1 week ago

Tried with your way works. Tried without your way, also works.

I am using Win10 with MinGW

brechtsanders commented 1 week ago

@JaylinYu The issue is specifically with MSVCRT toolchain, if you're using UCRT the issue will not happen.