{"id":11507,"date":"2017-06-19T20:52:12","date_gmt":"2017-06-19T19:52:12","guid":{"rendered":"https:\/\/sysprogs.com\/w\/?p=11507"},"modified":"2017-06-20T05:02:47","modified_gmt":"2017-06-20T04:02:47","slug":"visualgdb-cmake-project-subsystem","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/w\/visualgdb-cmake-project-subsystem\/","title":{"rendered":"Introducing the new Advanced CMake Project Subsystem"},"content":{"rendered":"<p>CMake has been getting increasingly popular over the past years due to\u00a0its simplicity and integration with the modern IDEs.\u00a0The recently added CMake Server mode made it even more usable, allowing it to share the precise project structure with different IDEs, but if you wanted to actively edit a large project, you would still inevitably have to edit the CMakeLists.txt files manually, making it less intuitive than working with regular Visual Studio projects.<\/p>\n<p>So we decided to bridge this gap and designed a new VisualGDB CMake Project subsystem that lets Visual Studio open\u00a0Linux CMake projects and treat them just like the normal Visual C++\u00a0projects.<!--more--><\/p>\n<h2>Displaying Project Contents<\/h2>\n<p>CMake already\u00a0supports reporting the detailed project structure (including targets and the associated source\u00a0files) to the IDEs via the <a href=\"https:\/\/cmake.org\/cmake\/help\/v3.8\/manual\/cmake-server.7.html\">CMake server protocol<\/a>. The information is sufficient to\u00a0see all of the project&#8217;s targets, tell their types and the related source files. Visual Studio\u00a0itself can use this information to\u00a0help IntelliSense find header files included\u00a0from your sources, but this only works for Windows projects and does not\u00a0show\u00a0the relations between\u00a0different targets\u00a0or specific sources used by each target. To get the maximum usability, our CMake project subsystem presents the CMake project contents in Solution Explorer using the same hierarchical view as the normal Visual C++ projects do (while normal Visual C++ projects require a separate project per target, \u00a0a singile VisualGDB CMake project will automatically show all the targets from all related CMake files):<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/structure.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11508\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/structure.png\" alt=\"structure\" width=\"1013\" height=\"791\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/structure.png 1013w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/structure-300x234.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/structure-768x600.png 768w\" sizes=\"(max-width: 1013px) 100vw, 1013px\" \/><\/a><\/p>\n<p>This makes it possible to define advanced settings for each target (e.g.\u00a0where and when to deploy it or which command-line arguments to use when debugging it):<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/deploy.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11509\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/deploy.png\" alt=\"deploy\" width=\"801\" height=\"469\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/deploy.png 801w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/deploy-300x176.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/deploy-768x450.png 768w\" sizes=\"(max-width: 801px) 100vw, 801px\" \/><\/a>This also allowed us to automatically determine the exact list of libraries that are required by the currently debugged target, so VisualGDB can automatically deploy them and set variables like <strong>LD_LIBRARY_PATH<\/strong> and <strong>solib-search-path<\/strong> to make debugging work out-of-the-box.<\/p>\n<h2>Editing Projects<\/h2>\n<p>So how do we get to not editing the CMakeLists.txt files manually when adding or removing sources or targets? CMake doesn&#8217;t normally let you do that, but if you look closer into\u00a0its sources you can actually see that it internally tracks\u00a0where each target is defined:<\/p>\n<pre class=\"\"> \/** Get a backtrace from the creation of the target. *\/\r\n cmListFileBacktrace const&amp; GetBacktrace() const;<\/pre>\n<p>Even better, CMake actually remembers\u00a0the locations of all statements like <strong>target_link_libraries()<\/strong> that affect each target and lets its components query them:<\/p>\n<pre class=\"\"> cmBacktraceRange GetIncludeDirectoriesBacktraces() const;\r\n cmBacktraceRange GetCompileOptionsBacktraces() const;\r\n cmBacktraceRange GetCompileFeaturesBacktraces() const;\r\n cmBacktraceRange GetCompileDefinitionsBacktraces() const;<\/pre>\n<p>So we made a very small modification to the CMake sources (see the <a href=\"https:\/\/github.com\/sysprogs\/CMake\/commit\/774def23e58c6a894b9850f551c645a4e8a5e456\">commit in our CMake fork on Github<\/a>) that lets CMake report where exactly is each\u00a0target defined and list all statements like target_link_libraries() that affected the target.<\/p>\n<p>This information\u00a0was just enough for VisualGDB to automatically edit the\u00a0<strong>add_executable()<\/strong> or <strong>add_library()<\/strong> statement\u00a0each time you add, remove or rename a source file:<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/newsrc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11510 size-full\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/newsrc.png\" alt=\"newsrc\" width=\"1235\" height=\"698\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/newsrc.png 1235w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/newsrc-300x170.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/newsrc-768x434.png 768w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/newsrc-1024x579.png 1024w\" sizes=\"(max-width: 1235px) 100vw, 1235px\" \/><\/a>We made it as non-intrusive as possible, so it actually detects your formatting style like the maximum amount of\u00a0source items per line and follows it\u00a0when editing CMakeLists.txt:<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11511 size-full\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit.png\" alt=\"edit\" width=\"1235\" height=\"698\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit.png 1235w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit-300x170.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit-768x434.png 768w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit-1024x579.png 1024w\" sizes=\"(max-width: 1235px) 100vw, 1235px\" \/><\/a><\/p>\n<p>It will also recognize\u00a0the cases where the sources (or source subdirectories) are defined via variables and will use the same variables when adding new source files:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11512 size-full\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/varname.png\" alt=\"varname\" width=\"1235\" height=\"698\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/varname.png 1235w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/varname-300x170.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/varname-768x434.png 768w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/varname-1024x579.png 1024w\" sizes=\"(max-width: 1235px) 100vw, 1235px\" \/>To make creation of new targets easier, we have added an option to create them from GUI as well. VisualGDB will automatically add the necessary statements\u00a0(and create\/reference new CMakeLists.txt files if you are creating targets in new subdirectories):<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/addtarget.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11513 size-full\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/addtarget.png\" alt=\"addtarget\" width=\"1235\" height=\"698\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/addtarget.png 1235w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/addtarget-300x170.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/addtarget-768x434.png 768w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/addtarget-1024x579.png 1024w\" sizes=\"(max-width: 1235px) 100vw, 1235px\" \/><\/a><\/p>\n<p>Renaming and deleting of targets and source files will also work completely automatically.<\/p>\n<h2>Editing Target Properties<\/h2>\n<p>Another common scenario is editing target properties (e.g. adding new\u00a0include directories or\u00a0preprocessor macros).\u00a0To make this easier, we connected the regular Visual Studio property pages with the\u00a0underlying CMake statements. E.g. you can select one or more targets, open Properties and set Additional Include Directories for them:<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/subdir.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11515 size-full\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/subdir.png\" alt=\"subdir\" width=\"1235\" height=\"698\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/subdir.png 1235w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/subdir-300x170.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/subdir-768x434.png 768w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/subdir-1024x579.png 1024w\" sizes=\"(max-width: 1235px) 100vw, 1235px\" \/><\/a><\/p>\n<p>VisualGDB will then ask CMake for the location of all <strong>target_include_directories<\/strong> statements related to the selected targets and will edit them or create new ones if none were found:<\/p>\n<p><a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11516 size-full\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit-1.png\" alt=\"edit\" width=\"1235\" height=\"698\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit-1.png 1235w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit-1-300x170.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit-1-768x434.png 768w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/edit-1-1024x579.png 1024w\" sizes=\"(max-width: 1235px) 100vw, 1235px\" \/><\/a><\/p>\n<p>This works in both directions: if you manually\u00a0add or edit similar statements in CMakeLists.txt files, VisualGDB will still recognize them, display their contents in VS property pages and let you edit them.<\/p>\n<h2>Editing Linux Projects via SSH<\/h2>\n<p>As the new CMake project subsystem works independently from the regular\u00a0Visual C++ projects\u00a0and all file and IntelliSense operations are under VisualGDB&#8217;s control,\u00a0the\u00a0sources don&#8217;t have to be physically located on the Windows machine anymore. So we added a special mode that\u00a0lets you create (or import) CMake projects directly on the Linux machine without setting up any shared folders:<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remote.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11517 size-full\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remote.png\" alt=\"remote\" width=\"1235\" height=\"698\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remote.png 1235w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remote-300x170.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remote-768x434.png 768w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remote-1024x579.png 1024w\" sizes=\"(max-width: 1235px) 100vw, 1235px\" \/><\/a>Visual Studio will\u00a0recognize the special vgdb-ssh:\/\/&#8230; URLs and let VisualGDB handle them, while the rest of the Visual Studio will treat those files as if they were located on the Windows machine.\u00a0To avoid downloading the same files over and over, VisualGDB caches them locally and periodically\u00a0determines if any of them are outdated by running the &#8220;ls&#8221; command in the background. This results in\u00a0great performance and\u00a0extremely accurate\u00a0IntelliSense &#8211; it will understand Linux paths and will always use the latest versions of the headers:<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remotesense.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11518 size-full\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remotesense.png\" alt=\"remotesense\" width=\"1235\" height=\"698\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remotesense.png 1235w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remotesense-300x170.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remotesense-768x434.png 768w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remotesense-1024x579.png 1024w\" sizes=\"(max-width: 1235px) 100vw, 1235px\" \/><\/a><\/p>\n<h2>Importing Existing Projects<\/h2>\n<p>Because the\u00a0project structure used by VisualGDB is accurately reported by CMake and does not rely on any\u00a0specific\u00a0layout of the CMakeLists.txt files, the new CMake Project Subsystem\u00a0supports\u00a0opening\u00a0huge 3rd-party projects as long as they are based on CMake. VisualGDB will locate and display the targets, sources and settings the same way as for projects created from scratch:<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remoteimport.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11519 size-full\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remoteimport.png\" alt=\"remoteimport\" width=\"1235\" height=\"698\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remoteimport.png 1235w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remoteimport-300x170.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remoteimport-768x434.png 768w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/remoteimport-1024x579.png 1024w\" sizes=\"(max-width: 1235px) 100vw, 1235px\" \/><\/a><\/p>\n<p>When combined\u00a0with the direct SSH file access,\u00a0this lets you\u00a0quickly open any CMake-based project on your Linux machine from\u00a0Visual Studio and\u00a0immediately begin developing it as if it was a normal\u00a0Windows project. We have also added a special diagnostics console that shows the internal status of the cache and calculates the exact time spent on fetching\u00a0files and listing directories on the Linux machine to make timing analysis easier:<a href=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/stats.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11520 size-full\" src=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/stats.png\" alt=\"stats\" width=\"1235\" height=\"698\" srcset=\"https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/stats.png 1235w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/stats-300x170.png 300w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/stats-768x434.png 768w, https:\/\/sysprogs.com\/w\/wp-content\/uploads\/2017\/06\/stats-1024x579.png 1024w\" sizes=\"(max-width: 1235px) 100vw, 1235px\" \/><\/a><\/p>\n<h2>Try it out<\/h2>\n<p>The new CMake Project Subsystem is available in the latest <a href=\"https:\/\/visualgdb.com\/download\/\">VisualGDB 5.3 Preview 1<\/a>.\u00a0Try it out with your CMake projects and we&#8217;ll be excited to hear your feedback via our <a href=\"http:\/\/sysprogs.com\/support\/\">support page<\/a> or <a href=\"https:\/\/sysprogs.com\/w\/\">forum<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CMake has been getting increasingly popular over the past years due to\u00a0its simplicity and integration with the modern IDEs.\u00a0The recently added CMake Server mode made it even more usable, allowing it to share the precise project structure with different IDEs, but if you wanted to actively edit a large project, you would still inevitably have &hellip; <a href=\"https:\/\/sysprogs.com\/w\/visualgdb-cmake-project-subsystem\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Introducing the new Advanced CMake Project Subsystem<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[1],"tags":[422],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts\/11507"}],"collection":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/comments?post=11507"}],"version-history":[{"count":11,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts\/11507\/revisions"}],"predecessor-version":[{"id":11535,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/posts\/11507\/revisions\/11535"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/media?parent=11507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/categories?post=11507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/w\/wp-json\/wp\/v2\/tags?post=11507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}