|
| 1 | +# |
| 2 | +# Copyright 2021-2025 Software Radio Systems Limited |
| 3 | +# |
| 4 | +# By using this file, you agree to the terms and conditions set |
| 5 | +# forth in the LICENSE file which can be found at the top level of |
| 6 | +# the distribution. |
| 7 | +# |
| 8 | + |
| 9 | +# This function reads the COPYRIGHT file, parse the external info and call sbom |
| 10 | +function(parse_copyright_file FILE) |
| 11 | + if(NOT EXISTS "${FILE}") |
| 12 | + message(FATAL_ERROR "COPYRIGHT file not found: ${FILE}") |
| 13 | + endif() |
| 14 | + |
| 15 | + file(READ "${FILE}" _raw) |
| 16 | + string(REPLACE "\r\n" "\n" _lines "${_raw}") # Normalize line endings |
| 17 | + string(REPLACE "\n\n\n" ";" _lines "${_lines}") # Split by sections when multiple new lines are present |
| 18 | + string(REPLACE "\n" " " _lines "${_lines}") # Remove single new lines inside sections |
| 19 | + |
| 20 | + foreach(_line IN LISTS _lines) |
| 21 | + if(_line MATCHES "^Files:") |
| 22 | + string(REGEX MATCH "Files: +(.*) Copyright: +(.*) License: +(.*)" _ ${_line}) |
| 23 | + set(_files_str "${CMAKE_MATCH_1}") |
| 24 | + set(_copy_str "${CMAKE_MATCH_2}") |
| 25 | + set(_license_str "${CMAKE_MATCH_3}") |
| 26 | + |
| 27 | + # Module name |
| 28 | + string(STRIP "${_files_str}" _files_str) |
| 29 | + string(REPLACE " " ";" _file_list "${_files_str}") |
| 30 | + get_module_name_from_files("${_file_list}" _module_name) |
| 31 | + |
| 32 | + if("${_module_name}" STREQUAL "*" OR ${_module_name} STREQUAL "docs") |
| 33 | + continue() |
| 34 | + endif() |
| 35 | + |
| 36 | + # Supplier |
| 37 | + string(REGEX REPLACE |
| 38 | + "^[0-9][0-9][0-9][0-9](-present|-[0-9][0-9][0-9][0-9])?[, ]*" |
| 39 | + "" |
| 40 | + _copy_clean |
| 41 | + "${_copy_str}" |
| 42 | + ) |
| 43 | + |
| 44 | + # Add to SBOM |
| 45 | + message(STATUS "Adding external module: ${_module_name} with license: ${_license_str} supplier: ${_copy_clean} and version: ${_version}") |
| 46 | + sbom_add( |
| 47 | + PACKAGE "${_module_name}" |
| 48 | + SUPPLIER "${_copy_clean}" |
| 49 | + LICENSE "${_license_str}" |
| 50 | + ) |
| 51 | + endif() |
| 52 | + endforeach() |
| 53 | +endfunction(parse_copyright_file) |
| 54 | + |
| 55 | +function(get_module_name_from_files FILE_LIST MODULE) |
| 56 | + list(LENGTH FILE_LIST _count) |
| 57 | + |
| 58 | + if(_count EQUAL 1) |
| 59 | + list(GET FILE_LIST 0 _f) |
| 60 | + get_filename_component(_name "${_f}" NAME_WE) |
| 61 | + set(${MODULE} "${_name}" PARENT_SCOPE) |
| 62 | + return() |
| 63 | + endif() |
| 64 | + |
| 65 | + list(GET FILE_LIST 0 _first) |
| 66 | + get_filename_component(_common "${_first}" DIRECTORY) |
| 67 | + |
| 68 | + foreach(_f IN LISTS FILE_LIST) |
| 69 | + # if the file is empty, skip it |
| 70 | + if("${_f}" STREQUAL "") |
| 71 | + continue() |
| 72 | + endif() |
| 73 | + |
| 74 | + get_filename_component(_dir "${_f}" DIRECTORY) |
| 75 | + |
| 76 | + while(NOT _dir MATCHES "^${_common}") |
| 77 | + get_filename_component(_common "${_common}" DIRECTORY) |
| 78 | + endwhile() |
| 79 | + endforeach() |
| 80 | + |
| 81 | + get_filename_component(_mod "${_common}" NAME) |
| 82 | + set(${MODULE} "${_mod}" PARENT_SCOPE) |
| 83 | +endfunction() |
0 commit comments