xref: /aosp_15_r20/external/pigweed/pw_toolchain/arm_clang/clang_flags.cmake (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2023 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15# Clang isn't a plug-and-play experience for Cortex-M baremetal targets; it's
16# missing C runtime libraries, C/C++ standard libraries, and a few other
17# things. This template uses the provided cflags, asmflags, ldflags, etc. to
18# generate a config that pulls the missing components from an arm-none-eabi-gcc
19# compiler on the system PATH. The end result is a clang-based compiler that
20# pulls in gcc-provided headers and libraries to complete the toolchain.
21#
22# This is effectively meant to be the cmake equivalent of clang_config.gni
23# which contains helper tools for getting these flags.
24function(_pw_get_clang_flags OUTPUT_VARIABLE TYPE)
25  execute_process(
26    COMMAND python
27            $ENV{PW_ROOT}/pw_toolchain/py/pw_toolchain/clang_arm_toolchain.py
28            ${TYPE} -- ${ARGN}
29    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
30    OUTPUT_VARIABLE _result
31    OUTPUT_STRIP_TRAILING_WHITESPACE
32  )
33  set(${OUTPUT_VARIABLE} ${_result} PARENT_SCOPE)
34endfunction()
35
36# This returns the compile flags needed for compiling with clang as a string.
37#
38# Usage:
39#
40#   pw_get_clang_compile_flags(OUTPUT_VARIABLE ARCH_FLAG [ARCH_FLAG ...])
41#
42# Example:
43#
44#   # Retrieve the compile flags needed for this arch and store them in "result".
45#   pw_get_clang_compile_flags(result -mcpu=cortex-m33 -mthumb -mfloat-abi=hard)
46#
47function(pw_get_clang_compile_flags OUTPUT_VARIABLE)
48  _pw_get_clang_flags(_result --cflags ${ARGN})
49  set(${OUTPUT_VARIABLE} ${_result} PARENT_SCOPE)
50endfunction()
51
52# This returns the link flags needed for compiling with clang as a string.
53#
54# Usage:
55#
56#   pw_get_clang_link_flags(OUTPUT_VARIABLE ARCH_FLAG [ARCH_FLAG ...])
57#
58# Example:
59#
60#   # Retrieve the compile flags needed for this arch and store them in "result".
61#   pw_get_clang_link_flags(result -mcpu=cortex-m33 -mthumb -mfloat-abi=hard)
62#
63function(pw_get_clang_link_flags OUTPUT_VARIABLE)
64  _pw_get_clang_flags(_result --ldflags ${ARGN})
65  set(${OUTPUT_VARIABLE} ${_result} PARENT_SCOPE)
66endfunction()
67