diff --git a/chapters/features/ides.md b/chapters/features/ides.md index 0a8c6ca..d42f237 100644 --- a/chapters/features/ides.md +++ b/chapters/features/ides.md @@ -2,7 +2,7 @@ In general, IDEs are already supported by a standard CMake project. There are just a few extra things that can help IDEs perform even better. -## Folders +## Folders for targets Some IDEs, like Xcode, support folders. You have to manually enable the `USE_FOLDERS` global property to allow CMake to organize your files by folders: @@ -16,13 +16,32 @@ Then, you can add targets to folders when after you create them: set_property(TARGET MyFile PROPERTY FOLDER "Scripts") ``` -Folders can be nested with `/`. You can control how files show up in each folder with regular expressions or explicit listings in [`source_group`](https://cmake.org/cmake/help/latest/command/source_group.html): +Folders can be nested with `/`. -``` -source_group("Source Files" REGULAR_EXPRESSION ".*\\.c[ucp]p?") + + +You can control how files show up in each folder with regular expressions or explicit listings in [`source_group`](https://cmake.org/cmake/help/latest/command/source_group.html): + +## Folders for files + +You can also control how the folders inside targets appear. There are two ways, both using the «command:source_group» command. The traditional way is + +```cmake +source_group("Source Files\\New Directory" REGULAR_EXPRESSION ".*\\.c[ucp]p?") ``` -Take a look at [this blog post][sorting] for tips on ways to automatically sort folders to match your source code layout. +You can explicitly list files with `FILES`, or use a `REGULAR_EXPRESSION`. This way you have complete control over the folder structure. However, if your on-disk layout is well designed, you might just want to mimic that. In CMake 3.8+, you can do so very easily with a new version of the «command:source_group» command: + +```cmake +source_group(TREE /full/path/to/start/at PREFIX "Header Files" FILES ${FILE_LIST}) +``` + +For the `TREE` option, you should usually give a full path starting with something like `${CMAKE_CURRENT_SOURCE_DIR}/`(because the command interprets paths relative to the build directory). +The prefix tells you where it puts it into the IDE structure, and the `FILES` option takes a list of files. +CMake will strip the `TREE` path from the `FILE_LIST` path, it will add `PREFIX`, and that will be the IDE folder structure. + +> Note: If you need to support CMake < 3.8, I would recommend just protecting the above command, and only supporting nice folder layout on CMake 3.8+. For older methods to do this folder layout, see +> [this blog post][sorting]. ## Running with an IDE diff --git a/chapters/intro/newcmake.md b/chapters/intro/newcmake.md index 88e229e..4bfc68f 100644 --- a/chapters/intro/newcmake.md +++ b/chapters/intro/newcmake.md @@ -97,6 +97,7 @@ This adds CUDA as a language, as well as `cxx_std_11` as a compiler meta-feature * `COMPILE_FLAGS` now supports generator expression * `*_CPPLINT` added * `$` added (wow!) +* `source_group(TREE` added (finally allowing IDE's to reflect the project folder structure!) ## [CMake 3.9][] : IPO