Skip to content

[5.1] Module filename and location changes #23021

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 8 commits into from
Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 8 additions & 1 deletion cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ endfunction()
# Compute the library subdirectory to use for the given sdk and
# architecture, placing the result in 'result_var_name'.
function(compute_library_subdir result_var_name sdk arch)
set("${result_var_name}" "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}" PARENT_SCOPE)
if(sdk IN_LIST SWIFT_APPLE_PLATFORMS)
set("${result_var_name}" "${SWIFT_SDK_${sdk}_LIB_SUBDIR}" PARENT_SCOPE)
else()
set("${result_var_name}" "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}" PARENT_SCOPE)
endif()
endfunction()

function(_compute_lto_flag option out_var)
Expand Down Expand Up @@ -1156,6 +1160,8 @@ function(_add_swift_library_single target name)
# Don't set PROPERTY COMPILE_FLAGS or LINK_FLAGS directly.
set(c_compile_flags ${SWIFTLIB_SINGLE_C_COMPILE_FLAGS})
set(link_flags ${SWIFTLIB_SINGLE_LINK_FLAGS})

set(library_search_subdir "${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
set(library_search_directories
"${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}"
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"
Expand Down Expand Up @@ -1300,6 +1306,7 @@ function(_add_swift_library_single target name)
if(target_static)
set_property(TARGET "${target_static}" APPEND_STRING PROPERTY
COMPILE_FLAGS " ${c_compile_flags}")
# FIXME: The fallback paths here are going to be dynamic libraries.
set(library_search_directories
"${SWIFTSTATICLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}"
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"
Expand Down
40 changes: 29 additions & 11 deletions cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ function(handle_swift_sources
endif()

if(swift_sources)
compute_library_subdir(SWIFTSOURCES_LIBRARY_SUBDIR
"${SWIFTSOURCES_SDK}" "${SWIFTSOURCES_ARCHITECTURE}")
set(objsubdir "/${SWIFTSOURCES_LIBRARY_SUBDIR}")
set(objsubdir "/${SWIFTSOURCES_SDK}/${SWIFTSOURCES_ARCHITECTURE}")

file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}${objsubdir}")

Expand Down Expand Up @@ -255,6 +253,8 @@ function(_compile_swift_files
"-nostdimport" "-parse-stdlib" "-module-name" "Swift")
list(APPEND swift_flags "-Xfrontend" "-group-info-path"
"-Xfrontend" "${GROUP_INFO_JSON_FILE}")
else()
list(APPEND swift_flags "-module-name" "${SWIFTFILE_MODULE_NAME}")
endif()

# Force swift 5 mode for Standard Library.
Expand Down Expand Up @@ -313,19 +313,32 @@ function(_compile_swift_files
list(APPEND swift_flags "-parse-as-library")

set(module_base "${module_dir}/${SWIFTFILE_MODULE_NAME}")
if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
set(specific_module_dir "${module_base}.swiftmodule")
set(module_base "${module_base}.swiftmodule/${SWIFTFILE_ARCHITECTURE}")
endif()
set(module_file "${module_base}.swiftmodule")
set(module_doc_file "${module_base}.swiftdoc")

# FIXME: These don't really belong inside the swiftmodule, but there's not
# an obvious alternate place to put them.
set(sib_file "${module_base}.Onone.sib")
set(sibopt_file "${module_base}.O.sib")
set(sibgen_file "${module_base}.sibgen")
set(module_doc_file "${module_base}.swiftdoc")

if(SWIFT_ENABLE_PARSEABLE_MODULE_INTERFACES)
set(interface_file "${module_base}.swiftinterface")
list(APPEND swift_flags "-emit-parseable-module-interface")
list(APPEND swift_flags
"-emit-parseable-module-interface-path" "${interface_file}")
endif()

list(APPEND command_create_dirs
COMMAND "${CMAKE_COMMAND}" -E make_directory "${module_dir}")
if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
list(APPEND command_create_dirs
COMMAND "${CMAKE_COMMAND}" -E make_directory "${specific_module_dir}")
else()
list(APPEND command_create_dirs
COMMAND "${CMAKE_COMMAND}" -E make_directory "${module_dir}")
endif()

# If we have extra regexp flags, check if we match any of the regexps. If so
# add the relevant flags to our swift_flags.
Expand All @@ -349,10 +362,15 @@ function(_compile_swift_files
set(optional_arg "OPTIONAL")
endif()

swift_install_in_component("${SWIFTFILE_INSTALL_IN_COMPONENT}"
FILES ${module_outputs}
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
"${optional_arg}")
if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
swift_install_in_component("${SWIFTFILE_INSTALL_IN_COMPONENT}"
DIRECTORY "${specific_module_dir}"
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}")
else()
swift_install_in_component("${SWIFTFILE_INSTALL_IN_COMPONENT}"
FILES ${module_outputs}
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}")
endif()

set(line_directive_tool "${SWIFT_SOURCE_DIR}/utils/line-directive")
set(swift_compiler_tool "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc")
Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,8 @@ ERROR(cannot_return_value_from_void_func,none,

ERROR(sema_no_import,Fatal,
"no such module '%0'", (StringRef))
ERROR(sema_no_import_arch,Fatal,
"could not find module '%0' for architecture '%1'; "
ERROR(sema_no_import_target,Fatal,
"could not find module '%0' for target '%1'; "
"found: %2", (StringRef, StringRef, StringRef))
ERROR(sema_no_import_repl,none,
"no such module '%0'", (StringRef))
Expand Down
11 changes: 11 additions & 0 deletions include/swift/Basic/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ namespace swift {
///
/// This is a stop-gap until full Triple support (ala Clang) exists within swiftc.
StringRef getMajorArchitectureName(const llvm::Triple &triple);

/// Computes the normalized target triple used as the most preferred name for
/// module loading.
///
/// For platforms with fat binaries, this canonicalizes architecture,
/// vendor, and OS names, strips OS versions, and makes inferred environments
/// explicit. For other platforms, it returns the unmodified triple.
///
/// The input triple should already be "normalized" in the sense that
/// llvm::Triple::normalize() would not affect it.
llvm::Triple getTargetSpecificModuleTriple(const llvm::Triple &triple);
} // end namespace swift

#endif // SWIFT_BASIC_PLATFORM_H
Expand Down
16 changes: 8 additions & 8 deletions include/swift/Serialization/SerializedModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ class SerializedModuleLoaderBase : public ModuleLoader {
/// to list the architectures that \e are present.
///
/// \returns true if an error diagnostic was emitted
virtual bool maybeDiagnoseArchitectureMismatch(SourceLoc sourceLocation,
StringRef moduleName,
StringRef archName,
StringRef directoryPath) {
virtual bool maybeDiagnoseTargetMismatch(SourceLoc sourceLocation,
StringRef moduleName,
StringRef archName,
StringRef directoryPath) {
return false;
}

Expand Down Expand Up @@ -139,10 +139,10 @@ class SerializedModuleLoader : public SerializedModuleLoaderBase {
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) override;

bool maybeDiagnoseArchitectureMismatch(SourceLoc sourceLocation,
StringRef moduleName,
StringRef archName,
StringRef directoryPath) override;
bool maybeDiagnoseTargetMismatch(SourceLoc sourceLocation,
StringRef moduleName,
StringRef archName,
StringRef directoryPath) override;

public:
virtual ~SerializedModuleLoader();
Expand Down
125 changes: 125 additions & 0 deletions lib/Basic/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
//===----------------------------------------------------------------------===//

#include "swift/Basic/Platform.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/Triple.h"

using namespace swift;
Expand Down Expand Up @@ -188,3 +190,126 @@ StringRef swift::getMajorArchitectureName(const llvm::Triple &Triple) {
return Triple.getArchName();
}
}

// The code below is responsible for normalizing target triples into the form
// used to name target-specific swiftmodule, swiftinterface, and swiftdoc files.
// If two triples have incompatible ABIs or can be distinguished by Swift #if
// declarations, they should normalize to different values.
//
// This code is only really used on platforms with toolchains supporting fat
// binaries (a single binary containing multiple architectures). On these
// platforms, this code should strip unnecessary details from target triple
// components and map synonyms to canonical values. Even values which don't need
// any special canonicalization should be documented here as comments.
//
// (Fallback behavior does not belong here; it should be implemented in code
// that calls this function, most importantly in SerializedModuleLoaderBase.)
//
// If you're trying to refer to this code to understand how Swift behaves and
// you're unfamiliar with LLVM internals, here's a cheat sheet for reading it:
//
// * llvm::Triple is the type for a target name. It's a bit of a misnomer,
// because it can contain three or four values: arch-vendor-os[-environment].
//
// * In .Cases and .Case, the last argument is the value the arguments before it
// map to. That is, `.Cases("bar", "baz", "foo")` will return "foo" if it sees
// "bar" or "baz".
//
// * llvm::Optional is similar to a Swift Optional: it either contains a value
// or represents the absence of one. `None` is equivalent to `nil`; leading
// `*` is equivalent to trailing `!`; conversion to `bool` is a not-`None`
// check.

static StringRef
getArchForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
auto tripleArchName = triple.getArchName();

return llvm::StringSwitch<StringRef>(tripleArchName)
.Cases("arm64", "aarch64", "arm64")
.Cases("x86_64", "amd64", "x86_64")
.Cases("i386", "i486", "i586", "i686", "i786", "i886", "i986",
"i386")
.Cases("unknown", "", "unknown")
// These values are also supported, but are handled by the default case below:
// .Case ("armv7s", "armv7s")
// .Case ("armv7k", "armv7k")
// .Case ("armv7", "armv7")
.Default(tripleArchName);
}

static StringRef
getVendorForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
// We unconditionally normalize to "apple" because it's relatively common for
// build systems to omit the vendor name or use an incorrect one like
// "unknown". Most parts of the compiler ignore the vendor, so you might not
// notice such a mistake.
//
// Please don't depend on this behavior--specify 'apple' if you're building
// for an Apple platform.

assert(triple.isOSDarwin() &&
"shouldn't normalize non-Darwin triple to 'apple'");

return "apple";
}

static StringRef
getOSForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
auto tripleOSName = triple.getOSName();

// Truncate the OS name before the first digit. "Digit" here is ASCII '0'-'9'.
auto tripleOSNameNoVersion = tripleOSName.take_until(llvm::isDigit);

return llvm::StringSwitch<StringRef>(tripleOSNameNoVersion)
.Cases("macos", "macosx", "darwin", "macos")
.Cases("unknown", "", "unknown")
// These values are also supported, but are handled by the default case below:
// .Case ("ios", "ios")
// .Case ("tvos", "tvos")
// .Case ("watchos", "watchos")
.Default(tripleOSNameNoVersion);
}

static Optional<StringRef>
getEnvironmentForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
auto tripleEnvironment = triple.getEnvironmentName();

// If the environment is empty, infer a "simulator" environment based on the
// OS and architecture combination. This feature is deprecated and exists for
// backwards compatibility only; build systems should pass the "simulator"
// environment explicitly if they know they're building for a simulator.
if (tripleEnvironment == "" && swift::tripleIsAnySimulator(triple))
return StringRef("simulator");

return llvm::StringSwitch<Optional<StringRef>>(tripleEnvironment)
.Cases("unknown", "", None)
// These values are also supported, but are handled by the default case below:
// .Case ("simulator", StringRef("simulator"))
.Default(tripleEnvironment);
}

llvm::Triple swift::getTargetSpecificModuleTriple(const llvm::Triple &triple) {
// isOSDarwin() returns true for all Darwin-style OSes, including macOS, iOS,
// etc.
if (triple.isOSDarwin()) {
StringRef newArch = getArchForAppleTargetSpecificModuleTriple(triple);

StringRef newVendor = getVendorForAppleTargetSpecificModuleTriple(triple);

StringRef newOS = getOSForAppleTargetSpecificModuleTriple(triple);

Optional<StringRef> newEnvironment =
getEnvironmentForAppleTargetSpecificModuleTriple(triple);

if (!newEnvironment)
// Generate an arch-vendor-os triple.
return llvm::Triple(newArch, newVendor, newOS);

// Generate an arch-vendor-os-environment triple.
return llvm::Triple(newArch, newVendor, newOS, *newEnvironment);
}

// Other platforms get no normalization.
return triple;
}

3 changes: 2 additions & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ static void updateRuntimeLibraryPath(SearchPathOptions &SearchPathOpts,
llvm::sys::path::append(LibPath, getPlatformNameForTriple(Triple));
SearchPathOpts.RuntimeLibraryPath = LibPath.str();

llvm::sys::path::append(LibPath, swift::getMajorArchitectureName(Triple));
if (!Triple.isOSDarwin())
llvm::sys::path::append(LibPath, swift::getMajorArchitectureName(Triple));
SearchPathOpts.RuntimeLibraryImportPath = LibPath.str();
}

Expand Down
Loading