Skip to content

Commit

Permalink
Add iOS examples
Browse files Browse the repository at this point in the history
  • Loading branch information
keith committed Sep 8, 2022
1 parent 4845af6 commit 3ecb2c5
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ tasks:
working_directory: examples/android
build_targets:
- "//:android_app"
ios_examples:
name: iOS Examples
platform: macos
working_directory: examples/ios
build_targets:
- "//..."

buildifier:
version: latest
Expand Down
1 change: 1 addition & 0 deletions examples/.bazelignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
android
cargo_manifest_dir/external_crate
crate_universe
ios
4 changes: 4 additions & 0 deletions examples/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ rules_rust_dependencies()

rust_register_toolchains(
edition = "2018",
extra_target_triples = [
"aarch64-apple-ios-sim",
"x86_64-apple-ios",
],
)

load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")
Expand Down
1 change: 1 addition & 0 deletions examples/ios/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bazel-*
91 changes: 91 additions & 0 deletions examples/ios/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application")
load("@build_bazel_rules_apple//apple:macos.bzl", "macos_application")
load("@rules_rust//rust:defs.bzl", "rust_library")

cc_library(
name = "allocator_library",
srcs = ["allocator_library.cc"],
tags = ["manual"],
)

rust_library(
name = "rust_lib",
srcs = ["demo.rs"],
edition = "2018",
tags = ["manual"],
deps = [":allocator_library"],
)

# This being required is a bug in objc_library, ideally 'main_lib' could depend directly on 'rust_lib'
cc_library(
name = "shim",
tags = ["manual"],
deps = [":rust_lib"],
)

objc_library(
name = "main_lib",
srcs = ["main_lib.m"],
tags = ["manual"],
deps = [":shim"],
)

ios_application(
name = "ios_app",
bundle_id = "com.example.iosapp",
families = ["iphone"],
infoplists = ["Info.plist"],
minimum_os_version = "13.0",
deps = [":main_lib"],
)

macos_application(
name = "macos_app",
bundle_id = "com.example.macosapp",
infoplists = ["Info.plist"],
minimum_os_version = "10.15",
deps = [":main_lib"],
)

platform(
name = "macos_x86_64",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:macos",
],
)

platform(
name = "macos_arm64",
constraint_values = [
"@platforms//cpu:arm64",
"@platforms//os:macos",
],
)

platform(
name = "ios_x86_64",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:ios",
"@build_bazel_apple_support//constraints:simulator",
],
)

platform(
name = "ios_sim_arm64",
constraint_values = [
"@platforms//cpu:arm64",
"@platforms//os:ios",
"@build_bazel_apple_support//constraints:simulator",
],
)

platform(
name = "ios_arm64",
constraint_values = [
"@platforms//cpu:arm64",
"@platforms//os:ios",
"@build_bazel_apple_support//constraints:device",
],
)
38 changes: 38 additions & 0 deletions examples/ios/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>arm64</string>
</array>
<key>UIStatusBarHidden</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDefault</string>
</dict>
</plist>
50 changes: 50 additions & 0 deletions examples/ios/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
workspace(name = "ios_examples")

