$(eval $(call import.MODULE.defs,MACOSX,macosx))
$(eval $(call import.GCC,MACOSX))

MACOSX.src/   = $(SRC/)macosx/
MACOSX.build/ = $(BUILD/)macosx/
MACOSX.xroot/ = $(BUILD/)xroot/
MACOSX.xarchive/ = $(BUILD/)xroot/HandBrake/

MACOSX.m4.in  = $(wildcard $(MACOSX.src/)*.m4)
MACOSX.m4.out = $(MACOSX.m4.in:$(MACOSX.src/)%.m4=$(MACOSX.build/)%)

MACOSX.osl.filelist = $(MACOSX.build/)osl.filelist.txt

###############################################################################

BUILD.out += $(MACOSX.m4.out)
BUILD.out += $(MACOSX.osl.filelist)

###############################################################################

MACOSX.project = -project $(MACOSX.src/)HandBrake.xcodeproj

## configuration: must be one of { release, debug }
MACOSX.configuration = -configuration $(MACOSX.map.g.$(MACOSX.GCC.g))

## mapping from symbolic debug value to xcode configuration
MACOSX.map.g.none = release
MACOSX.map.g.min  = debug
MACOSX.map.g.std  = debug
MACOSX.map.g.max  = debug

## xcconfig: must be one of macosx/xcconfig/*.xcconfig
MACOSX.xcconfig = $(foreach x,$(XCODE.xcconfig),-xcconfig $(MACOSX.src/)xcconfig/$(x))
MACOSX.sdk      = $(foreach sdk,$(GCC.sysroot),-sdk $(sdk))

## some optional features use ifdef directives outside of libhb (e.g. CLI)
ifeq (1,$(FEATURE.x265))
    MACOSX.GCC.D += -DUSE_X265
endif

## extra CFLAGS: macro definitions
MACOSX.extra_cflags = OTHER_CFLAGS='$(MACOSX.GCC.D)'

## launch a build through xcode; which in turn will do a nested make against
## this build system with normal build rules enabled.
##
## $(1) = list of targets
## $(2) = list of goals to shunt through xcodebuild->make
##

MACOSX.XCODE = $(strip \
    $(XCODEBUILD.exe) \
        $(MACOSX.project) \
        $(foreach t,$(1),-target $t) \
        $(MACOSX.configuration) \
        $(MACOSX.xcconfig) \
        \
        SYMROOT='$(XCODE.symroot)' \
        CONFIGURATION_BUILD_DIR='$(XCODE.symroot)' \
        CONFIGURATION_TEMP_DIR='$(XCODE.symroot)' \
        \
        EXTERNAL_BUILD='$(abspath $(BUILD))' \
        EXTERNAL_CONF_ARGS='$(CONF.args)' \
        EXTERNAL_DRIVER='$(XCODE.driver)' \
        EXTERNAL_GOALS='$(3)' \
        EXTERNAL_JOBS='$(BUILD.jobs)' \
        EXTERNAL_VARS='$(-*-command-variables-*-)' \
        \
        $(MACOSX.extra_cflags) \
        \
        $(2) )

MACOSX.XCODE_ARCHIVE = $(strip \
    $(XCODEBUILD.exe) \
        $(MACOSX.project) \
        $(foreach t,$(1),-scheme $t) \
        $(MACOSX.configuration) \
        $(MACOSX.xcconfig) \
        \
        SYMROOT='$(XCODE.symroot)' \
        CONFIGURATION_BUILD_DIR='$(XCODE.symroot)' \
        CONFIGURATION_TEMP_DIR='$(XCODE.symroot)' \
        \
        -derivedDataPath '$(MACOSX.xarchive/)' \
        -archivePath '$(MACOSX.xarchive/)' \
        \
        EXTERNAL_BUILD='$(abspath $(BUILD))' \
        EXTERNAL_CONF_ARGS='$(CONF.args)' \
        EXTERNAL_DRIVER='$(XCODE.driver)' \
        EXTERNAL_GOALS='$(3)' \
        EXTERNAL_JOBS='$(BUILD.jobs)' \
        EXTERNAL_VARS='$(-*-command-variables-*-)' \
        \
        $(MACOSX.extra_cflags) \
        \
        $(2) ) ; $(strip \
    $(XCODEBUILD.exe) \
        -exportArchive \
        -exportOptionsPlist '$(MACOSX.src/)archive.plist' \
        -exportPath '$(MACOSX.xarchive/)' \
        -archivePath '$(MACOSX.xarchive/)' )

MACOSX.SIGN = $(strip $(MACOSX.src/)hbsign '$(ID)' $(MACOSX.xroot/)HandBrake.app)
