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

Add intrinsic property function IsOSPlatform(string os) #568

Closed
wants to merge 1 commit into from

Conversation

radical
Copy link
Member

@radical radical commented Apr 11, 2016

Usage:
$([MSBuild]::IsOSPlatform(Windows))

Based on @dsplaisted's suggestion in issue #539 .

Usage:
    $([MSBuild]::IsOSPlatform(Windows))

Based on @dsplaisted's suggestion in issue dotnet#539 .
case "windows": return NativeMethodsShared.IsWindows;
case "osx": return NativeMethodsShared.IsOSX;
case "unix": return NativeMethodsShared.IsUnixLike;
default: return false;
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 this makes sense for full-framework and Mono builds, since the CoreFX method doesn't exist there. When it does, though, should this be a passthrough to RuntimeInformation.IsOSPlatform(OSPlatform.Create(os))?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, but then we need to ensure that they support the same "set".

Looking at this and this, the name depends on PAL_UNIX_NAME set in the configure.cmake using CMAKE_SYSTEM_NAME.

This and this suggest that it uses uname -s, when available, for this. So, we could use uname -s too on non-windows systems. Darwin->OSX being the special case.

And maybe add a Unix too (== IsUnixLike) ?

@jonpryor
Copy link
Member

Since you're adding an intrinsic, could we expand the scope a bit. :-)

There are many "Unix" or Unix-like platforms, e.g. Linux, AIX, FreeBSD, OpenBSD, NetBSD, Solaris... -- which all have their distinct features. Additionally, various versions of Windows also have their own "quirks" and features.

Aside, consider the uname(1) program, which allows determine the kernel name (e.g. "Darwin", "Linux"), version number, hardware architecture, and more.

What might be handy is if the IsOSPlatform() intrinsic could act as a query mechanism for things OS-related. This query could check for everything uname provides, with Windows equivalents, for example:

$([MSBuild]::IsOSPlatform(Unix{Name=Darwin, Machine=x86_64}))

would return True when running on x86_64 OS X and False elsewhere.

@AndyGerlicher
Copy link
Contributor

@jonpryor We discussed this today and while I don't disagree it could be helpful it's something the Framework should provide and we want this to pass through and support only what the Framework returns. In the case of full Framework or Mono where RuntimeInformation.IsOSPlatform doesn't exist yet it should just emulate it until it does. Definitely don't want to start our own, new, moniker for this if we can help it. So for Net Core we'll want the code for this to be:

return RuntimeInformation.IsOSPlatform(OSPlatform.Create(os))

Make sense? Do you think there will be cases where extra information would be important to the build? Generally it seems to be test for Windows or not Windows.

@jonpryor
Copy link
Member

Do you think there will be cases where extra information would be important to the build?

I have my own possible purposes for such a thing. :-)

In particular, native libraries. Visual Studio will generate MSBuild-compatible .vcxproj files with @(ClCompile) items. (Xamarin Studio, meanwhile, generates .cproj files with @(Compile) items.)

I would like to be able to use .vcxproj files/semantics to build shared libraries on Unix, hacking things up as necessary, and I can imagine needing to make certain properties conditional on the executing operating system, e.g. if on Unix set the compiler to use clang instead of CL.EXE.

This could presumably be handled with RuntimeInformation.IsOSPlatform().

Where things get potentially "wonky" is dealing with OS differences. Not all Unixes are alike (nor are all Windows versions...), so I can imagine that being able to query OS name/version/architecture/etc. would allow default-setting properties/items/etc. so that things can more sanely "Just Work" without needing to explicitly specify configurations or override properties.

Aside: Where does the @(ClCompile) and related logic live? Will MSBuild be able to handle .vcxproj files in a cross-platform manner?

Clear as mud? Or a reason to rethink this my fundamental brain damage in the first place? :-)

@jonpryor
Copy link
Member

I have my own possible purposes for such a thing. :-)

...and now that we've open-sourced some things, I can provide pointers. :-)

In the absence of a "useful" IsOSPlatform(), I've hacked my own $(HostOS) property:

<HostOS Condition=" '$(HostOS)' == '' And '$(OS)' == 'Windows_NT' ">Windows</HostOS>
<HostOS Condition=" '$(HostOS)' == '' And '$(OS)' == 'Unix' And Exists ('/Applications') ">Darwin</HostOS>
<HostOS Condition=" '$(HostOS)' == '' And '$(OS)' == 'Unix' ">Linux</HostOS>

This works, but is kinda ugly in that I need to use a filesystem path to distinguish between OS X and Linux (/Applications).

What do I do with that information? I choose which Android SDK files to install:

<CreateItem
      Include="@(AndroidSdkItem)"
      Condition=" '%(HostOS)' == '$(HostOS)' Or '%(HostOS)' == '' ">
    <Output TaskParameter="Include" ItemName="_PlatformAndroidSdkItem"/>
</CreateItem>

I change compiler build options based on the host OS

<_MonoRuntime Include="host-Darwin" Condition=" '$(HostOS)' == 'Darwin' ">
  ...
</_MonoRuntime>
<_MonoRuntime Include="host-Linux" Condition=" '$(HostOS)' == 'Linux' ">
  ...
</_MonoRuntime>

...and I change .zip extraction techniques based on Host OS, because System.IO.Compression.ZipFile doesn't respect +x on OS X and Linux, so the command-line unzip program must be used.

I'm sure there will be more.

@AndyGerlicher
Copy link
Contributor

We'd like to hold off on this now. Please reopen if you feel strongly.

Ideally MSBuild doesn't have a list of known operating systems of flavors of those and passes through to the framework. Since this isn't really possible with Mono (since it doesn't exist in full framework yet), there doesn't seem to be a good alternative. I'm not sure that there's a significant ask for it right now either, and an alternative on .NET Core (the method is in the white-list) already exists.

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.

5 participants