SubDir HAIKU_TOP ;

# Prepare the optional build features before parsing the Jamfile tree.
local architectureObject ;
for architectureObject in [ MultiArchSubDirSetup ] {
	on $(architectureObject) {
		include [ FDirName $(HAIKU_BUILD_RULES_DIR) BuildFeatures ] ;

		# Let the target platform depend on the external header directories such
		# that those will be unpacked before anything is built for the target
		# platform.
		Depends $(TARGET_PLATFORM) : [
			BuildFeatureAttribute gcc_syslibs_devel : c++-headers
		] [
			BuildFeatureAttribute gcc_syslibs_devel : gcc-headers
		] ;
	}
}

# Include required packages:
# primary architecture
AddHaikuImageSystemPackages [ FFilterByBuildFeatures
	bash bc freetype libsolv zlib

	!gcc2 @{ coreutils icu66 }@
	gcc2 @{ coreutils_x86 icu icu66_x86@secondary_x86 }@
	regular_image @{
		bzip2 expat ffmpeg glu gutenprint8 gzip
		lame less libedit libicns
		mesa mesa_devel mesa_swpipe@!gcc2 mesa_swrast@gcc2
		netcat
		sed sharutils
		unzip wget which zip

		!gcc2 @{ diffutils findutils gawk grep tar tcpdump zstd }@
		gcc2 @{
			diffutils_x86@secondary_x86 findutils_x86@secondary_x86
			mawk grep_x86@secondary_x86 tar_x86@secondary_x86
			tcpdump_x86@secondary_x86
		}@
	}@
] ;
AddHaikuImageSourcePackages [ FFilterByBuildFeatures
	bash bc

	!gcc2 @{ coreutils }@
	gcc2 @{ coreutils_x86 }@

	regular_image @{
		expat ffmpeg findutils grep gutenprint8 gzip
		lame less libicns
		sed sharutils
		tar wget which
		!gcc2 @{ gawk }@
		gcc2 @{ mawk }@
	}@
] ;
if $(HAIKU_PACKAGING_ARCHS[1]) != x86_gcc2 {
	AddHaikuImageSystemPackages [ FFilterByBuildFeatures gcc_syslibs ] ;
}

if $(HAIKU_PACKAGING_ARCHS[2]) {
	# secondary architectures
	local architectureObject ;
	for architectureObject
			in [ MultiArchSubDirSetup $(HAIKU_PACKAGING_ARCHS[2-]) ] {
		on $(architectureObject) {
			AddHaikuImageSystemPackages [ FFilterByBuildFeatures
				freetype icu libsolv zlib

				regular_image @{
					ffmpeg glu jasper jpeg libavif libicns libpng16 libwebp mesa
				}@
			] ;
			if $(TARGET_PACKAGING_ARCH) != x86_gcc2 {
				AddHaikuImageSystemPackages [ FFilterByBuildFeatures gcc_syslibs
					mesa_swpipe ] ;
			}
		}
	}
}

# If enabled, make sure that OpenSSL is added to the image.
if [ FIsBuildFeatureEnabled openssl ] {
	AddHaikuImageSystemPackages openssl ;
}

# add additionally requested packages
AddHaikuImageSystemPackages $(HAIKU_IMAGE_ADDITIONAL_PACKAGES) ;

# Optionally we allow not to include the "src" subdirectory.
if $(HAIKU_DONT_INCLUDE_SRC) {
	# Don't include "src", but at least include the stuff needed for the
	# build.
	SubInclude HAIKU_TOP src build ;
	SubInclude HAIKU_TOP src tools ;
} else {
	SubInclude HAIKU_TOP src ;
}
if $(HAIKU_INCLUDE_3RDPARTY) {
	SubInclude HAIKU_TOP 3rdparty ;
}

# Perform deferred SubIncludes.
ExecuteDeferredSubIncludes ;

# reset subdir
SubDir HAIKU_TOP ;

# Execute post-build-target user config rules.
UserBuildConfigRulePostBuildTargets	;

# specify the Haiku repository contents
include [ FDirName $(HAIKU_BUILD_RULES_DIR) repositories Haiku ] ;

# specify the actual Haiku image contents
include [ FDirName $(HAIKU_BUILD_RULES_DIR) images HaikuImage ] ;

# specify the additional image contents one for each boot type
include [ FDirName $(HAIKU_BUILD_RULES_DIR) images NetBootArchive ] ;
include [ FDirName $(HAIKU_BUILD_RULES_DIR) images FloppyBootImage ] ;
include [ FDirName $(HAIKU_BUILD_RULES_DIR) images CDBootImage ] ;
include [ FDirName $(HAIKU_BUILD_RULES_DIR) images HaikuCD ] ;
include [ FDirName $(HAIKU_BUILD_RULES_DIR) images AnybootImage ] ;
include [ FDirName $(HAIKU_BUILD_RULES_DIR) images MMCImage ] ;

# Check whether all requested optional packages do actually exist.
local package ;
local packageError ;
for package in $(HAIKU_ADDED_OPTIONAL_PACKAGES) {
	if ! [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_EXISTS) ] {
		Echo "ERROR: Requested optional package \"$(package)\" does not"
			"exist." ;
		packageError = 1 ;
	}
}
if $(packageError) {
	Exit ;
}

# Pseudo-target to build all targets that are localized.
NotFile LocalizedTargets ;
Depends LocalizedTargets : $(HAIKU_LOCALIZED_TARGETS) ;

NotFile catalogs ;
Depends catalogs : $(HAIKU_LOCALE_CATALOGS) ;

NotFile catkeys ;
Depends catkeys : $(HAIKU_LOCALE_OUTPUT_CATKEYS) ;