<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/68017>68017</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [flang] MSVC runtime library selection
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          bradking
      </td>
    </tr>
</table>

<pre>
    For targets such as `x86_64-pc-windows-msvc`, LLVM/Flang generates
object files for the MSVC ABI and uses MSVC-compatible tooling.

Compilers that target the MSVC ABI need to select one of the MSVC
runtime libraries. They should define preprocessor macros identifying
the selected runtime library, and add `#pragma comment`-style annotations
to object files to pass `/defaultlib:...` flags to the linker without
build system intervention.  With LLVM/Flang the latter is needed even
for a trivial empty Fortran program:

```
>type foo.f90
program foo
end program
>flang-new foo.f90
LINK : error LNK2001: unresolved external symbol mainCRTStartup
>flang-new foo.f90 -Xlinker -defaultlib:msvcrt
(works)
```

MSVC toolsets provide the following runtime library variants:

* `libcmt`: Multi-threaded, optimized, linked statically.
  Defines `-D_MT`, links `/defaultlib:libcmt`.

* `msvcrt`: Multi-threaded, optimized, linked shared.
  Defines `-D_MT -D_DLL`, links `/defaultlib:msvcrt`.

* `libcmtd`: Multi-threaded, debug, linked statically.
  Defines `-D_MT -D_DEBUG`, links `/defaultlib:libcmtd`.

* `msvcrtd`: Multi-threaded, debug, linked shared.
  Defines `-D_MT -D_DEBUG -D_DLL`, links `/defaultlib:msvcrtd`.

LLVM/Flang should be updated as follows:

* Add command-line flags to select a MSVC runtime library variant.
* Provide predefined macros indicating the selected MSVC runtime library
  for the preprocessor.
* Generate `/defaultlib:...` flags in object files referencing the
  selected MSVC runtime library for the linker.

For reference, LLVM/Clang does all of these when targeting the MSVC ABI.

Additionally, LLVM/Flang's own runtime libraries (`Fortran*.lib`)
could be provided as variants corresponding to each MSVC runtime
library variant.  Currently the `Fortran*.lib` libraries included in
the LLVM/Flang distribution only use the `msvcrt` runtime library.
LLVM/Flang should then write `/defaultlib:...` references in object
files, to link its own `Fortran*.lib` runtime libraries, matching the
selected MSVC runtime library variant.


Cc: @DavidTruby  
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVtFu2zgQ_Br5ZSFBpmPHfvCDG9dF0aQ4XHO9eysocSXtlSIFcmXX9_UHSrJrJU7aAIYMEeJwODucpfSeSoO4jubvovl2IluurFtnTqrvZMpJZtVxvbMOWLoS2YNv8wqkh2iR_lguvi1u4iaPD2SUPfi49vs8WqSRuIP7-68PkdjttDQllGjQSUYfpdso3djsX8wZCtLooQjgFcLDl693sHn3EaRR0Hr03Uic27qRTJlGYGs1mTLpQfrnna0b0ug8cCV5YDnGM4gK2IJHHVa1BsEW5096GNcaphpBU-akI_QJPFZ4BF_ZVitQWJBBaBw2zubovXVQy9xZD6TQMBVHMmUPFYD7pVDBGPcYhAnbk0pBp9OscbKsJeS2rtFwtEhjz0eNII2xLJmsGTRjCyPZ2EIjve9hdgoL2WrWlEWzTZIk0SKFQsuy-y4w0mS-o4MDcWVb7iGzlrQCf_SMNZBhdPuwF2sSgL-Jq3ENOxTJjA7Id6KiAtyj6cFCGSWwoz1JDVg3fISddeykgcbZ0sk6mm0uSxeY97_-dfaejw1CYW1SrIbBYWYY7AfQqDPcaVoR-MUGD-O59x8_f4JotgF0zjq4__xJpOk0DLTGobd6Hzbwg9EZqcEf68xqqCWZuz8fv7B03DYvLwHxP4Ok8Uj7cALcoG8klgfrvvtIrK7vuHt2Pg3W9uF4Nc7uSWGndmG1tgcy5VMbwV46kob9U0XFJvhBU5bXwUthrw-tZoq5cigVqmBA2zDV9F__0m1CgQ9ey6XWx-F0AWw7z3cGi7ffHh6Hcx0mXHXdedXkCqVBljdRqqRD9SIdiLfftvf3v2J1Xvgaq56yepGWwqwt36ZSR-v9u78-_J5c6lW93sDsN8QKrN6k2jNyozwYojFDaBslQ9hJP1j2m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