diff --git a/platformio/builder/tools/platformio.py b/platformio/builder/tools/platformio.py index 818c07ff..82b81a0f 100644 --- a/platformio/builder/tools/platformio.py +++ b/platformio/builder/tools/platformio.py @@ -44,7 +44,88 @@ def scons_patched_match_splitext(path, suffixes=None): return tokens -def _build_project_deps(env): +def GetBuildType(env): + return ( + "debug" + if ( + set(["debug", "sizedata"]) & set(COMMAND_LINE_TARGETS) + or env.GetProjectOption("build_type") == "debug" + ) + else "release" + ) + + +def BuildProgram(env): + env.ProcessProgramDeps() + env.ProcessProjectDeps() + + program = env.Program( + os.path.join("$BUILD_DIR", env.subst("$PROGNAME")), env["PIOBUILDFILES"] + ) + env.Replace(PIOMAINPROG=program) + + AlwaysBuild( + env.Alias( + "checkprogsize", + program, + env.VerboseAction(env.CheckUploadSize, "Checking size $PIOMAINPROG"), + ) + ) + + print("Building in %s mode" % env.GetBuildType()) + + return program + + +def ProcessProgramDeps(env): + def _append_pio_macros(): + env.AppendUnique( + CPPDEFINES=[ + ( + "PLATFORMIO", + int("{0:02d}{1:02d}{2:02d}".format(*pioversion_to_intstr())), + ) + ] + ) + + _append_pio_macros() + + env.PrintConfiguration() + + # fix ASM handling under non case-sensitive OS + if not Util.case_sensitive_suffixes(".s", ".S"): + env.Replace(AS="$CC", ASCOM="$ASPPCOM") + + # process extra flags from board + if "BOARD" in env and "build.extra_flags" in env.BoardConfig(): + env.ProcessFlags(env.BoardConfig().get("build.extra_flags")) + + # apply user flags + env.ProcessFlags(env.get("BUILD_FLAGS")) + + # process framework scripts + env.BuildFrameworks(env.get("PIOFRAMEWORK")) + + if env.GetBuildType() == "debug": + env.ConfigureDebugFlags() + + # remove specified flags + env.ProcessUnFlags(env.get("BUILD_UNFLAGS")) + + if "__test" in COMMAND_LINE_TARGETS: + env.ConfigureTestTarget() + + # append into the beginning a main LD script + if env.get("LDSCRIPT_PATH") and not any("-Wl,-T" in f for f in env["LINKFLAGS"]): + env.Prepend(LINKFLAGS=["-T", env.subst("$LDSCRIPT_PATH")]) + + # enable "cyclic reference" for linker + if env.get("LIBS") and env.GetCompilerType() == "gcc": + env.Prepend(_LIBFLAGS="-Wl,--start-group ") + env.Append(_LIBFLAGS=" -Wl,--end-group") + + +def ProcessProjectDeps(env): project_lib_builder = env.ConfigureProjectLibBuilder() # prepend project libs to the beginning of list @@ -85,78 +166,6 @@ def _build_project_deps(env): Export("projenv") -def BuildProgram(env): - def _append_pio_macros(): - env.AppendUnique( - CPPDEFINES=[ - ( - "PLATFORMIO", - int("{0:02d}{1:02d}{2:02d}".format(*pioversion_to_intstr())), - ) - ] - ) - - _append_pio_macros() - - env.PrintConfiguration() - - # fix ASM handling under non case-sensitive OS - if not Util.case_sensitive_suffixes(".s", ".S"): - env.Replace(AS="$CC", ASCOM="$ASPPCOM") - - # process extra flags from board - if "BOARD" in env and "build.extra_flags" in env.BoardConfig(): - env.ProcessFlags(env.BoardConfig().get("build.extra_flags")) - - # apply user flags - env.ProcessFlags(env.get("BUILD_FLAGS")) - - # process framework scripts - env.BuildFrameworks(env.get("PIOFRAMEWORK")) - - is_build_type_debug = ( - set(["debug", "sizedata"]) & set(COMMAND_LINE_TARGETS) - or env.GetProjectOption("build_type") == "debug" - ) - if is_build_type_debug: - env.ConfigureDebugFlags() - - # remove specified flags - env.ProcessUnFlags(env.get("BUILD_UNFLAGS")) - - if "__test" in COMMAND_LINE_TARGETS: - env.ConfigureTestTarget() - - # append into the beginning a main LD script - if env.get("LDSCRIPT_PATH") and not any("-Wl,-T" in f for f in env["LINKFLAGS"]): - env.Prepend(LINKFLAGS=["-T", env.subst("$LDSCRIPT_PATH")]) - - # enable "cyclic reference" for linker - if env.get("LIBS") and env.GetCompilerType() == "gcc": - env.Prepend(_LIBFLAGS="-Wl,--start-group ") - env.Append(_LIBFLAGS=" -Wl,--end-group") - - # build project with dependencies - _build_project_deps(env) - - program = env.Program( - os.path.join("$BUILD_DIR", env.subst("$PROGNAME")), env["PIOBUILDFILES"] - ) - env.Replace(PIOMAINPROG=program) - - AlwaysBuild( - env.Alias( - "checkprogsize", - program, - env.VerboseAction(env.CheckUploadSize, "Checking size $PIOMAINPROG"), - ) - ) - - print("Building in %s mode" % ("debug" if is_build_type_debug else "release")) - - return program - - def ParseFlagsExtended(env, flags): # pylint: disable=too-many-branches if not isinstance(flags, list): flags = [flags] @@ -343,7 +352,10 @@ def exists(_): def generate(env): + env.AddMethod(GetBuildType) env.AddMethod(BuildProgram) + env.AddMethod(ProcessProgramDeps) + env.AddMethod(ProcessProjectDeps) env.AddMethod(ParseFlagsExtended) env.AddMethod(ProcessFlags) env.AddMethod(ProcessUnFlags)