diff --git a/CMakeLists.txt b/CMakeLists.txt index 22ef3dcaa71c7..b53eea5f9ae52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -397,6 +397,19 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") set(SWIFT_PRIMARY_VARIANT_SDK_default "LINUX") set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64") +elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD") + configure_sdk_unix(FREEBSD "FreeBSD" "freebsd" "freebsd" "x86_64" "x86_64-freebsd10") + + set(CMAKE_EXECUTABLE_FORMAT "ELF") + + set(SWIFT_HOST_VARIANT "freebsd" CACHE STRING + "Deployment OS for Swift host tools (the compiler) [freebsd].") + + set(SWIFT_HOST_VARIANT_SDK "FREEBSD") + set(SWIFT_HOST_VARIANT_ARCH "x86_64") + + set(SWIFT_PRIMARY_VARIANT_SDK_default "FREEBSD") + set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64") elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") # Set defaults. diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 38df30a51872e..c523924b2935c 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -151,6 +151,8 @@ function(_add_variant_link_flags if("${sdk}" STREQUAL "LINUX") list(APPEND result "-lpthread" "-ldl") + elseif("${sdk}" STREQUAL "FREEBSD") + # No extra libraries required. else() list(APPEND result "-lobjc") endif() diff --git a/include/swift/Basic/LangOptions.h b/include/swift/Basic/LangOptions.h index 66dae000800ad..d7bdd65dea672 100644 --- a/include/swift/Basic/LangOptions.h +++ b/include/swift/Basic/LangOptions.h @@ -173,7 +173,9 @@ namespace swift { Target.getiOSVersion(major, minor, revision); } else if (Target.isWatchOS()) { Target.getOSVersion(major, minor, revision); - } else if (Target.isOSLinux() || Target.getTriple().empty()) { + } else if (Target.isOSLinux() || Target.isOSFreeBSD() || + Target.getTriple().empty()) + { major = minor = revision = 0; } else { llvm_unreachable("Unsupported target OS"); diff --git a/lib/Basic/LangOptions.cpp b/lib/Basic/LangOptions.cpp index 4ad980cbb4e40..ae8c69d125368 100644 --- a/lib/Basic/LangOptions.cpp +++ b/lib/Basic/LangOptions.cpp @@ -28,7 +28,8 @@ const std::vector LangOptions::SupportedOSBuildConfigArguments = { "tvOS", "watchOS", "iOS", - "Linux" + "Linux", + "FreeBSD" }; const std::vector LangOptions::SupportedArchBuildConfigArguments = { @@ -98,6 +99,8 @@ std::pair LangOptions::setTarget(llvm::Triple triple) { addTargetConfigOption("os", "iOS"); else if (triple.isOSLinux()) addTargetConfigOption("os", "Linux"); + else if (triple.isOSFreeBSD()) + addTargetConfigOption("os", "FreeBSD"); else { UnsupportedOS = true; } diff --git a/lib/Basic/Platform.cpp b/lib/Basic/Platform.cpp index a4808d1db918b..a620f9e9e13af 100644 --- a/lib/Basic/Platform.cpp +++ b/lib/Basic/Platform.cpp @@ -64,5 +64,8 @@ StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) { if (triple.isOSLinux()) return "linux"; + if (triple.isOSFreeBSD()) + return "freebsd"; + return ""; } diff --git a/lib/Driver/CMakeLists.txt b/lib/Driver/CMakeLists.txt index 10e1de08f7a57..2bc7e9b785986 100644 --- a/lib/Driver/CMakeLists.txt +++ b/lib/Driver/CMakeLists.txt @@ -12,6 +12,8 @@ set(swiftDriver_sources Types.cpp ) +set(swiftDriver_targetDefines) + add_swift_library(swiftDriver ${swiftDriver_sources} DEPENDS SwiftOptions diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index 7dfe5ee6b8789..25601072edcf2 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -2036,7 +2036,10 @@ const ToolChain *Driver::getToolChain(const ArgList &Args) const { TC = new toolchains::Darwin(*this, Target); break; case llvm::Triple::Linux: - TC = new toolchains::Linux(*this, Target); + TC = new toolchains::GenericUnix(*this, Target); + break; + case llvm::Triple::FreeBSD: + TC = new toolchains::GenericUnix(*this, Target); break; default: TC = nullptr; diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index f6e9faa78126f..829e68293cbb3 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -999,8 +999,8 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job, } ToolChain::InvocationInfo -toolchains::Linux::constructInvocation(const InterpretJobAction &job, - const JobContext &context) const { +toolchains::GenericUnix::constructInvocation(const InterpretJobAction &job, + const JobContext &context) const { InvocationInfo II = ToolChain::constructInvocation(job, context); SmallString<128> runtimeLibraryPath; @@ -1014,8 +1014,8 @@ toolchains::Linux::constructInvocation(const InterpretJobAction &job, ToolChain::InvocationInfo -toolchains::Linux::constructInvocation(const AutolinkExtractJobAction &job, - const JobContext &context) const { +toolchains::GenericUnix::constructInvocation(const AutolinkExtractJobAction &job, + const JobContext &context) const { assert(context.Output.getPrimaryOutputType() == types::TY_AutolinkFile); ArgStringList Arguments; @@ -1030,8 +1030,8 @@ toolchains::Linux::constructInvocation(const AutolinkExtractJobAction &job, } ToolChain::InvocationInfo -toolchains::Linux::constructInvocation(const LinkJobAction &job, - const JobContext &context) const { +toolchains::GenericUnix::constructInvocation(const LinkJobAction &job, + const JobContext &context) const { const Driver &D = getDriver(); assert(context.Output.getPrimaryOutputType() == types::TY_Image && diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h index dcda776468b83..7bc32e8f48452 100644 --- a/lib/Driver/ToolChains.h +++ b/lib/Driver/ToolChains.h @@ -36,7 +36,7 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain { }; -class LLVM_LIBRARY_VISIBILITY Linux : public ToolChain { +class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain { protected: InvocationInfo constructInvocation(const InterpretJobAction &job, const JobContext &context) const override; @@ -46,8 +46,8 @@ class LLVM_LIBRARY_VISIBILITY Linux : public ToolChain { const JobContext &context) const override; public: - Linux(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {} - ~Linux() = default; + GenericUnix(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {} + ~GenericUnix() = default; }; } // end namespace toolchains @@ -55,3 +55,4 @@ class LLVM_LIBRARY_VISIBILITY Linux : public ToolChain { } // end namespace swift #endif + diff --git a/stdlib/public/core/CMakeLists.txt b/stdlib/public/core/CMakeLists.txt index 456857aa39413..5e44d5c14e69c 100644 --- a/stdlib/public/core/CMakeLists.txt +++ b/stdlib/public/core/CMakeLists.txt @@ -154,9 +154,13 @@ else() # -Wl,--whole-archive swiftRuntime -Wl,--no-whole-archive) list(APPEND swift_core_private_link_libraries swiftRuntime swiftStdlibStubs) find_package(ICU REQUIRED COMPONENTS uc i18n) + list(APPEND swift_core_private_link_libraries + ${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY}) +endif() + +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") find_package(BSD REQUIRED) list(APPEND swift_core_private_link_libraries - ${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY} ${BSD_LIBRARIES}) endif() diff --git a/stdlib/public/runtime/CMakeLists.txt b/stdlib/public/runtime/CMakeLists.txt index 2decd7e230050..c3fbdc7614359 100644 --- a/stdlib/public/runtime/CMakeLists.txt +++ b/stdlib/public/runtime/CMakeLists.txt @@ -72,7 +72,7 @@ add_swift_library(swiftRuntime IS_STDLIB IS_STDLIB_CORE INSTALL_IN_COMPONENT stdlib) foreach(sdk ${SWIFT_CONFIGURED_SDKS}) - if("${sdk}" STREQUAL "LINUX") + if("${sdk}" STREQUAL "LINUX" OR "${sdk}" STREQUAL "FREEBSD") foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES}) set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}") diff --git a/stdlib/public/runtime/Casting.cpp b/stdlib/public/runtime/Casting.cpp index 34666e29110e8..48ea259ef21ca 100644 --- a/stdlib/public/runtime/Casting.cpp +++ b/stdlib/public/runtime/Casting.cpp @@ -2271,7 +2271,7 @@ namespace { # if __APPLE__ assert((!Success && Data <= 0xFFFFFFFFU) || (Success && Data > 0xFFFFFFFFU)); -# elif __linux__ +# elif __linux__ || __FreeBSD__ assert((!Success && Data <= 0x0FFFU) || (Success && Data > 0x0FFFU)); # else @@ -2306,7 +2306,7 @@ namespace { #if __LP64__ # if __APPLE__ return Data > 0xFFFFFFFFU; -# elif __linux__ +# elif __linux__ || __FreeBSD__ return Data > 0x0FFFU; # else # error "port me" diff --git a/stdlib/public/stubs/LibcShims.cpp b/stdlib/public/stubs/LibcShims.cpp index b29fd6507247d..13d9d74a9f9fc 100644 --- a/stdlib/public/stubs/LibcShims.cpp +++ b/stdlib/public/stubs/LibcShims.cpp @@ -55,6 +55,11 @@ size_t _swift_stdlib_malloc_size(const void *ptr) { return malloc_size(ptr); } size_t _swift_stdlib_malloc_size(const void *ptr) { return malloc_usable_size(const_cast(ptr)); } +#elif defined(__FreeBSD__) +#include +size_t _swift_stdlib_malloc_size(const void *ptr) { + return malloc_usable_size(const_cast(ptr)); +} #else #error No malloc_size analog known for this platform/libc. #endif diff --git a/stdlib/public/stubs/Stubs.cpp b/stdlib/public/stubs/Stubs.cpp index c1307fd12ee17..336f352010707 100644 --- a/stdlib/public/stubs/Stubs.cpp +++ b/stdlib/public/stubs/Stubs.cpp @@ -16,10 +16,15 @@ // //===----------------------------------------------------------------------===// +#if defined(__FreeBSD__) +#define _WITH_GETLINE +#endif + #include #include #include #include +#include #include #include #include diff --git a/test/BuildConfigurations/x64FreeBSDTarget.swift b/test/BuildConfigurations/x64FreeBSDTarget.swift new file mode 100644 index 0000000000000..284060fa6eb49 --- /dev/null +++ b/test/BuildConfigurations/x64FreeBSDTarget.swift @@ -0,0 +1,8 @@ +// RUN: %swift -parse %s -verify -D FOO -D BAR -target x86_64-unknown-freebsd10 -disable-objc-interop -D FOO -parse-stdlib +// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-freebsd10 + +#if arch(x86_64) && os(FreeBSD) && _runtime(_Native) +class C {} +var x = C() +#endif +var y = x diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d5b74f0470da7..6bc8ed31d26ab 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -56,7 +56,8 @@ function(get_test_dependencies SDK result_var_name) ("${SDK}" STREQUAL "IOS_SIMULATOR") OR ("${SDK}" STREQUAL "TVOS_SIMULATOR") OR ("${SDK}" STREQUAL "WATCHOS_SIMULATOR") OR - ("${SDK}" STREQUAL "LINUX")) + ("${SDK}" STREQUAL "LINUX") OR + ("${SDK}" STREQUAL "FREEBSD")) # No extra dependencies. else() message(FATAL_ERROR "Unknown SDK: ${SDK}")