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

provide an MSVC-compatible command line interface, similar to zig cc but with MSVC-flavored flags #4785

Closed
andrewrk opened this issue Mar 22, 2020 · 16 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. zig cc Zig as a drop-in C compiler feature
Milestone

Comments

@andrewrk
Copy link
Member

clang already has a "cl mode", and it should be pretty minimal efforts to also support zig cl, given the framework we already have in place for zig cc. Thanks to ihnorton for the idea.

@andrewrk andrewrk added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. zig cc Zig as a drop-in C compiler feature labels Mar 22, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Mar 22, 2020
@adontz
Copy link

adontz commented Mar 24, 2020

https://clang.llvm.org/docs/MSVCCompatibility.html

I think "drop in replacement" means no compatibility issues, while what CLang tries to achieve is ability to compile Windows SDK headers. I think "ability to compile latest Windows SDK headers" is what the real goal should be, not "drop in replacement", this is simply not a realistic task. MSVC has a lot of language extensions.

@jagt
Copy link
Sponsor Contributor

jagt commented Apr 21, 2020

Hi!

Does this mean that zig c++ is broken on windows for now?
I've tried a bit that zig cc works and zig c++ seems to error out on Windows with VS2017.

@andrewrk
Copy link
Member Author

On Windows you might want to use -target native-native-gnu so that Zig is not required to integrate with MSVC.

@jagt
Copy link
Sponsor Contributor

jagt commented Apr 22, 2020

Thanks for the reply.

-target native-native-gnu indeed works. But seems it can compile fine but fails to link a executable.
See example below:

zig cc -o hello_c.exe hello_c.c # works
zig c++ -target native-native-gnu -c foo.cc # works
zig -target native-native-gnu c++ -o hello_cpp.exe hello_cpp.cc  # process won't terminate

I'm runnning zig 0.6.0 on windows.
I think I'll wait a bit for zig cl to work.

@andrewrk
Copy link
Member Author

andrewrk commented Apr 22, 2020

process won't terminate

It will. Give it a minute while it builds and caches mingw-w64. The terminal progress indicator isn't implemented for Windows terminals yet. It only has to do this the first time.

@jagt
Copy link
Sponsor Contributor

jagt commented Apr 22, 2020

Yes it indeed work. Might need to print something atleast to stderr so it's less confusing.
I'll play with it a bit. Thanks a lot for your good work!

@andrewrk
Copy link
Member Author

There is a whole progress bar system in place, but it does not yet know how to display on windows terminals. Contribution welcome!

self.terminal = if (stderr.supportsAnsiEscapeCodes()) stderr else null;

@andrewrk
Copy link
Member Author

@LemonBoy implemented progress for Windows terminals just now in #5133, merged in 173a143.

@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 27, 2020
@GavinRay97
Copy link

Is there still interest in this?

I'm unfamiliar with Zig source (and C/C++ in general), but if Clang is in-tree, maybe this could be used?

llvm::opt::DerivedArgList* MSVCToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
                             							StringRef BoundArch,
                             							Action::OffloadKind OFK) {}

https://github.com/llvm/llvm-project/blob/fcf6800506862586e2d409aaa03a1cff818edfcc/clang/lib/Driver/ToolChains/MSVC.cpp#L1490-L1539

If I'm wrong, I think it could still be done with just a mapping/lookup table function (though more tedious and fragile).

I feel a lot of folks would benefit from this. It's not always the most intuitive process to translate MSVC cl.exe flags to clang.exe flags.

Have learned that you can pass -### to clang-cl.exe to print execution commands to stdout, like so:

