Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit f5eab67

Browse files
franksinankayaSinan Kaya
authored and
Sinan Kaya
committed
Add gcc option to build.sh script
Following clangx.y model add -gccx.y command line arguments with gcc5 and gcc7 being the currnetly supported options.
1 parent d4a4f7d commit f5eab67

File tree

5 files changed

+234
-33
lines changed

5 files changed

+234
-33
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ if(CLR_CMAKE_PLATFORM_UNIX AND NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
170170
endif()
171171

172172
if(CLR_CMAKE_PLATFORM_UNIX)
173-
add_subdirectory(src/ToolBox/SOS/lldbplugin)
173+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
174+
add_subdirectory(src/ToolBox/SOS/lldbplugin)
175+
endif()
174176
add_subdirectory(src/pal)
175177
add_subdirectory(src/coreclr/hosts)
176178
add_subdirectory(src/ildasm/unixcoreclrloader)

build.sh

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ export PYTHON
2525

2626
usage()
2727
{
28-
echo "Usage: $0 [BuildArch] [BuildType] [-verbose] [-coverage] [-cross] [-clangx.y] [-ninja] [-configureonly] [-skipconfigure] [-skipnative] [-skipcrossarchnative] [-skipmanaged] [-skipmscorlib] [-skiptests] [-stripsymbols] [-ignorewarnings] [-cmakeargs] [-bindir]"
28+
echo "Usage: $0 [BuildArch] [BuildType] [-verbose] [-coverage] [-cross] [-gccx.y] [-clangx.y] [-ninja] [-configureonly] [-skipconfigure] [-skipnative] [-skipcrossarchnative] [-skipmanaged] [-skipmscorlib] [-skiptests] [-stripsymbols] [-ignorewarnings] [-cmakeargs] [-bindir]"
2929
echo "BuildArch can be: -x64, -x86, -arm, -armel, -arm64"
3030
echo "BuildType can be: -debug, -checked, -release"
3131
echo "-coverage - optional argument to enable code coverage build (currently supported only for Linux and OSX)."
3232
echo "-ninja - target ninja instead of GNU make"
33+
echo "-gccx.y - optional argument to build using gcc version x.y."
3334
echo "-clangx.y - optional argument to build using clang version x.y."
3435
echo "-cross - optional argument to signify cross compilation,"
3536
echo " - will use ROOTFS_DIR environment variable if set."
@@ -158,18 +159,26 @@ check_prereqs()
158159

159160

160161
# Minimum required version of clang is version 4.0 for arm/armel cross build
161-
if [[ $__CrossBuild == 1 && ("$__BuildArch" == "arm" || "$__BuildArch" == "armel") ]]; then
162+
if [[ $__CrossBuild == 1 && $__GccMajorVersion == 0 && ("$__BuildArch" == "arm" || "$__BuildArch" == "armel") ]]; then
162163
if ! [[ "$__ClangMajorVersion" -ge "4" ]]; then
163164
echo "Please install clang4.0 or latest for arm/armel cross build"; exit 1;
164165
fi
165166
fi
166167

167168
# Check for clang
168-
__ClangCombinedDottedVersion=$__ClangMajorVersion;
169-
if [[ "$__ClangMinorVersion" != "" ]]; then
170-
__ClangCombinedDottedVersion=$__ClangCombinedDottedVersion.$__ClangMinorVersion
169+
if [[ $$__GccMajorVersion == 0 ]]; then
170+
__ClangCombinedDottedVersion=$__ClangMajorVersion;
171+
if [[ "$__ClangMinorVersion" != "" ]]; then
172+
__ClangCombinedDottedVersion=$__ClangCombinedDottedVersion.$__ClangMinorVersion
173+
fi
174+
hash clang-$__ClangCombinedDottedVersion 2>/dev/null || hash clang$__ClangMajorVersion$__ClangMinorVersion 2>/dev/null || hash clang 2>/dev/null || { echo >&2 "Please install clang-$__ClangMajorVersion.$__ClangMinorVersion before running this script"; exit 1; }
175+
else
176+
__GccCombinedDottedVersion=$__GccMajorVersion;
177+
if [[ "$__GccMinorVersion" != "" ]]; then
178+
__GccCombinedDottedVersion=$__GccCombinedDottedVersion.$__GccMinorVersion
179+
fi
180+
hash gcc-$__GccCombinedDottedVersion 2>/dev/null || hash gcc$__GccMajorVersion$__GccMinorVersion 2>/dev/null || { echo >&2 "Please install gcc-$__GccMajorVersion.$__GccMinorVersion before running this script"; exit 1; }
171181
fi
172-
hash clang-$__ClangCombinedDottedVersion 2>/dev/null || hash clang$__ClangMajorVersion$__ClangMinorVersion 2>/dev/null || hash clang 2>/dev/null || { echo >&2 "Please install clang-$__ClangMajorVersion.$__ClangMinorVersion before running this script"; exit 1; }
173182

174183
}
175184

@@ -317,8 +326,14 @@ build_native()
317326

318327
pushd "$intermediatesForBuild"
319328
# Regenerate the CMake solution
320-
echo "Invoking \"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh\" \"$__ProjectRoot\" $__ClangMajorVersion \"$__ClangMinorVersion\" $platformArch $__BuildType $__CodeCoverage $generator $extraCmakeArguments $__cmakeargs"
321-
"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh" "$__ProjectRoot" $__ClangMajorVersion "$__ClangMinorVersion" $platformArch $__BuildType $__CodeCoverage $generator "$extraCmakeArguments" "$__cmakeargs"
329+
330+
if [[ $__GccMajorVersion != 0 ]]; then
331+
echo "Invoking \"$__ProjectRoot/src/pal/tools/gen-buildsys-gcc.sh\" \"$__ProjectRoot\" $__GccMajorVersion \"$__GccMinorVersion\" $platformArch $__BuildType $__CodeCoverage $generator $extraCmakeArguments $__cmakeargs"
332+
"$__ProjectRoot/src/pal/tools/gen-buildsys-gcc.sh" "$__ProjectRoot" $__GccMajorVersion "$__CGccMinorVersion" $platformArch $__BuildType $__CodeCoverage $generator "$extraCmakeArguments" "$__cmakeargs"
333+
else
334+
echo "Invoking \"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh\" \"$__ProjectRoot\" $__ClangMajorVersion \"$__ClangMinorVersion\" $platformArch $__BuildType $__CodeCoverage $generator $extraCmakeArguments $__cmakeargs"
335+
"$__ProjectRoot/src/pal/tools/gen-buildsys-clang.sh" "$__ProjectRoot" $__ClangMajorVersion "$__ClangMinorVersion" $platformArch $__BuildType $__CodeCoverage $generator "$extraCmakeArguments" "$__cmakeargs"
336+
fi
322337
popd
323338
fi
324339

@@ -671,6 +686,8 @@ __SkipTests=0
671686
__CrossBuild=0
672687
__ClangMajorVersion=0
673688
__ClangMinorVersion=0
689+
__GccMajorVersion=0
690+
__GccMinorVersion=0
674691
__NuGetPath="$__PackagesDir/NuGet.exe"
675692
__HostDistroRid=""
676693
__DistroRid=""
@@ -807,6 +824,16 @@ while :; do
807824
__ClangMinorVersion=
808825
;;
809826

827+
gcc5|-gcc5)
828+
__GccMajorVersion=5
829+
__GccMinorVersion=
830+
;;
831+
832+
gcc7|-gcc7)
833+
__GccMajorVersion=7
834+
__GccMinorVersion=
835+
;;
836+
810837
ninja|-ninja)
811838
__UseNinja=1
812839
;;

