Browse Source

CodeBlocks: add option to exclude external files

Add variable `CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES` to optionally
exclude files from outside the project root from the project file
written by the CodeBlocks extra generator.  This optionally restores
logic that had been removed by commit v2.8.3~40^2 (CodeBlocks Generator:
Do not omit files in the project file listing, 2010-10-05) in response
to QTCREATORBUG-2250.

Issue: #12110
Fixes: #17188
Alexandr (Sagrer) Gridnev 8 years ago
parent
commit
fb19b7789a

+ 5 - 1
Help/generator/CodeBlocks.rst

@@ -6,7 +6,11 @@ Generates CodeBlocks project files.
 Project files for CodeBlocks will be created in the top directory and
 in every subdirectory which features a CMakeLists.txt file containing
 a PROJECT() call.  Additionally a hierarchy of makefiles is generated
-into the build tree.  The appropriate make program can build the
+into the build tree.
+The :variable:`CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES` variable may
+be set to ``ON`` to exclude any files which are located outside of
+the project root directory.
+The appropriate make program can build the
 project through the default make target.  A "make install" target is
 also provided.
 

+ 1 - 0
Help/manual/cmake-variables.7.rst

@@ -121,6 +121,7 @@ Variables that Change Behavior
    /variable/CMAKE_AUTOMOC_RELAXED_MODE
    /variable/CMAKE_BACKWARDS_COMPATIBILITY
    /variable/CMAKE_BUILD_TYPE
+   /variable/CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES
    /variable/CMAKE_CODELITE_USE_TARGETS
    /variable/CMAKE_COLOR_MAKEFILE
    /variable/CMAKE_CONFIGURATION_TYPES

+ 6 - 0
Help/release/dev/codeblocks-exclude-external.rst

@@ -0,0 +1,6 @@
+codeblocks-exclude-external
+---------------------------
+
+* A :variable:`CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES` variable was added
+  to tell the :generator:`CodeBlocks` extra generator to exclude files
+  from outside the project root directory from the generated project files.

+ 7 - 0
Help/variable/CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES.rst

@@ -0,0 +1,7 @@
+CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES
+---------------------------------------
+
+Change the way the CodeBlocks generator creates project files.
+
+If this variable evaluates to ``ON`` the generator excludes from
+the project file any files that are located outside the project root.

+ 20 - 0
Source/cmExtraCodeBlocksGenerator.cxx

@@ -235,7 +235,14 @@ void cmExtraCodeBlocksGenerator::CreateNewProjectFile(
       // We don't want paths with CMakeFiles in them
       // or do we?
       // In speedcrunch those where purely internal
+      //
+      // Also we can disable external (outside the project) files by setting ON
+      // CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES variable.
+      const bool excludeExternal =
+        cmSystemTools::IsOn(it.second[0]->GetMakefile()->GetSafeDefinition(
+          "CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES"));
       if (!splitted.empty() &&
+          (!excludeExternal || (relative.find("..") == std::string::npos)) &&
           relative.find("CMakeFiles") == std::string::npos) {
         tree.InsertPath(splitted, 1, fileName);
       }
@@ -380,6 +387,19 @@ void cmExtraCodeBlocksGenerator::CreateNewProjectFile(
 
             std::string const& fullPath = s->GetFullPath();
 
+            // Check file position relative to project root dir.
+            const std::string& relative = cmSystemTools::RelativePath(
+              (*lg).GetSourceDirectory(), fullPath.c_str());
+            // Do not add this file if it has ".." in relative path and
+            // if CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES variable is on.
+            const bool excludeExternal =
+              cmSystemTools::IsOn((*lg).GetMakefile()->GetSafeDefinition(
+                "CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES"));
+            if (excludeExternal &&
+                (relative.find("..") != std::string::npos)) {
+              continue;
+            }
+
             if (isCFile) {
               cFiles.push_back(fullPath);
             }