2017-06-05 16:02:39 +03:00
|
|
|
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
2015-11-18 17:16:17 +02:00
|
|
|
#
|
|
|
|
|
# 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.
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2020-06-09 18:43:50 +03:00
|
|
|
import os
|
2016-08-26 14:39:23 +03:00
|
|
|
import sys
|
2016-08-27 19:30:38 +03:00
|
|
|
|
2019-08-12 19:44:37 +03:00
|
|
|
from platformio import fs, util
|
2019-05-16 21:03:15 +03:00
|
|
|
from platformio.proc import exec_command
|
2015-08-01 17:39:15 +03:00
|
|
|
|
2015-06-22 15:06:39 +03:00
|
|
|
|
2018-03-23 00:08:07 +02:00
|
|
|
@util.memoized()
|
2022-04-19 11:36:05 +03:00
|
|
|
def GetCompilerType(env):
|
2019-06-15 18:53:13 +03:00
|
|
|
if env.subst("$CC").endswith("-gcc"):
|
|
|
|
|
return "gcc"
|
2015-08-01 17:39:15 +03:00
|
|
|
try:
|
2020-06-09 18:43:50 +03:00
|
|
|
sysenv = os.environ.copy()
|
2019-09-23 23:13:48 +03:00
|
|
|
sysenv["PATH"] = str(env["ENV"]["PATH"])
|
2019-05-16 21:03:15 +03:00
|
|
|
result = exec_command([env.subst("$CC"), "-v"], env=sysenv)
|
2015-08-01 17:39:15 +03:00
|
|
|
except OSError:
|
|
|
|
|
return None
|
2019-09-23 23:13:48 +03:00
|
|
|
if result["returncode"] != 0:
|
2015-08-01 17:39:15 +03:00
|
|
|
return None
|
2019-09-23 23:13:48 +03:00
|
|
|
output = "".join([result["out"], result["err"]]).lower()
|
2016-03-02 00:25:36 +02:00
|
|
|
if "clang" in output and "LLVM" in output:
|
|
|
|
|
return "clang"
|
2018-12-26 20:54:29 +02:00
|
|
|
if "gcc" in output:
|
2016-03-02 00:25:36 +02:00
|
|
|
return "gcc"
|
2015-08-01 17:39:15 +03:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2015-12-28 01:15:06 +02:00
|
|
|
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
|
2018-06-09 00:48:42 +03:00
|
|
|
script_in_next = False
|
2015-12-28 01:15:06 +02:00
|
|
|
for f in env.get("LINKFLAGS", []):
|
2018-06-09 00:48:42 +03:00
|
|
|
raw_script = None
|
|
|
|
|
if f == "-T":
|
|
|
|
|
script_in_next = True
|
|
|
|
|
continue
|
2019-09-27 14:13:53 +03:00
|
|
|
if script_in_next:
|
2018-06-09 00:48:42 +03:00
|
|
|
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):
|
2018-06-09 00:48:42 +03:00
|
|
|
return script
|
|
|
|
|
path = _lookup_in_ldpath(script)
|
|
|
|
|
if path:
|
|
|
|
|
return path
|
2015-12-28 20:09:48 +02:00
|
|
|
|
|
|
|
|
if script:
|
2016-08-26 14:39:23 +03:00
|
|
|
sys.stderr.write(
|
2019-09-23 23:13:48 +03:00
|
|
|
"Error: Could not find '%s' LD script in LDPATH '%s'\n"
|
|
|
|
|
% (script, env.subst("$LIBPATH"))
|
|
|
|
|
)
|
2016-08-26 14:39:23 +03:00
|
|
|
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:
|
2019-09-23 23:13:48 +03:00
|
|
|
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)
|
2015-12-28 01:15:06 +02:00
|
|
|
|
|
|
|
|
|
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"]
|
2020-01-22 20:41:42 +02:00
|
|
|
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)
|
2020-01-22 20:41:42 +02:00
|
|
|
|
2021-03-19 17:11:25 +02:00
|
|
|
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")
|
|
|
|
|
)
|
|
|
|
|
|
2020-01-22 20:41:42 +02:00
|
|
|
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"))
|
|
|
|
|
]
|
2020-01-22 20:41:42 +02:00
|
|
|
|
|
|
|
|
if optimization_flags:
|
2022-05-25 19:23:24 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-03-20 19:24:05 +02:00
|
|
|
def GetExtraScripts(env, scope):
|
|
|
|
|
items = []
|
2019-05-30 17:08:00 +03:00
|
|
|
for item in env.GetProjectOption("extra_scripts", []):
|
2018-03-20 19:24:05 +02:00
|
|
|
if scope == "post" and ":" not in item:
|
|
|
|
|
items.append(item)
|
|
|
|
|
elif item.startswith("%s:" % scope):
|
2019-09-23 23:13:48 +03:00
|
|
|
items.append(item[len(scope) + 1 :])
|
2018-03-20 19:24:05 +02:00
|
|
|
if not items:
|
|
|
|
|
return items
|
2019-08-12 19:44:37 +03:00
|
|
|
with fs.cd(env.subst("$PROJECT_DIR")):
|
2022-04-10 19:09:29 +03:00
|
|
|
return [os.path.abspath(env.subst(item)) for item in items]
|
2017-06-30 00:15:49 +03:00
|
|
|
|
|
|
|
|
|
2015-06-22 15:06:39 +03:00
|
|
|
def generate(env):
|
2015-08-02 19:52:37 +03:00
|
|
|
env.AddMethod(GetCompilerType)
|
2015-12-28 01:15:06 +02:00
|
|
|
env.AddMethod(GetActualLDScript)
|
2022-04-21 18:11:49 +03:00
|
|
|
env.AddMethod(ConfigureDebugTarget)
|
2018-03-20 19:24:05 +02:00
|
|
|
env.AddMethod(GetExtraScripts)
|
2022-04-22 18:14:28 +03:00
|
|
|
# bakward-compatibility with Zephyr build script
|
|
|
|
|
env.AddMethod(ConfigureDebugTarget, "ConfigureDebugFlags")
|
2022-04-21 18:11:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def exists(_):
|
|
|
|
|
return True
|