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

[RFC] Detecting current OS #539

Closed
radical opened this issue Mar 22, 2016 · 19 comments
Closed

[RFC] Detecting current OS #539

radical opened this issue Mar 22, 2016 · 19 comments
Labels
help wanted Issues that the core team doesn't plan to work on, but would accept a PR for. Comment to claim. triaged

Comments

@radical
Copy link
Member

radical commented Mar 22, 2016

MSBuild sets the $(OS) property to OSX/Unix/Windows at runtime, which can be used in targets to determine the current OS. But xbuild has been using $(OS)==Unix on OSX and Linux, because of an old mono bug. And it continues to do so for compatibility reasons.

It makes sense for MSBuild to follow the same, since users would want to use it with existing targets. PR #538 reverts back to that behavior. So, for non-windows case, $(OS) property is effectively acting as a IsUnixLike. Considering it like that, $(OS) should probably have only two values now - Windows_NT and Unix.

Now, I think it might be useful to be able to differentiate between OSX and other specific unices. But maybe we should use some other (new) property for that? $(OSName) ? I am not sure how the CoreCLR case is affected by this, but AFAIU, it would be in the same boat.

Thoughts?

@akoeplinger
Copy link
Member

We just discussed this in Xamarin and we came to the conclusion that MSBuild changing $(OS) to OSX is too much of a breaking change for us, so when we adopt MSBuild we need to quirk this to return Unix again to prevent breaking existing code. This has the unfortunate side effect of creating a behavior difference based on the runtime.

@rainersigwald you said in #446 (comment) that you think it's reasonable to update conditions when moving to MSBuild, but is there a chance we can revisit this decision?

@jonpryor
Copy link
Member

For bad added context, there are 140 matches on github for .targets files which contain both OS and Unix (my possibly futile effort at capturing constructs similar to Condition="'$(OS)' == 'Unix'").

There are 1,653 matches when targeting .csproj files.

This isn't necessarily "a lot" -- or even accurate? -- but it does show that plenty of existing code files use $(OS) in one form or another, and adding a new value has the potential to break all of these existing files.

Changing $(OS) to be OSX instead of Unix is at minimum a possible compatibility break, and I believe shows that the "abstraction" isn't abstract enough. Users would likely be better served with an alternate mechanism that was more conducive to supporting new platforms without requiring auditing all existing use of e.g. $(OS), though I'm not sure what such an abstraction would resemble.

@AndyGerlicher
Copy link
Contributor

We discussed this a bit today in our team meeting. This is a tricky one because msbuild isn't exactly the same as xbuild. So if we're migrating users from xbuild to msbuild, while we want it to be as easy as possible we definitely don't want to implement "bugs" in msbuild to match xbuild behavior (to the largest extent possible). "Bug" is a stretch here, but it's definitely not behavior we would choose (at least in hindsight).

Few options we talked about:

  1. Keep $(OS) as is: Windows_NT, OSX, Unix
    1. OS override in the xbuild targets fork (if possible).
    2. Breaking change for xbuild -> msbuild.
  2. Match xbuild behavior for $(OS): Windows_NT, Unix
    1. Rename the current $(OS) to something else and keep OSX in that one.
    2. Add $(OSPlatform) property, match OSPlatform Linux, Windows, OS. Document to prefer $(OSPlatform) for .NET Core.
  3. Diverge the #if code between Mono and Core and keep both implementations.

Personally I think this should not be done in MSBuild but passed off to the Runtime. The closest thing I know of there would be to use OSPlatform. Unfortunately that a) doesn't include Unix at all, b) Windows_NT is changed to Windows, and c) sadly because of the way that's implemented MSBuild would have to change to add another value. What I don't really want to do is keep $(OS) as legacy for xbuild compat and add a 2nd way that's again custom to MSBuild (which is basically 2.i) for those who do want to distinguish.

Also for the statistics @jonpryor mentioned, most of those 1600+ hits are '$(OS)' != 'Windows_NT' so those would not break. Not that it makes it much better since there are exceptions, but it is much smaller than 1600.

