diff --git a/Help/command/project.rst b/Help/command/project.rst index d6ebe5669e..57eb7acf94 100644 --- a/Help/command/project.rst +++ b/Help/command/project.rst @@ -12,6 +12,7 @@ Synopsis project( [VERSION [.[.[.]]]] [COMPAT_VERSION [.[.[.]]]] + [SPDX_LICENSE ] [DESCRIPTION ] [HOMEPAGE_URL ] [LANGUAGES ...]) @@ -106,6 +107,29 @@ The options are: ``CMakeLists.txt``, then the compatibility version is also stored in the variable :variable:`CMAKE_PROJECT_COMPAT_VERSION`. +``SPDX_LICENSE `` + .. versionadded:: 4.2 + + Optional. + Sets the variables + + * :variable:`PROJECT_SPDX_LICENSE`, + :variable:`_SPDX_LICENSE` + + to ````, which shall be a |SPDX|_ (SPDX) + `License Expression`_ that describes the license(s) of the project as a + whole, including documentation, resources, or other materials distributed + with the project, in addition to software artifacts. See the SPDX + `License List`_ for a list of commonly used licenses and their identifiers. + See the :prop_tgt:`SPDX_LICENSE` property for specifying the license(s) on + individual software artifacts. + +.. _SPDX: https://spdx.dev/ +.. |SPDX| replace:: System Package Data Exchange + +.. _License Expression: https://spdx.github.io/spdx-spec/v3.0.1/annexes/spdx-license-expressions/ +.. _License List: https://spdx.org/licenses/ + ``DESCRIPTION `` .. versionadded:: 3.9 diff --git a/Help/manual/cmake-variables.7.rst b/Help/manual/cmake-variables.7.rst index d3ff6817a0..1b8d0a791a 100644 --- a/Help/manual/cmake-variables.7.rst +++ b/Help/manual/cmake-variables.7.rst @@ -98,15 +98,16 @@ Variables that Provide Information /variable/CMAKE_OBJDUMP /variable/CMAKE_PARENT_LIST_FILE /variable/CMAKE_PATCH_VERSION + /variable/CMAKE_PROJECT_COMPAT_VERSION /variable/CMAKE_PROJECT_DESCRIPTION /variable/CMAKE_PROJECT_HOMEPAGE_URL /variable/CMAKE_PROJECT_NAME + /variable/CMAKE_PROJECT_SPDX_LICENSE /variable/CMAKE_PROJECT_VERSION /variable/CMAKE_PROJECT_VERSION_MAJOR /variable/CMAKE_PROJECT_VERSION_MINOR /variable/CMAKE_PROJECT_VERSION_PATCH /variable/CMAKE_PROJECT_VERSION_TWEAK - /variable/CMAKE_PROJECT_COMPAT_VERSION /variable/CMAKE_RANLIB /variable/CMAKE_ROOT /variable/CMAKE_RULE_MESSAGES @@ -154,28 +155,30 @@ Variables that Provide Information /variable/CMAKE_XCODE_BUILD_SYSTEM /variable/CMAKE_XCODE_PLATFORM_TOOLSET /variable/PROJECT-NAME_BINARY_DIR + /variable/PROJECT-NAME_COMPAT_VERSION /variable/PROJECT-NAME_DESCRIPTION /variable/PROJECT-NAME_HOMEPAGE_URL /variable/PROJECT-NAME_IS_TOP_LEVEL /variable/PROJECT-NAME_SOURCE_DIR + /variable/PROJECT-NAME_SPDX_LICENSE /variable/PROJECT-NAME_VERSION /variable/PROJECT-NAME_VERSION_MAJOR /variable/PROJECT-NAME_VERSION_MINOR /variable/PROJECT-NAME_VERSION_PATCH /variable/PROJECT-NAME_VERSION_TWEAK - /variable/PROJECT-NAME_COMPAT_VERSION /variable/PROJECT_BINARY_DIR + /variable/PROJECT_COMPAT_VERSION /variable/PROJECT_DESCRIPTION /variable/PROJECT_HOMEPAGE_URL /variable/PROJECT_IS_TOP_LEVEL /variable/PROJECT_NAME /variable/PROJECT_SOURCE_DIR + /variable/PROJECT_SPDX_LICENSE /variable/PROJECT_VERSION /variable/PROJECT_VERSION_MAJOR /variable/PROJECT_VERSION_MINOR /variable/PROJECT_VERSION_PATCH /variable/PROJECT_VERSION_TWEAK - /variable/PROJECT_COMPAT_VERSION Variables that Change Behavior ============================== diff --git a/Help/prop_tgt/SPDX_LICENSE.rst b/Help/prop_tgt/SPDX_LICENSE.rst index 056c8606f9..a030e6cd56 100644 --- a/Help/prop_tgt/SPDX_LICENSE.rst +++ b/Help/prop_tgt/SPDX_LICENSE.rst @@ -3,9 +3,9 @@ SPDX_LICENSE .. versionadded:: 4.1 -Specify the license of a target using a |SPDX|_ (SPDX) `License Expression`_. -See the SPDX `License List`_ for a list of commonly used licenses and their -identifiers. +Specify the license(s) of a target using a |SPDX|_ (SPDX) +`License Expression`_. See the SPDX `License List`_ for a list of commonly used +licenses and their identifiers. .. _SPDX: https://spdx.dev/ .. |SPDX| replace:: System Package Data Exchange diff --git a/Help/variable/CMAKE_PROJECT_SPDX_LICENSE.rst b/Help/variable/CMAKE_PROJECT_SPDX_LICENSE.rst new file mode 100644 index 0000000000..0cbde7c275 --- /dev/null +++ b/Help/variable/CMAKE_PROJECT_SPDX_LICENSE.rst @@ -0,0 +1,41 @@ +CMAKE_PROJECT_SPDX_LICENSE +-------------------------- + +.. versionadded:: 4.2 + +.. note:: + + Experimental. Gated by ``CMAKE_EXPERIMENTAL_EXPORT_PACKAGE_INFO``. + +The license(s) of the top level project. + +This variable holds the license expression of the project as specified in the +top level CMakeLists.txt file by a :command:`project` command. In the event +that the top level CMakeLists.txt contains multiple :command:`project` calls, +the most recently called one from that top level CMakeLists.txt will determine +the value that ``CMAKE_PROJECT_SPDX_LICENSE`` contains. For example, consider +the following top level CMakeLists.txt: + +.. code-block:: cmake + + cmake_minimum_required(VERSION 4.2) + project(First SPDX_LICENSE "BSD-3-Clause") + project(Second SPDX_LICENSE "BSD-3-Clause AND CC-BY-SA-4.0") + add_subdirectory(sub) + project(Third SPDX_LICENSE "BSD-3-Clause AND CC0-1.0") + +And ``sub/CMakeLists.txt`` with the following contents: + +.. code-block:: cmake + + project(SubProj SPDX_LICENSE Apache-2.0) + message("CMAKE_PROJECT_SPDX_LICENSE = ${CMAKE_PROJECT_SPDX_LICENSE}") + +The most recently seen :command:`project` command from the top level +CMakeLists.txt would be ``project(Second ...)``, so this will print:: + + CMAKE_PROJECT_SPDX_LICENSE = BSD-3-Clause AND CC-BY-SA-4.0 + +To obtain the version from the most recent call to :command:`project` in +the current directory scope or above, see the :variable:`PROJECT_SPDX_LICENSE` +variable. diff --git a/Help/variable/PROJECT-NAME_SPDX_LICENSE.rst b/Help/variable/PROJECT-NAME_SPDX_LICENSE.rst new file mode 100644 index 0000000000..06c395d7e6 --- /dev/null +++ b/Help/variable/PROJECT-NAME_SPDX_LICENSE.rst @@ -0,0 +1,11 @@ +_SPDX_LICENSE +--------------------------- + +.. versionadded:: 4.2 + +.. note:: + + Experimental. Gated by ``CMAKE_EXPERIMENTAL_EXPORT_PACKAGE_INFO``. + +Value given to the ``SPDX_LICENSE`` option of the most recent call to the +:command:`project` command with project name ````, if any. diff --git a/Help/variable/PROJECT_SPDX_LICENSE.rst b/Help/variable/PROJECT_SPDX_LICENSE.rst new file mode 100644 index 0000000000..4f62e944c9 --- /dev/null +++ b/Help/variable/PROJECT_SPDX_LICENSE.rst @@ -0,0 +1,12 @@ +PROJECT_SPDX_LICENSE +-------------------- + +.. versionadded:: 4.2 + +.. note:: + + Experimental. Gated by ``CMAKE_EXPERIMENTAL_EXPORT_PACKAGE_INFO``. + +Value given to the ``SPDX_LICENSE`` option of the most recent call to the +:command:`project` command, if any. To obtain the compatibility version of the +top level project, see the :variable:`CMAKE_PROJECT_SPDX_LICENSE` variable. diff --git a/Source/cmProjectCommand.cxx b/Source/cmProjectCommand.cxx index 45efaa5fa2..0835b1250e 100644 --- a/Source/cmProjectCommand.cxx +++ b/Source/cmProjectCommand.cxx @@ -38,6 +38,7 @@ struct ProjectArguments : ArgumentParser::ParseResult cm::optional ProjectName; cm::optional Version; cm::optional CompatVersion; + cm::optional License; cm::optional Description; cm::optional HomepageURL; cm::optional>> Languages; @@ -73,11 +74,12 @@ bool cmProjectCommand(std::vector const& args, .Bind("LANGUAGES"_s, prArgs.Languages); cmMakefile& mf = status.GetMakefile(); - bool enableCompatVersion = cmExperimental::HasSupportEnabled( + bool enablePackageInfo = cmExperimental::HasSupportEnabled( mf, cmExperimental::Feature::ExportPackageInfo); - if (enableCompatVersion) { + if (enablePackageInfo) { parser.Bind("COMPAT_VERSION"_s, prArgs.CompatVersion); + parser.Bind("SPDX_LICENSE"_s, prArgs.License); } parser.Parse(args, &unparsedArgs, 0); @@ -263,6 +265,7 @@ bool cmProjectCommand(std::vector const& args, createVariables("VERSION_PATCH"_s, version_components[2]); createVariables("VERSION_TWEAK"_s, version_components[3]); createVariables("COMPAT_VERSION"_s, prArgs.CompatVersion.value_or("")); + createVariables("SPDX_LICENSE"_s, prArgs.License.value_or("")); createVariables("DESCRIPTION"_s, prArgs.Description.value_or("")); createVariables("HOMEPAGE_URL"_s, prArgs.HomepageURL.value_or("")); diff --git a/Tests/RunCMake/project/Omissions.cmake b/Tests/RunCMake/project/Omissions.cmake index 79f695f604..88f8e82dba 100644 --- a/Tests/RunCMake/project/Omissions.cmake +++ b/Tests/RunCMake/project/Omissions.cmake @@ -20,5 +20,6 @@ expect_empty(VERSION_MINOR) expect_empty(VERSION_PATCH) expect_empty(VERSION_TWEAK) expect_empty(COMPAT_VERSION) +expect_empty(SPDX_LICENSE) expect_empty(DESCRIPTION) expect_empty(HOMEPAGE_URL) diff --git a/Tests/RunCMake/project/ProjectLicense-stdout.txt b/Tests/RunCMake/project/ProjectLicense-stdout.txt new file mode 100644 index 0000000000..ef086586a3 --- /dev/null +++ b/Tests/RunCMake/project/ProjectLicense-stdout.txt @@ -0,0 +1 @@ +PROJECT_SPDX_LICENSE=BSD-3-Clause diff --git a/Tests/RunCMake/project/ProjectLicense.cmake b/Tests/RunCMake/project/ProjectLicense.cmake new file mode 100644 index 0000000000..847ee64bc3 --- /dev/null +++ b/Tests/RunCMake/project/ProjectLicense.cmake @@ -0,0 +1,5 @@ +project(ProjectLicenseTest SPDX_LICENSE "BSD-3-Clause" LANGUAGES) +if(NOT PROJECT_SPDX_LICENSE) + message(FATAL_ERROR "PROJECT_SPDX_LICENSE expected to be set") +endif() +message(STATUS "PROJECT_SPDX_LICENSE=${PROJECT_SPDX_LICENSE}") diff --git a/Tests/RunCMake/project/ProjectLicense2-result.txt b/Tests/RunCMake/project/ProjectLicense2-result.txt new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/Tests/RunCMake/project/ProjectLicense2-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/project/ProjectLicense2-stderr.txt b/Tests/RunCMake/project/ProjectLicense2-stderr.txt new file mode 100644 index 0000000000..b3be8498db --- /dev/null +++ b/Tests/RunCMake/project/ProjectLicense2-stderr.txt @@ -0,0 +1 @@ + SPDX_LICENSE may be specified at most once. diff --git a/Tests/RunCMake/project/ProjectLicense2.cmake b/Tests/RunCMake/project/ProjectLicense2.cmake new file mode 100644 index 0000000000..c3ef7512fc --- /dev/null +++ b/Tests/RunCMake/project/ProjectLicense2.cmake @@ -0,0 +1 @@ +project(ProjectLicenseTest SPDX_LICENSE "BSD-3-Clause" SPDX_LICENSE "Apache-2.0" LANGUAGES) diff --git a/Tests/RunCMake/project/ProjectLicenseNoArg-stderr.txt b/Tests/RunCMake/project/ProjectLicenseNoArg-stderr.txt new file mode 100644 index 0000000000..5cb13faa7c --- /dev/null +++ b/Tests/RunCMake/project/ProjectLicenseNoArg-stderr.txt @@ -0,0 +1,2 @@ + SPDX_LICENSE keyword not followed by a value or was followed by a value + that expanded to nothing. diff --git a/Tests/RunCMake/project/ProjectLicenseNoArg.cmake b/Tests/RunCMake/project/ProjectLicenseNoArg.cmake new file mode 100644 index 0000000000..7404e4d425 --- /dev/null +++ b/Tests/RunCMake/project/ProjectLicenseNoArg.cmake @@ -0,0 +1 @@ +project(ProjectLicenseTest LANGUAGES NONE SPDX_LICENSE) diff --git a/Tests/RunCMake/project/ProjectLicenseNoArg2-stderr.txt b/Tests/RunCMake/project/ProjectLicenseNoArg2-stderr.txt new file mode 100644 index 0000000000..5cb13faa7c --- /dev/null +++ b/Tests/RunCMake/project/ProjectLicenseNoArg2-stderr.txt @@ -0,0 +1,2 @@ + SPDX_LICENSE keyword not followed by a value or was followed by a value + that expanded to nothing. diff --git a/Tests/RunCMake/project/ProjectLicenseNoArg2.cmake b/Tests/RunCMake/project/ProjectLicenseNoArg2.cmake new file mode 100644 index 0000000000..4370d5bc65 --- /dev/null +++ b/Tests/RunCMake/project/ProjectLicenseNoArg2.cmake @@ -0,0 +1 @@ +project(ProjectLicenseTest SPDX_LICENSE LANGUAGES NONE) diff --git a/Tests/RunCMake/project/RunCMakeTest.cmake b/Tests/RunCMake/project/RunCMakeTest.cmake index 8ffc645f95..31579e9d83 100644 --- a/Tests/RunCMake/project/RunCMakeTest.cmake +++ b/Tests/RunCMake/project/RunCMakeTest.cmake @@ -66,6 +66,10 @@ run_cmake_with_options(ProjectCompatVersionInvalid ${opts}) run_cmake_with_options(ProjectCompatVersionMissingVersion ${opts}) run_cmake_with_options(ProjectCompatVersionNewer ${opts}) run_cmake_with_options(ProjectCompatVersionNoArg ${opts}) +run_cmake_with_options(ProjectLicense ${opts}) +run_cmake_with_options(ProjectLicense2 ${opts}) +run_cmake_with_options(ProjectLicenseNoArg ${opts}) +run_cmake_with_options(ProjectLicenseNoArg2 ${opts}) run_cmake(CMP0048-NEW)