Files
platformio-core/platformio/builder/tools/piomisc.py

150 lines
4.3 KiB
Python
Raw Normal View History

# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2020-06-09 18:43:50 +03:00
import os
import sys
2016-08-27 19:30:38 +03:00
from platformio import fs, util
from platformio.proc import exec_command
@util.memoized()
2022-04-19 11:36:05 +03:00
def GetCompilerType(env):
if env.subst("$CC").endswith("-gcc"):
return "gcc"
try:
2020-06-09 18:43:50 +03:00
sysenv = os.environ.copy()
sysenv["PATH"] = str(env["ENV"]["PATH"])
result = exec_command([env.subst("$CC"), "-v"], env=sysenv)
except OSError:
return None
if result["returncode"] != 0:
return None
output = "".join([result["out"], result["err"]]).lower()
if "clang" in output and "LLVM" in output:
return "clang"
if "gcc" in output:
return "gcc"
return None
def GetActualLDScript(env):
2016-10-04 01:21:26 +03:00
def _lookup_in_ldpath(script):
for d in env.get("LIBPATH", []):
2020-06-09 18:43:50 +03:00
path = os.path.join(env.subst(d), script)
if os.path.isfile(path):
2016-10-04 01:21:26 +03:00
return path
return None
2015-12-28 20:09:48 +02:00
script = None
script_in_next = False
for f in env.get("LINKFLAGS", []):
raw_script = None
if f == "-T":
script_in_next = True
continue
if script_in_next:
script_in_next = False
raw_script = f
elif f.startswith("-Wl,-T"):
raw_script = f[6:]
else:
continue
script = env.subst(raw_script.replace('"', "").strip())
2020-06-09 18:43:50 +03:00
if os.path.isfile(script):
return script
path = _lookup_in_ldpath(script)
if path:
return path
2015-12-28 20:09:48 +02:00
if script:
sys.stderr.write(
"Error: Could not find '%s' LD script in LDPATH '%s'\n"
% (script, env.subst("$LIBPATH"))
)
env.Exit(1)
2015-12-28 20:09:48 +02:00
2016-10-04 01:21:26 +03:00
if not script and "LDSCRIPT_PATH" in env:
path = _lookup_in_ldpath(env["LDSCRIPT_PATH"])
2016-10-04 01:21:26 +03:00
if path:
return path
sys.stderr.write("Error: Could not find LD script\n")
env.Exit(1)
2022-04-19 11:36:05 +03:00
def ConfigureDebugTarget(env):
2019-09-27 17:22:21 +03:00
def _cleanup_debug_flags(scope):
if scope not in env:
return
unflags = ["-Os", "-g"]
for level in [0, 1, 2, 3]:
2019-09-27 17:22:21 +03:00
for flag in ("O", "g", "ggdb"):
unflags.append("-%s%d" % (flag, level))
env[scope] = [f for f in env.get(scope, []) if f not in unflags]
env.Append(CPPDEFINES=["__PLATFORMIO_BUILD_DEBUG__"])
2019-10-03 18:16:55 +03:00
for scope in ("ASFLAGS", "CCFLAGS", "LINKFLAGS"):
2019-09-27 17:22:21 +03:00
_cleanup_debug_flags(scope)
debug_flags = env.ParseFlags(
env.get("PIODEBUGFLAGS")
if env.get("PIODEBUGFLAGS")
and not env.GetProjectOptions(as_dict=True).get("debug_build_flags")
else env.GetProjectOption("debug_build_flags")
)
env.MergeFlags(debug_flags)
2020-01-24 19:47:20 +02:00
optimization_flags = [
f for f in debug_flags.get("CCFLAGS", []) if f.startswith(("-O", "-g"))
]
if optimization_flags:
env.AppendUnique(
ASFLAGS=[
# skip -O flags for assembler
f
for f in optimization_flags
if f.startswith("-g")
],
LINKFLAGS=optimization_flags,
)
2017-04-28 01:38:25 +03:00
def GetExtraScripts(env, scope):
items = []
for item in env.GetProjectOption("extra_scripts", []):
if scope == "post" and ":" not in item:
items.append(item)
elif item.startswith("%s:" % scope):
items.append(item[len(scope) + 1 :])
if not items:
return items
with fs.cd(env.subst("$PROJECT_DIR")):
return [os.path.abspath(env.subst(item)) for item in items]
def generate(env):
env.AddMethod(GetCompilerType)
env.AddMethod(GetActualLDScript)
env.AddMethod(ConfigureDebugTarget)
env.AddMethod(GetExtraScripts)
# bakward-compatibility with Zephyr build script
env.AddMethod(ConfigureDebugTarget, "ConfigureDebugFlags")
def exists(_):
return True