Skip to content

Commit

Permalink
Add proper ld script generation and include path for all examples
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgerhardt committed Feb 18, 2024
1 parent a91dd46 commit 060273b
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 145 deletions.
19 changes: 17 additions & 2 deletions .github/add_include.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
from os.path import join
import os.path
Import("env", "projenv")
# Rely on the fact that all examples to build_src_filter = +<EXAMPLE_THEY_WANT>
# And figure out correct include path from that
src_filter = str(env.subst("$SRC_FILTER"))
# A string with e.g. "+<ch32v003fun> +<examples/blink>"
# rudimentary parsing is fine for CI
src_filters = src_filter.split("+")
example_folder = ""
for filter in src_filters:
# cleanup unwanted characters
f = filter.replace("<", "", 1).replace(">", "", 1)
# starts with "examples" and looks like a path?
if f.startswith("examples") and ("/" in f or os.path.sep in f):
example_folder = f
break

# propagate to all construction environments
for e in env, projenv, DefaultEnvironment():
e.Append(CCFLAGS=[("-I", join("examples", env.subst("$PIOENV")))])
e.Append(CCFLAGS=[("-I", example_folder)])
93 changes: 81 additions & 12 deletions .github/gen_ldscript.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,94 @@
from os.path import join
import sys
Import("env")

mcu_package = 1
target_mcu = 0
mcu_ld = 0
linkerscript_cmd = env.Command(
join("$BUILD_DIR", "ldscript.ld"), # $TARGET
join(env.subst("$PROJECT_DIR"), "ch32v003fun", "ch32v003fun.ld"), # $SOURCE
# Returns TARGET_MCU, MCU_PACKAGE and TARGET_MCU_LD values
# In the same way that the Makefile would do.
def get_ld_defines(chip_name: str):
target_mcu: str = ""
mcu_package: int = 0
target_mcu_ld: int = 0
if chip_name.startswith("ch32v003"):
target_mcu = "CH32V003"
target_mcu_ld = 0
else:
mcu_package = 1
if chip_name.startswith("ch32v10"):
target_mcu = chip_name.upper()[0:len("ch32v10x")]
if "r8" in chip_name:
mcu_package = 1
elif "c8" in chip_name:
mcu_package = 1
elif "c6" in chip_name:
mcu_package = 2
target_mcu_ld = 1
elif chip_name.startswith("ch32v20"):
target_mcu = chip_name.upper()[0:len("ch32v20x")]
if "f8" in chip_name:
mcu_package = 1
elif "g8" in chip_name:
mcu_package = 1
elif "k8" in chip_name:
mcu_package = 1
elif "c8" in chip_name:
mcu_package = 1
elif "f6" in chip_name:
mcu_package = 2
elif "k6" in chip_name:
mcu_package = 2
elif "c6" in chip_name:
mcu_package = 2
elif "rb" in chip_name:
mcu_package = 3
elif "gb" in chip_name:
mcu_package = 3
elif "cb" in chip_name:
mcu_package = 3
elif "wb" in chip_name:
mcu_package = 3
target_mcu_ld = 2
elif chip_name.startswith("ch32v30"):
target_mcu = chip_name.upper()[0:len("ch32v30x")]
if "rc" in chip_name:
mcu_package = 1
elif "vc" in chip_name:
mcu_package = 1
elif "wc" in chip_name:
mcu_package = 1
elif "cb" in chip_name:
mcu_package = 2
elif "fb" in chip_name:
mcu_package = 2
elif "rb" in chip_name:
mcu_package = 2
target_mcu_ld = 3
else:
sys.stdout.write("Unkonwn MCU %s\n" % chip_name)
env.Exit(-1)
return (target_mcu, mcu_package, target_mcu_ld)

# Retrieve MCU name from selected board info
board = env.BoardConfig()
chip_name = str(board.get("build.mcu", "")).lower()
# retrieve needed macro values
target_mcu, mcu_package, target_mcu_ld = get_ld_defines(chip_name)

# Let the LD script be generated right before the .elf is built
env.AddPreAction(
"$BUILD_DIR/${PROGNAME}.elf",
env.VerboseAction(" ".join([
"$CC",
"-E",
"-P",
"-x",
"c",
"-DTARGET_MCU=%d" % target_mcu,
"-DTARGET_MCU=%s" % target_mcu,
"-DMCU_PACKAGE=%d" % mcu_package,
"-DTARGET_MCU_LD=%d" % mcu_ld,
"$SOURCE"
"-DTARGET_MCU_LD=%d" % target_mcu_ld,
join("ch32v003fun", "ch32v003fun.ld"),
">",
"$TARGET"
]), "Generating linkerscript $BUILD_DIR/ldscript.ld")
join("$BUILD_DIR", "ldscript.ld")
]), "Building %s" % join("$BUILD_DIR", "ldscript.ld"))
)
env.Depends("$BUILD_DIR/${PROGNAME}.elf", linkerscript_cmd)
# Already put in the right path for the to-be-generated file
env.Replace(LDSCRIPT_PATH=join("$BUILD_DIR", "ldscript.ld"))
115 changes: 0 additions & 115 deletions ch32v003fun/generated_ch32v003.ld

This file was deleted.

Loading

0 comments on commit 060273b

Please sign in to comment.