-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
android | ||
cargo_manifest_dir/external_crate | ||
crate_universe | ||
ios |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bazel-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |