Skip to content

Commit 3b3939d

Browse files
authored
Merge pull request #23039 from brentdax/target-practice-5.1-old-llvm
[old-llvm] [5.1] Module filename and location changes
2 parents 92ca393 + dce9583 commit 3b3939d

Some content is hidden

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

42 files changed

+625
-220
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ endfunction()
3030
# Compute the library subdirectory to use for the given sdk and
3131
# architecture, placing the result in 'result_var_name'.
3232
function(compute_library_subdir result_var_name sdk arch)
33-
set("${result_var_name}" "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}" PARENT_SCOPE)
33+
if(sdk IN_LIST SWIFT_APPLE_PLATFORMS)
34+
set("${result_var_name}" "${SWIFT_SDK_${sdk}_LIB_SUBDIR}" PARENT_SCOPE)
35+
else()
36+
set("${result_var_name}" "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}" PARENT_SCOPE)
37+
endif()
3438
endfunction()
3539

3640
function(_compute_lto_flag option out_var)
@@ -1156,6 +1160,8 @@ function(_add_swift_library_single target name)
11561160
# Don't set PROPERTY COMPILE_FLAGS or LINK_FLAGS directly.
11571161
set(c_compile_flags ${SWIFTLIB_SINGLE_C_COMPILE_FLAGS})
11581162
set(link_flags ${SWIFTLIB_SINGLE_LINK_FLAGS})
1163+
1164+
set(library_search_subdir "${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
11591165
set(library_search_directories
11601166
"${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}"
11611167
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"
@@ -1300,6 +1306,7 @@ function(_add_swift_library_single target name)
13001306
if(target_static)
13011307
set_property(TARGET "${target_static}" APPEND_STRING PROPERTY
13021308
COMPILE_FLAGS " ${c_compile_flags}")
1309+
# FIXME: The fallback paths here are going to be dynamic libraries.
13031310
set(library_search_directories
13041311
"${SWIFTSTATICLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}"
13051312
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"

cmake/modules/SwiftSource.cmake

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ function(handle_swift_sources
6161
endif()
6262

6363
if(swift_sources)
64-
compute_library_subdir(SWIFTSOURCES_LIBRARY_SUBDIR
65-
"${SWIFTSOURCES_SDK}" "${SWIFTSOURCES_ARCHITECTURE}")
66-
set(objsubdir "/${SWIFTSOURCES_LIBRARY_SUBDIR}")
64+
set(objsubdir "/${SWIFTSOURCES_SDK}/${SWIFTSOURCES_ARCHITECTURE}")
6765

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

@@ -255,6 +253,8 @@ function(_compile_swift_files
255253
"-nostdimport" "-parse-stdlib" "-module-name" "Swift")
256254
list(APPEND swift_flags "-Xfrontend" "-group-info-path"
257255
"-Xfrontend" "${GROUP_INFO_JSON_FILE}")
256+
else()
257+
list(APPEND swift_flags "-module-name" "${SWIFTFILE_MODULE_NAME}")
258258
endif()
259259

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

315315
set(module_base "${module_dir}/${SWIFTFILE_MODULE_NAME}")
316+
if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
317+
set(specific_module_dir "${module_base}.swiftmodule")
318+
set(module_base "${module_base}.swiftmodule/${SWIFTFILE_ARCHITECTURE}")
319+
endif()
316320
set(module_file "${module_base}.swiftmodule")
321+
set(module_doc_file "${module_base}.swiftdoc")
322+
323+
# FIXME: These don't really belong inside the swiftmodule, but there's not
324+
# an obvious alternate place to put them.
317325
set(sib_file "${module_base}.Onone.sib")
318326
set(sibopt_file "${module_base}.O.sib")
319327
set(sibgen_file "${module_base}.sibgen")
320-
set(module_doc_file "${module_base}.swiftdoc")
321328

322329
if(SWIFT_ENABLE_PARSEABLE_MODULE_INTERFACES)
323330
set(interface_file "${module_base}.swiftinterface")
324-
list(APPEND swift_flags "-emit-parseable-module-interface")
331+
list(APPEND swift_flags
332+
"-emit-parseable-module-interface-path" "${interface_file}")
325333
endif()
326334

327-
list(APPEND command_create_dirs
328-
COMMAND "${CMAKE_COMMAND}" -E make_directory "${module_dir}")
335+
if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
336+
list(APPEND command_create_dirs
337+
COMMAND "${CMAKE_COMMAND}" -E make_directory "${specific_module_dir}")
338+
else()
339+
list(APPEND command_create_dirs
340+
COMMAND "${CMAKE_COMMAND}" -E make_directory "${module_dir}")
341+
endif()
329342

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

352-
swift_install_in_component("${SWIFTFILE_INSTALL_IN_COMPONENT}"
353-
FILES ${module_outputs}
354-
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
355-
"${optional_arg}")
365+
if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
366+
swift_install_in_component("${SWIFTFILE_INSTALL_IN_COMPONENT}"
367+
DIRECTORY "${specific_module_dir}"
368+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}")
369+
else()
370+
swift_install_in_component("${SWIFTFILE_INSTALL_IN_COMPONENT}"
371+
FILES ${module_outputs}
372+
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}")
373+
endif()
356374

357375
set(line_directive_tool "${SWIFT_SOURCE_DIR}/utils/line-directive")
358376
set(swift_compiler_tool "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/swiftc")

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,8 @@ ERROR(cannot_return_value_from_void_func,none,
590590

591591
ERROR(sema_no_import,Fatal,
592592
"no such module '%0'", (StringRef))
593-
ERROR(sema_no_import_arch,Fatal,
594-
"could not find module '%0' for architecture '%1'; "
593+
ERROR(sema_no_import_target,Fatal,
594+
"could not find module '%0' for target '%1'; "
595595
"found: %2", (StringRef, StringRef, StringRef))
596596
ERROR(sema_no_import_repl,none,
597597
"no such module '%0'", (StringRef))

include/swift/Basic/Platform.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ namespace swift {
7474
///
7575
/// This is a stop-gap until full Triple support (ala Clang) exists within swiftc.
7676
StringRef getMajorArchitectureName(const llvm::Triple &triple);
77+
78+
/// Computes the normalized target triple used as the most preferred name for
79+
/// module loading.
80+
///
81+
/// For platforms with fat binaries, this canonicalizes architecture,
82+
/// vendor, and OS names, strips OS versions, and makes inferred environments
83+
/// explicit. For other platforms, it returns the unmodified triple.
84+
///
85+
/// The input triple should already be "normalized" in the sense that
86+
/// llvm::Triple::normalize() would not affect it.
87+
llvm::Triple getTargetSpecificModuleTriple(const llvm::Triple &triple);
7788
} // end namespace swift
7889

7990
#endif // SWIFT_BASIC_PLATFORM_H

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ class SerializedModuleLoaderBase : public ModuleLoader {
6868
/// to list the architectures that \e are present.
6969
///
7070
/// \returns true if an error diagnostic was emitted
71-
virtual bool maybeDiagnoseArchitectureMismatch(SourceLoc sourceLocation,
72-
StringRef moduleName,
73-
StringRef archName,
74-
StringRef directoryPath) {
71+
virtual bool maybeDiagnoseTargetMismatch(SourceLoc sourceLocation,
72+
StringRef moduleName,
73+
StringRef archName,
74+
StringRef directoryPath) {
7575
return false;
7676
}
7777

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

142-
bool maybeDiagnoseArchitectureMismatch(SourceLoc sourceLocation,
143-
StringRef moduleName,
144-
StringRef archName,
145-
StringRef directoryPath) override;
142+
bool maybeDiagnoseTargetMismatch(SourceLoc sourceLocation,
143+
StringRef moduleName,
144+
StringRef archName,
145+
StringRef directoryPath) override;
146146

147147
public:
148148
virtual ~SerializedModuleLoader();

lib/Basic/Platform.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/Basic/Platform.h"
14+
#include "llvm/ADT/StringExtras.h"
15+
#include "llvm/ADT/StringSwitch.h"
1416
#include "llvm/ADT/Triple.h"
1517

1618
using namespace swift;
@@ -186,3 +188,126 @@ StringRef swift::getMajorArchitectureName(const llvm::Triple &Triple) {
186188
return Triple.getArchName();
187189
}
188190
}
191+
192+
// The code below is responsible for normalizing target triples into the form
193+
// used to name target-specific swiftmodule, swiftinterface, and swiftdoc files.
194+
// If two triples have incompatible ABIs or can be distinguished by Swift #if
195+
// declarations, they should normalize to different values.
196+
//
197+
// This code is only really used on platforms with toolchains supporting fat
198+
// binaries (a single binary containing multiple architectures). On these
199+
// platforms, this code should strip unnecessary details from target triple
200+
// components and map synonyms to canonical values. Even values which don't need
201+
// any special canonicalization should be documented here as comments.
202+
//
203+
// (Fallback behavior does not belong here; it should be implemented in code
204+
// that calls this function, most importantly in SerializedModuleLoaderBase.)
205+
//
206+
// If you're trying to refer to this code to understand how Swift behaves and
207+
// you're unfamiliar with LLVM internals, here's a cheat sheet for reading it:
208+
//
209+
// * llvm::Triple is the type for a target name. It's a bit of a misnomer,
210+
// because it can contain three or four values: arch-vendor-os[-environment].
211+
//
212+
// * In .Cases and .Case, the last argument is the value the arguments before it
213+
// map to. That is, `.Cases("bar", "baz", "foo")` will return "foo" if it sees
214+
// "bar" or "baz".
215+
//
216+
// * llvm::Optional is similar to a Swift Optional: it either contains a value
217+
// or represents the absence of one. `None` is equivalent to `nil`; leading
218+
// `*` is equivalent to trailing `!`; conversion to `bool` is a not-`None`
219+
// check.
220+
221+
static StringRef
222+
getArchForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
223+
auto tripleArchName = triple.getArchName();
224+
225+
return llvm::StringSwitch<StringRef>(tripleArchName)
226+
.Cases("arm64", "aarch64", "arm64")
227+
.Cases("x86_64", "amd64", "x86_64")
228+
.Cases("i386", "i486", "i586", "i686", "i786", "i886", "i986",
229+
"i386")
230+
.Cases("unknown", "", "unknown")
231+
// These values are also supported, but are handled by the default case below:
232+
// .Case ("armv7s", "armv7s")
233+
// .Case ("armv7k", "armv7k")
234+
// .Case ("armv7", "armv7")
235+
.Default(tripleArchName);
236+
}
237+
238+
static StringRef
239+
getVendorForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
240+
// We unconditionally normalize to "apple" because it's relatively common for
241+
// build systems to omit the vendor name or use an incorrect one like
242+
// "unknown". Most parts of the compiler ignore the vendor, so you might not
243+
// notice such a mistake.
244+
//
245+
// Please don't depend on this behavior--specify 'apple' if you're building
246+
// for an Apple platform.
247+
248+
assert(triple.isOSDarwin() &&
249+
"shouldn't normalize non-Darwin triple to 'apple'");
250+
251+
return "apple";
252+
}
253+
254+
static StringRef
255+
getOSForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
256+
auto tripleOSName = triple.getOSName();
257+
258+
// Truncate the OS name before the first digit. "Digit" here is ASCII '0'-'9'.
259+
auto tripleOSNameNoVersion = tripleOSName.take_until(llvm::isDigit);
260+
261+
return llvm::StringSwitch<StringRef>(tripleOSNameNoVersion)
262+
.Cases("macos", "macosx", "darwin", "macos")
263+
.Cases("unknown", "", "unknown")
264+
// These values are also supported, but are handled by the default case below:
265+
// .Case ("ios", "ios")
266+
// .Case ("tvos", "tvos")
267+
// .Case ("watchos", "watchos")
268+
.Default(tripleOSNameNoVersion);
269+
}
270+
271+
static Optional<StringRef>
272+
getEnvironmentForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) {
273+
auto tripleEnvironment = triple.getEnvironmentName();
274+
275+
// If the environment is empty, infer a "simulator" environment based on the
276+
// OS and architecture combination. This feature is deprecated and exists for
277+
// backwards compatibility only; build systems should pass the "simulator"
278+
// environment explicitly if they know they're building for a simulator.
279+
if (tripleEnvironment == "" && swift::tripleIsAnySimulator(triple))
280+
return StringRef("simulator");
281+
282+
return llvm::StringSwitch<Optional<StringRef>>(tripleEnvironment)
283+
.Cases("unknown", "", None)
284+
// These values are also supported, but are handled by the default case below:
285+
// .Case ("simulator", StringRef("simulator"))
286+
.Default(tripleEnvironment);
287+
}
288+
289+
llvm::Triple swift::getTargetSpecificModuleTriple(const llvm::Triple &triple) {
290+
// isOSDarwin() returns true for all Darwin-style OSes, including macOS, iOS,
291+
// etc.
292+
if (triple.isOSDarwin()) {
293+
StringRef newArch = getArchForAppleTargetSpecificModuleTriple(triple);
294+
295+
StringRef newVendor = getVendorForAppleTargetSpecificModuleTriple(triple);
296+
297+
StringRef newOS = getOSForAppleTargetSpecificModuleTriple(triple);
298+
299+
Optional<StringRef> newEnvironment =
300+
getEnvironmentForAppleTargetSpecificModuleTriple(triple);
301+
302+
if (!newEnvironment)
303+
// Generate an arch-vendor-os triple.
304+
return llvm::Triple(newArch, newVendor, newOS);
305+
306+
// Generate an arch-vendor-os-environment triple.
307+
return llvm::Triple(newArch, newVendor, newOS, *newEnvironment);
308+
}
309+
310+
// Other platforms get no normalization.
311+
return triple;
312+
}
313+

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ static void updateRuntimeLibraryPath(SearchPathOptions &SearchPathOpts,
4949
llvm::sys::path::append(LibPath, getPlatformNameForTriple(Triple));
5050
SearchPathOpts.RuntimeLibraryPath = LibPath.str();
5151

52-
llvm::sys::path::append(LibPath, swift::getMajorArchitectureName(Triple));
52+
if (!Triple.isOSDarwin())
53+
llvm::sys::path::append(LibPath, swift::getMajorArchitectureName(Triple));
5354
SearchPathOpts.RuntimeLibraryImportPath = LibPath.str();
5455
}
5556

0 commit comments

Comments
 (0)