From 349e478687a00e28f8a68a2b90e85de4b5c2eb02 Mon Sep 17 00:00:00 2001 From: Dmitry Shevchenko Date: Sat, 5 Nov 2016 16:02:18 +0000 Subject: Allow swiftc to correctly recognize main.swift as script target. * This change removes forced -parse-as-library mode in favor of the driver figuring out how to parse each source. This means that main.swift will be parsed as a script and can contain top-level expressions. -- MOS_MIGRATED_REVID=138285669 --- tools/build_defs/apple/swift.bzl | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'tools/build_defs/apple/swift.bzl') diff --git a/tools/build_defs/apple/swift.bzl b/tools/build_defs/apple/swift.bzl index 336329f537..447134377e 100644 --- a/tools/build_defs/apple/swift.bzl +++ b/tools/build_defs/apple/swift.bzl @@ -116,6 +116,21 @@ def _swift_xcrun_args(ctx): return [] +def _swift_parsing_flags(ctx): + """Returns additional parsing flags for swiftc.""" + srcs = ctx.files.srcs + + # swiftc has two different parsing modes: script and library. + # The difference is that in script mode top-level expressions are allowed. + # This mode is triggered when the file compiled is called main.swift. + # Additionally, script mode is used when there's just one file in the + # compilation. we would like to avoid that and therefore force library mode + # when there's only one source and it's not called main. + if len(srcs) == 1 and srcs[0].basename != "main.swift": + return ["-parse-as-library"] + return [] + + def _is_valid_swift_module_name(string): """Returns True if the string is a valid Swift module name.""" if not string: @@ -288,7 +303,6 @@ def _swift_library_impl(ctx): module_name, "-emit-objc-header-path", output_header.path, - "-parse-as-library", "-target", target, "-sdk", @@ -297,11 +311,14 @@ def _swift_library_impl(ctx): module_cache_path(ctx), "-output-file-map", swiftc_output_map_file.path, - ] + _swift_compilation_mode_flags(ctx) + _swift_bitcode_flags(ctx) + ] if ctx.configuration.coverage_enabled: args.extend(["-profile-generate", "-profile-coverage-mapping"]) + args.extend(_swift_compilation_mode_flags(ctx)) + args.extend(_swift_bitcode_flags(ctx)) + args.extend(_swift_parsing_flags(ctx)) args.extend(srcs_args) args.extend(include_args) args.extend(framework_args) -- cgit v1.2.3