cstub 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf8996cf9356f4b4f6a9c967cb41ba62022aff66
4
+ data.tar.gz: d5c2437f8f89298e523ffb649ec0dc183dfbfb03
5
+ SHA512:
6
+ metadata.gz: b7bddc30e9964b6c6ff11c554d90bfb62e647e753a19b22761e4cf137b957f814507999fd2fe6005060ef9b45c54d34d4fd618c35988fa4a92c80420e0861693
7
+ data.tar.gz: 8e2d9a0e3037730ba840803554ebe20df541bf130b4d4738a49fb210983473f059ee3f4ea6528d67378ff25f4b289d997ac2487dc074bd2ee279cec995b69d62
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cstub.gemspec
4
+ gemspec
5
+ gem "cast"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 kunigaku
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Cstub
2
+
3
+ Simple C stub generator.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cstub'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cstub
18
+
19
+ ## Usage
20
+
21
+ Usage: cstub [options]
22
+ -I/your/include/path
23
+ -DMACRO
24
+ --cpp Preprocessor
25
+ --filter filter.txt
26
+
27
+ example:
28
+
29
+ $ ls
30
+ my_dependency.h my_product_code.c my_product_code.o
31
+
32
+ $ arm-none-eabi-nm -u my_product_code.o > filter.txt
33
+
34
+ $ cat filter.txt
35
+ U depend_function
36
+ U depend_function2
37
+ U depend_function3
38
+
39
+ $ cstub *.c --filter filter.txt --cpp "arm-none-eabi-gcc -P -E"
40
+ int depend_function(int par0)
41
+ {
42
+ return 0;
43
+ }
44
+ MY_INT depend_function2(volatile unsigned int a, int b, int c)
45
+ {
46
+ return 0;
47
+ }
48
+ ERROR_CODE depend_function3(int par0)
49
+ {
50
+ return ERROR_NONE;
51
+ }
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it ( https://github.com/kunigaku/cstub/fork )
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/cstub ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
5
+ require 'cstub'
6
+
7
+ Cstub.main(ARGV)
data/cstub.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cstub/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cstub"
8
+ spec.version = Cstub::VERSION
9
+ spec.authors = ["kunigaku"]
10
+ spec.email = ["kunigaku@gmail.com"]
11
+ spec.summary = %q{Simple C stub generator.}
12
+ #spec.description = %q{TODO: Write a longer description. Optional.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "cast"
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ end
data/lib/cstub.rb ADDED
@@ -0,0 +1,175 @@
1
+ require "cstub/version"
2
+
3
+ require 'cast'
4
+ require 'optparse'
5
+
6
+ module Cstub
7
+ def self.collect_types(ast)
8
+ typelist = {}
9
+
10
+ ast.entities.each do |node|
11
+ node.Declaration? or next
12
+ if node.type.Struct? && node.type.members
13
+ if node.type.name
14
+ typelist[node.type.name] = { value: "{0}" }
15
+ end
16
+ end
17
+ if node.type.Enum? && node.type.members
18
+ if node.type.name
19
+ typelist[node.type.name] = { value: node.type.members[0].name }
20
+ end
21
+ end
22
+ if node.typedef?
23
+ node.declarators.each do |decl|
24
+ if decl.type.Enum?
25
+ typelist[decl.name] = { value: decl.type.members[0].name }
26
+ end
27
+ if decl.type.Int?
28
+ typelist[decl.name] = { value: '0' }
29
+ end
30
+ if decl.type.Char?
31
+ typelist[decl.name] = { value: '0' }
32
+ end
33
+ if decl.type.Float?
34
+ typelist[decl.name] = { value: '0.0' }
35
+ end
36
+ if decl.type.Pointer?
37
+ typelist[decl.name] = { value: 'NULL' }
38
+ end
39
+ if decl.type.Struct?
40
+ typelist[decl.name] = { value: '{0}' }
41
+ end
42
+ if decl.type.CustomType?
43
+ typelist[decl.name] = typelist[decl.type.name]
44
+ end
45
+ end
46
+ end
47
+ end
48
+ typelist
49
+ end
50
+
51
+ def self.collect_functions(ast)
52
+ functions = {}
53
+ typelist = collect_types ast
54
+ ast.entities.each do |node|
55
+ node.Declaration? or next
56
+ node.declarators.each do |decl|
57
+ if decl.type.Function?
58
+ retval = ""
59
+ if node.type.Int?
60
+ retval = "0"
61
+ end
62
+ if node.type.Void?;
63
+ retval = ""
64
+ end
65
+ if node.type.Float?
66
+ retval = "0.0"
67
+ end
68
+ if node.type.CustomType? || node.type.Struct? || node.type.Enum?
69
+ if typelist[node.type.name]
70
+ retval = typelist[node.type.name][:value]
71
+ else
72
+ puts 'hey!'
73
+ puts node.type.name
74
+ end
75
+ end
76
+ if decl.type.indirect_type.type && decl.type.indirect_type.type.Pointer?
77
+ retval = "NULL"
78
+ end
79
+ sig = "#{node.type.to_s}#{decl.indirect_type.type.to_s} #{decl.name}("
80
+ paramlist = []
81
+ parnum = 0
82
+ decl.indirect_type.params.each do |param|
83
+ par = "#{param.type.to_s} "
84
+ if param.name
85
+ par << "#{param.name.to_s}"
86
+ else
87
+ par << "par" + parnum.to_s
88
+ parnum = parnum + 1
89
+ end
90
+ paramlist << par
91
+ end
92
+ sig << paramlist.join(", ") << ")\n{\n"
93
+ if retval == ""
94
+ sig << " return;\n"
95
+ else
96
+ sig << " return #{retval};\n"
97
+ end
98
+ sig << "}\n"
99
+ functions[decl.name] = {stub:sig, storage:node.storage}
100
+ end
101
+ end
102
+ end
103
+ functions
104
+ end
105
+
106
+ def self.make_tree(file, cpp_command="", include_path=[], macros=[])
107
+ code = File.read(file)
108
+
109
+ cpp = C::Preprocessor.new
110
+ cpp.include_path.concat include_path
111
+ macros.each do |m|
112
+ s = m.split("=")
113
+ if s.length == 1
114
+ cpp.macros[s[0]] = ''
115
+ else
116
+ cpp.macros[s[0]] = s[1]
117
+ end
118
+ end
119
+ cpp.macros['__attribute__(x)'] = " "
120
+ cpp.macros['__builtin_va_list'] = 'int'
121
+ cpp.macros['__asm(x)'] = " "
122
+ if cpp_command != ""
123
+ C::Preprocessor.command = cpp_command
124
+ end
125
+ source = cpp.preprocess(code)
126
+ C.parse(source)
127
+ end
128
+
129
+ def self.make_stubs(files, cpp_command, include_path, macros)
130
+ list = {}
131
+ files.each do |f|
132
+ ast = make_tree(f, cpp_command, include_path, macros)
133
+ list.merge! collect_functions(ast)
134
+ end
135
+ list
136
+ end
137
+
138
+ def self.read_filter(file)
139
+ ret = {}
140
+ IO.foreach(file) do |l|
141
+ ret[l.split(' ')[-1]] = true
142
+ end
143
+ ret
144
+ end
145
+
146
+ def self.main(argv)
147
+ cpp_command = ''
148
+ include_path = []
149
+ macros = []
150
+ filters = {}
151
+ opt = OptionParser.new
152
+ opt.on('-I/your/include/path') {|v| include_path << v }
153
+ opt.on('-DMACRO') {|v| macros << v }
154
+ opt.on('--cpp Preprocessor') {|v| cpp_command = v }
155
+ opt.on('--filter filter.txt') {|v| filters.merge! read_filter(v) }
156
+ opt.parse!(argv)
157
+
158
+ if argv.length == 0
159
+ puts "error: no input files."
160
+ exit 0
161
+ end
162
+ list = make_stubs(argv, cpp_command, include_path, macros)
163
+ if filters.length == 0
164
+ list.each_pair do |k,v|
165
+ puts v[:stub]
166
+ end
167
+ else
168
+ filters.each_pair do |k,v|
169
+ if list.key? k
170
+ puts list[k][:stub]
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,3 @@
1
+ module Cstub
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cstub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kunigaku
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cast
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - kunigaku@gmail.com
58
+ executables:
59
+ - cstub
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/cstub
69
+ - cstub.gemspec
70
+ - lib/cstub.rb
71
+ - lib/cstub/version.rb
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Simple C stub generator.
96
+ test_files: []