SubDir HAIKU_TOP src libs icu source data ;

ICU_UNICODE_VERSION = 5.1 ;
ICU_CLDR_VERSION = $(ICU_GENRB_CLDR_VERSION) ;
ICU_DAT_FILES = pnames.icu unames.icu cnvalias.icu ucadata.icu invuca.icu ;
ICU_VERSION = 42.1 ;
ICU_DATA_SYMBOL = icudt42 ;
# ICU is endianness-aware, so we need to set the endianness indicator such
# that it matches the endianness of the host platform
if $(HAIKU_HOST_IS_BIG_ENDIAN) = 1 {
	ICU_ENDIANNESS_INDICATOR = b ;
} else {
	ICU_ENDIANNESS_INDICATOR = l ;
}
ICU_DATA_VERSION = icudt42$(ICU_ENDIANNESS_INDICATOR) ;
ICU_DATA_DIR
	= [ FDirName $(HOST_COMMON_DEBUG_OBJECT_DIR) $(ICU_DATA_VERSION) ] ;

rule MakeLocateICUData files : subdir
{
	local file ;
	for file in $(files) {
		MakeLocate $(file) : [ FDirName $(ICU_DATA_DIR) $(subdir) ] ;
	}
}

# -----------------------------------------------------------------------------
# resource bundles (*.res)
# -----------------------------------------------------------------------------

NotFile ICU_ResourceBundles ;
Depends ICU_ResourceBundles : ICU_BreakIterators ;

rule GenerateIndexedResourceBundles
	indexName : installableSources : otherSources : targetDir : sourceDir
	: additionalDependencies
{
	local sources = $(installableSources) $(otherSources) ;
	GenerateResourceBundles $(installableSources) : $(targetDir)
		: $(sourceDir) ;
	GenerateResourceBundles $(otherSources) : $(targetDir) : $(sourceDir)
		: $(additionalDependencies) ;

	local indexSource = $(indexName:G=$(sourceDir)) ;
	MakeLocateICUData $(indexSource) : $(targetDir) ;
	GenerateResourceIndex $(indexSource) : $(installableSources)
		: $(ICU_CLDR_VERSION) ;

	local indexTarget = $(indexSource:S=.res) ;
	Depends $(indexTarget) : $(additionalDependencies) ;
	Depends ICU_ResourceBundles : $(indexTarget) ;
	GenerateResourceBundle $(indexTarget) : $(indexSource) : $(targetDir) ;
}

rule GenerateResourceBundles
	sources : targetDir : sourceDir : additionalDependencies
{
	for source in $(sources) {
		local target = $(source:D=:S=.res:G=$(sourceDir)) ;
		Depends ICU_ResourceBundles : $(target) ;
		Depends $(target) : $(additionalDependencies) ;
		GenerateResourceBundle $(target) : $(source) : $(targetDir) ;
	}
}

rule GenerateResourceBundle target : source : targetDir
{
	MakeLocateICUData $(target) : $(targetDir) ;
	Depends $(target) : <build>genrb $(ICU_DAT_FILES) $(source) ;
	LocalClean clean : $(target) ;
	GenerateResourceBundle1 $(target) : <build>genrb $(source) ;
}

actions GenerateResourceBundle1
{
	$(HOST_ADD_BUILD_COMPATIBILITY_LIB_DIR)
	$(2[1]) -k -q -i $(ICU_DATA_DIR) -s $(2[2]:D) -d $(1:D) $(2[2]:D=) ;
}

rule GenerateResourceIndex target : sources : cldrVersion
{
	Depends $(target) : $(sources) ;
	NotFile $(cldrVersion) ;
	LocalClean clean : $(target) ;
	GenerateResourceIndex1 $(target) : $(cldrVersion) $(sources) ;
}

actions GenerateResourceIndex1
{
	$(HOST_ADD_BUILD_COMPATIBILITY_LIB_DIR)
	echo "// Warning this file is automatically generated" >$(1)
	echo "res_index:table(nofallback) {" >>$(1)
	echo "    CLDRVersion { \"$(2[1]:D=)\" }" >>$(1)
	echo "    InstalledLocales {" >>$(1)
	for file in $(2[2-]:B) ; do echo "        $file {\"\"}" >>$(1) ; done
	echo "    }" >>$(1)
	echo "}" >>$(1)
}

