diff --git a/eng/common/native/find-native-compiler.sh b/eng/common/native/init-compiler.sh similarity index 52% rename from eng/common/native/find-native-compiler.sh rename to eng/common/native/init-compiler.sh index 861d1931e55e31..1daadf32a524fb 100644 --- a/eng/common/native/find-native-compiler.sh +++ b/eng/common/native/init-compiler.sh @@ -1,39 +1,32 @@ #!/usr/bin/env bash # -# This file locates the native compiler with the given name and version and sets the environment variables to locate it. +# This file detects the C/C++ compiler and exports it to the CC/CXX environment variables # -source="${BASH_SOURCE[0]}" - -# resolve $SOURCE until the file is no longer a symlink -while [[ -h $source ]]; do - scriptroot="$( cd -P "$( dirname "$source" )" && pwd )" - source="$(readlink "$source")" - - # if $source was a relative symlink, we need to resolve it relative to the path where the - # symlink file was located - [[ $source != /* ]] && source="$scriptroot/$source" -done -scriptroot="$( cd -P "$( dirname "$source" )" && pwd )" - -if [ $# -lt 0 ] -then +if [[ "$#" -lt 2 ]]; then echo "Usage..." - echo "find-native-compiler.sh " + echo "init-compiler.sh " + echo "Specify the target architecture." echo "Specify the name of compiler (clang or gcc)." echo "Specify the major version of compiler." echo "Specify the minor version of compiler." exit 1 fi -. $scriptroot/../pipeline-logging-functions.sh +. "$( cd -P "$( dirname "$0" )" && pwd )"/../pipeline-logging-functions.sh -compiler="$1" +build_arch="$1" +compiler="$2" cxxCompiler="$compiler++" -majorVersion="$2" -minorVersion="$3" +majorVersion="$3" +minorVersion="$4" -if [ "$compiler" = "gcc" ]; then cxxCompiler="g++"; fi +# clear the existing CC and CXX from environment +CC= +CXX= +LDFLAGS= + +if [[ "$compiler" == "gcc" ]]; then cxxCompiler="g++"; fi check_version_exists() { desired_version=-1 @@ -50,38 +43,38 @@ check_version_exists() { echo "$desired_version" } -if [ -z "$CLR_CC" ]; then +if [[ -z "$CLR_CC" ]]; then # Set default versions - if [ -z "$majorVersion" ]; then + if [[ -z "$majorVersion" ]]; then # note: gcc (all versions) and clang versions higher than 6 do not have minor version in file name, if it is zero. - if [ "$compiler" = "clang" ]; then versions=( 12 11 10 9 8 7 6.0 5.0 4.0 3.9 3.8 3.7 3.6 3.5 ) - elif [ "$compiler" = "gcc" ]; then versions=( 9 8 7 6 5 4.9 ); fi + if [[ "$compiler" == "clang" ]]; then versions=( 12 11 10 9 8 7 6.0 5.0 4.0 3.9 3.8 3.7 3.6 3.5 ) + elif [[ "$compiler" == "gcc" ]]; then versions=( 11 10 9 8 7 6 5 4.9 ); fi for version in "${versions[@]}"; do parts=(${version//./ }) desired_version="$(check_version_exists "${parts[0]}" "${parts[1]}")" - if [ "$desired_version" != "-1" ]; then majorVersion="${parts[0]}"; break; fi + if [[ "$desired_version" != "-1" ]]; then majorVersion="${parts[0]}"; break; fi done - if [ -z "$majorVersion" ]; then + if [[ -z "$majorVersion" ]]; then if command -v "$compiler" > /dev/null; then - if [ "$(uname)" != "Darwin" ]; then + if [[ "$(uname)" != "Darwin" ]]; then Write-PipelineTelemetryError -category "Build" -type "warning" "Specific version of $compiler not found, falling back to use the one in PATH." fi - export CC="$(command -v "$compiler")" - export CXX="$(command -v "$cxxCompiler")" + CC="$(command -v "$compiler")" + CXX="$(command -v "$cxxCompiler")" else Write-PipelineTelemetryError -category "Build" "No usable version of $compiler found." exit 1 fi else - if [ "$compiler" = "clang" ] && [ "$majorVersion" -lt 5 ]; then - if [ "$build_arch" = "arm" ] || [ "$build_arch" = "armel" ]; then + if [[ "$compiler" == "clang" && "$majorVersion" -lt 5 ]]; then + if [[ "$build_arch" == "arm" || "$build_arch" == "armel" ]]; then if command -v "$compiler" > /dev/null; then Write-PipelineTelemetryError -category "Build" -type "warning" "Found clang version $majorVersion which is not supported on arm/armel architectures, falling back to use clang from PATH." - export CC="$(command -v "$compiler")" - export CXX="$(command -v "$cxxCompiler")" + CC="$(command -v "$compiler")" + CXX="$(command -v "$cxxCompiler")" else Write-PipelineTelemetryError -category "Build" "Found clang version $majorVersion which is not supported on arm/armel architectures, and there is no clang in PATH." exit 1 @@ -91,31 +84,40 @@ if [ -z "$CLR_CC" ]; then fi else desired_version="$(check_version_exists "$majorVersion" "$minorVersion")" - if [ "$desired_version" = "-1" ]; then + if [[ "$desired_version" == "-1" ]]; then Write-PipelineTelemetryError -category "Build" "Could not find specific version of $compiler: $majorVersion $minorVersion." exit 1 fi fi - if [ -z "$CC" ]; then - export CC="$(command -v "$compiler$desired_version")" - export CXX="$(command -v "$cxxCompiler$desired_version")" - if [ -z "$CXX" ]; then export CXX="$(command -v "$cxxCompiler")"; fi + if [[ -z "$CC" ]]; then + CC="$(command -v "$compiler$desired_version")" + CXX="$(command -v "$cxxCompiler$desired_version")" + if [[ -z "$CXX" ]]; then CXX="$(command -v "$cxxCompiler")"; fi fi else - if [ ! -f "$CLR_CC" ]; then + if [[ ! -f "$CLR_CC" ]]; then Write-PipelineTelemetryError -category "Build" "CLR_CC is set but path '$CLR_CC' does not exist" exit 1 fi - export CC="$CLR_CC" - export CXX="$CLR_CXX" + CC="$CLR_CC" + CXX="$CLR_CXX" fi -if [ -z "$CC" ]; then - Write-PipelineTelemetryError -category "Build" "Unable to find $compiler." +if [[ -z "$CC" ]]; then + Write-PipelineTelemetryError -category "Build" "Unable to find $compiler." exit 1 fi -export CCC_CC="$CC" -export CCC_CXX="$CXX" -export SCAN_BUILD_COMMAND="$(command -v "scan-build$desired_version")" +if [[ "$compiler" == "clang" ]]; then + if command -v "lld$desired_version" > /dev/null; then + # Only lld version >= 9 can be considered stable + if [[ "$majorVersion" -ge 9 ]]; then + LDFLAGS="-fuse-ld=lld" + fi + fi +fi + +SCAN_BUILD_COMMAND="$(command -v "scan-build$desired_version")" + +export CC CXX LDFLAGS SCAN_BUILD_COMMAND diff --git a/eng/native/gen-buildsys.sh b/eng/native/gen-buildsys.sh index bf04c26f2b1b17..b4a5e3fca8cdad 100755 --- a/eng/native/gen-buildsys.sh +++ b/eng/native/gen-buildsys.sh @@ -26,10 +26,12 @@ compiler="$4" majorVersion="$5" minorVersion="$6" -source "$scriptroot/init-compiler.sh" "$build_arch" "$compiler" "$majorVersion" "$minorVersion" +if [[ "$compiler" != "default" ]]; then + source "$scriptroot/../common/native/init-compiler.sh" "$build_arch" "$compiler" "$majorVersion" "$minorVersion" -CCC_CC="$CC" -CCC_CXX="$CXX" + CCC_CC="$CC" + CCC_CXX="$CXX" +fi export CCC_CC CCC_CXX diff --git a/eng/native/init-compiler.sh b/eng/native/init-compiler.sh deleted file mode 100755 index 2f7ca37e50c410..00000000000000 --- a/eng/native/init-compiler.sh +++ /dev/null @@ -1,126 +0,0 @@ -#!/usr/bin/env bash -# -# This file detects the C/C++ compiler and exports it to the CC/CXX environment variables -# - -if [[ "$#" -lt 2 ]]; then - echo "Usage..." - echo "init-compiler.sh " - echo "Specify the target architecture." - echo "Specify the name of compiler (clang, gcc, or default for CMake-default)." - echo "Specify the major version of compiler." - echo "Specify the minor version of compiler." - exit 1 -fi - -build_arch="$1" -compiler="$2" -cxxCompiler="$compiler++" -majorVersion="$3" -minorVersion="$4" - -# clear the existing CC and CXX from environment -CC= -CXX= -LDFLAGS= - -if [[ "$compiler" == "gcc" ]]; then cxxCompiler="g++"; fi - -check_version_exists() { - desired_version=-1 - - # Set up the environment to be used for building with the desired compiler. - if command -v "$compiler-$1.$2" > /dev/null; then - desired_version="-$1.$2" - elif command -v "$compiler$1$2" > /dev/null; then - desired_version="$1$2" - elif command -v "$compiler-$1$2" > /dev/null; then - desired_version="-$1$2" - fi - - echo "$desired_version" -} - -if [[ "$compiler" == "default" ]]; then - # Don't set CC/CXX to anything and leave it up to CMake - true -else - if [[ -z "$CLR_CC" ]]; then - - # Set default versions - if [[ -z "$majorVersion" ]]; then - # note: gcc (all versions) and clang versions higher than 6 do not have minor version in file name, if it is zero. - if [[ "$compiler" == "clang" ]]; then versions=( 12 11 10 9 8 7 6.0 5.0 4.0 3.9 3.8 3.7 3.6 3.5 ) - elif [[ "$compiler" == "gcc" ]]; then versions=( 11 10 9 8 7 6 5 4.9 ); fi - - for version in "${versions[@]}"; do - parts=(${version//./ }) - desired_version="$(check_version_exists "${parts[0]}" "${parts[1]}")" - if [[ "$desired_version" != "-1" ]]; then majorVersion="${parts[0]}"; break; fi - done - - if [[ -z "$majorVersion" ]]; then - if command -v "$compiler" > /dev/null; then - if [[ "$(uname)" != "Darwin" ]]; then - echo "WARN: Specific version of $compiler not found, falling back to use the one in PATH." - fi - CC="$(command -v "$compiler")" - CXX="$(command -v "$cxxCompiler")" - else - echo "ERROR: No usable version of $compiler found." - exit 1 - fi - else - if [[ "$compiler" == "clang" && "$majorVersion" -lt 5 ]]; then - if [[ "$build_arch" == "arm" || "$build_arch" == "armel" ]]; then - if command -v "$compiler" > /dev/null; then - echo "WARN: Found clang version $majorVersion which is not supported on arm/armel architectures, falling back to use clang from PATH." - CC="$(command -v "$compiler")" - CXX="$(command -v "$cxxCompiler")" - else - echo "ERROR: Found clang version $majorVersion which is not supported on arm/armel architectures, and there is no clang in PATH." - exit 1 - fi - fi - fi - fi - else - desired_version="$(check_version_exists "$majorVersion" "$minorVersion")" - if [[ "$desired_version" == "-1" ]]; then - echo "ERROR: Could not find specific version of $compiler: $majorVersion $minorVersion." - exit 1 - fi - fi - - if [[ -z "$CC" ]]; then - CC="$(command -v "$compiler$desired_version")" - CXX="$(command -v "$cxxCompiler$desired_version")" - if [[ -z "$CXX" ]]; then CXX="$(command -v "$cxxCompiler")"; fi - fi - else - if [[ ! -f "$CLR_CC" ]]; then - echo "ERROR: CLR_CC is set but path '$CLR_CC' does not exist" - exit 1 - fi - CC="$CLR_CC" - CXX="$CLR_CXX" - fi - - if [[ -z "$CC" ]]; then - echo "ERROR: Unable to find $compiler." - exit 1 - fi -fi - -if [[ "$compiler" == "clang" ]]; then - if command -v "lld$desired_version" > /dev/null; then - # Only lld version >= 9 can be considered stable - if [[ "$majorVersion" -ge 9 ]]; then - LDFLAGS="-fuse-ld=lld" - fi - fi -fi - -SCAN_BUILD_COMMAND="$(command -v "scan-build$desired_version")" - -export CC CXX LDFLAGS SCAN_BUILD_COMMAND diff --git a/src/mono/mono.proj b/src/mono/mono.proj index 6d69846fe52e2a..48d7a215026f38 100644 --- a/src/mono/mono.proj +++ b/src/mono/mono.proj @@ -61,6 +61,8 @@ $(MonoCCompiler.TrimStart('-')) clang++ g++ + $([MSBuild]::NormalizeDirectory('$(RepositoryEngineeringDir)', 'common')) + $([MSBuild]::NormalizePath('$(RepositoryEngineeringCommonDir)', 'cross', 'toolchain.cmake')) @@ -224,7 +226,7 @@ - <_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$([MSBuild]::NormalizePath('$(RepositoryEngineeringDir)', 'common', 'cross', 'toolchain.cmake'))" /> + <_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$(CrossToolchainFile)" /> <_MonoBuildEnv Condition="'$(Platform)' == 'arm64'" Include="TARGET_BUILD_ARCH=arm64" /> <_MonoBuildEnv Condition="'$(Platform)' == 'arm'" Include="TARGET_BUILD_ARCH=arm" /> <_MonoBuildEnv Condition="'$(Platform)' == 'arm64'" Include="PKG_CONFIG_PATH=$(MonoCrossDir)/usr/lib/aarch64-linux-gnu/pkgconfig" /> @@ -233,21 +235,21 @@ - <_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$([MSBuild]::NormalizePath('$(RepositoryEngineeringDir)', 'common', 'cross', 'toolchain.cmake'))" /> + <_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$(CrossToolchainFile)" /> <_MonoBuildEnv Include="TARGET_BUILD_ARCH=x64" /> <_MonoBuildEnv Include="PKG_CONFIG_PATH=$(MonoCrossDir)/lib/pkgconfig" /> - <_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$([MSBuild]::NormalizePath('$(RepositoryEngineeringDir)', 'common', 'cross', 'toolchain.cmake'))" /> + <_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$(CrossToolchainFile)" /> <_MonoBuildEnv Include="TARGET_BUILD_ARCH=s390x" /> <_MonoBuildEnv Include="PKG_CONFIG_PATH=$(MonoCrossDir)/usr/lib/s390x-linux-gnu/pkgconfig" /> - <_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$([MSBuild]::NormalizePath('$(RepositoryEngineeringDir)', 'common', 'cross', 'toolchain.cmake'))" /> + <_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$(CrossToolchainFile)" /> <_MonoBuildEnv Include="TARGET_BUILD_ARCH=x64" /> @@ -464,7 +466,7 @@ $([MSBuild]::EnsureTrailingSlash('$(EMSDK_PATH)')) <_MonoCMakeConfigureCommand>cmake @(_MonoCMakeArgs, ' ') $(MonoCMakeExtraArgs) "$(MonoProjectRoot.TrimEnd('\/'))" - <_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' != 'false' and '$(OS)' != 'Windows_NT'">bash -c 'source $(RepositoryEngineeringDir)native/init-compiler.sh $(_CompilerTargetArch) $(MonoCCompiler) && @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)' + <_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' != 'false' and '$(OS)' != 'Windows_NT'">bash -c 'source $(RepositoryEngineeringCommonDir)native/init-compiler.sh $(_CompilerTargetArch) $(MonoCCompiler) && @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)' <_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' != 'false' and '$(OS)' == 'Windows_NT'">call "$(RepositoryEngineeringDir)native\init-vs-env.cmd" $(_CompilerTargetArch) && cd /D "$(MonoObjDir)" && @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand) <_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' == 'false'">$(_MonoCCOption) $(_MonoCXXOption) @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand) <_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' == 'true' and '$(OS)' != 'Windows_NT'">bash -c 'source $(EMSDK_PATH)/emsdk_env.sh 2>&1 && emcmake $(_MonoCMakeConfigureCommand)'