Skip to content

Commit 43dc209

Browse files
committed
Fix build on FreeBSD
1 parent db35cab commit 43dc209

File tree

79 files changed

+528
-88
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+528
-88
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL Windows AND NOT CMAKE_HOST_SYSTEM_NAME STREQUA
177177
set(SWIFT_USE_LINKER_default "lld")
178178
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
179179
set(SWIFT_USE_LINKER_default "")
180+
elseif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
181+
set(SWIFT_USE_LINKER_default "")
180182
else()
181183
set(SWIFT_USE_LINKER_default "gold")
182184
endif()

cmake/modules/SwiftConfigureSDK.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,11 @@ macro(configure_sdk_unix name architectures)
347347
message(FATAL_ERROR "unsupported arch for FreeBSD: ${arch}")
348348
endif()
349349

350-
if(CMAKE_HOST_SYSTEM_NAME NOT STREQUAL FreeBSD)
350+
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL FreeBSD)
351351
message(WARNING "CMAKE_SYSTEM_VERSION will not match target")
352352
endif()
353353

354-
string(REPLACE "[-].*" "" freebsd_system_version ${CMAKE_SYSTEM_VERSION})
354+
string(REGEX REPLACE "[-].*" "" freebsd_system_version ${CMAKE_SYSTEM_VERSION})
355355
message(STATUS "FreeBSD Version: ${freebsd_system_version}")
356356

357357
set(SWIFT_SDK_FREEBSD_ARCH_x86_64_TRIPLE "x86_64-unknown-freebsd${freebsd_system_version}")

include/swift/AST/AutoDiff.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,14 @@ class DerivativeFunctionTypeError
416416
Kind kind;
417417

418418
/// The type and index of a differentiability parameter or result.
419-
using TypeAndIndex = std::pair<Type, unsigned>;
419+
/// std::pair does not have a trivial copy constructor on FreeBSD for ABI reasons,
420+
/// so we have to define our own type here instead
421+
struct TypeAndIndex {
422+
Type first;
423+
unsigned second;
424+
425+
TypeAndIndex(Type type, unsigned index) : first(type), second(index) {}
426+
};
420427

