Skip to content

Commit

Permalink
add support for FreeBSD cross-compilation (#34000)
Browse files Browse the repository at this point in the history
* add support for FreeBSD cross-compilation

* feedback from review

* update detection

* add openssl to docs

* feedback from reviews

* feedback from reviews

* final cleanup

* feedback from reviews

* simplify detection of route.h

* correct __CrossBuild detection

* use TargetOS for installer
  • Loading branch information
wfurt authored Mar 31, 2020
1 parent 45b2095 commit 595a95c
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/workflow/building/libraries/freebsd-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Tested on plain FreeBSD 11.3 Azure image
## Prerequisites
This needs to be done once on fresh system.

```sudo pkg install cmake git icu libunwind bash python2 krb5 lttng-ust llvm60 libgit2```
```sudo pkg install cmake git icu libunwind bash python2 krb5 lttng-ust llvm90 libgit2 libinotify openssl```

some scripts may still assume /bin/bash exists. To workaround it for now do of needed:
```
Expand Down
1 change: 1 addition & 0 deletions eng/native/build-commons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ __BuildArch=$arch
__HostArch=$arch
__TargetOS=$os
__HostOS=$os
__BuildOS=$os

__msbuildonunsupportedplatform=0

Expand Down
2 changes: 1 addition & 1 deletion eng/native/configurecompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ if(CLR_CMAKE_HOST_LINUX)
endif(CLR_CMAKE_HOST_LINUX)
if(CLR_CMAKE_HOST_FREEBSD)
add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-Wa,--noexecstack>)
add_link_options(-fuse-ld=lld LINKER:--build-id=sha1)
add_link_options(LINKER:--build-id=sha1)
endif(CLR_CMAKE_HOST_FREEBSD)

#------------------------------------
Expand Down
15 changes: 5 additions & 10 deletions eng/native/init-distro-rid.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@
# If -portablebuild=false is passed a non-portable rid will be created for any
# distro.
#
# Below is the list of current non-portable platforms.
#
# Builds from the following *must* be non-portable:
#
# | OS | Expected RID |
# -------------------------------------------------
# | freeBSD | freebsd.(version)-x64 |
#
# It is important to note that the function does not return anything, but it
# exports __DistroRid, if there is a non-portable distro rid to be used.
#
Expand Down Expand Up @@ -70,8 +62,11 @@ initNonPortableDistroRid()
fi

if [ "$targetOs" = "FreeBSD" ]; then
__freebsd_major_version=$(freebsd-version | { read v; echo "${v%%.*}"; })
nonPortableBuildID="freebsd.$__freebsd_major_version-${buildArch}"
if (( isPortable == 0 )); then
# $rootfsDir can be empty. freebsd-version is shell script and it should always work.
__freebsd_major_version=$($rootfsDir/bin/freebsd-version | { read v; echo "${v%%.*}"; })
nonPortableBuildID="freebsd.$__freebsd_major_version-${buildArch}"
fi
elif getprop ro.product.system.model 2>&1 | grep -qi android; then
__android_sdk_version=$(getprop ro.build.version.sdk)
nonPortableBuildID="android.$__android_sdk_version-${buildArch}"
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/crossgen-corelib.proj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ItemGroup>
<_CoreClrBuildArg Condition="'$(TargetArchitecture)' != ''" Include="-$(TargetArchitecture)" />
<_CoreClrBuildArg Include="-$(Configuration.ToLower())" />
<_CoreClrBuildArg Include="-os $(TargetOS)" />
</ItemGroup>

<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/crossgen-corelib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ __UnprocessedBuildArgs=

source "$__ProjectRoot"/_build-commons.sh

if [[ "${__BuildArch}" != "${__HostArch}" ]]; then
if [[ "${__BuildArch}" != "${__HostArch}" ]] || [[ "$__BuildOS" != "$__TargetOS" ]]; then
__CrossBuild=1
fi

Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/runtime.proj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<_CoreClrBuildArg Include="$(Compiler)" />
<_CoreClrBuildArg Condition="'$(ContinuousIntegrationBuild)' == 'true'" Include="-ci" />
<_CoreClrBuildArg Condition="'$(CrossBuild)' == 'true'" Include="-cross" />
<_CoreClrBuildArg Include="-os $(TargetOS)" />

<_CoreClrBuildArg Condition="$([MSBuild]::IsOsPlatform(Windows)) and ('$(TargetArchitecture)' == 'x86' or '$(TargetArchitecture)' == 'x64') and '$(Configuration)' == 'Release'" Include="-enforcepgo" />
<_CoreClrBuildArg Condition="$([MSBuild]::IsOsPlatform(Windows)) and '$(CrossDac)' != ''" Include="-$(CrossDac)dac" />
<_CoreClrBuildArg Condition="'$(OfficialBuildId)' != ''" Include="/p:OfficialBuildId=$(OfficialBuildId)" />
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/src/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ endif(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_FREEBSD)

if(NOT DEFINED ENV{ROOTFS_DIR})
include_directories(SYSTEM /usr/local/include)
elseif (CLR_CMAKE_TARGET_FREEBSD)
include_directories(SYSTEM $ENV{ROOTFS_DIR}/usr/local/include)
endif()

if(NOT CLR_CMAKE_USE_SYSTEM_LIBUNWIND)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/src/pal/src/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include(CheckTypeSize)
include(CheckLibraryExists)

if(CLR_CMAKE_TARGET_FREEBSD)
set(CMAKE_REQUIRED_INCLUDES /usr/local/include)
set(CMAKE_REQUIRED_INCLUDES ${CROSS_ROOTFS}/usr/local/include)
elseif(CLR_CMAKE_TARGET_SUNOS)
set(CMAKE_REQUIRED_INCLUDES /opt/local/include)
endif()
Expand Down
19 changes: 18 additions & 1 deletion src/coreclr/tryrun.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/armv6-alpine-linux-musleabihf OR
SET(ALPINE_LINUX 1)
else()
SET(ALPINE_LINUX 0)
if(EXISTS ${CROSS_ROOTFS}/bin/freebsd-version)
set(FREEBSD 1)
set(CMAKE_SYSTEM_NAME FreeBSD)
set(CLR_CMAKE_TARGET_OS FreeBSD)
else()
SET(FREEBSD 0)
endif()
endif()

if(TARGET_ARCH_NAME MATCHES "^(armel|arm|arm64|x86)$")
if(TARGET_ARCH_NAME MATCHES "^(armel|arm|arm64|x86)$" OR FREEBSD)
set_cache_value(FILE_OPS_CHECK_FERROR_OF_PREVIOUS_CALL_EXITCODE 1)
set_cache_value(GETPWUID_R_SETS_ERRNO_EXITCODE 0)
set_cache_value(HAS_POSIX_SEMAPHORES_EXITCODE 0)
Expand Down Expand Up @@ -57,6 +64,16 @@ if(TARGET_ARCH_NAME MATCHES "^(armel|arm|arm64|x86)$")
set_cache_value(UNGETC_NOT_RETURN_EOF_EXITCODE 0)
endif()

if (FREEBSD)
set_cache_value(HAVE_BROKEN_FIFO_KEVENT_EXITCODE 1)
set_cache_value(HAVE_PROCFS_MAPS 0)
set_cache_value(HAVE_PROCFS_STAT 0)
set_cache_value(HAVE_PROCFS_STATUS 0)
set_cache_value(GETPWUID_R_SETS_ERRNO 0)
set_cache_value(UNGETC_NOT_RETURN_EOF 0)
set_cache_value(HAVE_COMPATIBLE_ILOGBNAN 1)
set_cache_value(HAVE_FUNCTIONAL_PTHREAD_ROBUST_MUTEXES_EXITCODE 0)
endif()
else()
message(FATAL_ERROR "Arch is ${TARGET_ARCH_NAME}. Only armel, arm, arm64 and x86 are supported!")
endif()
Expand Down
2 changes: 1 addition & 1 deletion src/installer/corehost/build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PropertyGroup>
<CMakeBuildDir>$(IntermediateOutputRootPath)corehost\cmake\</CMakeBuildDir>

<BuildArgs>$(Configuration) $(TargetArchitecture) -apphostver "$(AppHostVersion)" -hostver "$(HostVersion)" -fxrver "$(HostResolverVersion)" -policyver "$(HostPolicyVersion)" -commithash "$(LatestCommit)"</BuildArgs>
<BuildArgs>$(Configuration) $(TargetArchitecture) -apphostver "$(AppHostVersion)" -hostver "$(HostVersion)" -fxrver "$(HostResolverVersion)" -policyver "$(HostPolicyVersion)" -commithash "$(LatestCommit)" -os $(TargetOS)</BuildArgs>
<BuildArgs Condition="'$(PortableBuild)' != 'true'">$(BuildArgs) -portablebuild=false</BuildArgs>
<BuildArgs Condition="'$(CrossBuild)' == 'true'">$(BuildArgs) -cross</BuildArgs>
<BuildArgs Condition="'$(Compiler)' != ''">$(BuildArgs) $(Compiler)</BuildArgs>
Expand Down
1 change: 0 additions & 1 deletion src/libraries/Native/Unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ endif ()

if(CLR_CMAKE_TARGET_FREEBSD)
add_definitions(-D_BSD_SOURCE) # required for getline
add_link_options(-fuse-ld=lld)
endif(CLR_CMAKE_TARGET_FREEBSD)

# CLR_ADDITIONAL_LINKER_FLAGS - used for passing additional arguments to linker
Expand Down
9 changes: 4 additions & 5 deletions src/libraries/Native/Unix/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ elseif (CLR_CMAKE_TARGET_IOS)
set(PAL_UNIX_NAME \"IOS\")
elseif (CLR_CMAKE_TARGET_FREEBSD)
set(PAL_UNIX_NAME \"FREEBSD\")
include_directories(SYSTEM /usr/local/include)
set(CMAKE_REQUIRED_INCLUDES /usr/local/include)
include_directories(SYSTEM ${CROSS_ROOTFS}/usr/local/include)
set(CMAKE_REQUIRED_INCLUDES ${CROSS_ROOTFS}/usr/local/include)
elseif (CLR_CMAKE_TARGET_NETBSD)
set(PAL_UNIX_NAME \"NETBSD\")
elseif (CLR_CMAKE_TARGET_ARCH_WASM)
Expand Down Expand Up @@ -728,12 +728,11 @@ check_symbol_exists(

if(CLR_CMAKE_TARGET_IOS)
set(HAVE_IOS_NET_ROUTE_H 1)
set(NET_ROUTE_H_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/System.Native/ios/net/route.h")
set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h "${CMAKE_CURRENT_SOURCE_DIR}/System.Native/ios/net/route.h")
else()
set(NET_ROUTE_H_INCLUDE net/route.h)
set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h net/if.h net/route.h)
endif()

set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h ${NET_ROUTE_H_INCLUDE})
check_type_size(
"struct rt_msghdr"
HAVE_RT_MSGHDR
Expand Down
16 changes: 15 additions & 1 deletion src/libraries/Native/Unix/tryrun.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set(TARGET_ARCH_NAME $ENV{TARGET_BUILD_ARCH})
set(CROSS_ROOTFS $ENV{ROOTFS_DIR})

macro(set_cache_value)
set(${ARGV0} ${ARGV1} CACHE STRING "Result from TRY_RUN" FORCE)
Expand All @@ -9,11 +10,19 @@ if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/armv6-alpine-linux-musleabihf OR
EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/aarch64-alpine-linux-musl)

SET(ALPINE_LINUX 1)
SET(FREEBSD 0)
else()
SET(ALPINE_LINUX 0)
if(EXISTS ${CROSS_ROOTFS}/bin/freebsd-version)
set(FREEBSD 1)
set(CMAKE_SYSTEM_NAME FreeBSD)
set(CLR_CMAKE_TARGET_OS FreeBSD)
else()
SET(FREEBSD 0)
endif()
endif()

if(TARGET_ARCH_NAME MATCHES "^(armel|arm|arm64|x86)$")
if(TARGET_ARCH_NAME MATCHES "^(armel|arm|arm64|x86)$" OR FREEBSD)
set_cache_value(HAVE_CLOCK_MONOTONIC_EXITCODE 0)
set_cache_value(HAVE_CLOCK_REALTIME_EXITCODE 0)

Expand All @@ -22,6 +31,11 @@ if(TARGET_ARCH_NAME MATCHES "^(armel|arm|arm64|x86)$")
else()
set_cache_value(HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP_EXITCODE 0)
endif()
if (FREEBSD)
set_cache_value(HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP 1)
set_cache_value(HAVE_CLOCK_MONOTONIC 1)
set_cache_value(HAVE_CLOCK_REALTIME 1)
endif()
else()
message(FATAL_ERROR "Arch is ${TARGET_ARCH_NAME}. Only armel, arm, arm64 and x86 are supported!")
endif()

0 comments on commit 595a95c

Please sign in to comment.