From a7ec794287f1a48112b5683914d3e7953094660a Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Fri, 4 Dec 2015 12:24:46 -0700 Subject: [PATCH 01/16] Add basic build system and Driver support for FreeBSD targets/hosts. --- CMakeLists.txt | 17 ++++ cmake/modules/AddSwift.cmake | 1 + lib/Driver/CMakeLists.txt | 16 +++- lib/Driver/Driver.cpp | 5 ++ lib/Driver/ToolChains.cpp | 115 +++++++++++++++++++++++++++ lib/Driver/ToolChains.h | 18 +++++ stdlib/public/core/CMakeLists.txt | 6 +- stdlib/public/runtime/CMakeLists.txt | 2 +- test/CMakeLists.txt | 3 +- utils/build-script-impl | 1 + 10 files changed, 178 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30bac316739fd..cc7e765ba1b65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,10 @@ option(SWIFT_ENABLE_TARGET_LINUX "Enable compiler support for targeting Linux" TRUE) +option(SWIFT_ENABLE_TARGET_FREEBSD + "Enable compiler support for targeting FreeBSD" + TRUE) + set(SWIFT_VERSION "2.2" CACHE STRING "The user-visible version of the Swift compiler") set(SWIFT_VENDOR "" CACHE STRING @@ -401,6 +405,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..a379a2f12fdb6 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -151,6 +151,7 @@ function(_add_variant_link_flags if("${sdk}" STREQUAL "LINUX") list(APPEND result "-lpthread" "-ldl") + elseif("${sdk}" STREQUAL "FREEBSD") else() list(APPEND result "-lobjc") endif() diff --git a/lib/Driver/CMakeLists.txt b/lib/Driver/CMakeLists.txt index a8718746a0e86..d94d0b419a905 100644 --- a/lib/Driver/CMakeLists.txt +++ b/lib/Driver/CMakeLists.txt @@ -12,14 +12,24 @@ set(swiftDriver_sources Types.cpp ) +set(swiftDriver_targetDefines) + add_swift_library(swiftDriver ${swiftDriver_sources} DEPENDS SwiftOptions LINK_LIBRARIES swiftAST swiftBasic swiftFrontend swiftOption) if(${SWIFT_ENABLE_TARGET_LINUX}) - foreach(f ${swiftDriver_sources}) + list(APPEND swiftDriver_targetDefines "LINUX") +endif() + +if(${SWIFT_ENABLE_TARGET_FREEBSD}) + list(APPEND swiftDriver_targetDefines "FREEBSD") +endif() + +foreach(f ${swiftDriver_sources}) + foreach(d ${swiftDriver_targetDefines}) set_property(SOURCE ${f} APPEND_STRING PROPERTY COMPILE_FLAGS - " -DSWIFT_ENABLE_TARGET_LINUX=\"1\"") + " -DSWIFT_ENABLE_TARGET_${d}=1") endforeach() -endif() +endforeach() diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index 018b5d677d21c..8af12e3dd056b 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -2031,6 +2031,11 @@ const ToolChain *Driver::getToolChain(const ArgList &Args) const { TC = new toolchains::Linux(*this, Target); break; #endif // SWIFT_ENABLE_TARGET_LINUX +#if defined(SWIFT_ENABLE_TARGET_FREEBSD) + case llvm::Triple::FreeBSD: + TC = new toolchains::FreeBSD(*this, Target); + break; +#endif // SWIFT_ENABLE_TARGET_FREEBSD default: TC = nullptr; } diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index 8434da7ce2ba1..2b12aa00b0750 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -1043,3 +1043,118 @@ toolchains::Linux::constructInvocation(const LinkJobAction &job, #endif // SWIFT_ENABLE_TARGET_LINUX +#if defined(SWIFT_ENABLE_TARGET_FREEBSD) + +std::pair +toolchains::FreeBSD::constructInvocation(const AutolinkExtractJobAction &job, + const JobContext &context) const { + assert(context.Output.getPrimaryOutputType() == types::TY_AutolinkFile); + + ArgStringList Arguments; + addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object); + addInputsOfType(Arguments, context.InputActions, types::TY_Object); + + Arguments.push_back("-o"); + Arguments.push_back( + context.Args.MakeArgString(context.Output.getPrimaryOutputFilename())); + + return std::make_pair("swift-autolink-extract", Arguments); +} + +std::pair +toolchains::FreeBSD::constructInvocation(const LinkJobAction &job, + const JobContext &context) const { + const Driver &D = getDriver(); + + assert(context.Output.getPrimaryOutputType() == types::TY_Image && + "Invalid linker output type."); + + ArgStringList Arguments; + + switch (job.getKind()) { + case LinkKind::None: + llvm_unreachable("invalid link kind"); + case LinkKind::Executable: + // Default case, nothing extra needed + break; + case LinkKind::DynamicLibrary: + Arguments.push_back("-shared"); + break; + } + + addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object); + addInputsOfType(Arguments, context.InputActions, types::TY_Object); + + context.Args.AddAllArgs(Arguments, options::OPT_Xlinker); + context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group); + context.Args.AddAllArgs(Arguments, options::OPT_F); + + if (!context.OI.SDKPath.empty()) { + Arguments.push_back("--sysroot"); + Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath)); + } + + // Add the runtime library link path, which is platform-specific and found + // relative to the compiler. + // FIXME: Duplicated from CompilerInvocation, but in theory the runtime + // library link path and the standard library module import path don't + // need to be the same. + llvm::SmallString<128> RuntimeLibPath; + + if (const Arg *A = context.Args.getLastArg(options::OPT_resource_dir)) { + RuntimeLibPath = A->getValue(); + } else { + RuntimeLibPath = D.getSwiftProgramPath(); + llvm::sys::path::remove_filename(RuntimeLibPath); // remove /swift + llvm::sys::path::remove_filename(RuntimeLibPath); // remove /bin + llvm::sys::path::append(RuntimeLibPath, "lib", "swift"); + } + llvm::sys::path::append(RuntimeLibPath, + getPlatformNameForTriple(getTriple())); + Arguments.push_back("-L"); + Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath)); + + if (context.Args.hasArg(options::OPT_profile_generate)) { + SmallString<128> LibProfile(RuntimeLibPath); + llvm::sys::path::remove_filename(LibProfile); // remove platform name + llvm::sys::path::append(LibProfile, "clang", CLANG_VERSION_STRING); + + llvm::sys::path::append(LibProfile, "lib", getTriple().getOSName(), + Twine("libclang_rt.profile-") + + getTriple().getArchName() + + ".a"); + Arguments.push_back(context.Args.MakeArgString(LibProfile)); + } + + // FIXME: We probably shouldn't be adding an rpath here unless we know ahead + // of time the standard library won't be copied. + Arguments.push_back("-Xlinker"); + Arguments.push_back("-rpath"); + Arguments.push_back("-Xlinker"); + Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath)); + + // Always add the stdlib + Arguments.push_back("-lswiftCore"); + + // Add any autolinking scripts to the arguments + for (const Job *Cmd : context.Inputs) { + auto &OutputInfo = Cmd->getOutput(); + if (OutputInfo.getPrimaryOutputType() == types::TY_AutolinkFile) + Arguments.push_back(context.Args.MakeArgString( + Twine("@") + OutputInfo.getPrimaryOutputFilename())); + } + + // Add the linker script that coalesces protocol conformance sections. + Arguments.push_back("-Xlinker"); + Arguments.push_back("-T"); + Arguments.push_back( + context.Args.MakeArgString(Twine(RuntimeLibPath) + "/x86_64/swift.ld")); + + // This should be the last option, for convenience in checking output. + Arguments.push_back("-o"); + Arguments.push_back(context.Output.getPrimaryOutputFilename().c_str()); + + return std::make_pair("clang++", Arguments); +} + +#endif // SWIFT_ENABLE_TARGET_FREEBSD diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h index 629ef3369c6d1..f7f7276148af5 100644 --- a/lib/Driver/ToolChains.h +++ b/lib/Driver/ToolChains.h @@ -53,6 +53,24 @@ class LLVM_LIBRARY_VISIBILITY Linux : public ToolChain { #endif // SWIFT_ENABLE_TARGET_LINUX +#if defined(SWIFT_ENABLE_TARGET_FREEBSD) + +class LLVM_LIBRARY_VISIBILITY FreeBSD : public ToolChain { +protected: + std::pair + constructInvocation(const AutolinkExtractJobAction &job, + const JobContext &context) const override; + std::pair + constructInvocation(const LinkJobAction &job, + const JobContext &context) const override; + +public: + FreeBSD(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {} + ~FreeBSD() = default; +}; + +#endif // SWIFT_ENABLE_TARGET_FREEBSD + } // end namespace toolchains } // end namespace driver } // end namespace swift 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/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}") diff --git a/utils/build-script-impl b/utils/build-script-impl index 403d3fcb1d777..094df34f1f4e2 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -1415,6 +1415,7 @@ for deployment_target in "${NATIVE_TOOLS_DEPLOYMENT_TARGETS[@]}" "${CROSS_TOOLS_ -DSWIFT_RUNTIME_ENABLE_DTRACE:BOOL=$(true_false "${SWIFT_RUNTIME_ENABLE_DTRACE}") -DSWIFT_RUNTIME_ENABLE_LEAK_CHECKER:BOOL=$(true_false "${SWIFT_RUNTIME_ENABLE_LEAK_CHECKER}") -DSWIFT_ENABLE_TARGET_LINUX:BOOL=$(true_false "${SWIFT_ENABLE_TARGET_LINUX}") + -DSWIFT_ENABLE_TARGET_FREEBSD:BOOL=$(true_false "${SWIFT_ENABLE_TARGET_FREEBSD}") ) for product in "${PRODUCTS[@]}"; do From 23fee41713bf22de59f70bbf48bffcbac4c8bdee Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Thu, 3 Dec 2015 21:04:50 -0700 Subject: [PATCH 02/16] Add missing va_list include. --- stdlib/public/stubs/Stubs.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/public/stubs/Stubs.cpp b/stdlib/public/stubs/Stubs.cpp index c1307fd12ee17..14743df4161af 100644 --- a/stdlib/public/stubs/Stubs.cpp +++ b/stdlib/public/stubs/Stubs.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include From 30080ae2c453fe1cdc07250c8544fb4d0d259d67 Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Thu, 3 Dec 2015 21:11:45 -0700 Subject: [PATCH 03/16] FreeBSD hides getline()'s prototype by default. The alternative to _WITH_GETLINE would be to define _POSIX_C_SOURCE >= 200809. --- stdlib/public/stubs/Stubs.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stdlib/public/stubs/Stubs.cpp b/stdlib/public/stubs/Stubs.cpp index 14743df4161af..336f352010707 100644 --- a/stdlib/public/stubs/Stubs.cpp +++ b/stdlib/public/stubs/Stubs.cpp @@ -16,6 +16,10 @@ // //===----------------------------------------------------------------------===// +#if defined(__FreeBSD__) +#define _WITH_GETLINE +#endif + #include #include #include From d206dcba345b99fd5a39b09877503215fcd481f9 Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Thu, 3 Dec 2015 21:14:14 -0700 Subject: [PATCH 04/16] Define malloc_size() support for FreeBSD. --- stdlib/public/stubs/LibcShims.cpp | 5 +++++ 1 file changed, 5 insertions(+) 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 From 0f6380ef4ddd953ff20d2222a05e63aa217a3c4e Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Thu, 3 Dec 2015 21:28:59 -0700 Subject: [PATCH 05/16] Match the Linux approach of assuming only the 4K NULL page is reserved. --- stdlib/public/runtime/Casting.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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" From 6bf5a4989e07d294a31f22804f64e291dfe7951a Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Thu, 3 Dec 2015 23:37:16 -0700 Subject: [PATCH 06/16] Add FreeBSD Option handling. --- include/swift/Basic/LangOptions.h | 4 +++- lib/Basic/LangOptions.cpp | 5 ++++- lib/Basic/Platform.cpp | 3 +++ 3 files changed, 10 insertions(+), 2 deletions(-) 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 ""; } From e8bbe9997156d7a8db4e15e35d1390198eeb09ce Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Fri, 4 Dec 2015 14:01:43 -0700 Subject: [PATCH 07/16] Drop unnecessary target option, as per gribozavr's feedback. --- CMakeLists.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc7e765ba1b65..bc72575059efb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,10 +58,6 @@ option(SWIFT_ENABLE_TARGET_LINUX "Enable compiler support for targeting Linux" TRUE) -option(SWIFT_ENABLE_TARGET_FREEBSD - "Enable compiler support for targeting FreeBSD" - TRUE) - set(SWIFT_VERSION "2.2" CACHE STRING "The user-visible version of the Swift compiler") set(SWIFT_VENDOR "" CACHE STRING From a51494a146883e4ff8bbfe2c2801cbedb353ac13 Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Fri, 4 Dec 2015 14:02:28 -0700 Subject: [PATCH 08/16] Add comment to clarify the empty if block, as per gribozavr's review feedback. --- cmake/modules/AddSwift.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index a379a2f12fdb6..c523924b2935c 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -152,6 +152,7 @@ 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() From d7721781bc9cd4c660514c97d53d686f984da180 Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Fri, 4 Dec 2015 14:08:04 -0700 Subject: [PATCH 09/16] Unify the identical Linux/FreeBSD definitions. When these diverge, we can use the Unix toolchain as a common baseclass for both. --- lib/Driver/Driver.cpp | 4 +- lib/Driver/ToolChains.cpp | 128 ++------------------------------------ lib/Driver/ToolChains.h | 34 ++-------- 3 files changed, 13 insertions(+), 153 deletions(-) diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index 8af12e3dd056b..95e03183a9c1e 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -2028,12 +2028,12 @@ const ToolChain *Driver::getToolChain(const ArgList &Args) const { break; #if defined(SWIFT_ENABLE_TARGET_LINUX) case llvm::Triple::Linux: - TC = new toolchains::Linux(*this, Target); + TC = new toolchains::Unix(*this, Target); break; #endif // SWIFT_ENABLE_TARGET_LINUX #if defined(SWIFT_ENABLE_TARGET_FREEBSD) case llvm::Triple::FreeBSD: - TC = new toolchains::FreeBSD(*this, Target); + TC = new toolchains::Unix(*this, Target); break; #endif // SWIFT_ENABLE_TARGET_FREEBSD default: diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index 2b12aa00b0750..0db5f1e261e6a 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -927,11 +927,11 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job, return std::make_pair("ld", Arguments); } -#if defined(SWIFT_ENABLE_TARGET_LINUX) +#if defined(SWIFT_ENABLE_TARGET_LINUX) || defined(SWIFT_ENABLE_TARGET_FREEBSD) std::pair -toolchains::Linux::constructInvocation(const AutolinkExtractJobAction &job, - const JobContext &context) const { +toolchains::Unix::constructInvocation(const AutolinkExtractJobAction &job, + const JobContext &context) const { assert(context.Output.getPrimaryOutputType() == types::TY_AutolinkFile); ArgStringList Arguments; @@ -946,8 +946,8 @@ toolchains::Linux::constructInvocation(const AutolinkExtractJobAction &job, } std::pair -toolchains::Linux::constructInvocation(const LinkJobAction &job, - const JobContext &context) const { +toolchains::Unix::constructInvocation(const LinkJobAction &job, + const JobContext &context) const { const Driver &D = getDriver(); assert(context.Output.getPrimaryOutputType() == types::TY_Image && @@ -1041,120 +1041,4 @@ toolchains::Linux::constructInvocation(const LinkJobAction &job, return std::make_pair("clang++", Arguments); } -#endif // SWIFT_ENABLE_TARGET_LINUX - -#if defined(SWIFT_ENABLE_TARGET_FREEBSD) - -std::pair -toolchains::FreeBSD::constructInvocation(const AutolinkExtractJobAction &job, - const JobContext &context) const { - assert(context.Output.getPrimaryOutputType() == types::TY_AutolinkFile); - - ArgStringList Arguments; - addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object); - addInputsOfType(Arguments, context.InputActions, types::TY_Object); - - Arguments.push_back("-o"); - Arguments.push_back( - context.Args.MakeArgString(context.Output.getPrimaryOutputFilename())); - - return std::make_pair("swift-autolink-extract", Arguments); -} - -std::pair -toolchains::FreeBSD::constructInvocation(const LinkJobAction &job, - const JobContext &context) const { - const Driver &D = getDriver(); - - assert(context.Output.getPrimaryOutputType() == types::TY_Image && - "Invalid linker output type."); - - ArgStringList Arguments; - - switch (job.getKind()) { - case LinkKind::None: - llvm_unreachable("invalid link kind"); - case LinkKind::Executable: - // Default case, nothing extra needed - break; - case LinkKind::DynamicLibrary: - Arguments.push_back("-shared"); - break; - } - - addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object); - addInputsOfType(Arguments, context.InputActions, types::TY_Object); - - context.Args.AddAllArgs(Arguments, options::OPT_Xlinker); - context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group); - context.Args.AddAllArgs(Arguments, options::OPT_F); - - if (!context.OI.SDKPath.empty()) { - Arguments.push_back("--sysroot"); - Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath)); - } - - // Add the runtime library link path, which is platform-specific and found - // relative to the compiler. - // FIXME: Duplicated from CompilerInvocation, but in theory the runtime - // library link path and the standard library module import path don't - // need to be the same. - llvm::SmallString<128> RuntimeLibPath; - - if (const Arg *A = context.Args.getLastArg(options::OPT_resource_dir)) { - RuntimeLibPath = A->getValue(); - } else { - RuntimeLibPath = D.getSwiftProgramPath(); - llvm::sys::path::remove_filename(RuntimeLibPath); // remove /swift - llvm::sys::path::remove_filename(RuntimeLibPath); // remove /bin - llvm::sys::path::append(RuntimeLibPath, "lib", "swift"); - } - llvm::sys::path::append(RuntimeLibPath, - getPlatformNameForTriple(getTriple())); - Arguments.push_back("-L"); - Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath)); - - if (context.Args.hasArg(options::OPT_profile_generate)) { - SmallString<128> LibProfile(RuntimeLibPath); - llvm::sys::path::remove_filename(LibProfile); // remove platform name - llvm::sys::path::append(LibProfile, "clang", CLANG_VERSION_STRING); - - llvm::sys::path::append(LibProfile, "lib", getTriple().getOSName(), - Twine("libclang_rt.profile-") + - getTriple().getArchName() + - ".a"); - Arguments.push_back(context.Args.MakeArgString(LibProfile)); - } - - // FIXME: We probably shouldn't be adding an rpath here unless we know ahead - // of time the standard library won't be copied. - Arguments.push_back("-Xlinker"); - Arguments.push_back("-rpath"); - Arguments.push_back("-Xlinker"); - Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath)); - - // Always add the stdlib - Arguments.push_back("-lswiftCore"); - - // Add any autolinking scripts to the arguments - for (const Job *Cmd : context.Inputs) { - auto &OutputInfo = Cmd->getOutput(); - if (OutputInfo.getPrimaryOutputType() == types::TY_AutolinkFile) - Arguments.push_back(context.Args.MakeArgString( - Twine("@") + OutputInfo.getPrimaryOutputFilename())); - } - - // Add the linker script that coalesces protocol conformance sections. - Arguments.push_back("-Xlinker"); - Arguments.push_back("-T"); - Arguments.push_back( - context.Args.MakeArgString(Twine(RuntimeLibPath) + "/x86_64/swift.ld")); - - // This should be the last option, for convenience in checking output. - Arguments.push_back("-o"); - Arguments.push_back(context.Output.getPrimaryOutputFilename().c_str()); - - return std::make_pair("clang++", Arguments); -} - -#endif // SWIFT_ENABLE_TARGET_FREEBSD +#endif // SWIFT_ENABLE_TARGET_(LINUX|FREEBSD) diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h index f7f7276148af5..b6a001b2976d1 100644 --- a/lib/Driver/ToolChains.h +++ b/lib/Driver/ToolChains.h @@ -35,9 +35,9 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain { }; -#if defined(SWIFT_ENABLE_TARGET_LINUX) +#if defined(SWIFT_ENABLE_TARGET_LINUX) || defined(SWIFT_ENABLE_TARGET_FREEBSD) -class LLVM_LIBRARY_VISIBILITY Linux : public ToolChain { +class LLVM_LIBRARY_VISIBILITY Unix : public ToolChain { protected: std::pair constructInvocation(const AutolinkExtractJobAction &job, @@ -47,32 +47,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; + Unix(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {} + ~Unix() = default; }; -#endif // SWIFT_ENABLE_TARGET_LINUX - -#if defined(SWIFT_ENABLE_TARGET_FREEBSD) - -class LLVM_LIBRARY_VISIBILITY FreeBSD : public ToolChain { -protected: - std::pair - constructInvocation(const AutolinkExtractJobAction &job, - const JobContext &context) const override; - std::pair - constructInvocation(const LinkJobAction &job, - const JobContext &context) const override; - -public: - FreeBSD(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {} - ~FreeBSD() = default; -}; - -#endif // SWIFT_ENABLE_TARGET_FREEBSD - -} // end namespace toolchains -} // end namespace driver -} // end namespace swift - -#endif +#endif // SWIFT_ENABLE_TARGET_(LINUX|FREEBSD) From 1acab5ab01f0057c38a8056716267553b37d6521 Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Fri, 4 Dec 2015 14:26:29 -0700 Subject: [PATCH 10/16] Add a target configuration option test for FreeBSD. --- test/BuildConfigurations/x64FreeBSDTarget.swift | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 test/BuildConfigurations/x64FreeBSDTarget.swift diff --git a/test/BuildConfigurations/x64FreeBSDTarget.swift b/test/BuildConfigurations/x64FreeBSDTarget.swift new file mode 100644 index 0000000000000..619d4f5daf769 --- /dev/null +++ b/test/BuildConfigurations/x64FreeBSDTarget.swift @@ -0,0 +1,8 @@ +// RUN: %swift -parse %s -verify -D FOO -D BAR -target x86_64-freebsd10 -disable-objc-interop -D FOO -parse-stdlib +// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-freebsd10 + +#if arch(x86_64) && os(FreeBSD) && _runtime(_Native) +class C {} +var x = C() +#endif +var y = x From 49ba19f8c32d2bc7c5cdb1b821b1a69027410581 Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Fri, 4 Dec 2015 14:29:03 -0700 Subject: [PATCH 11/16] Fix namespace/#endif tokens lost in merge. --- lib/Driver/ToolChains.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h index f1e0de0113988..93c610a440552 100644 --- a/lib/Driver/ToolChains.h +++ b/lib/Driver/ToolChains.h @@ -53,3 +53,10 @@ class LLVM_LIBRARY_VISIBILITY Unix : public ToolChain { }; #endif // SWIFT_ENABLE_TARGET_(LINUX|FREEBSD) + +} // end namespace toolchains +} // end namespace driver +} // end namespace swift + +#endif + From daa97e00791349e03260bce2d0c5aa7ff4a6e6b7 Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Fri, 4 Dec 2015 17:30:13 -0700 Subject: [PATCH 12/16] Adopt suggested 'GenericUnix' toolchain class name. --- lib/Driver/Driver.cpp | 4 ++-- lib/Driver/ToolChains.cpp | 12 ++++++------ lib/Driver/ToolChains.h | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index 41fc114906a2b..25601072edcf2 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -2036,10 +2036,10 @@ const ToolChain *Driver::getToolChain(const ArgList &Args) const { TC = new toolchains::Darwin(*this, Target); break; case llvm::Triple::Linux: - TC = new toolchains::Unix(*this, Target); + TC = new toolchains::GenericUnix(*this, Target); break; case llvm::Triple::FreeBSD: - TC = new toolchains::Unix(*this, Target); + TC = new toolchains::GenericUnix(*this, Target); break; default: TC = nullptr; diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index 80c7906f6159e..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::Unix::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::Unix::constructInvocation(const InterpretJobAction &job, ToolChain::InvocationInfo -toolchains::Unix::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::Unix::constructInvocation(const AutolinkExtractJobAction &job, } ToolChain::InvocationInfo -toolchains::Unix::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 918630ea97e86..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 Unix : 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 Unix : public ToolChain { const JobContext &context) const override; public: - Unix(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {} - ~Unix() = default; + GenericUnix(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {} + ~GenericUnix() = default; }; } // end namespace toolchains From c6719476152bdf0760d13897d152dd609f50ad52 Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Fri, 4 Dec 2015 18:38:34 -0700 Subject: [PATCH 13/16] Apply patch provided by gribozavr; swift -target requires a proper triple. --- test/BuildConfigurations/x64FreeBSDTarget.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/BuildConfigurations/x64FreeBSDTarget.swift b/test/BuildConfigurations/x64FreeBSDTarget.swift index 619d4f5daf769..284060fa6eb49 100644 --- a/test/BuildConfigurations/x64FreeBSDTarget.swift +++ b/test/BuildConfigurations/x64FreeBSDTarget.swift @@ -1,5 +1,5 @@ -// RUN: %swift -parse %s -verify -D FOO -D BAR -target x86_64-freebsd10 -disable-objc-interop -D FOO -parse-stdlib -// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-freebsd10 +// 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 {} From 527fb6879a239a37971bd5f29969cecda5f8956a Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Fri, 4 Dec 2015 18:48:42 -0700 Subject: [PATCH 14/16] Adopt proper freebsd triple as required by Swift's build system. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b53eea5f9ae52..7b2583d1f225e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -398,7 +398,7 @@ 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") + configure_sdk_unix(FREEBSD "FreeBSD" "freebsd" "freebsd" "x86_64" "x86_64-unknown-freebsd10") set(CMAKE_EXECUTABLE_FORMAT "ELF") From 395ae26753cfb9474351b4784d0ac80620196266 Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Fri, 4 Dec 2015 19:00:40 -0700 Subject: [PATCH 15/16] Add FreeBSD target_* support. --- test/lit.cfg | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test/lit.cfg b/test/lit.cfg index 0543e5d9060e4..4aa77da7288d8 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -611,13 +611,20 @@ if run_vendor == 'apple': "%s ld -L%s" % (xcrun_prefix, os.path.join(test_resource_dir, config.target_sdk_name))) -elif run_os == 'linux-gnu': - # Linux - lit_config.note("Testing Linux " + config.variant_triple) +elif run_os == 'linux-gnu' or run_os.startswith('freebsd'): + # Linux/FreeBSD + if run_os == 'linux-gnu': + config.target_sdk_name = "linux" + elif run_os.startswith('freebsd'): + config.target_sdk_name = "freebsd" + + lit_config.note("Testing " + config.target_sdk_name + " " + + config.variant_triple) + config.target_object_format = "elf" config.target_runtime = "native" config.target_swift_autolink_extract = inferSwiftBinary("swift-autolink-extract") - config.target_sdk_name = "linux" + config.target_build_swift = ( '%s -target %s %s %s %s %s' % (config.swiftc, config.variant_triple, resource_dir_opt, mcp_opt, From c4daece7cedf57eb2a2a60937c3cc93d19fe7912 Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Fri, 4 Dec 2015 20:20:07 -0700 Subject: [PATCH 16/16] Revert changes not needed for test passage on supported platforms. This reverts: - Adopt proper freebsd triple as required by Swift's build system. - Revert "Add FreeBSD target_* support. These will be re-introduced in a later patch that brings up the FreeBSD test suite. --- CMakeLists.txt | 2 +- test/lit.cfg | 15 ++++----------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b2583d1f225e..b53eea5f9ae52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -398,7 +398,7 @@ 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-unknown-freebsd10") + configure_sdk_unix(FREEBSD "FreeBSD" "freebsd" "freebsd" "x86_64" "x86_64-freebsd10") set(CMAKE_EXECUTABLE_FORMAT "ELF") diff --git a/test/lit.cfg b/test/lit.cfg index 4aa77da7288d8..0543e5d9060e4 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -611,20 +611,13 @@ if run_vendor == 'apple': "%s ld -L%s" % (xcrun_prefix, os.path.join(test_resource_dir, config.target_sdk_name))) -elif run_os == 'linux-gnu' or run_os.startswith('freebsd'): - # Linux/FreeBSD - if run_os == 'linux-gnu': - config.target_sdk_name = "linux" - elif run_os.startswith('freebsd'): - config.target_sdk_name = "freebsd" - - lit_config.note("Testing " + config.target_sdk_name + " " + - config.variant_triple) - +elif run_os == 'linux-gnu': + # Linux + lit_config.note("Testing Linux " + config.variant_triple) config.target_object_format = "elf" config.target_runtime = "native" config.target_swift_autolink_extract = inferSwiftBinary("swift-autolink-extract") - + config.target_sdk_name = "linux" config.target_build_swift = ( '%s -target %s %s %s %s %s' % (config.swiftc, config.variant_triple, resource_dir_opt, mcp_opt,