configurecompiler.cmake

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -441,36 +441,41 @@ if (CLR_CMAKE_PLATFORM_UNIX)
441441
add_definitions(-DDISABLE_CONTRACTS)
442442
# The -ferror-limit is helpful during the porting, it makes sure the compiler doesn't stop
443443
# after hitting just about 20 errors.
444-
add_compile_options(-ferror-limit=4096)
445444

446445
if (CLR_CMAKE_WARNINGS_ARE_ERRORS)
447446
# All warnings that are not explicitly disabled are reported as errors
448447
add_compile_options(-Werror)
449448
endif(CLR_CMAKE_WARNINGS_ARE_ERRORS)
450449

451-
# Disabled warnings
452-
add_compile_options(-Wno-unused-private-field)
453-
add_compile_options(-Wno-unused-variable)
454-
# Explicit constructor calls are not supported by clang (this->ClassName::ClassName())
455-
add_compile_options(-Wno-microsoft)
456-
# This warning is caused by comparing 'this' to NULL
457-
add_compile_options(-Wno-tautological-compare)
458-
# There are constants of type BOOL used in a condition. But BOOL is defined as int
459-
# and so the compiler thinks that there is a mistake.
460-
add_compile_options(-Wno-constant-logical-operand)
461-
# We use pshpack1/2/4/8.h and poppack.h headers to set and restore packing. However
462-
# clang 6.0 complains when the packing change lifetime is not contained within
463-
# a header file.
464-
add_compile_options(-Wno-pragma-pack)
465-
466-
add_compile_options(-Wno-unknown-warning-option)
467-
468-
#These seem to indicate real issues
469-
add_compile_options(-Wno-invalid-offsetof)
470-
# The following warning indicates that an attribute __attribute__((__ms_struct__)) was applied
471-
# to a struct or a class that has virtual members or a base class. In that case, clang
472-
# may not generate the same object layout as MSVC.
473-
add_compile_options(-Wno-incompatible-ms-struct)
450+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
451+
add_compile_options(-ferror-limit=4096)
452+
453+
# Disabled warnings
454+
add_compile_options(-Wno-unused-private-field)
455+
add_compile_options(-Wno-unused-variable)
456+
# Explicit constructor calls are not supported by clang (this->ClassName::ClassName())
457+
add_compile_options(-Wno-microsoft)
458+
# This warning is caused by comparing 'this' to NULL
459+
add_compile_options(-Wno-tautological-compare)
460+
# There are constants of type BOOL used in a condition. But BOOL is defined as int
461+
# and so the compiler thinks that there is a mistake.
462+
add_compile_options(-Wno-constant-logical-operand)
463+
# We use pshpack1/2/4/8.h and poppack.h headers to set and restore packing. However
464+
# clang 6.0 complains when the packing change lifetime is not contained within
465+
# a header file.
466+
add_compile_options(-Wno-pragma-pack)
467+
468+
add_compile_options(-Wno-unknown-warning-option)
469+
470+
#These seem to indicate real issues
471+
add_compile_options(-Wno-invalid-offsetof)
472+
# The following warning indicates that an attribute __attribute__((__ms_struct__)) was applied
473+
# to a struct or a class that has virtual members or a base class. In that case, clang
474+
# may not generate the same object layout as MSVC.
475+
add_compile_options(-Wno-incompatible-ms-struct)
476+
else()
477+
add_compile_options(-Wno-unused-variable)
478+
endif()
474479

