lib/teapot/build.rb in teapot-0.3.2 vs lib/teapot/build.rb in teapot-0.5.0
- old
+ new
@@ -16,229 +16,26 @@
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
-require 'teapot/commands'
-require 'teapot/environment'
+require 'teapot/build/targets/directory'
+require 'teapot/build/targets/files'
+require 'teapot/build/targets/library'
+require 'teapot/build/targets/executable'
+require 'teapot/build/targets/application'
-require 'pathname'
-require 'rainbow'
-require 'shellwords'
-
-require 'teapot/build/linker'
-
module Teapot
module Build
- class UnsupportedPlatform < StandardError
+ def self.top(path)
+ Targets::Directory.target(nil, path)
end
- class Task
- def initialize(inputs, outputs)
- @inputs = inputs
- @outputs = outputs
- end
- end
-
- class Target
- def initialize(parent)
- @parent = parent
- @tasks = []
-
- @configure = nil
- end
+ def self.install_directory(root, directory, *args)
+ target = top(root)
- def root
- @parent.root
- end
+ target.add_directory(directory)
- def configure(&block)
- @configure = Proc.new &block
- end
-
- def self.target(*args, &block)
- instance = self.new(*args)
-
- if block_given?
- instance.instance_eval(&block)
- end
-
- return instance
- end
-
- def execute(command, environment, *arguments)
- if @configure
- environment = environment.merge &@configure
- end
-
- # Flatten the environment to a hash:
- values = environment.flatten
-
- puts "Executing command #{command} for #{root}...".color(:cyan)
-
- # Show the environment to the user:
- Environment::System::dump(values)
-
- self.send(command, values, *arguments)
- end
- end
-
- class CompilerTarget < Target
- def initialize(parent, name, options = {})
- super parent
-
- @name = name
- @options = options
- end
-
- def build_prefix!(environment)
- build_prefix = Pathname.new(environment[:build_prefix]) + @name
-
- build_prefix.mkpath
-
- return build_prefix
- end
-
- def install_prefix!(environment)
- install_prefix = Pathname.new(environment[:install_prefix])
-
- install_prefix.mkpath
-
- return install_prefix
- end
-
- def compile(environment, source_file)
- object_file = (build_prefix!(environment) + source_file.basename).sub_ext('.o')
-
- case source_file.extname
- when ".cpp"
- Commands.run(
- environment[:cxx],
- environment[:cxxflags],
- "-c", source_file, "-o", object_file
- )
- when ".c"
- Commands.run(
- environment[:cc],
- environment[:cflags],
- "-c", source_file, "-o", object_file
- )
- end
-
- return Array object_file
- end
- end
-
- class Library < CompilerTarget
- def subdirectory
- "lib"
- end
-
- def link(environment, objects)
- library_file = build_prefix!(environment) + "lib#{@name}.a"
-
- Linker.link_static(environment, library_file, objects)
-
- return library_file
- end
-
- def build(environment)
- files = sources
-
- objects = sources.collect do |file|
- compile(environment, file)
- end
-
- return Array link(environment, objects)
- end
-
- def install(environment)
- prefix = install_prefix!(environment)
-
- build(environment).each do |path|
- destination_path = prefix + subdirectory + path.basename
-
- destination_path.dirname.mkpath
- FileUtils.cp path, destination_path
- end
-
- if defined? headers
- headers.each do |path|
- relative_path = path.relative_path_from(root)
- destination_path = prefix + "include" + relative_path
-
- destination_path.dirname.mkpath
- FileUtils.cp path, destination_path
- end
- end
-
- if defined? files
- files.each do |path|
- relative_path = path.relative_path_from(root)
- destination_path = prefix + relative_path
-
- destination_path.dirname.mkpath
- FileUtils.cp path, destination_path
- end
- end
- end
- end
-
- class Executable < Library
- def subdirectory
- "bin"
- end
-
- def link(environment, objects)
- executable_file = build_prefix!(environment) + "#{@name}"
-
- Commands.run(
- environment[:cxx],
- environment[:cxxflags],
- "-o", executable_file, objects,
- environment[:ldflags]
- )
-
- return executable_file
- end
- end
-
- class Directory < Target
- def initialize(parent, root)
- @root = root
- @targets = []
- end
-
- attr :root
- attr :tasks
-
- def add_library(*args, &block)
- @targets << Library.target(self, *args, &block)
- end
-
- def add_executable(*args, &block)
- @targets << Executable.target(self, *args, &block)
- end
-
- def add_directory(path)
- directory = Directory.target(self, @root + path)
-
- $stderr.puts "Loading #{directory.root}..."
- build_path = (directory.root + "build.rb").to_s
- directory.instance_eval(File.read(build_path), build_path)
-
- @targets << directory
- end
-
- def execute(command, *arguments)
- $stderr.puts "Executing #{command} for #{@root}"
- @targets.each do |target|
- target.execute(command, *arguments)
- end
- end
- end
-
- def self.top(path)
- Directory.target(nil, path)
+ target.execute(:install, *args)
end
end
end