Skip to content

Instantly share code, notes, and snippets.

@snuxoll
Created May 14, 2009 19:04
Show Gist options
  • Save snuxoll/111827 to your computer and use it in GitHub Desktop.
Save snuxoll/111827 to your computer and use it in GitHub Desktop.
# pkg_config.rb - pkg-config wrapper
# Author:: Stefan Nuxoll
# License:: BSD License
# Copyright:: (C) 2009 Stefan Nuxoll
class PkgConfig
attr_accessor :packages
def initialize(packages)
@packages = packages
end
def self.check_pkg_config(version)
message = 'Checking for pkg-config... '
if system "pkg-config --atleast-pkgconfig-version=\"#{version}\""
puts message + "yes"
true
else
puts message + "no"
false
end
end
def self.check_pkg(package, version)
package_string = "\"#{package} #{version}\""
message = "Checking for #{package}... "
if system "pkg-config --exists #{package_string}"
puts message + "yes"
true
else
puts message + "no"
false
end
end
def cflags
%x[pkg-config --cflags #{packages.join(" ")}]
end
def libs
%x[pkg-config --libs #{packages.join(" ")}]
end
end
require 'ftools'
require "vala"
require "pkg_config"
require "source_modules"
desc "Compile droppit"
task :default => [:build]
task :build => [:check_deps, :droppit]
desc "Regenerate code and build"
task :rebuild => [:generate, :build]
modules = SourceModules.new "src", [
'application',
'ui/main_window',
'ui/settings_window',
'util/drop_selector'
]
dependencies = PkgConfig.new [
"glib-2.0",
"gobject-2.0",
"gmodule-2.0",
"gtk+-2.0"
]
task :check_deps do
if not PkgConfig.check_pkg_config(">= 0.15.0")
raise Exception, "pkg-config >= 0.15.0 not found."
end
if not PkgConfig.check_pkg("glib-2.0", ">= 2.20.0")
raise Exception, "glib-2.0 >= 2.20.0 not found."
end
if not PkgConfig.check_pkg("gobject-2.0", ">= 2.20.0")
raise Exception, "gobject-2.0 >= 2.20.0 not found."
end
if not PkgConfig.check_pkg("gmodule-2.0", ">= 2.20.0")
raise Exception, "gmodule-2.0 >= 2.20.0 not found."
end
if not PkgConfig.check_pkg("gtk+-2.0", ">= 2.16.0")
raise Exception, "gtk+-2.0 >= 2.16.0 not found."
end
end
desc "Clean the source directory"
task :clean do
files = Dir["#{modules.src_dir}/**/*.o",
"droppit"]
files.each do |file|
puts "rm #{file}"
File.delete(file)
end
end
desc "Fully clean the source directory (only leave original source files)"
task :full_clean => [:clean] do
files = Dir["#{modules.src_dir}/**/*.c"]
files.each do |file|
puts "rm #{file}"
File.delete(file)
end
end
desc "Generate C code"
task :generate do
valac = Vala.new(modules.deps('.vala'))
valac.packages = [
"gmodule-2.0",
"gtk+-2.0"
]
valac.generate
end
# Final droppit binary
file 'droppit' => modules.deps('.o') do |t|
sh "cc -o #{t.name} #{modules.deps('.o').join(" ")} #{dependencies.libs}"
end
# Build object files from C code
rule '.o' => ['.c'] do |t|
sh "cc -o #{t.name} #{t.source} -c #{dependencies.cflags}"
end
class SourceModules
attr_accessor :src_dir
attr_accessor :list
def initialize(src_dir = "src", list = [])
@src_dir = src_dir
@list = list
end
def deps(suffix)
list.collect { |x| "#{src_dir}/#{x}#{suffix}" }
end
end
# vala.rb - vala compiler wrapper
# Author:: Stefan Nuxoll
# License:: BSD License
# Copyright: 2009 Stefan Nuxoll
class Vala
attr_accessor :files
attr_accessor :packages
attr_accessor :valac
attr_accessor :flags
def initialize(files)
@files = files
@packages = []
@flags = []
@valac = ENV["VALAC"] || 'valac'
end
# Generates C code from Vala source files
def generate
sh "#{@valac} -C#{valac_flags}#{@files.join(" ")}"
end
private
# Generates the list of flags to be passed to the compiler
def valac_flags
flags = " "
@packages.each do |pkg|
flags << "--pkg=#{pkg} "
end
@flags.each do |flag|
flags << "#{flag} "
end
flags
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment