# Copyright 2013 David Axmark
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# 	http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This file defines helper classes used for compiling MoSync libraries for both
# native and pipe platforms.
# To use, call MoSyncLib.invoke with a module
# which has the setup_native and setup_pipe methods defined.

require "#{File.dirname(__FILE__)}/dll.rb"
require "#{File.dirname(__FILE__)}/pipe.rb"

module MoSyncMod
	include MoSyncInclude
	def modSetup
		@EXTRA_INCLUDES = @EXTRA_INCLUDES.to_a + [mosync_include]
	end

	def copyHeaders(endings = ['.h', '.hpp'])
		dir = mosync_include + "/" + @INSTALL_INCDIR
		# create a bunch of CopyFileTasks, then invoke them all.
		endings.each do |ending|
			collect_headers(ending).each do |h|
				task = CopyFileTask.new(self, dir + "/" + File.basename(h.to_s), h)
				@prerequisites = [task] + @prerequisites
			end
		end
		@prerequisites = [DirTask.new(self, dir)] + @prerequisites
	end

	private

	def collect_headers(ending)
		default(:HEADER_DIRS, @SOURCES)
		files = []
		@HEADER_DIRS.each {|dir| files += Dir[dir+"/*"+ending]}
		files.flatten!
		if(defined?(@IGNORED_HEADERS))
			files.reject! {|file| @IGNORED_HEADERS.member?(File.basename(file)) }
		end
		files.reject! {|file| File.directory?(file) }
		return files.collect do |file| FileTask.new(self, file) end
	end
end

class MoSyncDllWork < DllWork
	include MoSyncMod
	def setup
		set_defaults
		setup_native
		modSetup
		if(HOST == :win32)
			@EXTRA_LINKFLAGS = @EXTRA_LINKFLAGS.to_s + " -Wl,--enable-auto-import"
		end
		@EXTRA_CFLAGS = @EXTRA_CFLAGS.to_s + " -D_POSIX_SOURCE"	#avoid silly bsd functions
		copyHeaders
		super
	end
end

class PipeLibWork < PipeGccWork
	include MoSyncMod
	def setup
		set_defaults
		@FLAGS = " -L -quiet"
		setup_pipe
		modSetup
		copyHeaders
		super
	end
	def setup3(all_objects, have_cppfiles)
		@TARGET_PATH = "#{mosync_libdir}/#{@BUILDDIR_NAME}/#{@NAME}.lib"
		super(all_objects, have_cppfiles)
		if(!USE_NEWLIB)	# rake support
			d = (CONFIG == "debug") ? 'D' : ''
			dir = "#{mosync_libdir}/pipe/"
			@prerequisites << DirTask.new(self, dir)
			@prerequisites << CopyFileTask.new(self, "#{dir}#{@NAME}#{d}.lib", @TARGET)
		end
	end
	#def filename; @NAME + ".lib"; end
end

module MoSyncLib end

def MoSyncLib.inin(work, mod)
	work.extend(mod)
	work.invoke
end

def MoSyncLib.clean(work, mod)
	work.extend(mod)
	work.setup
	work.execute_clean
end

def MoSyncLib.invoke(mod)
	target :pipe do
		MoSyncLib.inin(PipeLibWork.new, mod)
	end
	target :native do
		MoSyncLib.inin(MoSyncDllWork.new, mod)
	end
	target :default => [:pipe]
	target :clean do
		MoSyncLib.clean(PipeLibWork.new, mod)
		#MoSyncLib.clean(MoSyncDllWork.new, mod)
	end
	target :clean_native do
		MoSyncLib.clean(MoSyncDllWork.new, mod)
	end

	Targets.invoke
end