λ  clang-cl /c /EHa /EHc -### a.c
clang version 12.0.0
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
 (in-process)
 "C:\\Program Files\\LLVM\\bin\\clang-cl.exe" "-cc1" "-triple" "x86_64-pc-windows-msvc19.28.29913" "-emit-obj" "-mrelax-all" "-mincremental-linker-compatible" "--mrelax-relocations" "-disable-free" "-main-file-name" "a.c" "-mrelocation-model" "pic" "-pic-level" "2" "-mframe-pointer=none" "-relaxed-aliasing" "-fmath-errno" "-fno-rounding-math" "-mconstructor-aliases" "-munwind-tables" "-target-cpu" "x86-64" "-mllvm" "-x86-asm-syntax=intel" "-tune-cpu" "generic" "-D_MT" "-flto-visibility-public-std" "--dependent-lib=libcmt" "--dependent-lib=oldnames" "-stack-protector" "2" "-fexceptions" "-fms-volatile" "-fdiagnostics-format" "msvc" "-resource-dir" "C:\\Program Files\\LLVM\\lib\\clang\\12.0.0" "-internal-isystem" "C:\\Program Files\\LLVM\\lib\\clang\\12.0.0\\include" "-internal-isystem" "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.28.29910\\include" "-internal-isystem" "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.28.29910\\atlmfc\\include" "-internal-isystem" "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.19041.0\\ucrt" "-internal-isystem" "C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.19041.0\\shared" "-internal-isystem" "C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.19041.0\\um" "-internal-isystem" "C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.19041.0\\winrt" "-fdebug-compilation-dir" "C:\\Users\\rayga" "-ferror-limit" "19" "-fmessage-length=120" "-fno-use-cxa-atexit" "-fms-extensions" "-fms-compatibility" "-fms-compatibility-version=19.28.29913" "-fdelayed-template-parsing" "-fcolor-diagnostics" "-faddrsig" "-o" "a.obj" "-x" "c" "a.c"

So maybe it's possible to script/automate the generation of the conversion by writing an array of all possible flags (with dummy input where needed), and iterating them one-by-one and taking only the diff between each invocation to get the converted output of the flag.

Then you'd have a mapping from clang-cl -> clang flag generated somewhat automatically.


Alternatively, something like this could be used as a duct-tape hack maybe:

#!/bin/sh
$clang_flags = $(clang-cl.exe "-###" "$@") # Do some processing on the text too so it's only the clang flags
zig cc $clang_flags

If there's still interest in this or a good way of implementing it, maybe I could try to take a crack it.
Or if it's easy, someone familiar with Zig, the codebase, and the dev workflow could crank it out.

=)

@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0, 0.11.0 Nov 23, 2021
@will-ca
Copy link

will-ca commented Oct 28, 2022

Wouldn't making this work out-of-the-box require the user to also have the MSVCRT and SDK, which are both huge and not freely licensed/forbidden from redistribution?

E.G. I have followed this as an example and tool for using Clang/CL:

https://jake-shadle.github.io/xwin/

https://github.com/Jake-Shadle/xwin

@safasofuoglu
Copy link

possibly related:
https://internals.rust-lang.org/t/pre-rfc-remove-rusts-dependency-on-visual-studio-in-4-complex-steps/16708
microsoft/STL#2046 (comment)

@kassane
Copy link
Contributor

kassane commented Jun 22, 2023

Wouldn't making this work out-of-the-box require the user to also have the MSVCRT and SDK, which are both huge and not freely licensed/forbidden from redistribution?

E.G. I have followed this as an example and tool for using Clang/CL:

https://jake-shadle.github.io/xwin/

https://github.com/Jake-Shadle/xwin

Zig with Xwin test: https://github.com/kassane/xwin-zig-test

@ghost
Copy link

ghost commented Jun 23, 2023

note that Xwin is not ideal, as it does not handle (and owner refuses to handle) important parts of MSVC such as cl.exe. I hope that any Zig foray into this area will have that as an option.

@andrewrk
Copy link
Member Author

andrewrk commented Jun 24, 2023

I am convinced this is not worth it.

Note that this proposal is about the CLI interface of Zig. It has already been possible for a long time to use Zig as a replacement for MSVC.

@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Jun 24, 2023
@andrewrk andrewrk modified the milestones: 0.14.0, 0.11.0 Jun 24, 2023
@andrewrk andrewrk changed the title using zig as a drop in replacement for msvc provide an MSVC-compatible command line interface, similar to zig cc but with MSVC-flavored flags Jun 24, 2023
@ghost
Copy link

ghost commented Jun 24, 2023

It has already been possible for a long time to use Zig as a replacement for MSVC.

you mean in the context of headers and libraries. in the context of executables such as cl.exe, Zig currently cannot replace that. If I am wrong about that fact, I will be VERY happy to hear it.

@andrewrk
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. zig cc Zig as a drop-in C compiler feature
Projects
None yet
Development

No branches or pull requests

7 participants