koknat / callGraph

A multi-language tool which parses source code for function definitions and calls
GNU General Public License v3.0
245 stars 28 forks source link

Running callGraph on mac #1

Closed lukemmtt closed 3 years ago

lukemmtt commented 3 years ago

Hi there,

This is a very useful looking tool, but I'm having trouble running it on mac.

I've run brew install graphviz as well as sudo cpan install GraphViz, copied callGraph's executable into /usr/local/Cellar/graphviz/2.48.0/bin, and called it like so, with the following error:

macbook ~ % callgraph /Users/username/Developer/Project/FileToGraph.swift ERROR: Install GraphViz and the CPAN GraphViz module to create the call graph sudo apt-get install Graphviz and sudo cpan install GraphViz

Am I doing something wrong? Is it possible to use callGraph on macOS?

Thanks, Luke

koknat commented 3 years ago

Hi, I don't have a mac, but I'd like to help you figure this out. The message is printed because the perl eval fails:

eval 'use GraphViz ()'; if ($@) { say "ERROR: Install GraphViz and the CPAN GraphViz module to create the call graph"; ... }

To troubleshoot, you could try to get a basic Perl GraphViz example running

lukemmtt commented 3 years ago

Thanks for the pointer: that helped a lot and I got everything working.

Side note: This tool works fantastically well—thank you!

Here's the output generated for my project. It's a bit cluttered, but all-encompassing and extremely helpful. CallGraphSwiftOutput

——— For the record, here's some background on how I got this working.

Full disclosure: I'm out of my element here—I know nothing about Perl, GraphViz, or how any of these things are supposed to work together on Mac. I basically just followed the error messages and moved files to where the errors told me they should be. Ugly and wrong, yes, but it works for now.

Things I tried:

  1. brew install graphviz

  2. sudo cpan install GraphViz

  3. Downloaded callGraph, perl_call_graph, graphviz-perl, and IPC-Run directly.

  4. Ran perl_call_graph, which returned the following error

    user@lmbp ~ % /Users/user/Downloads/callGraph-main/perl_call_graph-master/bin/perl_call_graph Can't locate GraphViz.pm in @INC (you may need to install the GraphViz module) (@INC contains: /Library/Perl/5.30/darwin-thread-multi-2level [...]

  5. Navigated to /Library/Perl/5.30 and transferred the IPC-Run and GraphViz components there

  6. Ran perl_call_graph again, this time successfully

user@lmbp ~ % /Users/lukememet/Downloads/callGraph-main/perl_call_graph-master/bin/perl_call_graph Please specify some files to parse!

  1. Ran callGraph successfully

    user@lmbp ~ % callGraph /Users/user/Developer/Project/Models/DataModel.swift Generating: /tmp/call_graph_rdzg/DataModel.swift.dot Converting to /tmp/call_graph_rdzg/DataModel.swift.png Displaying /tmp/call_graph_rdzg/DataModel.swift.png using 'open'

Lastly, to generate the image shown above, I navigated to my project's directory, searched for all .swift files within the directory, selected all of them, typed callGraph in terminal, drag & dropped the selected files into terminal and hit enter.

koknat commented 3 years ago

I'm glad you were able to figure it out. Thank you for providing your notes.

You may be able to clean up your image by providing options such as: -start __MAIN__ -ignore '(topViewController|dateStringForDate)'

I've enhanced the callGraph error message to include the "@inc contains" message

If anyone else has problems getting Perl GraphViz to work, I suggest creating the Perl GraphViz example program "Graphviz_example.pl" to aid in troubleshooting:

use GraphViz; my $g = GraphViz->new(); $g->add_node('London'); $g->add_node('Paris', label => 'City of\nlurve'); $g->add_node('New York'); $g->add_edge('London' => 'Paris'); $g->add_edge('London' => 'New York', label => 'Far'); $g->add_edge('Paris' => 'London'); print $g->as_png;

Save this in a text editor and run it using: perl Graphviz_example.pl > Graphviz_example.png

Once this works, then 'callGraph' should also work