Skip to content

Partial FreeBSD Support #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Dec 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 4 additions & 1 deletion lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const std::vector<std::string> LangOptions::SupportedOSBuildConfigArguments = {
"tvOS",
"watchOS",
"iOS",
"Linux"
"Linux",
"FreeBSD"
};

const std::vector<std::string> LangOptions::SupportedArchBuildConfigArguments = {
Expand Down Expand Up @@ -98,6 +99,8 @@ std::pair<bool, bool> 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;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Basic/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@ StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
if (triple.isOSLinux())
return "linux";

if (triple.isOSFreeBSD())
return "freebsd";

return "";
}
2 changes: 2 additions & 0 deletions lib/Driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ set(swiftDriver_sources
Types.cpp
)

set(swiftDriver_targetDefines)

add_swift_library(swiftDriver
${swiftDriver_sources}
DEPENDS SwiftOptions
Expand Down
5 changes: 4 additions & 1 deletion lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just let this fall through instead of duplicating code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed via 109d8e6, thanks!

TC = new toolchains::GenericUnix(*this, Target);
break;
default:
TC = nullptr;
Expand Down
12 changes: 6 additions & 6 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 &&
Expand Down
7 changes: 4 additions & 3 deletions lib/Driver/ToolChains.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,12 +46,13 @@ 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
} // end namespace driver
} // end namespace swift

#endif

6 changes: 5 additions & 1 deletion stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/runtime/Casting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2306,7 +2306,7 @@ namespace {
#if __LP64__
# if __APPLE__
return Data > 0xFFFFFFFFU;
# elif __linux__
# elif __linux__ || __FreeBSD__
return Data > 0x0FFFU;
# else
# error "port me"
Expand Down
5 changes: 5 additions & 0 deletions stdlib/public/stubs/LibcShims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void *>(ptr));
}
#elif defined(__FreeBSD__)
#include <malloc_np.h>
size_t _swift_stdlib_malloc_size(const void *ptr) {
return malloc_usable_size(const_cast<void *>(ptr));
}
#else
#error No malloc_size analog known for this platform/libc.
#endif
Expand Down
5 changes: 5 additions & 0 deletions stdlib/public/stubs/Stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
//
//===----------------------------------------------------------------------===//

#if defined(__FreeBSD__)
#define _WITH_GETLINE
#endif

#include <sys/resource.h>
#include <sys/errno.h>
#include <unistd.h>
#include <climits>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
Expand Down
8 changes: 8 additions & 0 deletions test/BuildConfigurations/x64FreeBSDTarget.swift
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down