SUMMARY="A modern high performance safe general-purpose programming language"
DESCRIPTION="Swift is a general-purpose programming language built using a \
modern approach to safety, performance, and software design patterns. \
The goal of the Swift project is to create the best available \
language for uses ranging from systems programming to mobile \
and desktop apps, scaling up to cloud services. Most importantly, \
Swift  is designed to make writing and maintaining correct programs \
easier for the developer."
HOMEPAGE="https://swift.org"
COPYRIGHT="2017 Apple Inc"
LICENSE="Apache v2"
REVISION="3"
srcGitRev="94e444f22015d32b81e6dc1a0316a8243360559b"
SOURCE_URI="https://github.com/return/swift/archive/$srcGitRev.zip"
CHECKSUM_SHA256="e0d3e048e57ee0fc85b41392d5d075dc037ce5c03f13e41f15bd45d647322155"
SOURCE_DIR="swift-$srcGitRev"
srcGitRev_2="2c9b458a0db83c1aecafb12cef1ccc2d479d09bd"
SOURCE_URI_2="https://github.com/return/swift-clang/archive/$srcGitRev_2.zip"
CHECKSUM_SHA256_2="e0aaa0a0a38e47f1306ac5ad540c0e663ecf42a4d290d93601527421cbc3da16"
SOURCE_DIR_2="swift-clang-$srcGitRev_2"
srcGitRev_3="be1632fd0514632ae51073e5ed109b00b68e1cfd"
SOURCE_URI_3="https://github.com/return/swift-llvm/archive/$srcGitRev_3.zip"
CHECKSUM_SHA256_3="49789886819989bc97ec83dc7c5cfbca768bd76aa9e507882799f52c5a222270"
SOURCE_DIR_3="swift-llvm-$srcGitRev_3"
srcGitRev_4="912d9d4ccbfa41ba225c37351a217b678358cc3e"
SOURCE_URI_4="https://github.com/return/swift-cmark/archive/$srcGitRev_4.zip"
CHECKSUM_SHA256_4="cf7dae35aa844bc438cbca8f335494416c32eee3fa491eaee5a36e2625bc73be"
SOURCE_DIR_4="swift-cmark-$srcGitRev_4"
srcGitRev_5="9732781059bddc467677396562c987db4587074c"
SOURCE_URI_5="https://github.com/return/swift-compiler-rt/archive/$srcGitRev_5.zip"
CHECKSUM_SHA256_5="58b0aa642cf15b99d889b923c7624cb7c092facb86a5329449efa42a6443fc5d"
SOURCE_DIR_5="swift-compiler-rt-$srcGitRev_5"
PATCHES="swift_lang-3.1.patchset"

ARCHITECTURES="all !x86_gcc2 !x86"
SECONDARY_ARCHITECTURES="!x86"

PROVIDES="
	swift_lang$secondaryArchSuffix = $portVersion
	cmd:lldb_moduleimport_test$secondaryArchSuffix = $portVersion
	cmd:sil_func_extractor$secondaryArchSuffix = $portVersion
	cmd:sil_nm$secondaryArchSuffix = $portVersion
	cmd:sil_opt$secondaryArchSuffix = $portVersion
	cmd:sil_passpipeline_dumper$secondaryArchSuffix = $portVersion
	cmd:swift$secondaryArchSuffix = $portVersion
	cmd:swift_api_digester$secondaryArchSuffix = $portVersion
	cmd:swift_autolink_extract$secondaryArchSuffix = $portVersion
	cmd:swift_demangle$secondaryArchSuffix = $portVersion
	cmd:swift_format$secondaryArchSuffix = $portVersion
	cmd:swift_ide_test$secondaryArchSuffix = $portVersion
	cmd:swift_llvm_opt$secondaryArchSuffix = $portVersion
	cmd:swift_reflection_dump$secondaryArchSuffix = $portVersion
	cmd:swift_remoteast_test$secondaryArchSuffix = $portVersion
	cmd:swiftc$secondaryArchSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	llvm$secondaryArchSuffix >= 3.7.0 base
	lib:libcharset$secondaryArchSuffix
	lib:libedit$secondaryArchSuffix
	lib:libexecinfo$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libicudata$secondaryArchSuffix >= 66
	lib:libicui18n$secondaryArchSuffix >= 66
	lib:libicuuc$secondaryArchSuffix >= 66
	lib:libncurses$secondaryArchSuffix
	lib:libsqlite3$secondaryArchSuffix
	lib:libuuid$secondaryArchSuffix
	lib:libxml2$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	gcc_syslibs$secondaryArchSuffix_devel
	devel:libcharset$secondaryArchSuffix
	devel:libedit$secondaryArchSuffix
	devel:libexecinfo$secondaryArchSuffix
	devel:libgcc$secondaryArchSuffix
	devel:libiconv$secondaryArchSuffix
	devel:libicudata$secondaryArchSuffix >= 66
	devel:libicui18n$secondaryArchSuffix >= 66
	devel:libicuuc$secondaryArchSuffix >= 66
	devel:libncurses$secondaryArchSuffix
	devel:libsqlite3$secondaryArchSuffix
	devel:libuuid$secondaryArchSuffix
	devel:libxml2$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:clang$secondaryArchSuffix
	cmd:clang++$secondaryArchSuffix
	cmd:cmake
	cmd:diff
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:grep
	cmd:groff
	cmd:ld$secondaryArchSuffix
	cmd:libtool$secondaryArchSuffix
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:python2
	cmd:sed
	cmd:swig
	cmd:which
	"