Thoughts? We will discuss this again during our team triage tomorrow.

@dsplaisted
Copy link
Member

I would lean towards option 2, matching the xbuild behavior, as right now it has a lot more adoption than .NET Core MSBuild and it is better to keep this behavior consistent if possible.

The OSPlatform APIs don't allow you to get the name (by design), they only allow you to check to see if you are on a specified OS. So if we want to expose this functionality, I think we should do so as an intrinsic function instead of a property, ie IsOSPlatform('OSX').

@AndyGerlicher AndyGerlicher added the help wanted Issues that the core team doesn't plan to work on, but would accept a PR for. Comment to claim. label Mar 29, 2016
@AndyGerlicher
Copy link
Contributor

Team Triage: Marking as up-for-grabs. We would like to make $(OS) follow the xbuild behavior (Windows_NT, Unix) and implement what @dsplaisted mentioned (IsOSPlatform('OSX')).

@rainersigwald
Copy link
Member

We talked about this a bit more. It's unfortunate that IsOSPlatform is CoreCLR-only, so we might need to have a special case for running on the full framework that returns false for anything that's not Windows.

@akoeplinger
Copy link
Member

@rainersigwald afaik you can use the System.Runtime.InteropServices.RuntimeInformation package which has these new APIs on full framework too, but @ericstj can confirm.

radical pushed a commit to radical/msbuild that referenced this issue Apr 6, 2016
Based on the discussion in issue dotnet#539, $(OS) property will return
`Windows_NT` whenever running on windows and `Unix` for any other OS.

NativeMethodsShared.OSName is used to set that property, hence this is
being changed.
radical pushed a commit to radical/msbuild that referenced this issue Apr 11, 2016
Usage:
    $([MSBuild]::IsOSPlatform(Windows))

Based on @dsplaisted's suggestion in issue dotnet#539 .
radical pushed a commit to radical/msbuild that referenced this issue Apr 11, 2016
Usage:
    $([MSBuild]::IsOSPlatform(Windows))

Based on @dsplaisted's suggestion in issue dotnet#539 .
radical pushed a commit to radical/msbuild that referenced this issue Apr 11, 2016
Usage:
    $([MSBuild]::IsOSPlatform(Windows))

Based on @dsplaisted's suggestion in issue dotnet#539 .
radical pushed a commit to radical/msbuild that referenced this issue Apr 12, 2016
Usage:
    $([MSBuild]::IsOSPlatform(Windows))

Based on @dsplaisted's suggestion in issue dotnet#539 .
@am11
Copy link
Member

am11 commented Jan 19, 2017

Hello, just wanted to chime in with the detection task I have added today using RuntimeInformation in one of our projects:
https://github.com/am11/libsass-net/blob/68861f8/LibSass.NET/LibSass.NET.csproj#L75-L107

I saw @jeffkl's pull request #1486, which will probably allow us to get rid of the reflection calls and make code look decent, but is there a way to get the list of all dependencies that are in closure of S.R.IS.RuntimeInformation, that we would need to specify?

Regardless, it would be nice to get this information using RuntimeInformation type (which provides xplat OS description and architectures beyond X86/X64 ; ARM and ARM64 etc.) intrinsically from MSBuild. :)

Note that the project is targeting Framework 4.0 at the moment, while S.R.IS.RuntimeInformation v4.3.0 only provides support for Framework v4.5+, so I added the entry in packages.config, restored package and load it using Assembly.LoadFrom in our inline task and it worked! Is it some sort of a false-positive behavior?

(cc: @nschonni, @darrenkopp for visibility)

@jeffkl
Copy link
Contributor

jeffkl commented Jan 19, 2017

@am11 I made a sample based on your needs that doesn't use reflection: https://github.com/jeffkl/MSBuild-NetCore/tree/master/src/UsingTaskWithReference

I only needed to add a reference to System.Runtime to make it work.

@am11
Copy link
Member

am11 commented Jan 19, 2017

