summaryrefslogtreecommitdiff
path: root/Tools/crossgen.sh
blob: 10fc5a2d18cf7d3f86d6e7c184a7cc8cef5ca509 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env bash
set -euo pipefail

# Restores crossgen and runs it on all tools components.
usage()
{
    echo "crossgen.sh <directory> <rid> <targetframework>"
    echo "    Restores crossgen and runs it on all assemblies in <directory>."
    exit 0
}

restore_crossgen()
{
    __crossgen=$__sharedFxDir/crossgen
    if [ -e $__crossgen ]; then
        return
    fi

    __pjDir=$__toolsDir/crossgen
    mkdir -p $__pjDir
    echo "<Project Sdk=\"Microsoft.NET.Sdk\"><PropertyGroup><DisableImplicitNuGetFallbackFolder>false</DisableImplicitNuGetFallbackFolder><TreatWarningsAsErrors>false</TreatWarningsAsErrors><NoWarn>\$(NoWarn);NU1605;NU1103</NoWarn><TargetFramework>$__TargetFramework</TargetFramework><DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences><RuntimeIdentifiers>$__packageRid</RuntimeIdentifiers></PropertyGroup><ItemGroup><PackageReference Include=\"Microsoft.NETCore.App\" Version=\"$__sharedFxVersion\" /></ItemGroup></Project>" > "$__pjDir/crossgen.csproj"
    $__dotnet restore $__pjDir/crossgen.csproj --packages $__packagesDir --source $__MyGetFeed
    __crossgen=$__packagesDir/runtime.$__packageRid.microsoft.netcore.app/$__sharedFxVersion/tools/crossgen
    if [ ! -e $__crossgen ]; then
        echo "The crossgen executable could not be found at "$__crossgen". Aborting crossgen.sh."
        exit 1
    fi
    # Executables restored with .NET Core 2.0 do not have executable permission flags. https://github.com/NuGet/Home/issues/4424
    chmod +x $__crossgen
}

crossgen_everything()
{
    echo "Running crossgen on all assemblies in $__targetDir."
    for file in $__targetDir/*.{dll,exe}
    do
        if [[ ($(basename $file) != "Microsoft.Build.Framework.dll") && ($(basename $file) != "Microsoft.DotNet.Build.Tasks.dll") ]]; then
            crossgen_single $file & pid=$!
            __pids+=" $pid"
        fi
    done

    trap "kill $__pids 2&> /dev/null" SIGINT
    wait $__pids
    echo "Crossgen finished."
}

crossgen_single()
{
    __file=$1
    if [[ $__file != *.ni.dll && $__file != *.ni.exe ]]; then
        if [[ ($__file == *.dll && -e ${__file/.dll/.ni.dll}) || ($__file == *.exe && -e ${__file/.exe/.ni.exe}) ]]; then
            echo "$__file has already been crossgen'd.  Skipping."
        else
            set +e
            $__crossgen /Platform_Assemblies_Paths $__sharedFxDir:$__toolsDir /JitPath $__sharedFxDir/libclrjit.$__libraryExtension /nologo /MissingDependenciesOK /ReadyToRun $__file > /dev/null
            if [ $? -eq 0 ]; then
                __outname="${__file/.dll/.ni.dll}"
                __outname="${__outname/.exe/.ni.exe}"
                echo "$__file -> $__outname"
            else
                echo "Unable to successfully compile $__file"
            fi
            set -e
        fi
    fi
}

if [ ! -z ${BUILDTOOLS_SKIP_CROSSGEN:-} ]; then
    echo "BUILDTOOLS_SKIP_CROSSGEN is set. Skipping crossgen step."
    exit 0
fi

if [[ -z "${1:-}" || "$1" == "-?" || "$1" == "--help" || "$1" == "-h" ]]; then
    usage
fi

__MyGetFeed=${BUILDTOOLS_CROSSGEN_FEED:-https://dotnet.myget.org/F/dotnet-core/api/v3/index.json}
__targetDir=$1
__packageRid=${2:-}
__TargetFramework=${3:-}
__scriptpath=$(cd "$(dirname "$0")"; pwd -P)
__toolsDir=$__scriptpath/../Tools
__dotnet=$__toolsDir/dotnetcli/dotnet
__packagesDir="${NUGET_PACKAGES:-${__scriptpath}/../packages}"
__mncaFolder=$__toolsDir/dotnetcli/shared/Microsoft.NETCore.App
__sharedFxVersion=`ls $__mncaFolder | sed 'r/\([0-9]\+\).*/\1/g' | sort -n | tail -1`
__sharedFxDir=$__toolsDir/dotnetcli/shared/Microsoft.NETCore.App/$__sharedFxVersion/

if [ -z "$__packageRid" ]; then
    case $(uname -s) in
        Darwin)
            __packageRid=osx-x64
            ;;
        Linux)
            __packageRid=linux-x64
            ;;
        *)
            echo "Unsupported OS $(uname -s) detected. Skipping crossgen of the toolset."
            exit 0
            ;;
    esac
fi

if [ "$__packageRid" == "osx-x64" ]; then
    __libraryExtension=dylib
else
    __libraryExtension=so
fi

if [ -z "$__TargetFramework" ]; then
    __TargetFramework="netcoreapp2.0"
fi

restore_crossgen
crossgen_everything
exit 0