Skip to content

Improve support of clang #158

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

Merged
merged 3 commits into from
May 6, 2025
Merged

Conversation

MisterDA
Copy link
Contributor

@MisterDA MisterDA commented Mar 27, 2025

I'm trying to use clang everywhere. The MSYS2 CLANG64 environment uses UCRT, but we're missing bits to link with it right now. It is however possible to use clang with MSYS2 MINGW64 environment, or with Cygwin+mingw, or with the MSYS2 MSYS and Cygwin+cygwin1.dll environments (although in this last two cases clang packages are currently unmaintained).
It is also possible to use clang-cl instead of MSVC.

This PR makes sure that flexlink doesn't default to GCC or MSVC when clang is desired instead.

The first step is to retain the compilers used when building for searching library directories. This allows using alternative compilers, otherwise, FlexDLL would default to querying GCC, which may report different path list than clang.
The second step teaches FlexDLL to parse clang's output. clang.exe -print-search-dirs doesn't print an install field and its libraries path list is already in mixed-style (Windows path with forward slashes separated by semicolons). This happens when using clang in both MSYS2 MINGW64 or CLANG64 environments.

@MisterDA MisterDA force-pushed the clang-print-search-dirs branch 12 times, most recently from b4a33c4 to 52775f6 Compare April 2, 2025 18:23
Copy link
Member

@dra27 dra27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it's a slightly nitty set of review comments - overall, I unsurprisingly like being able to add support for clang!

I'm mechanically suggesting to capture the basename of the command - i.e. make it snapshot the fact its gcc, cl, clang, etc., but don't ever allow the full path to be embedded). Given that flexlink is essentially built as part of the compiler these days, I think it's OK to be doing it this way, rather than adding "never to be used" mechanisms for specifying the compiler on the command line (which I think we may have already talked about offline?)

Makefile Outdated
Comment on lines 248 to 250
echo 'let cygwin64 = "$(subst ",\",$(subst \,\\,$(CYG64CC)))"' >> $@
echo 'let mingw = "$(subst ",\",$(subst \,\\,$(MINCC)))"' >> $@
echo 'let mingw64 = "$(subst ",\",$(subst \,\\,$(MIN64CC)))"' >> $@
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in practice these should never end up with anything other than a compiler name, so perhaps the subst can be omitted?

Makefile Outdated
Comment on lines 246 to 247
echo 'let msvc = "$(subst ",\",$(subst \,\\,$(MSVCC)))"' >> $@
echo 'let msvc64 = "$(subst ",\",$(subst \,\\,$(MSVCC64)))"' >> $@
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the intention - but, for example, it's not hardened against a path with an apostrophe - which is a legal character in a directory name - while quoting double-quotes, which are not. However - I think $(notdir $(MSVCC)) gets us enough?

reloc.ml Outdated
let separator, run_through_cygpath =
if Sys.win32 then
if dir_exists_no_cygpath install then
if install = None (* clang *) || dir_exists_no_cygpath (Option.get install) then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if install = None (* clang *) || dir_exists_no_cygpath (Option.get install) then
if Option.fold ~none:true ~some:dir_exists_no_cygpath install then

(I think Option.get then isn't needed)

Makefile Outdated
@@ -239,6 +240,15 @@ flexdll_initer_gnat.o: flexdll_initer.c
flexdll_initer_mingw64.o: flexdll_initer.c
$(MIN64CC) $(GCC_FLAGS) -c -o $@ $<

build_config.ml: Makefile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, just update version.ml with the extra definitions (as things stand, you've not build_config.ml to the clean target but as version.ml already had the mingw ones there 🤷‍♀️)

@MisterDA MisterDA force-pushed the clang-print-search-dirs branch from 52775f6 to 75a6fcc Compare May 6, 2025 05:19
@MisterDA
Copy link
Contributor Author

MisterDA commented May 6, 2025

I've applied your suggestions, thanks!

MisterDA added 3 commits May 6, 2025 11:14
Record the compiler used during FlexDLL's build in a new module.

In the case of MINGW* and CYGWIN64 toolchains, this allows using
alternative compilers such as clang. Otherwise, FlexDLL would default
to querying GCC, which may report different path list than clang.

In the case of MSVC and MSVC64 toolchains, this allows using
alternative compilers such as clang-cl. Otherwise, FlexDLL would
default to using cl.exe, which may not be desired or even available.
`clang -print-search-dirs` doesn't print an `install` field and its
`libraries` path list is already in mixed-style (Windows path with
forward slashes separated by semicolons). This happens when using
clang in both MSYS2 MINGW64 or CLANG64 environments.
The `libraries` path list may also contain empty paths, strip them.
@MisterDA MisterDA force-pushed the clang-print-search-dirs branch from 75a6fcc to f37b539 Compare May 6, 2025 09:14
Copy link
Member

@dra27 dra27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@dra27 dra27 merged commit 649a3f3 into ocaml:master May 6, 2025
1 check passed
@MisterDA MisterDA deleted the clang-print-search-dirs branch May 6, 2025 12:39
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 this pull request may close these issues.

2 participants