|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# Copyright (c) 2025, Nordic Semiconductor ASA |
| 4 | + |
| 5 | +# Template file for optional Zephyr compiler functions. |
| 6 | +# |
| 7 | +# This file will define optional compiler functions for toolchains that are not |
| 8 | +# defining these functions themselves. |
| 9 | + |
| 10 | +if(NOT COMMAND compiler_file_path) |
| 11 | + |
| 12 | + # Search for filename in default compiler library path using the |
| 13 | + # --print-file-name option which is common to gcc and clang. If the |
| 14 | + # file is not found, filepath_out will be set to an empty string. |
| 15 | + # |
| 16 | + # This won't work correctly when additional compiler flags that affect |
| 17 | + # the search path are specified using zephyr_compile_options rather than |
| 18 | + # via TOOLCHAIN_C_FLAGS or OPTIMIZATION_FLAG. We can't use those values |
| 19 | + # as they are embedded in a generator expresssion. |
| 20 | + # |
| 21 | + # Compilers needing a different implementation should provide this |
| 22 | + # function in their target.cmake file |
| 23 | + |
| 24 | + function(compiler_file_path filename filepath_out) |
| 25 | + execute_process( |
| 26 | + COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} ${OPTIMIZATION_FLAG} |
| 27 | + --print-file-name ${filename} |
| 28 | + OUTPUT_VARIABLE filepath |
| 29 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 30 | + ) |
| 31 | + if(${filepath} STREQUAL ${filename}) |
| 32 | + set(filepath "") |
| 33 | + endif() |
| 34 | + set(${filepath_out} "${filepath}" PARENT_SCOPE) |
| 35 | + endfunction() |
| 36 | + |
| 37 | +endif() |
| 38 | + |
| 39 | +if(NOT COMMAND compiler_set_linker_properties) |
| 40 | + |
| 41 | + # Set the lib_include_dir and rt_library linker properties |
| 42 | + # by searching for the runtime library in the compiler default |
| 43 | + # library search path. If no runtime library is found, these |
| 44 | + # properties will remain unset |
| 45 | + # |
| 46 | + # Compilers needing a different implementation should provide this |
| 47 | + # function in their target.cmake file |
| 48 | + |
| 49 | + function(compiler_set_linker_properties) |
| 50 | + |
| 51 | + # Compute complete path to the runtime library using the |
| 52 | + # --print-libgcc-file-name compiler flag |
| 53 | + execute_process( |
| 54 | + COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} ${OPTIMIZATION_FLAG} |
| 55 | + --print-libgcc-file-name |
| 56 | + OUTPUT_VARIABLE library_path |
| 57 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 58 | + ) |
| 59 | + |
| 60 | + # Compute the directory name and library basename (removing the .a suffix) |
| 61 | + |
| 62 | + # Don't set the lib_include_dir as it is probably wrong; we don't have |
| 63 | + # many of the necessary compiler flags available at this point as they're |
| 64 | + # all hidden inside the INTERFACE_COMPILE_OPTIONS generator expression |
| 65 | + # which cannot be expanded at this time. Instead, trust that the |
| 66 | + # linker will search the correct directory using the linker flags. |
| 67 | + |
| 68 | + #get_filename_component(library_dir ${library_path} DIRECTORY) |
| 69 | + #set_linker_property(PROPERTY lib_include_dir "-L${library_dir}") |
| 70 | + |
| 71 | + get_filename_component(library_basename ${library_path} NAME_WLE) |
| 72 | + |
| 73 | + # Remove the leading 'lib' prefix to leave a value suitable for use with |
| 74 | + # the linker -l flag |
| 75 | + string(REPLACE lib "" library_name ${library_basename}) |
| 76 | + |
| 77 | + set_linker_property(PROPERTY rt_library "-l${library_name}") |
| 78 | + endfunction() |
| 79 | + |
| 80 | +endif() |
0 commit comments