Skip to content

Commit 55eaedc

Browse files
committed
docs: add a more comprehensive example
1 parent b7454fb commit 55eaedc

File tree

5 files changed

+187
-95
lines changed

5 files changed

+187
-95
lines changed

example/CMakeLists.txt

+49-27
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# SPDX-FileCopyrightText: 2023-2024 Jochem Rutgers
2-
#
3-
# SPDX-License-Identifier: CC0-1.0
4-
5-
# ##################################################################################################
6-
# Preamble
7-
81
cmake_minimum_required(VERSION 3.16)
92

10-
project(example-project)
3+
project(Example
4+
VERSION 0.2.1
5+
DESCRIPTION "Example project for SBOM-Builder"
6+
LANGUAGES CXX
7+
HOMEPAGE_URL "https://github.com/sodgeit/CMake-SBOM-Builder"
8+
)
9+
10+
set(CMAKE_CXX_STANDARD 23)
1111

1212
# Set some install location. This should probably be done by scripts that control CMake, but for
1313
# this example, embed it here.
@@ -19,43 +19,65 @@ if("${CMAKE_BUILD_TYPE}" STREQUAL "")
1919
set(CMAKE_BUILD_TYPE "Debug")
2020
endif()
2121

22-
# ##################################################################################################
23-
# SBOM setup
22+
# Include CPM.cmake to download dependencies.
23+
file(
24+
DOWNLOAD
25+
https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/CPM.cmake
26+
${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake
27+
EXPECTED_HASH SHA256=7b354f3a5976c4626c876850c93944e52c83ec59a159ae5de5be7983f0e17a2a
28+
)
2429

30+
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)
31+
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/deps.cmake)
32+
33+
# Include the SBOM.cmake file to generate the SBOM.
2534
include(../cmake/sbom.cmake)
2635

27-
version_extract()
36+
# generate the version header and script files
37+
version_generate()
2838

2939
# Setup the SBOM to be generated during install.
3040
sbom_generate(
31-
OUTPUT
32-
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATAROOTDIR}/example/sbom-${GIT_VERSION_PATH}.spdx
3341
LICENSE CC0-1.0
34-
SUPPLIER Example
35-
SUPPLIER_URL https://example_company.com
42+
SUPPLIER ${PROJECT_NAME}
43+
SUPPLIER_URL ${PROJECT_HOMEPAGE_URL}
3644
)
3745

38-
# ##################################################################################################
39-
# Example binary
46+
# mention the dependencies used in the SBOM
47+
sbom_add_package(
48+
cxxopts
49+
VERSION 3.2.0
50+
SUPPLIER "Jarryd Beck (https://github.com/jarro2783/cxxopts)"
51+
LICENSE MIT
52+
)
4053

41-
# We now have set GIT_VERSION and friends set to the current project's version. We also have a
42-
# version static library, version.sh and version.txt for further processing.
43-
version_generate()
54+
sbom_add_package(
55+
Boost
56+
VERSION "1.85.0"
57+
SUPPLIER "https://www.boost.org"
58+
LICENSE BSL-1.0
59+
)
4460

4561
add_executable(example example.cpp)
46-
target_link_libraries(example example-project-version)
4762

