Skip to content
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

zig-msvc target error: 'unistd.h' file not found #635

Closed
kassane opened this issue Apr 19, 2024 · 18 comments
Closed

zig-msvc target error: 'unistd.h' file not found #635

kassane opened this issue Apr 19, 2024 · 18 comments

Comments

@kassane
Copy link
Contributor

kassane commented Apr 19, 2024

+- run middletest
   +- zig build-exe middletest Debug native-native-msvc
      +- zig build-lib gc Debug native-native-msvc 1 errors
D:\a\bdwgc\bdwgc\include/private/gcconfig.h:2588:11: error: 'unistd.h' file not found
D:\a\bdwgc\bdwgc\extra/gc.c:40:10: note: in file included from D:\a\bdwgc\bdwgc\extra/gc.c:40:
D:\a\bdwgc\bdwgc\extra/../backgraph.c:15:10: note: in file included from D:\a\bdwgc\bdwgc\extra/../backgraph.c:15:
D:\a\bdwgc\bdwgc\include/private/dbg_mlc.h:28:10: note: in file included from D:\a\bdwgc\bdwgc\include/private/dbg_mlc.h:28:

References

@ivmai
Copy link
Owner

ivmai commented Apr 19, 2024

Is _MSC_VER macro predefined by the compiler? I guess no.

@ivmai
Copy link
Owner

ivmai commented Apr 19, 2024

If not predefined, then how could we detect msvc target (using some predefined macro)?

@kassane
Copy link
Contributor Author

kassane commented Apr 19, 2024

Is _MSC_VER macro predefined by the compiler? I guess no.

in my fork: master...kassane:bdwgc:zig-msvc

bdwgc/build.zig

Lines 469 to 470 in ff19442

if (lib.rootModuleTarget().abi == .msvc)
lib.defineCMacro("_MSC_VER", "");

 +- run subthreadcreatetest
   +- zig build-exe subthreadcreatetest Debug native-native-msvc
      +- zig build-lib gc Debug native-native-msvc 20 errors
<command line>:2:9: error: '_MSC_VER' macro redefined
<built-in>:407:2: note: in file included from <built-in>:407:
<built-in>:393:9: note: previous definition is here

@ivmai
Copy link
Owner

ivmai commented Apr 22, 2024

How to reproduce it?
I tried master with zig 0.12.0 (final) zig build test on Win64 host, it works OK.

@ivmai
Copy link
Owner

ivmai commented Apr 22, 2024

flags.append("-D HAVE_UNISTD_H") catch unreachable; - this should not be defined on Win32

@kassane
Copy link
Contributor Author

kassane commented Apr 22, 2024

flags.append("-D HAVE_UNISTD_H") catch unreachable; - this should not be defined on Win32

bdwgc/build.zig

Lines 398 to 399 in c77eee1

if (lib.rootModuleTarget().abi != .msvc)
flags.append("-D HAVE_UNISTD_H") catch unreachable;

new error

D:\a\bdwgc\bdwgc\extra/../gcj_mlc.c:88:31: error: 'getenv' is deprecated: This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
D:\a\bdwgc\bdwgc\extra/gc.c:43:10: note: in file included from D:\a\bdwgc\bdwgc\extra/gc.c:43:
D:\a\bdwgc\bdwgc\include/private/gc_priv.h:781:23: note: expanded from macro 'GETENV'
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt/stdlib.h:1183:20: note: 'getenv' has been explicitly marked deprecated here
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.39.33519\include/vcruntime.h:355:55: note: expanded from macro '_CRT_INSECURE_DEPRECATE'
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.39.33519\include/vcruntime.h:345:47: note: expanded from macro '_CRT_DEPRECATE_TEXT'

Try again

bdwgc/build.zig

Lines 398 to 401 in a97781a

if (lib.rootModuleTarget().abi != .msvc)
flags.append("-D HAVE_UNISTD_H") catch unreachable
else
flags.append("-D _CRT_SECURE_NO_WARNINGS") catch unreachable;

