Skip to content

Commit

Permalink
working build
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Schore <[email protected]>
  • Loading branch information
goaway committed Jun 27, 2019
1 parent e8d8ec2 commit 63a5c69
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 11 deletions.
134 changes: 134 additions & 0 deletions bazel/swift_static_framework.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
"""
This rules creates a fat static framework that can be included later with
static_framework_import
"""

load("@build_bazel_rules_swift//swift:swift.bzl", "SwiftInfo", "swift_library")

MINIMUM_IOS_VERSION = "11.0"

_PLATFORM_TO_SWIFTMODULE = {
"ios_arm64": "arm64",
"ios_x86_64": "x86_64",
}

def _tar_binary_arg(module_name, input_file):
return "{module_name}.framework/{module_name}={file_path}".format(
module_name = module_name,
file_path = input_file.path,
)

def _tar_swift_arg(module_name, swift_identifier, input_file):
return "{module_name}.framework/Modules/{module_name}.swiftmodule/{swift_identifier}.{ext}={file_path}".format(
module_name = module_name,
swift_identifier = swift_identifier,
ext = input_file.extension,
file_path = input_file.path,
)

def _prebuilt_swift_static_framework_impl(ctx):
module_name = ctx.attr.framework_name
fat_file = ctx.outputs.fat_file

input_archives = []
input_modules_docs = []
tar_args = [_tar_binary_arg(module_name, fat_file)]

for platform, archive in ctx.split_attr.archive.items():
swiftmodule_identifier = _PLATFORM_TO_SWIFTMODULE[platform]
if not swiftmodule_identifier:
fail("Unhandled platform '{}'".format(platform))
library = archive[CcInfo].linking_context.libraries_to_link.to_list()[0].pic_static_library
swift_info = archive[SwiftInfo]
swiftdoc = swift_info.direct_swiftdocs[0]
swiftmodule = swift_info.direct_swiftmodules[0]

input_archives.append(library)
input_modules_docs += [swiftdoc, swiftmodule]
tar_args += [
_tar_swift_arg(module_name, swiftmodule_identifier, swiftdoc),
_tar_swift_arg(module_name, swiftmodule_identifier, swiftmodule),
]

ctx.actions.run(
inputs = input_archives,
outputs = [fat_file],
mnemonic = "LipoSwiftLibraries",
progress_message = "Combining libraries for {}".format(module_name),
executable = "lipo",
arguments = ["-create", "-output", fat_file.path] + [x.path for x in input_archives],
)

output_file = ctx.outputs.output_file
ctx.actions.run(
inputs = input_modules_docs + [fat_file],
outputs = [output_file],
mnemonic = "CreateSwiftFrameworkTar",
progress_message = "Creating framework tar for {}".format(module_name),
executable = ctx.executable._create_tar,
arguments = [output_file.path] + tar_args,
)

return [
DefaultInfo(
files = depset([output_file]),
),
]

_prebuilt_swift_static_framework = rule(
implementation = _prebuilt_swift_static_framework_impl,
attrs = dict(
archive = attr.label(
mandatory = True,
providers = [CcInfo, SwiftInfo],
cfg = apple_common.multi_arch_split,
),
framework_name = attr.string(mandatory = True),
minimum_os_version = attr.string(default = MINIMUM_IOS_VERSION),
platform_type = attr.string(
default = str(apple_common.platform_type.ios),
),
_create_tar = attr.label(
default = "//tools:create_tar",
cfg = "host",
executable = True,
),
),
outputs = {
"fat_file": "%{framework_name}.fat",
"output_file": "%{framework_name}.tar.bz2",
},
)

def prebuilt_swift_static_framework(
name,
version = None,
srcs = [],
copts = [],
deps = []):
"""Create a static library, and static framework target for a swift module
Args:
name: The name of the module, the framework's name will be this name
appending Framework so you can depend on this from other modules
version: The version of the library for use with metadata files
srcs: Custom source paths for the swift files
copts: Any custom swiftc opts passed through to the swift_library
deps: Any deps the swift_library requires
"""
srcs = srcs or native.glob(["Sources/**/*.swift"])
swift_library(
name = name,
module_name = name,
srcs = srcs,
copts = copts,
deps = deps,
visibility = ["//visibility:public"],
)

framework_name = name + "Framework"
_prebuilt_swift_static_framework(
name = framework_name,
framework_name = name,
archive = name,
)
7 changes: 5 additions & 2 deletions library/objective-c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ licenses(["notice"]) # Apache 2

objc_library(
name = "envoy_engine_objc_lib",
hdrs = [
"EnvoyEngine.h",
],
srcs = [
"Envoy.h",
"Envoy.mm",
"EnvoyEngine.h",
"EnvoyEngine.mm",
],
copts = ["-std=c++14"],
visibility = ["//visibility:public"],
Expand Down
4 changes: 2 additions & 2 deletions library/objective-c/EnvoyEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

/// Run the Envoy engine with the provided config and log level. This call is synchronous
/// and will not yield.
+ (int)runWithConfig:(NSString *)config logLevel:(NSString *)logLevel;
+ (int)runWithConfig:(NSString *)config;

/// Run the Envoy engine with the provided config and log level. This call is synchronous
/// and will not yield.
+ (int)runWithConfig:(NSString *)config;
+ (int)runWithConfig:(NSString *)config logLevel:(NSString *)logLevel;

@end
4 changes: 2 additions & 2 deletions library/objective-c/EnvoyEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

@implementation EnvoyEngine

+ (int)runWithConfig:(NSString *config) {
+ (int)runWithConfig:(NSString *)config {
return [self runWithConfig:config logLevel:@"info"];
}

+ (void)runWithConfig:(NSString *config) logLevel:(NSString *logLevel) {
+ (int)runWithConfig:(NSString *)config logLevel:(NSString *)logLevel {
try {
return run_envoy(config.UTF8String, logLevel.UTF8String);
} catch (NSException *e) {
Expand Down
9 changes: 9 additions & 0 deletions library/swift/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
licenses(["notice"]) # Apache 2

load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
load("//bazel:swift_static_framework.bzl", "prebuilt_swift_static_framework")

prebuilt_swift_static_framework(
name = "swift_framework",
srcs = ["Envoy.swift"],
deps = ["//library/objective-c:envoy_engine_objc_lib"]
)

swift_library(
name = "envoy_swift_lib",
srcs = [
Expand Down
10 changes: 5 additions & 5 deletions library/swift/Envoy.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import Foundation
import EnvoyEngine
import library_objective_c_envoy_engine_objc_lib

public class Envoy {
private let runner: EnvoyRunner

public var isRunning: Bool {
runner.isExecuting()
return runner.isExecuting
}

public var isTerminated: Bool {
runner.isFinished()
return runner.isFinished
}

public init(config: String, logLevel: String) {
Expand All @@ -30,8 +30,8 @@ public class Envoy {
self.logLevel = logLevel
}

func main() {
EngineEngine.run(config: config, logLevel: logLevel)
override func main() {
EnvoyEngine.run(withConfig: config, logLevel: logLevel)
}
}
}

0 comments on commit 63a5c69

Please sign in to comment.