Skip to content

Commit 16de5d1

Browse files
committed
Merge pull request #203 from landonf/landonf/freebsd-patchset-1
Partial FreeBSD Support
2 parents 884df46 + c4daece commit 16de5d1

File tree

16 files changed

+69
-17
lines changed

16 files changed

+69
-17
lines changed

Diff for: CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,19 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
397397

398398
set(SWIFT_PRIMARY_VARIANT_SDK_default "LINUX")
399399
set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64")
400+
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
401+
configure_sdk_unix(FREEBSD "FreeBSD" "freebsd" "freebsd" "x86_64" "x86_64-freebsd10")
402+
403+
set(CMAKE_EXECUTABLE_FORMAT "ELF")
404+
405+
set(SWIFT_HOST_VARIANT "freebsd" CACHE STRING
406+
"Deployment OS for Swift host tools (the compiler) [freebsd].")
407+
408+
set(SWIFT_HOST_VARIANT_SDK "FREEBSD")
409+
set(SWIFT_HOST_VARIANT_ARCH "x86_64")
410+
411+
set(SWIFT_PRIMARY_VARIANT_SDK_default "FREEBSD")
412+
set(SWIFT_PRIMARY_VARIANT_ARCH_default "x86_64")
400413
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
401414
# Set defaults.
402415