D:\a\bdwgc\bdwgc\extra/../mark.c:531:11: error: extension used
D:\a\bdwgc\bdwgc\extra/gc.c:56:10: note: in file included from D:\a\bdwgc\bdwgc\extra/gc.c:56:
D:\a\bdwgc\bdwgc\extra/../win32_threads.c:1387:7: error: extension used
D:\a\bdwgc\bdwgc\extra/gc.c:77:10: note: in file included from D:\a\bdwgc\bdwgc\extra/gc.c:77:
D:\a\bdwgc\bdwgc\extra/../win32_threads.c:1393:7: error: extension used
D:\a\bdwgc\bdwgc\extra/gc.c:77:10: note: in file included from D:\a\bdwgc\bdwgc\extra/gc.c:77:
error: the following command failed with 3 compilation errors:

latest try

bdwgc/build.zig

Lines 398 to 403 in 4cf78f3

if (lib.rootModuleTarget().abi != .msvc)
flags.append("-D HAVE_UNISTD_H") catch unreachable
else {
flags.append("-D _CRT_SECURE_NO_WARNINGS") catch unreachable;
flags.append("-D NO_SEH_AVAILABLE") catch unreachable;
}

error: lld-link: undefined symbol: __declspec(dllimport) MessageBoxA
    note: referenced by D:\a\bdwgc\bdwgc\misc.c:858
    note:               D:\a\bdwgc\bdwgc\zig-cache\o\18ec8cc31c1db3c26fcae6daa28609b4\gc.obj:(GC_win32_MessageBoxA)
error: the following command failed with 1 compilation errors:

Works in some configurations, see
CI: https://github.com/kassane/bdwgc/actions/runs/8783889751

@ivmai
Copy link
Owner

ivmai commented Apr 22, 2024

error: lld-link: undefined symbol: __declspec(dllimport) MessageBoxA

Add "-D DONT_USE_USER32_DLL" to flags

@ivmai
Copy link
Owner

ivmai commented Apr 22, 2024

Make a PR please.

@kassane
Copy link
Contributor Author

kassane commented Apr 22, 2024

Add "-D DONT_USE_USER32_DLL" to flags

Doesn't it apply to windows in general (gnu & msvc)?

bdwgc/build.zig

Lines 386 to 390 in df1a787

if (t.os.tag == .windows) {
// Do not require the clients to link with "user32" system library.
flags.append("-D DONT_USE_USER32_DLL") catch unreachable;
}
}

Make a PR please.

I do.

@ivmai
Copy link
Owner

ivmai commented Apr 22, 2024

Doesn't it apply to windows in general (gnu & msvc)?
bdwgc/build

Currently it is applied to static builds only.
You may try to define it for static ones as well, but it is better to specify dependency on user32 system library for static builds.

@kassane
Copy link
Contributor Author

kassane commented Apr 22, 2024

Currently it is applied to static builds only. You may try to define it for static ones as well, but it is better to specify dependency on user32 system library for static builds.

Got it.

@kassane
Copy link
Contributor Author

kassane commented Apr 23, 2024

I successfully tested using the first PR #636, with the exception of msvc-debug mode.
CI (releases x debug): https://github.com/kassane/bdwgc-d/actions/runs/8803927218

gc.lib(misc.obj) : error LNK2019: unresolved external symbol _CrtDbgReport referenced in function GC_write

@ivmai
Copy link
Owner

ivmai commented Apr 23, 2024

gc.lib(misc.obj) : error LNK2019: unresolved external symbol _CrtDbgReport referenced in function GC_write

This symbol is defined only in debug version of msvc libs. Please check which version of msvc libs are used.

@ivmai
Copy link
Owner

ivmai commented Apr 23, 2024

If not possible to instruct msvc to use the correct version of the libs, then there could be a workaround in misc.c (plus a define of NO_CRTDBGREPORT in build.zig):