475480
# Some architectures (e.g., ARM) assume char type is unsigned while CoreCLR assumes char is signed
476481
# as x64 does. It has been causing issues in ARM (https://github.com/dotnet/coreclr/issues/4746)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
SET (CMAKE_C_FLAGS_INIT "-Wall -std=c11")
2+
SET (CMAKE_C_FLAGS_DEBUG_INIT "-g -O0")
3+
SET (CLR_C_FLAGS_CHECKED_INIT "-g -O2")
4+
# Refer to the below instruction to support __thread with -O2/-O3 on Linux/ARM
5+
# https://github.com/dotnet/coreclr/blob/master/Documentation/building/linux-instructions.md
6+
SET (CMAKE_C_FLAGS_RELEASE_INIT "-g -O3")
7+
SET (CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "-g -O2")
8+
9+
SET (CMAKE_CXX_FLAGS_INIT "-Wall -Werror=conversion-null -std=c++11")
10+
SET (CMAKE_CXX_FLAGS_DEBUG_INIT "-g -O0")
11+
SET (CLR_CXX_FLAGS_CHECKED_INIT "-g -O2")
12+
SET (CMAKE_CXX_FLAGS_RELEASE_INIT "-g -O3")
13+
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-g -O2")
14+
15+
SET (CLR_DEFINES_DEBUG_INIT DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1)
16+
SET (CLR_DEFINES_CHECKED_INIT DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1)
17+
SET (CLR_DEFINES_RELEASE_INIT NDEBUG URTBLDENV_FRIENDLY=Retail)
18+
SET (CLR_DEFINES_RELWITHDEBINFO_INIT NDEBUG URTBLDENV_FRIENDLY=Retail)
19+
20+
SET (CMAKE_INSTALL_PREFIX $ENV{__CMakeBinDir})

