rake-c 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rake/c.rb +140 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 531bc38fca469b090b01b66a0affadd8f5336207
4
+ data.tar.gz: 3baa5288d0425b506b078ccc2ee1f86cc463ec74
5
+ SHA512:
6
+ metadata.gz: c0c56b25059ce5dce79a5dc7007ff49facad22580501073b90b972ad5f84c23a68b9852a306d63b071f6a5bc44938bde6491cb5b5e060602b3279883ac43286f
7
+ data.tar.gz: 4e50e1b3089929da3fc44b6eb1dca7889742487728008bafe5c82145c82243b3fd26187e82d644143d983c2b78d05dba65471e7ac5230b41774ab96421b264b2
data/lib/rake/c.rb ADDED
@@ -0,0 +1,140 @@
1
+ require 'rake'
2
+
3
+ class C
4
+ @@cflags = '-Wall'
5
+ @@ldflags = ''
6
+ @@compiler = ENV['CC'] || 'cc'
7
+ @@ar = ENV['AR'] || 'ar'
8
+ @@builddir = 'build'
9
+ @@sourcedir = 'src'
10
+ @@libs = ''
11
+
12
+ def C.run cmd
13
+ r = `#{cmd}`
14
+ raise "Error: '#{cmd}'" unless $?.success?
15
+ return r
16
+ end
17
+
18
+ def C.clean
19
+ run "rm -rf \"#{@@builddir}\""
20
+ end
21
+
22
+ def C.cc
23
+ @@compiler
24
+ end
25
+
26
+ def C.cc= cc
27
+ @@compiler = cc
28
+ end
29
+
30
+ def C.ar
31
+ @@ar
32
+ end
33
+
34
+ def C.ar= ar
35
+ @@ar = ar
36
+ end
37
+
38
+ def C.cflags
39
+ @@cflags
40
+ end
41
+
42
+ def C.cflags= flags
43
+ @@cflags = flags
44
+ end
45
+
46
+ def C.ldflags
47
+ @@ldflags
48
+ end
49
+
50
+ def C.ldflags= flags
51
+ @@ldflags = flags
52
+ end
53
+
54
+ def C.library name, files
55
+ @@libs += " \"-I#{@@sourcedir}/#{name}\""
56
+ cflags = @@cflags
57
+ thelib = "#{@@builddir}/lib#{name}.a"
58
+ objects = Array.new
59
+ files.each do |f|
60
+ type = /\.[a-zA-Z0-9]+$/.match(f).to_s
61
+ if type == '.c' || type == '.cc' || type == '.cpp' then
62
+ o = f.ext('.o')
63
+ d = f.ext('.deps.rb')
64
+ theobject = "#{@@builddir}/objects/#{name}/#{o}"
65
+ thedeps = "#{@@builddir}/objects/#{name}/#{d}"
66
+ thesource = "#{@@sourcedir}/#{name}/#{f}"
67
+ objects.push theobject
68
+ Rake::FileTask.define_task theobject => [thesource] do
69
+ puts "[CC] #{theobject}"
70
+ run "mkdir -p \"#{@@builddir}/objects/#{name}\""
71
+ xxx = run "#{@@compiler} #{cflags}#{@@libs} -M \"#{thesource}\""
72
+ xx = []
73
+ File.open(thedeps, 'w') do |f|
74
+ f.puts '# This file was automatically generated. Do not edit!'
75
+ xxx.scan(/\s([^\s\\]+)/) do |x| xx.push x[0] end
76
+ f.puts "Rake::FileTask.define_task #{theobject.dump} => #{xx}"
77
+ end
78
+ run "#{@@compiler} #{cflags}#{@@libs} -c -o \"#{theobject}\" \"#{thesource}\""
79
+ end
80
+ load thedeps if File.file? thedeps
81
+ Rake::FileTask.define_task thelib => [theobject]
82
+ else
83
+ objects.push f
84
+ end
85
+ end
86
+ Rake::FileTask.define_task thelib do
87
+ puts "[AR] #{thelib}"
88
+ run "#{@@ar} rcs \"#{thelib}\" \"#{objects.join '" "'}\""
89
+ end
90
+ Rake::Task.define_task :default => thelib
91
+ Rake::Task.define_task :clean => :c_clean
92
+ end
93
+
94
+ def C.program name, files
95
+ cflags = @@cflags
96
+ theprogram = "#{@@builddir}/#{name}"
97
+ objects = Array.new
98
+ files.each do |f|
99
+ type = /\.[a-zA-Z0-9]+$/.match(f).to_s
100
+ if type == '.c' || type == '.cc' || type == '.cpp' then
101
+ o = f.ext('.o')
102
+ d = f.ext('.deps.rb')
103
+ theobject = "#{@@builddir}/objects/#{o}"
104
+ thedeps = "#{@@builddir}/objects/#{d}"
105
+ thesource = "#{@@sourcedir}/#{f}"
106
+ objects.push theobject
107
+ Rake::FileTask.define_task theobject => [thesource] do
108
+ puts "[CC] #{theobject}"
109
+ run "mkdir -p \"#{@@builddir}/objects\""
110
+ xxx = run "#{@@compiler} #{cflags}#{@@libs} -M \"#{thesource}\""
111
+ xx = []
112
+ File.open(thedeps, 'w') do |f|
113
+ f.puts '# This file was automatically generated. Do not edit!'
114
+ xxx.scan(/\s([^\s\\]+)/) do |x| xx.push x[0] end
115
+ f.puts "Rake::FileTask.define_task #{theobject.dump} => #{xx}"
116
+ end
117
+ run "#{@@compiler} #{cflags}#{@@libs} -c -o \"#{theobject}\" \"#{thesource}\""
118
+ end
119
+ load thedeps if File.file? thedeps
120
+ Rake::FileTask.define_task theprogram => [theobject]
121
+ elsif type == '.a' then
122
+ thefile = "#{@@builddir}/#{f}"
123
+ Rake::FileTask.define_task theprogram => [thefile]
124
+ objects.push thefile
125
+ else
126
+ objects.push f
127
+ end
128
+ end
129
+ Rake::FileTask.define_task theprogram do
130
+ puts "[LD] #{theprogram}"
131
+ run "#{@@compiler} #{@@ldflags} \"-L#{@@builddir}\" -o \"#{theprogram}\" \"#{objects.join '" "'}\""
132
+ end
133
+ Rake::Task.define_task :default => theprogram
134
+ Rake::Task.define_task :clean => :c_clean
135
+ end
136
+ end
137
+
138
+ Rake::Task.define_task :c_clean do
139
+ C.clean
140
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rake-c
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lukas Joeressen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This Gem can build small C programs.
14
+ email: lukas@joeressen.net
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/rake/c.rb
20
+ homepage: https://github.com/kext
21
+ licenses:
22
+ - Public Domain
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.5.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: C Build System
44
+ test_files: []