Skip to content

Commit 3474d2f

Browse files
Initial commit
0 parents  commit 3474d2f

24 files changed

+562
-0
lines changed

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
4+
/coverage/
5+
/doc/
6+
/pkg/
7+
/spec/reports/
8+
/tmp/
9+
10+
# rspec failure tracking
11+
.rspec_status

.rspec

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

.rubocop.yml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
AllCops:
2+
TargetRubyVersion: 2.5
3+
DisabledByDefault: true
4+
5+
Metrics/LineLength:
6+
Enabled: true
7+
Max: 120
8+
9+
Style/HashSyntax:
10+
Enabled: true
11+
12+
Style/StringLiterals:
13+
Enabled: true
14+
EnforcedStyle: single_quotes
15+
16+
Style/MultilineIfThen:
17+
Enabled: true
18+
19+
Style/MethodDefParentheses:
20+
Enabled: true
21+
22+
Style/BracesAroundHashParameters:
23+
Enabled: true
24+
25+
Layout/IndentationWidth:
26+
Enabled: true
27+
28+
Layout/Tab:
29+
Enabled: true
30+
31+
Layout/EmptyLines:
32+
Enabled: true
33+
34+
Layout/TrailingBlankLines:
35+
Enabled: true
36+
37+
Layout/TrailingWhitespace:
38+
Enabled: true
39+
40+
Layout/SpaceBeforeBlockBraces:
41+
Enabled: true
42+
43+
Layout/SpaceInsideBlockBraces:
44+
Enabled: true
45+
46+
Layout/SpaceInsideHashLiteralBraces:
47+
Enabled: true
48+
49+
Layout/CaseIndentation:
50+
Enabled: true
51+
52+
Lint/EndAlignment:
53+
Enabled: true
54+
EnforcedStyleAlignWith: variable

.travis.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: ruby
2+
sudo: false
3+
rvm:
4+
- 2.0
5+
- 2.1
6+
- 2.2
7+
- 2.3
8+
- 2.4
9+
- 2.5
10+
before_install:
11+
- gem update bundler
12+
branches:
13+
only:
14+
- master
15+
jobs:
16+
include:
17+
- stage: test
18+
rvm: 2.5
19+
before_script: bundle install
20+
script: bundle exec rake rubocop
21+
- sript: bundle exec rake spec

Gemfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
git_source(:github) { |repo_name| 'https://github.com/shreyasbharath/cpp_dependency_graph' }
6+
7+
gemspec