BUILD()
{
	cd ..

	mkdir -p swift clang llvm cmark compiler-rt

	# Copy all the source files
	cp -rd $sourceDir/* swift
	cp -rd $sourceDir2/* clang
	cp -rd $sourceDir3/* llvm
	cp -rd $sourceDir4/* cmark
	cp -rd $sourceDir5/* compiler-rt

	# Fix permisssions for build-script.
	chmod +x ./swift/utils/build-script
	chmod +x ./swift/utils/build-script-impl
	chmod -R 755 ./swift/utils

	export HOST_CC="/bin/clang"
	export HOST_CXX="/bin/clang++"

	# Build faster by building only for X86
	python2 ./swift/utils/build-script -R -m \
		--extra-cmake-options='-DLLVM_ENABLE_ASSERTIONS=TRUE \
			-DCMAKE_SKIP_RPATH=FALSE -DLLVM_ENABLE_RTTI=ON \
			-DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_THREADS=YES \
			-DCMAKE_C_FLAGS=-fPIC -DCMAKE_CXX_FLAGS=-fPIC' \
		--stdlib-deployment-targets=haiku-x86_64 $jobArgs
}

INSTALL()
{
	mkdir -p $binDir $libDir
	mkdir -p $libDir/swift
	mkdir -p $libDir/swift/shims $libDir/swift/haiku/x86_64 $libDir/swift/clang/include

	cd ../build/Unix_Makefiles-ReleaseAssert/swift-haiku-x86_64

	# Copy the executables into the binDir.
	cp ./bin/* $binDir
	cd ./lib/swift/

	# Symlink and copy the clang headers.
	ln -sfn ../../../llvm-haiku-x86_64/lib/clang/4.0.0/include/ clang
	cp -R ./clang/* $libDir/swift/clang/include
	cp -R ./shims/* $libDir/swift/shims

	# Copy the specific library files.
	cp ./haiku/libswiftCore.so $libDir/swift/haiku
	cp ./haiku/libswiftGlibc.so $libDir/swift/haiku
	cp ./haiku/libswiftRemoteMirror.so $libDir/swift/haiku
	cp ./haiku/libswiftSwiftOnoneSupport.so $libDir/swift/haiku

	# Copy the essential swiftmodule files.
	cp ./haiku/x86_64/SwiftOnoneSupport.swiftmodule $libDir/swift/haiku/x86_64
	cp ./haiku/x86_64/Glibc.swiftmodule $libDir/swift/haiku/x86_64
	cp ./haiku/x86_64/Swift.swiftmodule $libDir/swift/haiku/x86_64

	# Copy the essential swiftdoc files.
	cp ./haiku/x86_64/SwiftOnoneSupport.swiftdoc $libDir/swift/haiku/x86_64
	cp ./haiku/x86_64/Glibc.swiftdoc $libDir/swift/haiku/x86_64
	cp ./haiku/x86_64/Swift.swiftdoc $libDir/swift/haiku/x86_64
	cp ./haiku/x86_64/glibc.modulemap  $libDir/swift/haiku/x86_64
	cp ./haiku/x86_64/swift_begin.o  $libDir/swift/haiku/x86_64
	cp ./haiku/x86_64/swift_end.o   $libDir/swift/haiku/x86_64
}

TEST()
{
	python2 utils/build-script -R -t
}