Diff for: cmake/modules/AddSwift.cmake

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ function(_add_variant_link_flags
151151

152152
if("${sdk}" STREQUAL "LINUX")
153153
list(APPEND result "-lpthread" "-ldl")
154+
elseif("${sdk}" STREQUAL "FREEBSD")
155+
# No extra libraries required.
154156
else()
155157
list(APPEND result "-lobjc")
156158
endif()

Diff for: include/swift/Basic/LangOptions.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ namespace swift {
173173
Target.getiOSVersion(major, minor, revision);
174174
} else if (Target.isWatchOS()) {
175175
Target.getOSVersion(major, minor, revision);
176-
} else if (Target.isOSLinux() || Target.getTriple().empty()) {
176+
} else if (Target.isOSLinux() || Target.isOSFreeBSD() ||
177+
Target.getTriple().empty())
178+
{
177179
major = minor = revision = 0;
178180
} else {
179181
llvm_unreachable("Unsupported target OS");

Diff for: lib/Basic/LangOptions.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const std::vector<std::string> LangOptions::SupportedOSBuildConfigArguments = {
2828
"tvOS",
2929
"watchOS",
3030
"iOS",
31-
"Linux"
31+
"Linux",
32+
"FreeBSD"
3233
};
3334

3435
const std::vector<std::string> LangOptions::SupportedArchBuildConfigArguments = {
@@ -98,6 +99,8 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
9899
addTargetConfigOption("os", "iOS");
99100
else if (triple.isOSLinux())
100101
addTargetConfigOption("os", "Linux");
102+
else if (triple.isOSFreeBSD())
103+
addTargetConfigOption("os", "FreeBSD");
101104
else {
102105
UnsupportedOS = true;
103106
}

Diff for: lib/Basic/Platform.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,8 @@ StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
6464
if (triple.isOSLinux())
6565
return "linux";
6666

67+
if (triple.isOSFreeBSD())
68+
return "freebsd";
69+
6770
return "";
6871
}

Diff for: lib/Driver/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ set(swiftDriver_sources
1212
Types.cpp
1313
)
1414

15+
set(swiftDriver_targetDefines)
16+
1517
add_swift_library(swiftDriver
1618
${swiftDriver_sources}
1719
DEPENDS SwiftOptions

Diff for: lib/Driver/Driver.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,10 @@ const ToolChain *Driver::getToolChain(const ArgList &Args) const {
20362036
TC = new toolchains::Darwin(*this, Target);
20372037
break;
20382038
case llvm::Triple::Linux:
2039-
TC = new toolchains::Linux(*this, Target);
2039+
TC = new toolchains::GenericUnix(*this, Target);
2040+
break;
2041+
case llvm::Triple::FreeBSD:
2042+
TC = new toolchains::GenericUnix(*this, Target);
20402043
break;
20412044
default:
20422045
TC = nullptr;

Diff for: lib/Driver/ToolChains.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -999,8 +999,8 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
999999
}
10001000

10011001
ToolChain::InvocationInfo
1002-
toolchains::Linux::constructInvocation(const InterpretJobAction &job,
1003-
const JobContext &context) const {
1002+
toolchains::GenericUnix::constructInvocation(const InterpretJobAction &job,
1003+
const JobContext &context) const {
10041004
InvocationInfo II = ToolChain::constructInvocation(job, context);
10051005

10061006
SmallString<128> runtimeLibraryPath;
@@ -1014,8 +1014,8 @@ toolchains::Linux::constructInvocation(const InterpretJobAction &job,
10141014

10151015

10161016
ToolChain::InvocationInfo
1017-
toolchains::Linux::constructInvocation(const AutolinkExtractJobAction &job,
1018-
const JobContext &context) const {
1017+
toolchains::GenericUnix::constructInvocation(const AutolinkExtractJobAction &job,
1018+
const JobContext &context) const {
10191019
assert(context.Output.getPrimaryOutputType() == types::TY_AutolinkFile);
10201020

10211021
ArgStringList Arguments;
@@ -1030,8 +1030,8 @@ toolchains::Linux::constructInvocation(const AutolinkExtractJobAction &job,
10301030
}
10311031

10321032
ToolChain::InvocationInfo
1033-
toolchains::Linux::constructInvocation(const LinkJobAction &job,
1034-
const JobContext &context) const {
1033+
toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
1034+
const JobContext &context) const {
10351035
const Driver &D = getDriver();
10361036

10371037
assert(context.Output.getPrimaryOutputType() == types::TY_Image &&

Diff for: lib/Driver/ToolChains.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain {
3636

3737
};
3838

39-
class LLVM_LIBRARY_VISIBILITY Linux : public ToolChain {
39+
class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain {
4040
protected:
4141
InvocationInfo constructInvocation(const InterpretJobAction &job,
4242
const JobContext &context) const override;
@@ -46,12 +46,13 @@ class LLVM_LIBRARY_VISIBILITY Linux : public ToolChain {
4646
const JobContext &context) const override;
4747

4848
public:
49-
Linux(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}
50-
~Linux() = default;
49+
GenericUnix(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}
50+
~GenericUnix() = default;
5151
};
5252

5353
} // end namespace toolchains
5454
} // end namespace driver
5555
} // end namespace swift
5656

5757
#endif
58+

Diff for: stdlib/public/core/CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,13 @@ else()
154154
# -Wl,--whole-archive swiftRuntime -Wl,--no-whole-archive)
155155
list(APPEND swift_core_private_link_libraries swiftRuntime swiftStdlibStubs)
156156
find_package(ICU REQUIRED COMPONENTS uc i18n)
157+
list(APPEND swift_core_private_link_libraries
158+
${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY})
159+
endif()
160+
161+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
157162
find_package(BSD REQUIRED)
158163
list(APPEND swift_core_private_link_libraries
159-
${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY}
160164
${BSD_LIBRARIES})
161165
endif()
162166

Diff for: stdlib/public/runtime/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ add_swift_library(swiftRuntime IS_STDLIB IS_STDLIB_CORE
7272
INSTALL_IN_COMPONENT stdlib)
7373

7474
foreach(sdk ${SWIFT_CONFIGURED_SDKS})
75-
if("${sdk}" STREQUAL "LINUX")
75+
if("${sdk}" STREQUAL "LINUX" OR "${sdk}" STREQUAL "FREEBSD")
7676
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
7777
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
7878

Diff for: stdlib/public/runtime/Casting.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,7 @@ namespace {
22712271
# if __APPLE__
22722272
assert((!Success && Data <= 0xFFFFFFFFU) ||
22732273
(Success && Data > 0xFFFFFFFFU));
2274-
# elif __linux__
2274+
# elif __linux__ || __FreeBSD__
22752275
assert((!Success && Data <= 0x0FFFU) ||
22762276
(Success && Data > 0x0FFFU));
22772277
# else
@@ -2306,7 +2306,7 @@ namespace {
23062306
#if __LP64__
23072307
# if __APPLE__
23082308
return Data > 0xFFFFFFFFU;
2309-
# elif __linux__
2309+
# elif __linux__ || __FreeBSD__
23102310
return Data > 0x0FFFU;
23112311
# else
23122312
# error "port me"

Diff for: stdlib/public/stubs/LibcShims.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ size_t _swift_stdlib_malloc_size(const void *ptr) { return malloc_size(ptr); }
5555
size_t _swift_stdlib_malloc_size(const void *ptr) {
5656
return malloc_usable_size(const_cast<void *>(ptr));
5757
}
58+
#elif defined(__FreeBSD__)
59+
#include <malloc_np.h>
60+
size_t _swift_stdlib_malloc_size(const void *ptr) {
61+
return malloc_usable_size(const_cast<void *>(ptr));
62+
}
5863
#else
5964
#error No malloc_size analog known for this platform/libc.
6065
#endif

Diff for: stdlib/public/stubs/Stubs.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616
//
1717
//===----------------------------------------------------------------------===//
1818

19+
#if defined(__FreeBSD__)
20+
#define _WITH_GETLINE
21+
#endif
22+
1923
#include <sys/resource.h>
2024
#include <sys/errno.h>
2125
#include <unistd.h>
2226
#include <climits>
27+
#include <cstdarg>
2328
#include <cstdint>
2429
#include <cstdio>
2530
#include <cstdlib>

Diff for: test/BuildConfigurations/x64FreeBSDTarget.swift

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %swift -parse %s -verify -D FOO -D BAR -target x86_64-unknown-freebsd10 -disable-objc-interop -D FOO -parse-stdlib
2+
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-freebsd10
3+
4+
#if arch(x86_64) && os(FreeBSD) && _runtime(_Native)
5+
class C {}
6+
var x = C()
7+
#endif
8+
var y = x

Diff for: test/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ function(get_test_dependencies SDK result_var_name)
5656
("${SDK}" STREQUAL "IOS_SIMULATOR") OR
5757
("${SDK}" STREQUAL "TVOS_SIMULATOR") OR
5858
("${SDK}" STREQUAL "WATCHOS_SIMULATOR") OR
59-
("${SDK}" STREQUAL "LINUX"))
59+
("${SDK}" STREQUAL "LINUX") OR
60+
("${SDK}" STREQUAL "FREEBSD"))
6061
# No extra dependencies.
6162
else()
6263
message(FATAL_ERROR "Unknown SDK: ${SDK}")

0 commit comments

Comments
 (0)