Skip to content
SciTools Blog
Menu
  • Home
  • Blog
  • Support
  • Contact
  • Login
  • Pricing
  • Free Trial
Menu

Analyzing Makefile Based Source Code

Posted on July 14, 2021

TLDR:

Understand parses code like a compiler and to do its best work it needs to know compiler build options like include paths and macro definitions. We use “bear“, an open-source tool, to watch builds from a Makefile built project (bitcoin) and create a very useful Understand parse with it.

Details:

We have a parser (or parsers) for each language we support. For C/C++ variant languages we usually need to know the include paths and macro definitions sent to the compiler when a project is built.

For CMake and Visual Studio built projects we read project artifacts to get parsing information (compile_commands.json for CMake, and various project/solution files for Visual Studio).

Some projects, however, use Makefiles or other build systems. We could try to make a parser of Makefiles, but they are often too dynamic and integrated in with shell scripts or other building programs.

On Windows, we can “watch” the build from inside the Understand New Project Wizard and pick up those files, but for security reasons that’s not an option on Mac, so we use an open-source tool called “bear”:

rizsotto/Bear
Bear is a tool that generates a compilation database for clang tooling. – rizsotto/Bear
GitHubrizsotto

which watches you build and detects the necessary compiler commands and then generates a CMake format “compile_commands.json” file.

tip – you can build bear, or grab binaries from the bear website, or as I did use “brew install bear” to put it on my OSX Mac.

I used it to build an Understand project that successfully parses and analyzes the source for the core “bitcoin” mining daemon.

Here’s what I did:

  1. Figured out how to build “bitcoin”. This was not trivial, but once I got the right dependencies installed I was able to run their automake and configure scripts and generate a set of Makefiles to build for my OSX Mac.
  2. I made “bitcoin” using the normal make target but on the “bear” command-line:

which eventually finished and a “compile_commands.json” file was now available and full of good project building information that Understand can use:

My next step is now easy…. fire up Understand, make a new project and let it scan the “bitcoin” directory for things it recognizes can help with project building:

It found a bunch of MSCV project files, which won’t be of use on my Mac machine, but here is the special one that will help:

I chose “Import” and it loaded up 1674 project files and started parsing them:

Parsing the 479 source files (.c/.cpp files) took a couple of minutes, and I ended up with 15 compile errors and 37 warnings – which roughly matches what I got when I did the command line make. I’ll clean up the compile errors, but I can get to work looking at “bitcoin” with a few errors here and there.

Some basic metrics from the project overview page

External Libraries that “bitcoin” is using

bitcoin directories and source. Box size is lines of code. Color intensity maps to cyclomatic complexity

How much effort?

Total time from starting to generating the Understand project?

A little over an hour divided up this way:

  • 15 minutes for me to figure out how to build “bitcoin” on my Mac
  • 35 minutes for it to build while “bear” watched. I had lunch while I waited.
  • 10 minutes to make the Understand project and make the above graphs.

Summary:

If you can build your software using Make or shell scripts, “bear” can watch and tell Understand what it needs to know to properly analyze that source code.

  • Instagram
  • Facebook
  • LinkedIn
  • Twitter
  • YouTube

Learn more about Understand's Features

  • Dependency Graph
    View Dependency Graphs
  • Comply with Standards
  • View Useful Metrics
  • Team Annotations
  • Browse Depenedencies
  • Edit and Refactor

Related

Leave a Reply Cancel reply

You must be logged in to post a comment.

  • API
  • Architectures
  • Business
  • Code Comparison
  • Code Comprehension
  • Code Navigation
  • Code Visualization
  • Coding Standards
  • Dependencies
  • Developer Notes
  • DevOps
  • Getting Started
  • Legacy Code
  • Licensing
  • Metrics
  • Platform
  • Plugins
  • Power User Tips
  • Programming Practices
  • Uncategorized
  • Useful Scripts
  • User Stories
  • May 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • April 2023
  • January 2023
  • December 2022
  • November 2022
  • September 2022
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

©2025 SciTools Blog | Design: Newspaperly WordPress Theme