SubInclude HAIKU_TOP src libs icu source data brkitr ;
SubInclude HAIKU_TOP src libs icu source data coll ;
SubInclude HAIKU_TOP src libs icu source data locales ;
SubInclude HAIKU_TOP src libs icu source data mappings ;
SubInclude HAIKU_TOP src libs icu source data misc ;
SubInclude HAIKU_TOP src libs icu source data rbnf ;
SubInclude HAIKU_TOP src libs icu source data sprep ;
SubInclude HAIKU_TOP src libs icu source data translit ;
SubInclude HAIKU_TOP src libs icu source data unidata ;

SubDir HAIKU_TOP src libs icu source data ;

# -----------------------------------------------------------------------------
# package everything into a library (libicu-data.so)
# -----------------------------------------------------------------------------

NotFile ICU_PackageData ;
Depends ICU_PackageData
	:
	ICU_BreakIterators
	ICU_CharsetConverters
	ICU_Confusables
	ICU_ResourceBundles
	ICU_StringPreps
	;

rule GeneratePackageList target
{
	LocalClean clean : $(target) ;
	GeneratePackageList1 $(target) ;
}

actions GeneratePackageList1
{
	cd $(ICU_DATA_DIR)
	find . -type f -name \*.brk -print | sed 's/^\.\///' >$(1:D=)
	find . -type f -name \*.cfu -print | sed 's/^\.\///' >>$(1:D=)
	find . -type f -name \*.cnv -print | sed 's/^\.\///' >>$(1:D=)
	find . -type f -name \*.ctd -print | sed 's/^\.\///' >>$(1:D=)
	find . -type f -name \*.icu -print | sed 's/^\.\///' >>$(1:D=)
	find . -type f -name \*.res -print | sed 's/^\.\///' >>$(1:D=)
	find . -type f -name \*.spp -print | sed 's/^\.\///' >>$(1:D=)
}

rule GeneratePackageInclude target
{
	MakeLocateICUData $(target) ;
	LocalClean clean : $(target) ;
	GeneratePackageInclude1 $(target) ;
}

actions GeneratePackageInclude1
{
	echo "GENCCODE_ASSEMBLY_TYPE=-a gcc" >$(1)
	echo "SO=" >>$(1)
	echo "SOBJ=" >>$(1)
	echo "A=" >>$(1)
	echo "LIBPREFIX=" >>$(1)
	echo "LIB_EXT_ORDER=" >>$(1)
	echo "COMPILE=" >>$(1)
	echo "LIBFLAGS=" >>$(1)
	echo "GENLIB=" >>$(1)
	echo "LDICUDTFLAGS=" >>$(1)
	echo "LD_SONAME=" >>$(1)
	echo "RPATH_FLAGS=" >>$(1)
	echo "BIR_LDFLAGS=" >>$(1)
	echo "AR=" >>$(1)
	echo "ARFLAGS=" >>$(1)
	echo "RANLIB=" >>$(1)
	echo "INSTALL_CMD=" >>$(1)
}

rule GeneratePackageDataAsm target : source
{
	# the asm code is where we cross the border from host to target arch
	MakeLocateArch $(target) ;

	Depends $(target) : <build>pkgdata $(source) icupkg.inc ;
	LocalClean clean : $(target) ;
	GeneratePackageDataAsm1 $(target) : <build>pkgdata $(source) icupkg.inc ;
}

actions GeneratePackageDataAsm1
{
	$(HOST_ADD_BUILD_COMPATIBILITY_LIB_DIR)
	$(2[1]) -O $(2[3]) -c -s $(ICU_DATA_DIR) -d $(1:D) -e $(ICU_DATA_SYMBOL) -T $(1:D) -p $(ICU_DATA_VERSION) -m asm -r $(ICU_VERSION) -L icu-data $(2[2])
}

MakeLocateICUData icudata.lst ;
Depends icudata.lst : ICU_PackageData ;
GeneratePackageList icudata.lst ;
GeneratePackageInclude icupkg.inc ;

local icuDataAsmSource = [ FGristFiles $(ICU_DATA_VERSION)_dat.s ] ;
GeneratePackageDataAsm $(icuDataAsmSource) : icudata.lst ;
SharedLibrary libicu-data.so
	: $(icuDataAsmSource) : : $(HAIKU_ICU_API_VERSION) ;