# Users of `rules_rust` will commonly be unable to load it
# using a `local_repository`. Instead, to setup the rules,
# please see https://bazelbuild.github.io/rules_rust/#setup
local_repository(
name = "rules_rust",
path = "../..",
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains(
edition = "2018",
extra_target_triples = [
"aarch64-apple-ios-sim",
"x86_64-apple-ios",
],
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "build_bazel_rules_apple",
sha256 = "a5f00fd89eff67291f6cd3efdc8fad30f4727e6ebb90718f3f05bbf3c3dd5ed7",
url = "https://github.com/bazelbuild/rules_apple/releases/download/0.33.0/rules_apple.0.33.0.tar.gz",
)

load(
"@build_bazel_rules_apple//apple:repositories.bzl",
"apple_rules_dependencies",
)

apple_rules_dependencies()

load(
"@build_bazel_rules_swift//swift:repositories.bzl",
"swift_rules_dependencies",
)

swift_rules_dependencies()

load(
"@build_bazel_apple_support//lib:repositories.bzl",
"apple_support_dependencies",
)

apple_support_dependencies()
52 changes: 52 additions & 0 deletions examples/ios/allocator_library.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdint.h>

// This file has some exciting magic to get Rust code linking in a cc_binary.
// The Rust compiler generates some similar symbol aliases when it links, so we
// have to do it manually. We mark all our symbols as weak so that linking this
// via Rust tooling to produce a binary with a Rust main works.
//
// It is intended to be used in rust_toolchain.allocator_library.
//
// https://github.com/rust-lang/rust/blob/master/library/alloc/src/alloc.rs
// and https://github.com/rust-lang/rust/blob/master/library/std/src/alloc.rs
// are the best source of docs I've found on these functions and variables.
// https://doc.rust-lang.org/std/alloc/index.html talks about how this is
// intended to be used.
//
// Also note
// https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html for
// the sizes of the various integer types.
//
// This file strongly assumes that the default allocator is used. It will
// not work with any other allocated switched in via `#[global_allocator]`.

// New feature as of https://github.com/rust-lang/rust/pull/88098.
__attribute__((weak)) uint8_t __rust_alloc_error_handler_should_panic = 0;

extern "C" uint8_t *__rdl_alloc(uintptr_t size, uintptr_t align);
extern "C" __attribute__((weak))
uint8_t *__rust_alloc(uintptr_t size, uintptr_t align) {
return __rdl_alloc(size, align);
}
extern "C" void __rdl_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align);
extern "C" __attribute__((weak))
void __rust_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align) {
__rdl_dealloc(ptr, size, align);
}
extern "C" uint8_t *__rdl_realloc(uint8_t *ptr, uintptr_t old_size, uintptr_t align,
uintptr_t new_size);
extern "C" __attribute__((weak))
uint8_t *__rust_realloc(uint8_t *ptr, uintptr_t old_size, uintptr_t align,
uintptr_t new_size) {
return __rdl_realloc(ptr, old_size, align, new_size);
}
extern "C" uint8_t *__rdl_alloc_zeroed(uintptr_t size, uintptr_t align);
extern "C" __attribute__((weak))
uint8_t *__rust_alloc_zeroed(uintptr_t size, uintptr_t align) {
return __rdl_alloc_zeroed(size, align);
}
extern "C" void __rdl_oom(uintptr_t size, uintptr_t align);
extern "C" __attribute__((weak))
void __rust_alloc_error_handler(uintptr_t size, uintptr_t align) {
__rdl_oom(size, align);
}
9 changes: 9 additions & 0 deletions examples/ios/demo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[no_mangle]
pub extern fn print_something_from_rust() {
println!("Ferris says hello!");
}

#[no_mangle]
pub extern fn get_a_value_from_rust() -> i32 {
42
}
11 changes: 11 additions & 0 deletions examples/ios/main_lib.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>

// Instead of doing this manually https://github.com/dtolnay/cxx could be used for complex examples
extern int32_t get_a_value_from_rust(void);
extern void print_something_from_rust(void);

int main() {
print_something_from_rust();
printf("Some value from rust: %d\n", get_a_value_from_rust());
return 0;
}
20 changes: 20 additions & 0 deletions examples/ios/platform_mappings
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
flags:
--cpu=darwin_x86_64
--apple_platform_type=macos
//:macos_x86_64

--cpu=darwin_arm64
--apple_platform_type=macos
//:macos_arm64

--cpu=ios_x86_64
--apple_platform_type=ios
//:ios_x86_64

--cpu=ios_sim_arm64
--apple_platform_type=ios
//:ios_sim_arm64

--cpu=ios_arm64
--apple_platform_type=ios
//:ios_arm64

0 comments on commit 3ecb2c5

Please sign in to comment.