Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove input argument from sbom generate #27

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 25 additions & 60 deletions cmake/sbom.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ function(sbom_generate)
PACKAGE_COPYRIGHT
ENABLE_CHECKS
)
set(multiValueArgs INPUT CREATOR PACKAGE_NOTES PACKAGE_PURPOSE)
set(multiValueArgs CREATOR PACKAGE_NOTES PACKAGE_PURPOSE)
cmake_parse_arguments(
SBOM_GENERATE "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}
)
Expand All @@ -576,33 +576,31 @@ function(sbom_generate)
set(SBOM_GENERATE_PACKAGE_NAME ${PROJECT_NAME})
endif()

if(NOT DEFINED SBOM_GENERATE_INPUT)
if(NOT DEFINED SBOM_GENERATE_CREATOR)
message(FATAL_ERROR "Specify a CREATOR or pass INPUT files")
endif()
if(NOT DEFINED SBOM_GENERATE_CREATOR)
message(FATAL_ERROR "Missing required argument CREATOR.")
endif()

cmake_parse_arguments(SBOM_GENERATE_CREATOR "" "PERSON;ORGANIZATION;EMAIL" "" ${SBOM_GENERATE_CREATOR})
if(SBOM_GENERATE_CREATOR_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown subarguments: ${SBOM_GENERATE_CREATOR_UNPARSED_ARGUMENTS} for CREATOR.")
endif()
if((NOT DEFINED SBOM_GENERATE_CREATOR_PERSON) AND (NOT DEFINED SBOM_GENERATE_CREATOR_ORGANIZATION))
message(FATAL_ERROR "Missing <PERSON|ORGANIZATION> <name> for argument CREATOR.")
elseif(DEFINED SBOM_GENERATE_CREATOR_PERSON AND DEFINED SBOM_GENERATE_CREATOR_ORGANIZATION)
message(FATAL_ERROR "Specify either PERSON or ORGANIZATION, not both.")
endif()
cmake_parse_arguments(SBOM_GENERATE_CREATOR "" "PERSON;ORGANIZATION;EMAIL" "" ${SBOM_GENERATE_CREATOR})
if(SBOM_GENERATE_CREATOR_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown subarguments: ${SBOM_GENERATE_CREATOR_UNPARSED_ARGUMENTS} for CREATOR.")
endif()
if((NOT DEFINED SBOM_GENERATE_CREATOR_PERSON) AND (NOT DEFINED SBOM_GENERATE_CREATOR_ORGANIZATION))
message(FATAL_ERROR "Missing <PERSON|ORGANIZATION> <name> for argument CREATOR.")
elseif(DEFINED SBOM_GENERATE_CREATOR_PERSON AND DEFINED SBOM_GENERATE_CREATOR_ORGANIZATION)
message(FATAL_ERROR "Specify either PERSON or ORGANIZATION, not both.")
endif()

if(NOT DEFINED SBOM_GENERATE_PACKAGE_URL)
if(NOT DEFINED SBOM_GENERATE_NAMESPACE)
message(FATAL_ERROR "Specify NAMESPACE when PACKAGE_URL is omitted.")
endif()
if(NOT DEFINED SBOM_GENERATE_PACKAGE_URL)
if(NOT DEFINED SBOM_GENERATE_NAMESPACE)
message(FATAL_ERROR "Specify NAMESPACE when PACKAGE_URL is omitted.")
endif()
endif()

if(NOT DEFINED SBOM_GENERATE_NAMESPACE)
if((NOT DEFINED SBOM_GENERATE_PACKAGE_URL) OR (SBOM_GENERATE_PACKAGE_URL STREQUAL "NONE") OR (SBOM_GENERATE_PACKAGE_URL STREQUAL "NOASSERTION"))
message(FATAL_ERROR "Specifiy PACKAGE_URL <url> when NAMESPACE is omitted.")
endif()
set(SBOM_GENERATE_NAMESPACE "${SBOM_GENERATE_PACKAGE_URL}/spdxdocs/${SBOM_GENERATE_PACKAGE_NAME}-${SBOM_GENERATE_PACKAGE_VERSION}")
if(NOT DEFINED SBOM_GENERATE_NAMESPACE)
if((NOT DEFINED SBOM_GENERATE_PACKAGE_URL) OR (SBOM_GENERATE_PACKAGE_URL STREQUAL "NONE") OR (SBOM_GENERATE_PACKAGE_URL STREQUAL "NOASSERTION"))
message(FATAL_ERROR "Specifiy PACKAGE_URL <url> when NAMESPACE is omitted.")
endif()
set(SBOM_GENERATE_NAMESPACE "${SBOM_GENERATE_PACKAGE_URL}/spdxdocs/${SBOM_GENERATE_PACKAGE_NAME}-${SBOM_GENERATE_PACKAGE_VERSION}")
endif()

if(NOT DEFINED GIT_VERSION)
Expand Down Expand Up @@ -709,36 +707,13 @@ function(sbom_generate)
set(_sbom_intermediate_file "$<CONFIG>/sbom.spdx.in")
set(_sbom_document_template "SPDXRef-DOCUMENT.spdx.in")
set(_sbom_export_path "${SBOM_GENERATE_OUTPUT}")
set(_sbom_provided_input false)

if(NOT IS_ABSOLUTE "${SBOM_GENERATE_OUTPUT}")
set(_sbom_export_path "\${CMAKE_INSTALL_PREFIX}/${SBOM_GENERATE_OUTPUT}")
endif()

if(NOT DEFINED SBOM_GENERATE_INPUT)
_sbom_generate_document_template()
set(SBOM_LAST_SPDXID "SPDXRef-${SBOM_GENERATE_PACKAGE_NAME}" PARENT_SCOPE)
else()
set(_sbom_provided_input true)
set(_sbom_provided_input_files "")
foreach(_f IN LISTS SBOM_GENERATE_INPUT)
if( NOT IS_ABSOLUTE "${_f}" )
message(FATAL_ERROR "Input file must be an absolute path: ${_f}")
endif()
get_filename_component(_f_name "${_f}" NAME) #REFAC(>=3.20): Use cmake_path() instead of get_filename_component().
set(_f_in "${SBOM_BINARY_DIR}/${_f_name}")
set(_f_in_gen "${_f_in}_gen")
configure_file("${_f}" "${_f_in}" @ONLY)
file(
GENERATE
OUTPUT "${_f_in_gen}"
INPUT "${_f_in}"
)
list(APPEND _sbom_provided_input_files "${_f_in_gen}")
endforeach()

set(SBOM_LAST_SPDXID "" PARENT_SCOPE)
endif()
_sbom_generate_document_template()
set(SBOM_LAST_SPDXID "SPDXRef-${SBOM_GENERATE_PACKAGE_NAME}" PARENT_SCOPE)

_sbom_append_sbom_snippet("setup.cmake")
file(GENERATE
Expand All @@ -755,18 +730,8 @@ message(STATUS \"Installing: \${SBOM_EXPORT_FILENAME}\")
set(SBOM_INTERMEDIATE_FILE \"\${SBOM_BINARY_DIR}/sbom-build/${_sbom_intermediate_file}\")
file(WRITE \${SBOM_INTERMEDIATE_FILE} \"\")

set(SBOM_PROVIDED_INPUT_FILES \"${_sbom_provided_input_files}\")
set(SBOM_PROVIDED_INPUT ${_sbom_provided_input})

if(NOT SBOM_PROVIDED_INPUT)
file(READ \"\${SBOM_SNIPPET_DIR}/\${SBOM_DOCUMENT_TEMPLATE}\" _f_contents)
file(APPEND \"\${SBOM_INTERMEDIATE_FILE}\" \"\${_f_contents}\")
else()
foreach(_f IN LISTS SBOM_PROVIDED_INPUT_FILES)
file(READ \"\${_f}\" _f_contents)
file(APPEND \"\${SBOM_INTERMEDIATE_FILE}\" \"\${_f_contents}\")
endforeach()
endif()
file(READ \"\${SBOM_SNIPPET_DIR}/\${SBOM_DOCUMENT_TEMPLATE}\" _f_contents)
file(APPEND \"\${SBOM_INTERMEDIATE_FILE}\" \"\${_f_contents}\")

set(SBOM_VERIFICATION_CODES \"\")
"
Expand Down
27 changes: 8 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,9 @@ Generates the SBOM creator information and the package information of the packag

```cmake
sbom_generate(
[INPUT <filename>...]
CREATOR <PERSON|ORGANIZATION> <name> [EMAIL <email>]
[OUTPUT <filename>]
[NAMESPACE <URI>]
[CREATOR <PERSON|ORGANIZATION> <name> [EMAIL <email>]]
[PACKAGE_NAME <package_name>]
[PACKAGE_VERSION <version_string>]
[PACKAGE_FILENAME <filename>]
Expand All @@ -198,23 +197,7 @@ sbom_generate(
)
```

- `INPUT`: One or more file names, which are concatenated into the SBOM output file.
- ***Restrictions:***
- Absolute paths only.
- Variables and generator expressions are supported in these files.
- Variables in the form `@var@` are replaced during config, `${var}` during install.
- When omitted, a standard document/package SBOM is generated.
- The other parameters can be referenced in the input files, prefixed with `SBOM_GENERATE_`.
- `OUTPUT`: Output filename.
- Can be absolute or relative to `CMAKE_INSTALL_PREFIX`.
- Default location is `${CMAKE_INSTALL_PREFIX}/share/${PACKAGE_NAME}-sbom-${GIT_VERSION_PATH}.spdx`.
- `--prefix` option is honoured when added to the install command.
- `--prefix` and `${CMAKE_INSTALL_PREFIX}` have no effect when `OUTPUT` is an absolute path.
- `NAMESPACE`: Document namespace.
- may be omitted when any `INPUT` is given.
- If not specified, default to a URL based on `PACKAGE_URL`, `PACKAGE_NAME` and `PACKAGE_VERSION`.
- `CREATOR`: Supplier of the Package and Creator of the sbom
- May be omitted when any `INPUT` is given.
- See [SPDX clause 6.8](https://spdx.github.io/spdx-spec/v2.3/document-creation-information/#68-creator-field) & [SPDX clause 7.5](https://spdx.github.io/spdx-spec/v2.3/package-information/#75-package-supplier-field) for more information.
- One of the `<PERSON|ORGANIZATION>` keywords must be provided.
- `EMAIL` is optional.
Expand All @@ -224,6 +207,13 @@ sbom_generate(
- ***Note:***
- The SPDX specification differentiates between the creator of the SBOM and the supplier of the package it describes. However, this project treats them as the same entity. This is based on the assumption that whoever uses this project, uses it to generate a SBOM for a package they are building. In this case, the creator of the SBOM and the supplier of the package are the same entity.
- The SBOM-Builder is always added as an additional creator of the SBOM.
- `OUTPUT`: Output filename.
- Can be absolute or relative to `CMAKE_INSTALL_PREFIX`.
- Default location is `${CMAKE_INSTALL_PREFIX}/share/${PACKAGE_NAME}-sbom-${GIT_VERSION_PATH}.spdx`.
- `--prefix` option is honoured when added to the install command.
- `--prefix` and `${CMAKE_INSTALL_PREFIX}` have no effect when `OUTPUT` is an absolute path.
- `NAMESPACE`: Document namespace.
- If not specified, default to a URL based on `PACKAGE_URL`, `PACKAGE_NAME` and `PACKAGE_VERSION`.
- `PACKAGE_NAME`: Package name.
- Defaults to `${PROJECT_NAME}`.
- See [SPDX clause 7.1](https://spdx.github.io/spdx-spec/v2.3/package-information/#71-package-name-field) for more information.
Expand All @@ -238,7 +228,6 @@ sbom_generate(
- Defaults to `NOASSERTION`.
- See [SPDX clause 7.7](https://spdx.github.io/spdx-spec/v2.3/package-information/#77-package-download-location-field) for more information.
- `PACKAGE_URL`: Package home page.
- may be omitted when any `INPUT` is given.
- `NONE` or `NOASSERTION` require that `NAMESPACE` is provided.
- otherwise `<url>` is required.
- See [SPDX clause 7.11](https://spdx.github.io/spdx-spec/v2.3/package-information/#711-package-home-page-field) for more information.
Expand Down
1 change: 0 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ endfunction()
test(minimal)
test(minimal2)
test(full_doc)
test(input_doc)
test(file)
test(package)
test(dir)
Expand Down
13 changes: 0 additions & 13 deletions test/input_doc.cmake

This file was deleted.

25 changes: 0 additions & 25 deletions test/input_doc.spdx.in

This file was deleted.

Loading