src/pal/tools/gen-buildsys-gcc.sh

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This file invokes cmake and generates the build system for Gcc.
4+
#
5+
6+
if [ $# -lt 4 ]
7+
then
8+
echo "Usage..."
9+
echo "gen-buildsys-gcc.sh <path to top level CMakeLists.txt> <GccMajorVersion> <GccMinorVersion> <Architecture> [build flavor] [coverage] [ninja] [cmakeargs]"
10+
echo "Specify the path to the top level CMake file - <ProjectK>/src/NDP"
11+
echo "Specify the Gcc version to use, split into major and minor version"
12+
echo "Specify the target architecture."
13+
echo "Optionally specify the build configuration (flavor.) Defaults to DEBUG."
14+
echo "Optionally specify 'coverage' to enable code coverage build."
15+
echo "Target ninja instead of make. ninja must be on the PATH."
16+
echo "Pass additional arguments to CMake call."
17+
exit 1
18+
fi
19+
20+
# Set up the environment to be used for building with gcc.
21+
if command -v "gcc-$2.$3" > /dev/null
22+
then
23+
desired_gcc_version="-$2.$3"
24+
elif command -v "gcc$2$3" > /dev/null
25+
then
26+
desired_gcc_version="$2$3"
27+
elif command -v "gcc-$2$3" > /dev/null
28+
then
29+
desired_gcc_version="-$2$3"
30+
elif command -v gcc > /dev/null
31+
then
32+
desired_gcc_version=
33+
else
34+
echo "Unable to find gcc Compiler"
35+
exit 1
36+
fi
37+
38+
export CC="$(command -v gcc$desired_gcc_version)"
39+
export CXX="$(command -v g++$desired_gcc_version)"
40+
41+
build_arch="$4"
42+
buildtype=DEBUG
43+
code_coverage=OFF
44+
build_tests=OFF
45+
generator="Unix Makefiles"
46+
__UnprocessedCMakeArgs=""
47+
48+
for i in "${@:5}"; do
49+
upperI="$(echo $i | awk '{print toupper($0)}')"
50+
case $upperI in
51+
# Possible build types are DEBUG, CHECKED, RELEASE, RELWITHDEBINFO, MINSIZEREL.
52+
DEBUG | CHECKED | RELEASE | RELWITHDEBINFO | MINSIZEREL)
53+
buildtype=$upperI
54+
;;
55+
COVERAGE)
56+
echo "Code coverage is turned on for this build."
57+
code_coverage=ON
58+
;;
59+
NINJA)
60+
generator=Ninja
61+
;;
62+
*)
63+
__UnprocessedCMakeArgs="${__UnprocessedCMakeArgs}${__UnprocessedCMakeArgs:+ }$i"
64+
esac
65+
done
66+
67+
OS=`uname`
68+
69+
# Locate gcc
70+
gcc_prefix=""
71+
72+
locate_gcc_exec() {
73+
if command -v "$gcc_prefix$1$desired_gcc_version" > /dev/null 2>&1
74+
then
75+
echo "$(command -v $gcc_prefix$1$desired_gcc_version)"
76+
elif command -v "$gcc_prefix$1" > /dev/null 2>&1
77+
then
78+
echo "$(command -v $gcc_prefix$1)"
79+
else
80+
exit 1
81+
fi
82+
}
83+
84+
gcc_ar="$(locate_gcc_exec ar)"
85+
[[ $? -eq 0 ]] || { echo "Unable to locate gcc-ar"; exit 1; }
86+
gcc_link="$(locate_gcc_exec gcc)"
87+
[[ $? -eq 0 ]] || { echo "Unable to locate gcc-link"; exit 1; }
88+
gcc_nm="$(locate_gcc_exec nm)"
89+
[[ $? -eq 0 ]] || { echo "Unable to locate gcc-nm"; exit 1; }
90+
if [ $OS = "Linux" -o $OS = "FreeBSD" -o $OS = "OpenBSD" -o $OS = "NetBSD" -o $OS = "SunOS" ]; then
91+
gcc_objdump="$(locate_gcc_exec objdump)"
92+
[[ $? -eq 0 ]] || { echo "Unable to locate gcc-objdump"; exit 1; }
93+
fi
94+
95+
cmake_extra_defines=
96+
if [[ -n "$LLDB_LIB_DIR" ]]; then
97+
cmake_extra_defines="$cmake_extra_defines -DWITH_LLDB_LIBS=$LLDB_LIB_DIR"
98+
fi
99+
if [[ -n "$LLDB_INCLUDE_DIR" ]]; then
100+
cmake_extra_defines="$cmake_extra_defines -DWITH_LLDB_INCLUDES=$LLDB_INCLUDE_DIR"
101+
fi
102+
if [ "$CROSSCOMPILE" == "1" ]; then
103+
if ! [[ -n "$ROOTFS_DIR" ]]; then
104+
echo "ROOTFS_DIR not set for crosscompile"
105+
exit 1
106+
fi
107+
if [[ -z $CONFIG_DIR ]]; then
108+
CONFIG_DIR="$1/cross"
109+
fi
110+
export TARGET_BUILD_ARCH=$build_arch
111+
cmake_extra_defines="$cmake_extra_defines -C $CONFIG_DIR/tryrun.cmake"
112+
cmake_extra_defines="$cmake_extra_defines -DCMAKE_TOOLCHAIN_FILE=$CONFIG_DIR/toolchain.cmake"
113+
cmake_extra_defines="$cmake_extra_defines -DCLR_UNIX_CROSS_BUILD=1"
114+
fi
115+
if [ $OS == "Linux" ]; then
116+
linux_id_file="/etc/os-release"
117+
if [[ -n "$CROSSCOMPILE" ]]; then
118+
linux_id_file="$ROOTFS_DIR/$linux_id_file"
119+
fi
120+
if [[ -e $linux_id_file ]]; then
121+
source $linux_id_file
122+
cmake_extra_defines="$cmake_extra_defines -DCLR_CMAKE_LINUX_ID=$ID"
123+
fi
124+
fi
125+
if [ "$build_arch" == "armel" ]; then
126+
cmake_extra_defines="$cmake_extra_defines -DARM_SOFTFP=1"
127+
fi
128+
129+
Gcc_version=$( $CC --version | head -1 | sed 's/[^0-9]*\([0-9]*\.[0-9]*\).*/\1/' )
130+
overridefile=gcc-compiler-override.txt
131+
132+
# Determine the current script directory
133+
__currentScriptDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
134+
135+
cmake \
136+
-G "$generator" \
137+
"-DCMAKE_USER_MAKE_RULES_OVERRIDE=${__currentScriptDir}/$overridefile" \
138+
"-DCMAKE_AR=$gcc_ar" \
139+
"-DCMAKE_LINKER=$gcc_link" \
140+
"-DCMAKE_NM=$gcc_nm" \
141+
"-DCMAKE_OBJDUMP=$gcc_objdump" \
142+
"-DCMAKE_BUILD_TYPE=$buildtype" \
143+
"-DCMAKE_EXPORT_COMPILE_COMMANDS=1 " \
144+
"-DCLR_CMAKE_ENABLE_CODE_COVERAGE=$code_coverage" \
145+
$cmake_extra_defines \
146+
$__UnprocessedCMakeArgs \
147+
"$1"

0 commit comments

Comments
 (0)