Skip to content

#if defined(_MSC_VER) is not sufficient to detect "building for Windows" #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
linusg opened this issue Dec 9, 2023 · 6 comments
Closed

Comments

@linusg
Copy link
Contributor

linusg commented Dec 9, 2023

This breaks when using the Zig toolchain to (cross-)compile for Windows, for instance.

/home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/cutils.c:660:7: error: call to undeclared function 'clock_gettime'; ISO C99 and later do not support implicit function declarations
  if (clock_gettime(CLOCK_MONOTONIC, &t))
      ^
/home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/cutils.c:660:21: error: use of undeclared identifier 'CLOCK_MONOTONIC'
  if (clock_gettime(CLOCK_MONOTONIC, &t))
                    ^
@linusg
Copy link
Contributor Author

linusg commented Dec 9, 2023

Patching in a #define _MSC_VER reveals a few more issue that were not present in the original quickjs:

/nix/store/l957vh49c3mqn19h4b3mqa33lim69hmb-zig-0.12.0-dev.1800+559e216f3/lib/include/inttypes.h:18:2: error: MSVC does not have inttypes.h prior to Visual Studio 2013
#error MSVC does not have inttypes.h prior to Visual Studio 2013
 ^
/home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/libunicode.c:30:10: note: in file included from /home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/libunicode.c:30:
#include "cutils.h"
         ^
/home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/cutils.h:29:10: note: in file included from /home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/cutils.h:29:
#include <inttypes.h>
         ^
/nix/store/l957vh49c3mqn19h4b3mqa33lim69hmb-zig-0.12.0-dev.1800+559e216f3/lib/libc/include/any-windows-any/winsock2.h:15:2: error: Please include winsock2.h before windows.h
#warning Please include winsock2.h before windows.h
 ^
/home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/libunicode.c:30:10: note: in file included from /home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/libunicode.c:30:
#include "cutils.h"
         ^
/home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/cutils.h:36:10: note: in file included from /home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/cutils.h:36:
#include <winsock2.h>
         ^
/home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/cutils.h:38:9: error: 'alloca' macro redefined
#define alloca _alloca
        ^
/home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/libunicode.c:30:10: note: in file included from /home/linus/.cache/zig/p/1220548d4c0c4895ae0b0d3e8e4c66bae071d069f8405f8e3812b6c122e3fe0c7fe4/libunicode.c:30:
#include "cutils.h"
         ^
/nix/store/l957vh49c3mqn19h4b3mqa33lim69hmb-zig-0.12.0-dev.1800+559e216f3/lib/libc/include/any-windows-any/malloc.h:159:9: note: previous definition is here
#define alloca(x) __builtin_alloca((x))
        ^

@saghul
Copy link
Contributor

saghul commented Dec 9, 2023

That define is not for detecting windows but the compiler. The mingw tool chain for example is also windows, but it does provide clock_gettime.

What does the Zig compiler target?

@linusg
Copy link
Contributor Author

linusg commented Dec 9, 2023

ah, I see - Zig targets mingw-w64 by default (or when using a triple like x86_64-windows-gnu, ziglang/zig#8878). It appears the actual availability of clock_gettime depends on a couple of conditions, which are probably not being met in this case?

https://github.com/ziglang/zig/blob/69195d0cd43468f213332c5792df04c941f8313d/lib/libc/include/any-windows-any/time.h#L318

@saghul
Copy link
Contributor

saghul commented Dec 9, 2023

We do define _GNU_SOURCE which I think should do it?

@linusg
Copy link
Contributor Author

linusg commented Dec 9, 2023

After further investigation this seems to be a shortcoming of the Zig toolchain itself, which ships a stub header for MinGW's pthread_time.h, hence the missing definitions:

https://github.com/ziglang/zig/blob/master/lib/libc/include/any-windows-any/pthread_time.h

I worked around that using https://github.com/kassane/winpthreads-zigbuild. Sorry for the noise :)

@linusg linusg closed this as completed Dec 9, 2023
@saghul
Copy link
Contributor

saghul commented Dec 9, 2023

No worries, glad you sorted it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants