{"id":6576,"date":"2020-09-29T12:15:47","date_gmt":"2020-09-29T19:15:47","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6576"},"modified":"2020-09-29T12:15:47","modified_gmt":"2020-09-29T19:15:47","slug":"improving-build-speed-of-mbed-projects-with-ccache","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/mbed\/ccache\/","title":{"rendered":"Improving Build Speed of Mbed Projects with CCache"},"content":{"rendered":"<p>This tutorial shows how to use CCache to improve the build speed of mbed projects with VisualGDB.<\/p>\n<p><a href=\"https:\/\/ccache.dev\/\">CCache<\/a> is a tool that maintains a global cache (one per user account) of commonly compiled sources. E.g. if projects A and B both compile a shared source called <strong>adc.cpp <\/strong>with <strong>exactly the same<\/strong> parameters, CCache will automatically reuse the result of compilation even if the file timestamp changes. CCache automatically checks the contents of all included files as well, making sure that actual file changes do trigger a rebuild.<\/p>\n<p>In practice, this helps speed up rebuilding of projects when common configuration files are regenerated without changing their contents.<\/p>\n<p>We will now show how to create a basic mbed-based project and configure it to use CCache for building.<\/p>\n<ol>\n<li>Start Visual Studio and locate the VisualGDB Mbed Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/01-newprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6577\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/01-newprj.png\" alt=\"\" width=\"966\" height=\"624\" \/><\/a><\/li>\n<li>Enter the name and location for your project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/02-name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6578\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/02-name.png\" alt=\"\" width=\"966\" height=\"624\" \/><\/a><\/li>\n<li>As each mbed project uses its own unique configuration file, CCache won&#8217;t be able to reuse built files between different projects, however it can considerably speed up subsequent builds of the same project when the mbed tools would otherwise rebuild all the sources. We will demonstrate this using the regular project layout (mbed repository cloned inside the project folder). Make sure you check the &#8220;speed up build using CCache&#8221; flag:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/03-get.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6579\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/03-get.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>On the next page of the wizard, select the ARM toolchain and choose your mbed target: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/04-target.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6580\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/04-target.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>Finally, specify the debug method that works with your target and click &#8220;Finish&#8221; to generate the project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/05-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6581\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/05-build.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>If this is the first time you are using CCache, you can configure the folder where it will store the cached object files via <strong>Tools-&gt;Options-&gt;VisualGDB-&gt;CCache<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/folder.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6586\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/folder.png\" alt=\"\" width=\"817\" height=\"649\" \/><\/a><\/li>\n<li>If you try to use CCache on the unmodified <strong>mbed-cli<\/strong> build logic, it will not be of much use because mbed reshuffles the preprocessor macros passed to the gcc on each build. This can be fixed by patching the <strong>mbed-os\\tools\\toolchains\\gcc.py<\/strong> file. Locate <strong>cmd = cc + &#8230;<\/strong> line in the <strong>compile()<\/strong> function in <strong>gcc.py<\/strong> and replace it with this code:\n<pre class=\"\">        #CCACHE_WORKAROUND_PRESENT\r\n        symbols = self.get_symbols()\r\n        symbols.sort()\r\n        \r\n        extra_macros = [x for x in cc if x[0:2] == \"-D\"]\r\n        cc = [x for x in cc if x[0:2] != \"-D\"]\r\n        extra_macros.sort()\r\n        \r\n        cmd = cc + extra_macros + self.get_compile_options(symbols, includes)<\/pre>\n<p><strong>Watch that the tabs\/spaces in the pasted code match the original &#8220;cmd = cc + &#8230;&#8221; line, as otherwise Python will trigger an error.<br \/>\n<\/strong><\/li>\n<li>Now you can start building the project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/06-patched.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6582\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/06-patched.png\" alt=\"\" width=\"1501\" height=\"945\" \/><\/a><\/li>\n<li>The initial build will take slightly longer, as CCache will copy each built object file into the cache directory:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/07-built.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6583\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/07-built.png\" alt=\"\" width=\"1501\" height=\"945\" \/><\/a><\/li>\n<li>However, if you try rebuilding the project (even if you delete the object directory), the build will be much faster (25x faster in this example), as ccache will reuse the previously built files: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/08-faster.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6584\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/08-faster.png\" alt=\"\" width=\"1501\" height=\"945\" \/><\/a><\/li>\n<li>You can turn CCache on and off for individual projects via <strong>VisualGDB Project Properties -&gt; Mbed Project -&gt; Common Build Settings<\/strong>: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/09-speedup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6585\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/08\/09-speedup.png\" alt=\"\" width=\"1501\" height=\"945\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use CCache to improve the build speed of mbed projects with VisualGDB. CCache is a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[221,115],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6576"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=6576"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6576\/revisions"}],"predecessor-version":[{"id":6587,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6576\/revisions\/6587"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}