• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
14include_guard(GLOBAL)
15
16include("$ENV{PW_ROOT}/pw_build/pigweed.cmake")
17
18# Clang isn't a plug-and-play experience for Cortex-M baremetal targets; it's
19# missing C runtime libraries, C/C++ standard libraries, and a few other
20# things. This template uses the provided cflags, asmflags, ldflags, etc. to
21# generate a config that pulls the missing components from an arm-none-eabi-gcc
22# compiler on the system PATH. The end result is a clang-based compiler that
23# pulls in gcc-provided headers and libraries to complete the toolchain.
24#
25# This is effectively meant to be the cmake equivalent of clang_config.gni
26# which contains helper tools for getting these flags.
27function(_pw_get_clang_flags OUTPUT_VARIABLE TYPE)
28  execute_process(
29    COMMAND python
30            $ENV{PW_ROOT}/pw_toolchain/py/pw_toolchain/clang_arm_toolchain.py
31            ${TYPE} -- ${ARGN}
32    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
33    OUTPUT_VARIABLE _result
34    OUTPUT_STRIP_TRAILING_WHITESPACE
35  )
36  set(${OUTPUT_VARIABLE} ${_result} PARENT_SCOPE)
37endfunction()
38
39# This function is meant to replace _pw_get_clang_flags and now uses named
40# variables.
41function(_pw_get_clang_runtime_env_flags OUTPUT_VARIABLE TYPE)
42  pw_parse_arguments(
43    NUM_POSITIONAL_ARGS
44      2
45    ONE_VALUE_ARGS
46      GCC_TOOLCHAIN_PATH
47    MULTI_VALUE_ARGS
48      ARCH_FLAGS
49  )
50
51  set(GCC_PATH_ARG "")
52  if (NOT "${arg_GCC_TOOLCHAIN_PATH}" STREQUAL "")
53    set(GCC_PATH_ARG "--gcc-exe-path;${arg_GCC_TOOLCHAIN_PATH}")
54  endif()
55
56  execute_process(
57    COMMAND python
58            $ENV{PW_ROOT}/pw_toolchain/py/pw_toolchain/clang_arm_toolchain.py
59            ${GCC_PATH_ARG}
60            ${TYPE} -- ${arg_ARCH_FLAGS}
61    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
62    OUTPUT_VARIABLE _result
63    OUTPUT_STRIP_TRAILING_WHITESPACE
64  )
65  set(${OUTPUT_VARIABLE} ${_result} PARENT_SCOPE)
66endfunction()
67
68
69# This returns the compile flags needed for compiling with clang as a string.
70# This function is deprecated. Use pw_get_clang_runtime_env_compile_flags.
71#
72# Usage:
73#
74#   pw_get_clang_compile_flags(OUTPUT_VARIABLE ARCH_FLAG [ARCH_FLAG ...])
75#
76# Example:
77#
78#   # Retrieve the compile flags needed for this arch and store them in "result".
79#   pw_get_clang_compile_flags(result -mcpu=cortex-m33 -mthumb -mfloat-abi=hard)
80#
81function(pw_get_clang_compile_flags OUTPUT_VARIABLE)
82  message(DEPRECATION "Use pw_get_clang_runtime_env_compile_flags")
83  _pw_get_clang_flags(_result --cflags ${ARGN})
84  set(${OUTPUT_VARIABLE} ${_result} PARENT_SCOPE)
85endfunction()
86
87# This returns the compile flags needed for compiling with clang as a string.
88#
89# Usage:
90#
91#   pw_get_clang_runtime_env_compile_flags(OUTPUT_VARIABLE
92#                                         [GCC_TOOLCHAIN_PATH <path>]
93#                                         [ARCH_FLAGS <flags>...])
94#
95# Example:
96#
97#   # Retrieve the compile flags needed for this arch and store them in "result".
98#   pw_get_clang_runtime_env_compile_flags(result ARCH_FLAGS -mcpu=cortex-m33 -mthumb -mfloat-abi=hard)
99#
100function(pw_get_clang_runtime_env_compile_flags OUTPUT_VARIABLE)
101  pw_parse_arguments(
102    NUM_POSITIONAL_ARGS
103      1
104    ONE_VALUE_ARGS
105      GCC_TOOLCHAIN_PATH
106    MULTI_VALUE_ARGS
107      ARCH_FLAGS
108  )
109
110  _pw_get_clang_runtime_env_flags(_result --cflags
111    GCC_TOOLCHAIN_PATH
112      ${arg_GCC_TOOLCHAIN_PATH}
113    ARCH_FLAGS
114      ${arg_ARCH_FLAGS})
115  set(${OUTPUT_VARIABLE} ${_result} PARENT_SCOPE)
116endfunction()
117
118# This returns the link flags needed for compiling with clang as a string.
119# This function is deprecated. Use pw_get_clang_runtime_env_link_flags.
120#
121# Usage:
122#
123#   pw_get_clang_link_flags(OUTPUT_VARIABLE ARCH_FLAG [ARCH_FLAG ...])
124#
125# Example:
126#
127#   # Retrieve the compile flags needed for this arch and store them in "result".
128#   pw_get_clang_link_flags(result -mcpu=cortex-m33 -mthumb -mfloat-abi=hard)
129#
130function(pw_get_clang_link_flags OUTPUT_VARIABLE)
131  message(DEPRECATION "Use pw_get_clang_runtime_env_link_flags")
132  _pw_get_clang_flags(_result --ldflags ${ARGN})
133  set(${OUTPUT_VARIABLE} ${_result} PARENT_SCOPE)
134endfunction()
135
136# This returns the compile flags needed for compiling with clang as a string.
137#
138# Usage:
139#
140#   pw_get_clang_runtime_env_link_flags(OUTPUT_VARIABLE
141#                                       [GCC_TOOLCHAIN_PATH <path>]
142#                                       [ARCH_FLAGS <flags>...])
143#
144# Example:
145#
146#   # Retrieve the compile flags needed for this arch and store them in "result".
147#   pw_get_clang_runtime_env_link_flags(result ARCH_FLAGS -mcpu=cortex-m33 -mthumb -mfloat-abi=hard)
148#
149function(pw_get_clang_runtime_env_link_flags OUTPUT_VARIABLE)
150  pw_parse_arguments(
151    NUM_POSITIONAL_ARGS
152      1
153    ONE_VALUE_ARGS
154      GCC_TOOLCHAIN_PATH
155    MULTI_VALUE_ARGS
156      ARCH_FLAGS
157  )
158
159  _pw_get_clang_runtime_env_flags(_result --ldflags
160    GCC_TOOLCHAIN_PATH
161      ${arg_GCC_TOOLCHAIN_PATH}
162    ARCH_FLAGS
163      ${arg_ARCH_FLAGS})
164  set(${OUTPUT_VARIABLE} ${_result} PARENT_SCOPE)
165endfunction()
166