48-
# Install the application.
49-
install(TARGETS example RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
63+
target_link_libraries(example
64+
PRIVATE
65+
${PROJECT_NAME}-version
66+
cxxopts
67+
Boost::algorithm
68+
)
5069

51-
# Mention the example binary in the SBOM.
52-
sbom_add_target(example)
70+
# Install the version header and mention it in the SBOM.
71+
install(FILES ${PROJECT_BINARY_DIR}/include/${PROJECT_NAME}_version.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
72+
sbom_add_file(${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}_version.h FILETYPE SOURCE)
5373

5474
# Install some other documentation (the version in this case).
5575
install(FILES ${PROJECT_BINARY_DIR}/version.txt DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/example)
56-
57-
# Mention the version file in the SBOM.
5876
sbom_add_file(${CMAKE_INSTALL_DATAROOTDIR}/example/version.txt FILETYPE DOCUMENTATION TEXT)
5977

78+
# Install the application & mention the example binary in the SBOM.
79+
install(TARGETS example RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
80+
sbom_add_target(example)
81+
6082
# Trigger SBOM finalization and verification.
6183
sbom_finalize()

example/cmake/deps.cmake

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CPMAddPackage( "gh:jarro2783/[email protected]" )
2+
3+
CPMAddPackage(
4+
NAME Boost
5+
VERSION 1.85.0
6+
URL https://github.com/boostorg/boost/releases/download/boost-1.85.0/boost-1.85.0-cmake.tar.gz
7+
URL_HASH SHA256=ab9c9c4797384b0949dd676cf86b4f99553f8c148d767485aaac412af25183e6
8+
OPTIONS "BOOST_INCLUDE_LIBRARIES algorithm\\\;asio"
9+
)

example/example.cpp

+35-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
1-
// SPDX-FileCopyrightText: 2023-2024 Jochem Rutgers
2-
//
3-
// SPDX-License-Identifier: CC0-1.0
1+
#include <print>
42

5-
// Include generated version header file.
6-
#include <example-project_version.h>
3+
#include <Example_version.h>
4+
#include <boost/algorithm/clamp.hpp>
5+
#include <cxxopts.hpp>
76

8-
#include <cstdio>
9-
10-
int main()
7+
int main( int argc, char *argv[] )
118
{
12-
printf("Our version is: %s\n", EXAMPLE_PROJECT_VERSION);
9+
std::println( "Example: Example-Project version {}", EXAMPLE_VERSION );
10+
11+
// clang-format off
12+
cxxopts::Options options( "CPM-Test", "Testing CPM" );
13+
options.add_options()
14+
( "a", "Option A" )
15+
( "b", "Option B" )
16+
( "c", "Option C" );
17+
// clang-format on
18+
19+
auto result = options.parse( argc, argv );
20+
21+
if ( result[ "a" ].as<bool>() )
22+
{
23+
std::println( "Option 'a' is set" );
24+
}
25+
if ( result[ "b" ].as<bool>() )
26+
{
27+
std::println( "Option 'b' is set" );
28+
}
29+
if ( result[ "c" ].as<bool>() )
30+
{
31+
std::println( "Option 'c' is set" );
32+
}
33+
34+
std::println( "Boost Algorithm Test: {}", boost::algorithm::clamp( 5, 0, 10 ) );
35+
std::println( "Boost Algorithm Test: {}", boost::algorithm::clamp( 5, 7, 10 ) );
36+
std::println( "Boost Algorithm Test: {}", boost::algorithm::clamp( 5, 0, 3 ) );
37+
38+
exit( EXIT_SUCCESS );
1339
}
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
SPDXVersion: SPDX-2.3
2+
DataLicense: CC0-1.0
3+
SPDXID: SPDXRef-DOCUMENT
4+
DocumentName: Example-sbom.spdx
5+
DocumentNamespace: https://github.com/sodgeit/CMake-SBOM-Builder/spdxdocs/Example-0.2.1
6+
Creator: Organization: Example
7+
Creator: Tool: CMake-SBOM-Builder-0.2.1
8+
CreatorComment: <text>This SPDX document was created from CMake 3.30.1, using CMake-SBOM-Builder from https://github.com/sodgeit/CMake-SBOM-Builder</text>
9+
Created: 2024-07-23T19:52:50Z
10+
11+
PackageName: Clang
12+
SPDXID: SPDXRef-compiler
13+
PackageVersion: 18.1.8
14+
PackageDownloadLocation: NOASSERTION
15+
PackageLicenseConcluded: NOASSERTION
16+
PackageLicenseDeclared: NOASSERTION
17+
PackageCopyrightText: NOASSERTION
18+
PackageSupplier: Organization: Anonymous
19+
FilesAnalyzed: false
20+
PackageSummary: <text>The compiler as identified by CMake, running on Windows (AMD64)</text>
21+
PrimaryPackagePurpose: APPLICATION
22+
Relationship: SPDXRef-compiler CONTAINS NOASSERTION
23+
Relationship: SPDXRef-compiler BUILD_DEPENDENCY_OF SPDXRef-Example
24+
RelationshipComment: <text>SPDXRef-Example is built by compiler Clang (C:/Program Files/LLVM/bin/clang++.exe) version 18.1.8</text>
25+
26+
PackageName: Example
27+
SPDXID: SPDXRef-Example
28+
ExternalRef: SECURITY cpe23Type cpe:2.3:o:microsoft:windows_10:-:*:*:*:*:*:x64:*
29+
ExternalRef: PACKAGE-MANAGER purl pkg:supplier/Example/[email protected]
30+
PackageVersion: 0.2.1
31+
PackageSupplier: Organization: Example
32+
PackageDownloadLocation: NOASSERTION
33+
PackageLicenseConcluded: CC0-1.0
34+
PackageLicenseDeclared: CC0-1.0
35+
PackageCopyrightText: 2024;Example
36+
PackageHomePage: https://github.com/sodgeit/CMake-SBOM-Builder
37+
PackageComment: <text>Built by CMake 3.30.1 with Debug configuration for Windows (AMD64)</text>
38+
PackageVerificationCode: 829065e9867d9b9dbd601cf43946e5bc1f3b9018
39+
BuiltDate: 2024-07-23T19:52:50Z
40+
Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-Example
41+
42+
FileName: ./include/Example_version.h
43+
SPDXID: SPDXRef-include-Example-version-h-2
44+
FileType: SOURCE
45+
FileChecksum: SHA1: 8733f1a5d796d841a5a952013cfb202b5699c0c4
46+
LicenseConcluded: NOASSERTION
47+
LicenseInfoInFile: NOASSERTION
48+
FileCopyrightText: NOASSERTION
49+
Relationship: SPDXRef-Example CONTAINS SPDXRef-include-Example-version-h-2
50+
51+
FileName: ./share/example/version.txt
52+
SPDXID: SPDXRef-share-example-version-txt-3
53+
FileType: DOCUMENTATION
54+
FileType: TEXT
55+
FileChecksum: SHA1: b3edaa0ec2dc99651b9a15f4c90a0ab5b7bd2a30
56+
LicenseConcluded: NOASSERTION
57+
LicenseInfoInFile: NOASSERTION
58+
FileCopyrightText: NOASSERTION
59+
Relationship: SPDXRef-Example CONTAINS SPDXRef-share-example-version-txt-3
60+
61+
FileName: ./bin/example.exe
62+
SPDXID: SPDXRef-bin-TARGET-FILE-NAME-example-4
63+
FileType: BINARY
64+
FileChecksum: SHA1: fbeb387ffbf0f1b6df7407fa72b57b4205ac83ce
65+
LicenseConcluded: NOASSERTION
66+
LicenseInfoInFile: NOASSERTION
67+
FileCopyrightText: NOASSERTION
68+
Relationship: SPDXRef-Example CONTAINS SPDXRef-bin-TARGET-FILE-NAME-example-4
69+
70+
PackageName: cxxopts
71+
SPDXID: SPDXRef-cxxopts-0
72+
ExternalRef: SECURITY cpe23Type cpe:2.3:o:microsoft:windows_10:-:*:*:*:*:*:x64:*
73+
PackageDownloadLocation: NOASSERTION
74+
PackageLicenseDeclared: NOASSERTION
75+
PackageCopyrightText: NOASSERTION
76+
PackageVersion: 3.2.0
77+
PackageSupplier: Jarryd Beck (https://github.com/jarro2783/cxxopts)
78+
FilesAnalyzed: false
79+
PackageLicenseConcluded: MIT
80+
Relationship: SPDXRef-Example DEPENDS_ON SPDXRef-cxxopts-0
81+
Relationship: SPDXRef-cxxopts-0 CONTAINS NOASSERTION
82+
83+
PackageName: Boost
84+
SPDXID: SPDXRef-Boost-1
85+
ExternalRef: SECURITY cpe23Type cpe:2.3:o:microsoft:windows_10:-:*:*:*:*:*:x64:*
86+
PackageDownloadLocation: NOASSERTION
87+
PackageLicenseDeclared: NOASSERTION
88+
PackageCopyrightText: NOASSERTION
89+
PackageVersion: 1.85.0
90+
PackageSupplier: https://www.boost.org
91+
FilesAnalyzed: false
92+
PackageLicenseConcluded: BSL-1.0
93+
Relationship: SPDXRef-Example DEPENDS_ON SPDXRef-Boost-1
94+
Relationship: SPDXRef-Boost-1 CONTAINS NOASSERTION

example/output/sbom-1.1.2.spdx

-59
This file was deleted.

0 commit comments

Comments
 (0)