# Nact is thorough, but slow. MSBuild is fast, but has an inconvenient command-line syntax.

# Make "NX_Develop" the default configuration
BUILD ?= NX_Develop
# Make "NX32" the default platform
ARCH ?= NX32

BUILD_DIR := $(patsubst NX_%,%,$(BUILD))
ARCH_DIR := $(patsubst NX%,NX-NXFP2-a%,$(ARCH))

# Bash has problems with variables containing parentheses
MSBUILDPATH := $(shell cmd /c echo '%ProgramFiles(x86)%')/MSBuild/12.0/Bin/MSBuild.exe
# REPOSITORY_ROOT has quotation marks at beginning and end that cause problems
REPOROOT := $(shell cygpath $(patsubst "%,%,$(patsubst %",%,$(REPOSITORY_ROOT))))
VCPROPSDIR := $(REPOROOT)/Tests/Common/Build/Vc/SigloTestProgramProjectTemplate
MSBUILDARGS := /property:Configuration=$(BUILD) /property:Platform=$(ARCH) /consoleloggerparameters:ErrorsOnly /consoleloggerparameters:WarningsOnly /nologo /maxcpucount
#~ MSBUILDARGS := /property:Configuration=$(BUILD) /property:Platform=$(ARCH) /consoleloggerparameters:ShowCommandLine /nologo /maxcpucount

TEST := testMultimedia_MediaCodecSanity
VCPROJ := $(TEST)-spec.NX.vcxproj
VCLOCALPROPS := SigloTestVcProjectSettings.local.props
NCA := $(REPOROOT)/Tests/Outputs/$(ARCH_DIR)/Tests/$(TEST)/$(BUILD_DIR)/$(TEST).nca
SOFILE := $(REPOROOT)/Programs/Iris/Libraries/$(ARCH_DIR)/$(BUILD_DIR)/nnSdk.so
SRCS := test_MediaCodecSanity.cpp

.PHONY: all
all: $(NCA)

.PHONY: clean
clean:
	@echo Cleaning with MSBuild…
	@"$(shell cygpath "$(MSBUILDPATH)")" $(MSBUILDARGS) /target:clean $(VCPROJ)

.PHONY: run
run: $(NCA)
	@echo Running with RunOnTarget…
	@$(REPOROOT)/Tools/CommandLineTools/RunOnTarget "$(shell cygpath -aw "$^")" $(RUNARGS)

$(NCA): $(VCPROJ) $(VCLOCALPROPS) $(SRCS) $(SOFILE)
	@echo Checking code formatting…
	@$(REPOROOT)/Externals/google-styleguide/cpplint/cpplint.py $(SRCS)
	@echo Building with MSBuild…
	@"$(shell cygpath "$(MSBUILDPATH)")" $(MSBUILDARGS) $(VCPROJ)

$(VCPROJ): !.nact Makefile
	@echo Creating VS project with nact…
	@nact platforms=NXFP2-a32,NXFP2-a64 specs=NX BUILD=Develop outputs=vcprojects

$(VCLOCALPROPS): $(VCPROPSDIR)/$(VCLOCALPROPS)
	@echo Copying test program template \"props\" file…
	@cp $^ ./

# Shared object files are now called NSS files, but something that the test VS projects reference still uses the old name
$(SOFILE): $(patsubst %.so,%.nss,$(SOFILE))
	ln $^ $@
