Programming Languages on Power

 View Only

Cross building .NET 7 on x86 for IBM Power

By Sapana Khemkar posted Fri January 13, 2023 09:52 AM

  

If you have been struggling to generate a .NET 7 tarball from source code, this blog is for you. You may have wondered why you cannot directly compile it on Power to generate SDK? The reason is that the compiler code itself is written in C# . So, you have to first cross build all required repositories and generate the .NET SDK tarball for Power on an x86 machine. You can then install this tarball on a Power virtual machine (VM) and use .NET for development.

Follow this step-by-step guide to cross build .NET 7 on IBM Power.

Step 1. Download the required scripts and patches

Download the dotnet7 folder from: https://github.com/ppc64le/build-scripts/tree/master/d/dotnet7. This folder contains all required scripts and patches for cross building.

Step 2. Setup the x86 VM with Ubuntu 18.04

Install ppc64el architecture specific packages on an x86 machine for cross compiling .NET. Before installing the ppc64el packages, update the /etc/apt/sources.list file with following content:

  • Update the existing entries by specifying [arch=amd64]. Your entries should look as follows:
    deb [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic main restricted deb-src [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic main restricted​
  • Add a new entry in each section by specifying [arch=ppc64el] for Power architecture and add this link, http://ports.ubuntu.com/ubuntu-ports/, as shown below: 
    deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports/ bionic main restricted​
  • Ensure that the contents of the final /etc/apt/sources.list file is as follows:
     ## Note, this file is written by cloud-init on first boot of an instance
    ## modifications made here will not survive a re-bundle.
    ## if you wish to make changes you can:
    ## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
    ##     or do the same in user-data
    ## b.) add sources in /etc/apt/sources.list.d
    ## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl
    
    # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
    # newer versions of the distribution.
    deb [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic main restricted
    deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports/ bionic main restricted
    deb-src [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic main restricted
    
    ## Major bug fix updates produced after the final release of the
    ## distribution.
    deb [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-updates main restricted
    deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports/ bionic-updates main restricted
    deb-src [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-updates main restricted
    
    ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
    ## team. Also, please note that software in universe WILL NOT receive any
    ## review or updates from the Ubuntu security team.
    deb [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic universe
    deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports/ bionic universe
    deb-src [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic universe
    deb [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-updates universe
    deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports/ bionic-updates universe
    deb-src [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-updates universe
    
    ## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
    ## team, and may not be under a free licence. Please satisfy yourself as to
    ## your rights to use the software. Also, please note that software in
    ## multiverse WILL NOT receive any review or updates from the Ubuntu
    ## security team.
    deb [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic multiverse
    deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports/ bionic multiverse
    deb-src [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic multiverse
    deb [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-updates multiverse
    deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports/ bionic-updates multiverse
    deb-src [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-updates multiverse
    
    ## N.B. software from this repository may not have been tested as
    ## extensively as that contained in the main release, although it includes
    ## newer versions of some applications which may provide useful features.
    ## Also, please note that software in backports WILL NOT receive any review
    ## or updates from the Ubuntu security team.
    deb [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-backports main restricted universe multiverse
    deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports/ bionic-backports main restricted universe multiverse
    deb-src [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-backports main restricted universe multiverse
    
    deb [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-security main restricted
    deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports/ bionic-security main restricted
    deb-src [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-security main restricted
    deb [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-security universe
    deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports/ bionic-security universe
    deb-src [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-security universe
    deb [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-security multiverse
    deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports/ bionic-security multiverse
    deb-src [arch=amd64] http://mirrors.adn.networklayer.com/ubuntu bionic-security multiverse
    
    ## Uncomment the following two lines to add software from Canonical's
    ## 'partner' repository.
    ## This software is not part of Ubuntu, but is offered by Canonical and the
    ## respective vendors as a service to Ubuntu users.
    # deb http://archive.canonical.com/ubuntu bionic partner
    # deb-src http://archive.canonical.com/ubuntu bionic partner
    
     
  • Run the prereq_install script, which is a part of the downloaded dotnet7 folder, to install prerequisite packages for cross building .NET 7.

Step 3. Cross build .NET repositories to generate tarball for Power

  • After all the required packages are installed, run the dotnet-prepare-ppc64le script from the downloaded dotnet7 folder. This script clones the required dotnet repositories and applies a few patches that are not part of the release.
  • Ensure you have all repositories and patches in place.
  • Run the dotnet-build-ppc64le script to build the tarball.
  • After your build is successful, you will see dotnet-runtime-7.0.0-linux-ppc64le.tar.gz in the output folder as follows:
     #ls
    dotnet-runtime-7.0.0-linux-ppc64le.tar.gz
    dotnet-runtime-internal-7.0.0-linux-ppc64le.tar.gz
    dotnet-sdk-7.0.100-linux-ppc64le.tar.gz
    Microsoft.AspNetCore.App.Runtime.linux-ppc64le.7.0.0.nupkg
    Microsoft.AspNetCore.App.Runtime.linux-ppc64le.7.0.0.symbols.nupkg
    Microsoft.NETCore.App.Host.linux-ppc64le.7.0.0.nupkg
    Microsoft.NETCore.App.Host.linux-ppc64le.7.0.0.symbols.nupkg
    Microsoft.NETCore.App.Runtime.linux-ppc64le.7.0.0.nupkg
    Microsoft.NETCore.App.Runtime.linux-ppc64le.7.0.0.symbols.nupkg
    runtime.linux-ppc64le.Microsoft.NETCore.DotNetHost.7.0.0.nupkg
    runtime.linux-ppc64le.Microsoft.NETCore.DotNetHost.7.0.0.symbols.nupkg
    runtime.linux-ppc64le.Microsoft.NETCore.DotNetHostPolicy.7.0.0.nupkg
    runtime.linux-ppc64le.Microsoft.NETCore.DotNetHostPolicy.7.0.0.symbols.nupkg
    runtime.linux-ppc64le.Microsoft.NETCore.DotNetHostResolver.7.0.0.nupkg
    runtime.linux-ppc64le.Microsoft.NETCore.DotNetHostResolver.7.0.0.symbols.nupkg
    runtime.linux-ppc64le.Microsoft.NETCore.ILAsm.7.0.0.nupkg
    runtime.linux-ppc64le.Microsoft.NETCore.ILDAsm.7.0.0.nupkg
     
  • Copy this full output folder onto your Power VM with Ubuntu 18.04.

Step 4. Install tarball on Power VM with Ubuntu 18.04

  • On your Power VM, go to the directory where you have copied the tarball generated on the x86 machine.
  • Create a .dotnet subfolder and extract the SDK tarball that's in that folder as follows:
     DOTNET_FILE=dotnet-sdk-7.0.100-linux-ppc64le.tar.gz
    mkdir -p .dotnet 
    export DOTNET_ROOT=$(pwd)/.dotnet
    tar zxf "$DOTNET_FILE" -C "$DOTNET_ROOT" 
  • Add a dotnet root folder in the path as shown below:
     export PATH=$PATH:$DOTNET_ROOT 
  • To verify that .NET is installed, run the following command:
     dotnet --info 

    The output should look as follows:
     $ dotnet --info
    .NET SDK:
     Version:   7.0.100
     Commit:    e12b7af219
    
    Runtime Environment:
     OS Name:     ubuntu
     OS Version:  18.04
     OS Platform: Linux
     RID:         linux-ppc64le
     Base Path:   /home/ubuntu/dotnet7_ga_tarball/output/.dotnet/sdk/7.0.100/
    
    Host:
      Version:      7.0.0
      Architecture: ppc64le
      Commit:       d099f075e4
    
    .NET SDKs installed:
      7.0.100 [/home/ubuntu/dotnet7_ga_tarball/output/.dotnet/sdk]
    
    .NET runtimes installed:
      Microsoft.AspNetCore.App 7.0.0 [/home/ubuntu/dotnet7_ga_tarball/output/.dotnet/shared/Microsoft.AspNetCore.App]
      Microsoft.NETCore.App 7.0.0 [/home/ubuntu/dotnet7_ga_tarball/output/.dotnet/shared/Microsoft.NETCore.App]
    
    Other architectures found:
      None
    
    Environment variables:
      DOTNET_ROOT       [/home/ubuntu/dotnet7_ga_tarball/output/.dotnet]
    
    global.json file:
      Not found
    
    Learn more:
      https://aka.ms/dotnet/info
    
    Download .NET:
      https://aka.ms/dotnet/download
    ubuntu@dotnet2--2:~/dotnet7_ga_tarball/output$
     

Step 5. Run Hello World program on the Power VM with Ubuntu 18.04

  • Create the Hello World console application using the dotnet new console command:
     mkdir HelloWorld
    cd HelloWorld
    dotnet new console 

    Your output should look as follows:
     $ mkdir HelloWorld
    $ cd HelloWorld
    $/HelloWorld$ dotnet new console
    
    Welcome to .NET 7.0!
    ---------------------
    SDK Version: 7.0.100
    
    ----------------
    Installed an ASP.NET Core HTTPS development certificate.
    To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
    Learn about HTTPS: https://aka.ms/dotnet-https
    ----------------
    Write your first app: https://aka.ms/dotnet-hello-world
    Find out what's new: https://aka.ms/dotnet-whats-new
    Explore documentation: https://aka.ms/dotnet-docs
    Report issues and find source on GitHub: https://github.com/dotnet/core
    Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
    --------------------------------------------------------------------------------------
    The template "Console App" was created successfully.
    
    Processing post-creation actions...
    Restoring /home/ubuntu/HelloWorld/HelloWorld.csproj:
      Determining projects to restore...
      Restored /home/ubuntu/HelloWorld/HelloWorld.csproj (in 449 ms).
    Restore succeeded.
    
     
  • Build and run Hello World using the dotnet build and dotnet run commands:
     dotnet build
    dotnet run 

    Your output should look as follows:
     ~/HelloWorld$ dotnet build
    MSBuild version 17.4.0+18d5aef85 for .NET
      Determining projects to restore...
      All projects are up-to-date for restore.
      HelloWorld -> /home/ubuntu/HelloWorld/bin/Debug/net7.0/HelloWorld.dll
    
    Build succeeded.
        0 Warning(s)
        0 Error(s)
    
    Time Elapsed 00:00:15.31
    ~/HelloWorld$ dotnet run
    Hello, World! 
    Note: While we have tested these scripts on Ubuntu 18.04, they should work fine on Ubuntu 20.04 as well.

Conclusion

In this blog you have learned to cross build .NET 7 on x86 for IBM Power. Now you are ready to install a .NET 7 tarball on a Power VM and use begin using .NET for development.

Permalink