@jeffkl, thanks a lot! 🎉 Running into few issues with MSBuild v14.0, I have started a thread in your repo: jeffkl/MSBuild-NetCore#1. :)

@cdmihai cdmihai changed the title [RFC] Detecting current OS in targets [RFC] Detecting current OS Mar 14, 2017
@eerhardt
Copy link
Member

So I wanted to figure out "am I building on OSX or not?" and came across this issue. I want to know whether I am building on Windows, OSX, or Linux, and I want to know it statically (so I can set some other properties statically).

I've figured out that if I'm running MSBuild on .NET Core, I can call RuntimeInformation:

  <PropertyGroup>
    <IsWindows Condition="'$(OS)' == 'Windows_NT'">true</IsWindows>
  </PropertyGroup>
  
  <PropertyGroup Condition="'$(MSBuildRuntimeType)' == 'Core'">
    <IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
    <IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux>
  </PropertyGroup>

  <Target Name="PrintRID" BeforeTargets="Build">
    <Message Text="IsWindows $(IsWindows)" Importance="high" />
    <Message Text="IsOSX $(IsOSX)" Importance="high" />
    <Message Text="IsLinux $(IsLinux)" Importance="high" />
  </Target>

The above works in Visual Studio and when using dotnet build. However, there is another case I need to consider: what if my project is building using MSBuild running on Mono? @radical - do you know how I can tell statically whether I am on OSX or Linux in MSBuild on Mono?

@akoeplinger
Copy link
Member

@eerhardt the above should work on Mono MSBuild as well since (recent) Mono ships with System.Runtime.InteropServices.RuntimeInformation. You'll need to change the MSBuildRuntimeType check of course.

@eerhardt
Copy link
Member

Perfect! That's exactly what I wanted to hear.

And since RuntimeInformation is available on netstandard1.1, I think MSBuild could even support these checks on Full MSBuild, if they shipped with System.Runtime.InteropServices.RuntimeInformation as well. That would completely eliminate the need for the $(MSBuildRuntimeType) check all together.

@akoeplinger
Copy link
Member

akoeplinger commented Mar 28, 2017

Hmm, seems we're missing the FEATURE_RUNTIMEINFORMATION in dir.props in our Mono MSBuild build so it might not be available at runtime. @radical I don't see a reason why this shouldn't work, could you look into enabling it?

@cdmihai
Copy link
Contributor

cdmihai commented Mar 31, 2017

Enabled cross-platform in #1926

@cdmihai cdmihai closed this as completed Mar 31, 2017
@migueldeicaza
Copy link

The above trick does not work with regular .NET on VS 2017. What incarnation should someone use to detect Mac/Linux/Windows platforms?

@cdmihai
Copy link
Contributor

cdmihai commented May 23, 2017

@migueldeicaza It should be there with the next big Visual Studio update. Daily CLI builds should also have it. It brings the following new stuff: MicrosoftDocs/visualstudio-docs#111

@bhanu1989
Copy link

@jsauvexamarin

@0xced
Copy link

0xced commented Oct 4, 2019

For the record, you can use $([MSBuild]::IsOsPlatform()) since MSBuild 15.3, see #2468 (comment).

Sample usage:

<PropertyGroup>
  <OpenCommand Condition="$([MSBuild]::IsOSPlatform('Linux'))">xdg-open</OpenCommand>
  <OpenCommand Condition="$([MSBuild]::IsOSPlatform('OSX'))">open</OpenCommand>
  <OpenCommand Condition="$([MSBuild]::IsOSPlatform('Windows'))">explorer</OpenCommand>
</PropertyGroup>

jonpryor added a commit to jonpryor/java.interop that referenced this issue Mar 16, 2021
Context: dotnet/msbuild#539 (comment)

Sometimes the `%OS%` environment variable is set, which overrides the
normally expected `$(OS)` MSBuild property.

Instead of using `$(OS)`, use `$([MSBuild]::IsOSPlatform(name))`.
jonpryor added a commit to xamarin/java.interop that referenced this issue Mar 17, 2021
Context: 5c756b1
Context: 08ff4db

