From d85238a2f29c552041293f1f7e64e3de55e55907 Mon Sep 17 00:00:00 2001 From: Alex Neundorf Date: Thu, 29 Jun 2023 00:02:29 +0200 Subject: source_group: Fix TREE without FILES According to the documentation of `source_group()` the `FILES` parameter is optional, but that was actually not the case. When using `source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR})` cmake did not previously create the source tree, but recognized it as the old syntax. With this patch, cmake recognizes it as `TREE` syntax if the first argument is TREE followed by a directory. Then, if no files are given, it defaults to all files in the directory. PREFIX works too. Fixes: #24590 --- Source/cmSourceGroupCommand.cxx | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Source/cmSourceGroupCommand.cxx b/Source/cmSourceGroupCommand.cxx index bb75a14..4b1685f 100644 --- a/Source/cmSourceGroupCommand.cxx +++ b/Source/cmSourceGroupCommand.cxx @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -11,6 +12,8 @@ #include "cmExecutionStatus.h" #include "cmMakefile.h" +#include "cmSourceFile.h" +#include "cmSourceFileLocation.h" #include "cmSourceGroup.h" #include "cmStringAlgorithms.h" #include "cmSystemTools.h" @@ -194,7 +197,10 @@ bool cmSourceGroupCommand(std::vector const& args, // If only two arguments are given, the pre-1.8 version of the // command is being invoked. - if (args.size() == 2 && args[1] != "FILES") { + bool isShortTreeSyntax = + ((args.size() == 2) && (args[0] == kTreeOptionName) && + cmSystemTools::FileIsDirectory(args[1])); + if (args.size() == 2 && args[1] != kFilesOptionName && !isShortTreeSyntax) { cmSourceGroup* sg = mf.GetOrCreateSourceGroup(args[0]); if (!sg) { @@ -274,8 +280,19 @@ static bool processTree(cmMakefile& mf, ParsedArguments& parsedArguments, ? "" : parsedArguments[kPrefixOptionName].front(); - const std::vector filesVector = prepareFilesPathsForTree( - parsedArguments[kFilesOptionName], mf.GetCurrentSourceDirectory()); + std::vector files = parsedArguments[kFilesOptionName]; + if (files.empty()) { + const std::vector>& srcFiles = + mf.GetSourceFiles(); + for (const auto& srcFile : srcFiles) { + if (!srcFile->GetIsGenerated()) { + files.push_back(srcFile->GetLocation().GetFullPath()); + } + } + } + + const std::vector filesVector = + prepareFilesPathsForTree(files, mf.GetCurrentSourceDirectory()); if (!rootIsPrefix(root, filesVector, errorMsg)) { return false; -- cgit v0.12 From 87788353cc1cae7ee3b6f47d5f0f7a46df11d75e Mon Sep 17 00:00:00 2001 From: Alex Neundorf Date: Fri, 30 Jun 2023 22:21:43 +0200 Subject: source_group: Add test/example for TREE without FILES This creates an additional target and for its three source files a directory tree is created. --- Tests/SourceGroups/CMakeLists.txt | 2 ++ Tests/SourceGroups/sub2/CMakeLists.txt | 4 ++++ Tests/SourceGroups/sub2/main.c | 11 +++++++++++ Tests/SourceGroups/sub2/qux.c | 4 ++++ Tests/SourceGroups/sub2/subsub/qax.c | 4 ++++ 5 files changed, 25 insertions(+) create mode 100644 Tests/SourceGroups/sub2/CMakeLists.txt create mode 100644 Tests/SourceGroups/sub2/main.c create mode 100644 Tests/SourceGroups/sub2/qux.c create mode 100644 Tests/SourceGroups/sub2/subsub/qax.c diff --git a/Tests/SourceGroups/CMakeLists.txt b/Tests/SourceGroups/CMakeLists.txt index d726395..550fe9e 100644 --- a/Tests/SourceGroups/CMakeLists.txt +++ b/Tests/SourceGroups/CMakeLists.txt @@ -63,3 +63,5 @@ add_executable(SourceGroups main.c bar.c foo.c sub1/foo.c sub1/foobar.c baz.c ${tree_files_with_prefix} ${tree_files_without_prefix} ${tree_files_with_empty_prefix} README.txt nested.c) + +add_subdirectory(sub2) diff --git a/Tests/SourceGroups/sub2/CMakeLists.txt b/Tests/SourceGroups/sub2/CMakeLists.txt new file mode 100644 index 0000000..e457bc4 --- /dev/null +++ b/Tests/SourceGroups/sub2/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(SourceGroups2 main.c + qux.c subsub/qax.c) + +source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" ) #PREFIX TheSubDir2 ) diff --git a/Tests/SourceGroups/sub2/main.c b/Tests/SourceGroups/sub2/main.c new file mode 100644 index 0000000..4cd8ae0 --- /dev/null +++ b/Tests/SourceGroups/sub2/main.c @@ -0,0 +1,11 @@ +#include + +extern int qax(void); +extern int qux(void); + +int main() +{ + printf("qux: %d qax: %d\n", qux(), qax()); + + return 0; +} diff --git a/Tests/SourceGroups/sub2/qux.c b/Tests/SourceGroups/sub2/qux.c new file mode 100644 index 0000000..1a8b6f9 --- /dev/null +++ b/Tests/SourceGroups/sub2/qux.c @@ -0,0 +1,4 @@ +int qux(void) +{ + return 1234; +} diff --git a/Tests/SourceGroups/sub2/subsub/qax.c b/Tests/SourceGroups/sub2/subsub/qax.c new file mode 100644 index 0000000..c1b1042 --- /dev/null +++ b/Tests/SourceGroups/sub2/subsub/qax.c @@ -0,0 +1,4 @@ +int qax(void) +{ + return 123; +} -- cgit v0.12