Skip to content

Commit

Permalink
[EXAMPLES] better examples (#23)
Browse files Browse the repository at this point in the history
* [EXAMPLES] better examples

* [EXAMPLES] fix tools
  • Loading branch information
hexdae authored Nov 17, 2023
1 parent adf6841 commit fb31092
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 5 deletions.
18 changes: 13 additions & 5 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ module(
compatibility_level = 1,
)

# INTERNAL
bazel_dep(name = "aspect_bazel_lib", version = "2.0.0", dev_dependency = True)
bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True)

arm_none_eabi = use_extension("@arm_none_eabi//:extensions.bzl", "arm_none_eabi")
arm_none_eabi.toolchain(version = "9.2.1")
use_repo(
Expand All @@ -18,4 +14,16 @@ use_repo(
"arm_none_eabi_windows_x86_64",
)

register_toolchains("@arm_none_eabi//toolchain:all")
# DEV ONLY (not needed for release)
bazel_dep(name = "aspect_bazel_lib", version = "2.0.0", dev_dependency = True)
bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True)

register_toolchains(
"@arm_none_eabi//toolchain:all",
dev_dependency = True,
)

register_toolchains(
"@arm_none_eabi//examples/custom/toolchain:all",
dev_dependency = True,
)
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Examples

These examples can be used to understand the different features of this toolchain
27 changes: 27 additions & 0 deletions examples/custom/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# project/BUILD.bazel

load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")

cc_binary(
name = "binary",
srcs = ["main.c"],
linkopts = [
"-Wl,-entry=main",
],
target_compatible_with = [
"@platforms//os:none",
],
)

platform_transition_filegroup(
name = "cortex_m3_elf",
srcs = [":binary"],
target_platform = "//examples/custom/platform:cortex_m3",
)

platform_transition_filegroup(
name = "cortex_m4_elf",
srcs = [":binary"],
target_platform = "//examples/custom/platform:cortex_m4",
)
5 changes: 5 additions & 0 deletions examples/custom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Custom toolchain example

This example shows how to create a custom toolchain with different CPUs based
on custom constraint values. The toolchain has different flags for each CPU and
is applied to all dependencies.
15 changes: 15 additions & 0 deletions examples/custom/cpu/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
constraint_setting(
name = "arm_none_eabi_cpu",
)

constraint_value(
name = "cortex_m3",
constraint_setting = "arm_none_eabi_cpu",
visibility = ["//visibility:public"],
)

constraint_value(
name = "cortex_m4",
constraint_setting = "arm_none_eabi_cpu",
visibility = ["//visibility:public"],
)
11 changes: 11 additions & 0 deletions examples/custom/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// The simplest possible main function

int main()
{
return 0;
}

extern void _start()
{
main();
}
18 changes: 18 additions & 0 deletions examples/custom/platform/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

platform(
name = "cortex_m4",
constraint_values = [
"@platforms//os:none",
"//examples/custom/cpu:cortex_m4"
],
visibility = ["//visibility:public"],
)

platform(
name = "cortex_m3",
constraint_values = [
"@platforms//os:none",
"//examples/custom/cpu:cortex_m3"
],
visibility = ["//visibility:public"],
)
43 changes: 43 additions & 0 deletions examples/custom/toolchain/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
load("@arm_none_eabi//toolchain:toolchain.bzl", "arm_none_eabi_toolchain")

arm_none_eabi_toolchain(
name = "cortex_m3_toolchain",
target_compatible_with = [
"@platforms//os:none",
"//examples/custom/cpu:cortex_m3"
],
copts = [
"-mcpu=cortex-m3",
"-mthumb",
"-mfloat-abi=hard",
"-mfpu=fpv4-sp-d16",
],
linkopts = [
"-mcpu=cortex-m3",
"-mthumb",
"-mfloat-abi=hard",
"-mfpu=fpv4-sp-d16",
"-nostartfiles",
],
)

arm_none_eabi_toolchain(
name = "cortex_m4_toolchain",
target_compatible_with = [
"@platforms//os:none",
"//examples/custom/cpu:cortex_m4"
],
copts = [
"-mcpu=cortex-m4",
"-mthumb",
"-mfloat-abi=hard",
"-mfpu=fpv4-sp-d16",
],
linkopts = [
"-mcpu=cortex-m4",
"-mthumb",
"-mfloat-abi=hard",
"-mfpu=fpv4-sp-d16",
"-nostartfiles",
],
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions examples/gdb/BUILD → examples/tools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ native_binary(
out = "gdb",
visibility = ["//visibility:public"],
)

native_binary(
name = "readelf",
src = "@arm_none_eabi//:readelf",
out = "readelf",
visibility = ["//visibility:public"],
)
Empty file added examples/tools/README.md
Empty file.

0 comments on commit fb31092

Please sign in to comment.