#     if defined(_MSC_VER) && defined(_DEBUG) && !defined(NO_CRT)

->

#     if defined(_MSC_VER) && defined(_DEBUG) && !defined(NO_CRT) \
         && !defined(NO_CRTDBGREPORT)

@ivmai
Copy link
Owner

ivmai commented Apr 24, 2024

I'm processing your PR #636. I've to break it into several commits.

@ivmai
Copy link
Owner

ivmai commented Apr 24, 2024

What's preventing to turn on -Werror? cmake script uses /W4 /wd4127 /WX /wd4091 options.

ivmai added a commit that referenced this issue Apr 24, 2024
Issue #635 (bdwgc).

* misc.c [(MSWIN32 && !CONSOLE_LOG || MSWINCE) && _MSC_VER && _DEBUG
&& !NO_CRT && MSWINCE] (GC_write): Do not call MultiByteToWideChar()
and OutputDebugStringW() if NO_CRTDBGREPORT is defined.
* misc.c [(MSWIN32 && !CONSOLE_LOG || MSWINCE) && _MSC_VER && _DEBUG
&& !NO_CRT && !MSWINCE] (GC_write): Do not call _CrtDbgReport()
if NO_CRTDBGREPORT is defined.
@kassane
Copy link
Contributor Author

kassane commented Apr 24, 2024

Please check which version of msvc libs are used.

Windows (msvc/gnu) linked libs (LLD):
https://github.com/ziglang/zig/blob/refs%2Fheads%2Fmaster/src%2Flink%2FCoff%2Flld.zig#L425-L432

MSVC_libdir search: https://github.com/ziglang/zig/blob/f6f7a47aad7da251f1e4ee6f45b6de79ce8bbbd8/lib/std/zig/WindowsSdk.zig#L589 (std.zig.WindowsSdk)

What's preventing to turn on -Werror? cmake script uses /W4 /wd4127 /WX /wd4091 options.

zig cc, need clang-cl flags for MSVC!

Currently zig-toolchain, can't build llvm-libcxx + llvm-libunwind for MSVC. So, for this (specific) target, the flags -nostdlib++ and -nostdinc++ are used. However, zig-cc (build.zig called cc1 as opposed to c++) emits warnings about the non-use of flags, with -Werror these warnings become errors and stop the compilation.

previoud CI: https://github.com/kassane/bdwgc/actions/runs/8787776049/job/24113766036#step:5:5

  zig build -Dtarget=native-native-msvc -DBUILD_SHARED_LIBS=false -Ddisable_handle_fork=false -Denable_gc_assertions=true -Denable_gc_debug=false -Denable_large_config=false -Denable_munmap=false -Denable_parallel_mark=false -Denable_redirect_malloc=false -Denable_rwlock=false -Denable_thread_local_alloc=false -Denable_threads=true -Denable_cplusplus=true -Denable_werror test
  shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
test
+- run cpptest
   +- zig build-exe cpptest Debug native-native-msvc
      +- zig build-lib gccpp Debug native-native-msvc 2 errors
:1:1: error: argument unused during compilation: '-nostdinc++'
:1:1: error: argument unused during compilation: '-nostdinc++'

Reference

@kassane
Copy link
Contributor Author

kassane commented Apr 24, 2024

I'm processing your PR #636. I've to break it into several commits.

rebased.

This symbol is defined only in debug version of msvc libs. Please check which version of msvc libs are used.

After rebase, I re-examined this problem and discovered that it was currently caused by the D (ldc2 - msvc-compat only) compiler. Missing msvcrtd.lib for ldc2 + clang-cl (default by windows-runner), but zig add on libgc.
CI: https://github.com/kassane/bdwgc-d/actions/runs/8820113813

@ivmai ivmai closed this as completed Apr 27, 2024
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

Successfully merging a pull request may close this issue.

2 participants