421428
private:
422429
union Value {

include/swift/AST/PlatformKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ AVAILABILITY_PLATFORM(watchOSApplicationExtension, "application extensions for w
3232
AVAILABILITY_PLATFORM(macOSApplicationExtension, "application extensions for macOS")
3333
AVAILABILITY_PLATFORM(macCatalyst, "Mac Catalyst")
3434
AVAILABILITY_PLATFORM(macCatalystApplicationExtension, "application extensions for Mac Catalyst")
35+
AVAILABILITY_PLATFORM(FreeBSD, "FreeBSD")
3536
AVAILABILITY_PLATFORM(OpenBSD, "OpenBSD")
3637
AVAILABILITY_PLATFORM(Windows, "Windows")
3738

include/swift/SILOptimizer/Differentiation/DifferentiationInvoker.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,15 @@ struct DifferentiationInvoker {
7171

7272
/// The parent `apply` instruction and the witness associated with the
7373
/// `IndirectDifferentiation` case.
74-
std::pair<ApplyInst *, SILDifferentiabilityWitness *>
75-
indirectDifferentiation;
74+
///
75+
/// Note: This used to be a std::pair, but on FreeBSD std::pair can't be
76+
/// used here, because it does not have a trivial copy constructor.
77+
struct IndirectDifferentiation {
78+
ApplyInst *applyInst;
79+
SILDifferentiabilityWitness *witness;
80+
};
81+
IndirectDifferentiation indirectDifferentiation;
82+
7683
Value(ApplyInst *applyInst, SILDifferentiabilityWitness *witness)
7784
: indirectDifferentiation({applyInst, witness}) {}
7885

@@ -111,7 +118,8 @@ struct DifferentiationInvoker {
111118
std::pair<ApplyInst *, SILDifferentiabilityWitness *>
112119
getIndirectDifferentiation() const {
113120
assert(kind == Kind::IndirectDifferentiation);
114-
return value.indirectDifferentiation;
121+
return std::make_pair(value.indirectDifferentiation.applyInst,
122+
value.indirectDifferentiation.witness);
115123
}
116124

117125
SILDifferentiabilityWitness *getSILDifferentiabilityWitnessInvoker() const {

lib/AST/PlatformKind.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ static bool isPlatformActiveForTarget(PlatformKind Platform,
8989
return Target.isWatchOS();
9090
case PlatformKind::OpenBSD:
9191
return Target.isOSOpenBSD();
92+
case PlatformKind::FreeBSD:
93+
return Target.isOSFreeBSD();
9294
case PlatformKind::Windows:
9395
return Target.isOSWindows();
9496
case PlatformKind::none:

lib/AST/Type.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5489,7 +5489,8 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
54895489
if (!resultTan) {
54905490
return llvm::make_error<DerivativeFunctionTypeError>(
54915491
this, DerivativeFunctionTypeError::Kind::NonDifferentiableResult,
5492-
std::make_pair(originalResultType, /*index*/ 0));
5492+
DerivativeFunctionTypeError::TypeAndIndex(
5493+
originalResultType, /*index*/ 0));
54935494
}
54945495
auto resultTanType = resultTan->getType();
54955496

@@ -5522,7 +5523,8 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
55225523
this,
55235524
DerivativeFunctionTypeError::Kind::
55245525
NonDifferentiableDifferentiabilityParameter,
5525-
std::make_pair(paramType, i));
5526+
DerivativeFunctionTypeError::TypeAndIndex(
5527+
paramType, 0));
55265528
}
55275529
differentialParams.push_back(AnyFunctionType::Param(
55285530
paramTan->getType(), Identifier(), diffParam.getParameterFlags()));
@@ -5563,7 +5565,7 @@ AnyFunctionType::getAutoDiffDerivativeFunctionLinearMapType(
55635565
this,
55645566
DerivativeFunctionTypeError::Kind::
55655567
NonDifferentiableDifferentiabilityParameter,
5566-
std::make_pair(paramType, i));
5568+
DerivativeFunctionTypeError::TypeAndIndex(paramType, i));
55675569
}
55685570
if (diffParam.isInOut()) {
55695571
hasInoutDiffParameter = true;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ importer::getNormalInvocationArguments(
622622
// using Glibc or a libc that respects that flag. This will cause some
623623
// source breakage however (specifically with strerror_r()) on Linux
624624
// without a workaround.
625-
if (triple.isOSFuchsia() || triple.isAndroid()) {
625+
if (triple.isOSFuchsia() || triple.isAndroid() || triple.isOSFreeBSD()) {
626626
// Many of the modern libc features are hidden behind feature macros like
627627
// _GNU_SOURCE or _XOPEN_SOURCE.
628628
invocationArgStrs.insert(invocationArgStrs.end(), {
@@ -658,7 +658,7 @@ importer::getNormalInvocationArguments(
658658
}
659659
}
660660

661-
if (searchPathOpts.SDKPath.empty()) {
661+
if (searchPathOpts.SDKPath.empty() && !triple.isOSFreeBSD()) {
662662
invocationArgStrs.push_back("-Xclang");
663663
invocationArgStrs.push_back("-nostdsysteminc");
664664
} else {
@@ -2069,6 +2069,10 @@ PlatformAvailability::PlatformAvailability(const LangOptions &langOpts)
20692069
deprecatedAsUnavailableMessage = "";
20702070
break;
20712071

2072+
case PlatformKind::FreeBSD:
2073+
deprecatedAsUnavailableMessage = "";
2074+
break;
2075+
20722076
case PlatformKind::Windows:
20732077
deprecatedAsUnavailableMessage = "";
20742078
break;
@@ -2108,6 +2112,9 @@ bool PlatformAvailability::isPlatformRelevant(StringRef name) const {
21082112
case PlatformKind::OpenBSD:
21092113
return name == "openbsd";
21102114

2115+
case PlatformKind::FreeBSD:
2116+
return name == "freebsd";
2117+
21112118
case PlatformKind::Windows:
21122119
return name == "windows";
21132120

@@ -2175,6 +2182,10 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
21752182
// No deprecation filter on OpenBSD
21762183
return false;
21772184

2185+
case PlatformKind::FreeBSD:
2186+
// No deprecation filter on FreeBSD
2187+
return false;
2188+
21782189
case PlatformKind::Windows:
21792190
// No deprecation filter on Windows
21802191
return false;

lib/Driver/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ Driver::buildToolChain(const llvm::opt::InputArgList &ArgList) {
342342
return std::make_unique<toolchains::Android>(*this, target);
343343
return std::make_unique<toolchains::GenericUnix>(*this, target);
344344
case llvm::Triple::FreeBSD:
345-
return std::make_unique<toolchains::GenericUnix>(*this, target);
345+
return std::make_unique<toolchains::FreeBSD>(*this, target);
346346
case llvm::Triple::OpenBSD:
347347
return std::make_unique<toolchains::OpenBSD>(*this, target);
348348
case llvm::Triple::Win32:

lib/Driver/ToolChains.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ class LLVM_LIBRARY_VISIBILITY OpenBSD : public GenericUnix {
173173
~OpenBSD() = default;
174174
};
175175

176+
class LLVM_LIBRARY_VISIBILITY FreeBSD : public GenericUnix {
177+
protected:
178+
std::string getDefaultLinker() const override;
179+
180+
public:
181+
FreeBSD(const Driver &D, const llvm::Triple &Triple)
182+
: GenericUnix(D, Triple) {}
183+
~FreeBSD() = default;
184+
};
185+
176186
} // end namespace toolchains
177187
} // end namespace driver
178188
} // end namespace swift

lib/Driver/UnixToolChains.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,7 @@ std::string toolchains::Cygwin::getDefaultLinker() const {
410410
std::string toolchains::OpenBSD::getDefaultLinker() const {
411411
return "lld";
412412
}
413+
414+
std::string toolchains::FreeBSD::getDefaultLinker() const {
415+
return "lld";
416+
}

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,8 @@ static bool shouldImportConcurrencyByDefault(const llvm::Triple &target) {
775775
#if SWIFT_IMPLICIT_CONCURRENCY_IMPORT
776776
if (target.isOSOpenBSD())
777777
return true;
778+
if (target.isOSFreeBSD())
779+
return true;
778780
#endif
779781
return false;
780782
}

lib/Option/SanitizerOptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ OptionSet<SanitizerKind> swift::parseSanitizerArgValues(
162162
}
163163

164164
// Check that we're one of the known supported targets for sanitizers.
165-
if (!(Triple.isOSDarwin() || Triple.isOSLinux() || Triple.isOSWindows())) {
165+
if (!(Triple.isOSDarwin() || Triple.isOSLinux() || Triple.isOSWindows() || Triple.isOSFreeBSD())) {
166166
SmallString<128> b;
167167
Diags.diagnose(SourceLoc(), diag::error_unsupported_opt_for_target,
168168
(A->getOption().getPrefixedName() +

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,9 @@ class DeclAndTypePrinter::Implementation
875875
case PlatformKind::watchOSApplicationExtension:
876876
plat = "watchos_app_extension";
877877
break;
878+
case PlatformKind::FreeBSD:
879+
plat = "freebsd";
880+
break;
878881
case PlatformKind::OpenBSD:
879882
plat = "openbsd";
880883
break;

lib/SymbolGraphGen/AvailabilityMixin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ StringRef getDomain(const AvailableAttr &AvAttr) {
5656
return { "tvOSAppExtension" };
5757
case swift::PlatformKind::watchOSApplicationExtension:
5858
return { "watchOSAppExtension" };
59+
case swift::PlatformKind::FreeBSD:
60+
return { "FreeBSD" };
5961
case swift::PlatformKind::OpenBSD:
6062
return { "OpenBSD" };
6163
case swift::PlatformKind::Windows:

lib/TBDGen/TBDGen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ getLinkerPlatformId(OriginallyDefinedInAttr::ActiveVersion Ver) {
238238
switch(Ver.Platform) {
239239
case swift::PlatformKind::none:
240240
llvm_unreachable("cannot find platform kind");
241+
case swift::PlatformKind::FreeBSD:
242+
llvm_unreachable("not used for this platform");
241243
case swift::PlatformKind::OpenBSD:
242244
llvm_unreachable("not used for this platform");
243245
case swift::PlatformKind::Windows:

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ function(_add_target_variant_c_compile_flags)
290290
endif()
291291
endif()
292292

293+
if("${CFLAGS_SDK}" STREQUAL "FREEBSD")
294+
list(APPEND result "-D_GNU_SOURCE")
295+
endif()
296+
293297
if("${CFLAGS_SDK}" STREQUAL "WASI")
294298
list(APPEND result "-D_WASI_EMULATED_MMAN")
295299
endif()
@@ -976,6 +980,10 @@ function(_add_swift_target_library_single target name)
976980
set_target_properties("${target}"
977981
PROPERTIES
978982
INSTALL_RPATH "$ORIGIN")
983+
elseif("${SWIFTLIB_SINGLE_SDK}" STREQUAL "FREEBSD")
984+
set_target_properties("${target}"
985+
PROPERTIES
986+
INSTALL_RPATH "$ORIGIN")
979987
elseif("${SWIFTLIB_SINGLE_SDK}" STREQUAL "CYGWIN")
980988
set_target_properties("${target}"
981989
PROPERTIES

stdlib/private/OSLog/OSLogFloatFormatting.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ extension OSLogFloatFormatting {
350350
// fprintf formatters promote Float to Double
351351
case is Float.Type: return ""
352352
case is Double.Type: return ""
353-
#if !os(Windows) && (arch(i386) || arch(x86_64))
353+
#if !(os(Windows) || os(FreeBSD)) && (arch(i386) || arch(x86_64))
354354
// fprintf formatters use L for Float80
355355
case is Float80.Type: return "L"
356356
#endif

stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public func getFloat32(_ x: Float32) -> Float32 { return _opaqueIdentity(x) }
7777
@inline(never)
7878
public func getFloat64(_ x: Float64) -> Float64 { return _opaqueIdentity(x) }
7979

80-
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
80+
#if !(os(Windows) || os(Android) || os(FreeBSD)) && (arch(i386) || arch(x86_64))
8181
@inline(never)
8282
public func getFloat80(_ x: Float80) -> Float80 { return _opaqueIdentity(x) }
8383
#endif

stdlib/private/StdlibUnittest/SymbolLookup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#error("Unsupported platform")
2222
#endif
2323

24-
#if canImport(Darwin) || os(OpenBSD)
24+
#if canImport(Darwin) || os(FreeBSD) || os(OpenBSD)
2525
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
2626
#elseif os(Linux)
2727
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: 0)

stdlib/public/Differentiation/FloatingPointDifferentiation.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def Availability(bits):
2626
}%
2727

2828
% if bits == 80:
29-
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
29+
#if !(os(Windows) || os(Android) || os(FreeBSD)) && (arch(i386) || arch(x86_64))
3030
% end
3131
% if bits == 16:
3232
#if !os(macOS) && !(os(iOS) && targetEnvironment(macCatalyst))

stdlib/public/Differentiation/TgmathDerivatives.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func _${derivative_kind}Trunc<T: FloatingPoint & Differentiable> (
135135
% linear_map_kind = 'differential' if derivative_kind == 'jvp' else 'pullback'
136136
% for T in ['Float', 'Double', 'Float80']:
137137
% if T == 'Float80':
138-
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
138+
#if !(os(Windows) || os(Android) || os(FreeBSD)) && (arch(i386) || arch(x86_64))
139139
% end
140140
@inlinable
141141
@derivative(of: exp)
@@ -276,7 +276,7 @@ func _${derivative_kind}Erfc(_ x: ${T}) -> (value: ${T}, ${linear_map_kind}: (${
276276
// Binary functions
277277
%for T in ['Float', 'Float80']:
278278
% if T == 'Float80':
279-
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
279+
#if !(os(Windows) || os(Android) || os(FreeBSD)) && (arch(i386) || arch(x86_64))
280280
% end
281281
@inlinable
282282
@derivative(of: pow)

stdlib/public/Platform/CMakeLists.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ foreach(sdk ${SWIFT_SDKS})
8484
set(glibc_modulemap_source "bionic.modulemap.gyb")
8585
elseif(${sdk} STREQUAL OPENBSD)
8686
set(glibc_modulemap_source "libc-openbsd.modulemap.gyb")
87+
elseif(${sdk} STREQUAL FREEBSD)
88+
set(glibc_modulemap_source "libc-freebsd.modulemap.gyb")
8789
else()
8890
set(glibc_modulemap_source "glibc.modulemap.gyb")
8991
endif()
@@ -119,12 +121,16 @@ foreach(sdk ${SWIFT_SDKS})
119121
list(APPEND glibc_modulemap_target_list ${copy_glibc_modulemap_static})
120122
endif()
121123

122-
set(glibc_header_out "${module_dir}/SwiftGlibc.h")
123-
handle_gyb_source_single(glibc_header_target
124-
SOURCE "SwiftGlibc.h.gyb"
125-
OUTPUT "${glibc_header_out}"
126-
FLAGS "-DCMAKE_SDK=${sdk}")
127-
list(APPEND glibc_modulemap_target_list ${glibc_header_target})
124+
# FreeBSD uses a different module map that does not use this header,
125+
# so we don't generate it
126+
if(NOT ${sdk} STREQUAL FREEBSD)
127+
set(glibc_header_out "${module_dir}/SwiftGlibc.h")
128+
handle_gyb_source_single(glibc_header_target
129+
SOURCE "SwiftGlibc.h.gyb"
130+
OUTPUT "${glibc_header_out}"
131+
FLAGS "-DCMAKE_SDK=${sdk}")
132+
list(APPEND glibc_modulemap_target_list ${glibc_header_target})
133+
endif()
128134

129135
# If this SDK is a target for a non-native host, except if it's for Android
130136
# with its own native sysroot, create a native modulemap without a sysroot

stdlib/public/Platform/Glibc.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public let FLT_RADIX = Double.radix
3636

3737
%for type, prefix in [('Float', 'FLT'), ('Double', 'DBL'), ('Float80', 'LDBL')]:
3838
% if type == "Float80":
39-
#if !os(Android) && (arch(i386) || arch(x86_64))
39+
#if !(os(Android) || os(FreeBSD)) && (arch(i386) || arch(x86_64))
4040
% end
4141
// Where does the 1 come from? C counts the usually-implicit leading
4242
// significand bit, but Swift does not. Neither is really right or wrong.

0 commit comments

Comments
 (0)