Gemfile.lock

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
PATH
2+
remote: .
3+
specs:
4+
cpp_dependency_graph (0.1.0)
5+
6+
GEM
7+
remote: https://rubygems.org/
8+
specs:
9+
ast (2.4.0)
10+
coveralls (0.8.21)
11+
json (>= 1.8, < 3)
12+
simplecov (~> 0.14.1)
13+
term-ansicolor (~> 1.3)
14+
thor (~> 0.19.4)
15+
tins (~> 1.6)
16+
diff-lcs (1.3)
17+
docile (1.1.5)
18+
json (2.1.0)
19+
parallel (1.12.1)
20+
parser (2.5.0.4)
21+
ast (~> 2.4.0)
22+
powerpack (0.1.1)
23+
rainbow (3.0.0)
24+
rake (12.3.0)
25+
rspec (3.7.0)
26+
rspec-core (~> 3.7.0)
27+
rspec-expectations (~> 3.7.0)
28+
rspec-mocks (~> 3.7.0)
29+
rspec-core (3.7.1)
30+
rspec-support (~> 3.7.0)
31+
rspec-expectations (3.7.0)
32+
diff-lcs (>= 1.2.0, < 2.0)
33+
rspec-support (~> 3.7.0)
34+
rspec-mocks (3.7.0)
35+
diff-lcs (>= 1.2.0, < 2.0)
36+
rspec-support (~> 3.7.0)
37+
rspec-support (3.7.1)
38+
rubocop (0.53.0)
39+
parallel (~> 1.10)
40+
parser (>= 2.5)
41+
powerpack (~> 0.1)
42+
rainbow (>= 2.2.2, < 4.0)
43+
ruby-progressbar (~> 1.7)
44+
unicode-display_width (~> 1.0, >= 1.0.1)
45+
ruby-progressbar (1.9.0)
46+
simplecov (0.14.1)
47+
docile (~> 1.1.0)
48+
json (>= 1.8, < 3)
49+
simplecov-html (~> 0.10.0)
50+
simplecov-html (0.10.2)
51+
term-ansicolor (1.6.0)
52+
tins (~> 1.0)
53+
thor (0.19.4)
54+
tins (1.16.3)
55+
unicode-display_width (1.3.0)
56+
57+
PLATFORMS
58+
x64-mingw32
59+
x86_64-linux
60+
61+
DEPENDENCIES
62+
bundler (~> 1.16)
63+
coveralls (~> 0.8)
64+
cpp_dependency_graph!
65+
rake (~> 12.0)
66+
rspec (~> 3.0)
67+
rubocop (~> 0.53)
68+
69+
BUNDLED WITH
70+
1.16.1

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Shreyas Balakrishna
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Cpp Dependency Graph
2+
3+
[![Build Status](https://travis-ci.org/shreyasbharath/cpp_dependency_graph.svg?branch=master)](https://travis-ci.org/shreyasbharath/cpp_dependency_graph)
4+
5+
Generates useful dependency graphs to study the architecture of a C/C++ codebase.
6+
7+
Why do all the other languages have awesome tools to analyse codebases but C/C++ codebases do not?
8+
9+
It's time to change that.
10+
11+
This gem is inspired by http://www.emadelsaid.com/rubrowser/ and https://github.com/tomtom-international/cpp-dependencies

Rakefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'bundler/gem_tasks'
2+
require 'rspec/core/rake_task'
3+
require 'rubocop/rake_task'
4+
5+
RSpec::Core::RakeTask.new(:spec)
6+
7+
# task default: spec
8+
9+
RuboCop::RakeTask.new

TODO.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- Allow user to specify a single component and the tool should print only that component
2+
- Switch to detect cyclic dependencies (cyclic dependencies highlighted in rendered file)
3+
- Highlight strongly coupled components (i.e. have lots of outgoing/incoming dependencies)
4+
- Provide coupling/cohesion metrics (https://softwareengineering.stackexchange.com/questions/151004/are-there-metrics-for-cohesion-and-coupling)
5+
- should work with any type of include (relative, absolute or just the filenames)

bin/console

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'bundler/setup'
4+
require 'cpp_dependency_graph'
5+
6+
# You can add fixtures and/or initialization code here to make experimenting
7+
# with your gem easier. You can also use a different console, if you like.
8+
9+
# (If you use this, don't forget to add pry to your Gemfile!)
10+
# require "pry"
11+
# Pry.start
12+
13+
require 'irb'
14+
IRB.start(__FILE__)

bin/setup

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

cpp_dependency_graph.gemspec

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
lib = File.expand_path('../lib', __FILE__)
4+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5+
6+
require 'cpp_dependency_graph/version'
7+
8+
Gem::Specification.new do |s|
9+
s.name = 'cpp_dependency_graph'
10+
s.version = CppDependencyGraph::VERSION
11+
s.authors = ['Shreyas Balakrishna']
12+
s.email = ['[email protected]>']
13+
s.summary = <<-SUMMARY
14+
CppDependencyGraph is a program that generates dependency graphs to study the architecture of a C/C++ codebase
15+
SUMMARY
16+
s.description = <<-DESCRIPTION
17+
Generates interactive dependency graphs to study the architecture of a C/C++ project in detail
18+
DESCRIPTION
19+
s.homepage = 'https://github.com/shreyasbharath/cpp_dependency_graph'
20+
s.licenses = ['MIT']
21+
22+
s.files = %x[git ls-files -z].split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } -
23+
%w[.rubocop.yml .travis.yml appveyor.yml]
24+
s.bindir = 'exe'
25+
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
26+
s.require_paths = ['lib']
27+
28+
s.required_ruby_version = Gem::Requirement.new('>= 2.0.0')
29+
s.rubygems_version = '2.6.14'
30+
31+
s.add_development_dependency 'bundler', '~> 1.16'
32+
s.add_development_dependency 'rake', '~> 12.0'
33+
s.add_development_dependency 'rspec', '~> 3.0'
34+
s.add_development_dependency 'coveralls', '~> 0.8'
35+
s.add_development_dependency 'rubocop', '~> 0.53'
36+
end

exe/cpp_dependency_graph

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env ruby
2+
3+
#--
4+
# Copyright (c) 2018 Shreyas Balakrishna
5+
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
#++
24+
25+
require 'optparse'
26+
require 'cpp_dependency_graph'
27+
28+
include CppDependencyGraph
29+
30+
options = {}
31+
optparse = OptionParser.new do |opts|
32+
opts.banner = 'Usage: cpp_dependency_graph [options]'
33+
34+
opts.on('-r dir', '--root dir', 'top level root directory of C/C++ project') do |dir|
35+
options[:root_dir] = dir.gsub(/\\+/, '/')
36+
end
37+
38+
opts.on('-o file', '--output file', 'output dot file to be generated') do |file|
39+
options[:output_file] = file
40+
end
41+
42+
opts.on('-h', '--help', 'Prints this help') do
43+
puts opts
44+
exit
45+
end
46+
end
47+
48+
begin
49+
optparse.parse!
50+
mandatory = [:root_dir, :output_file]
51+
missing = mandatory.select { |param| options[param].nil? }
52+
raise OptionParser::MissingArgument.new(missing.join(', ')) unless missing.empty?
53+
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
54+
puts $ERROR_INFO.to_s
55+
puts optparse
56+
exit
57+
end
58+
59+
unless File.directory?(options[:root_dir])
60+
puts('Not a valid source directory')
61+
sys.exit(-1)
62+
end
63+
64+
generate(options[:root_dir], options[:output_file])

lib/cpp_dependency_graph.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'cpp_dependency_graph/project'
4+
require_relative 'cpp_dependency_graph/dependency_graph'
5+
require_relative 'cpp_dependency_graph/graph_to_dot_converter'
6+
7+
# Generate dependency graph of a project as a dot file
8+
module CppDependencyGraph
9+
def generate(project_dir, output_file)
10+
project = Project.new(project_dir)
11+
dependency_scanner = DependencyGraph.new(project)
12+
GraphToDotConverter.generate(dependency_scanner.component_dependencies, output_file)
13+
end
14+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'project'
4+
require_relative 'tsortable_hash'
5+
6+
# Returns a hash of component dependencies
7+
class DependencyGraph
8+
def initialize(project)
9+
@project = project
10+
end
11+
12+
def component_dependencies
13+
@component_dependencies ||= scan_component_dependencies
14+
end
15+
16+
private
17+
18+
def scan_component_dependencies
19+
dependencies = TsortableHash.new
20+
@project.source_components.each do |component|
21+
dependencies[component.name] = scan_dependencies(component)
22+
end
23+
dependencies.tsort
24+
end
25+
26+
def scan_dependencies(component)
27+
component.outgoing_includes.map { |include| component_for_include(include) }.uniq
28+
end
29+
30+
def component_for_include(include)
31+
source_file = @project.source_files.find { |file| file.basename == include }
32+
source_file.parent_component
33+
end
34+
end

0 commit comments

Comments
 (0)