Context: https://discord.com/channels/732297728826277939/732297952676020316/819978878948868166
Context: microsoft/vstest#2571
Context: dotnet/msbuild#539 (comment)
Context: #816 (comment)

With the `cmake`-based build system knowledge behind 5c756b1 in
mind, update `src/java-interop` to *also* use a `cmake`-based build
system, then add support so it can build on Windows.

On Windows, this builds both e.g.
a 32-bit `bin\Debug\win-x86\java-interop.dll`
*and* 64-bit `bin\Debug\win-x64\java-interop.dll` libraries.

This allows the `Java.Interop-Tests.dll` unit tests to *also* be run
on Windows, under .NET Core [^0].

Note that this requires updating `java_interop_lib_load()` to also
include [`LOAD_LIBRARY_SEARCH_SYSTEM32`][0]:

  * `LOAD_LIBRARY_SEARCH_SYSTEM32`: `%windows%\system32` is searched
    for the DLL and its dependencies.

This specifically is needed so that `JVM.DLL` dependencies such as
`KERNEL32.DLL` can be resolved, allowing `JVM.DLL` to be loaded.

This allows .NET Core on Windows to run `Java.Interop-Tests.dll`!

	> dotnet test bin\TestDebug-netcoreapp3.1\Java.Interop-Tests.dll
	…
	A total of 1 test files matched the specified pattern.
	  Skipped Dispose_ClearsLocalReferences [11 ms]

	Passed!  - Failed:     0, Passed:   617, Skipped:     1, Total:   618, Duration: 2 s - Java.Interop-Tests.dll (netcoreapp3.1)

Install .NET 5.0.103, as that version is required in order for
`dotnet test Java.Interop-Tests.dll` to reliably use a 64-bit process.

~~~

Aside: So you want to create a file which is built into a
*subdirectory* of `$(OutputPath)`.  The "obvious thing" fails:

	<!-- Fails -->
	<ItemGroup>
	  <None Include="$(OutputPath)win-x64\java-interop.dll" />
	</ItemGroup>

This results in:

	error MSB3030: Could not copy the file…

This can be done by:

 1. Providing `%(Link)` item metadata which contains the relative
    directory and filename, *not* `$(OutputPath)`, and

 2. Using `\` *everywhere*.

Thus:

	<!-- Works -->
	<ItemGroup>
	  <None Include="$(OutputPath)win-x64\java-interop.dll">
	    <Link>win-x64\java-interop.dll</Link>
	  </None>
	</ItemGroup>

This works because:

 1. The [`<AppendTargetPath/> task`][1] uses `%(Link)` as an
    *override* when setting `%(TargetPath)`.

 2. The [`_CopyOutOfDateSourceItemsToOutputDirectory` target][2] uses
    `$(OutDir)%(TargetPath)` for `Copy.DestinationFiles`..

 3. The [`<Copy/>` task][3] *ignores errors* when `SourceFiles[i]` and
    `DestinationFiles[i]` are *identical*.  This is why `\` must be
    used, to ensure that the values are identical.

[^0]: …but not .NET Framework, as .NET Framework on CI is launching a
      32-bit process for the unit tests, while we need a 64-bit
      process to match the `JVM.DLL` we're loading.

[0]: https://docs.microsoft.com/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw
[1]: https://github.com/dotnet/msbuild/blob/17677364d656a98ce4c6eff73b49eddf24f0fd72/src/Tasks/AssignTargetPath.cs#L74
[2]: https://github.com/dotnet/msbuild/blob/17677364d656a98ce4c6eff73b49eddf24f0fd72/src/Tasks/Microsoft.Common.CurrentVersion.targets#L4965-L4977
[3]: https://github.com/dotnet/msbuild/blob/17677364d656a98ce4c6eff73b49eddf24f0fd72/src/Tasks/Copy.cs#L787-L796
@AR-May AR-May added the triaged label Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues that the core team doesn't plan to work on, but would accept a PR for. Comment to claim. triaged
Projects
None yet
Development

No branches or pull requests