# SPDX-FileCopyrightText: Copyright (c) 2017 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: LicenseRef-NvidiaProprietary
#
# NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
# property and proprietary rights in and to this material, related
# documentation and any modifications thereto. Any use, reproduction,
# disclosure or distribution of this material and related documentation
# without an express license agreement from NVIDIA CORPORATION or
# its affiliates is strictly prohibited.

TEST = tsort6
NVXX = nvcc
CXX ?= nvc++
EXE = exe

CUFILE = keys.cu
# nvcc requires specifying compute capability to generate code for. The following logic
# attempts to figure this out automatically by looking through GPUs on current systems.
# However, it if fails to do so it may need done manually like the example.
# Example for Ampere: --generate-code arch=compute_80,code=sm_80
SYSCOMPCAP := $(shell nvaccelinfo | grep "Default Target:" | awk -F "cc" '{ print $$2 }' | sort | uniq)
NVCCOPTIONS ?= $(foreach CCAP,$(SYSCOMPCAP), --generate-code arch=compute_$(CCAP),code=sm_$(CCAP))

UNAME := $(shell uname -a)
ifeq ($(findstring ppc64, $(UNAME)), ppc64)
NVXXFLAGS ?= -c $(NVCCOPTIONS)
else
NVXXFLAGS ?= -ccbin nvc++ -c $(NVCCOPTIONS)
endif

# These are compatible flags between NVHPC and nvcc
ifeq ($(TEST),tsort6)
CXXFLAGS ?= -acc=gpu -gpu=nordc -cudalib=curand
else
CXXFLAGS ?= -acc=gpu -gpu=nordc,managed -cudalib=curand
endif

all: build run verify

build: $(TEST).cpp
	$(NVXX) $(NVXXFLAGS) $(CUFILE) -o keys.o
	$(CXX) $(CXXFLAGS) keys.o -o $(TEST).$(EXE) $<

run: $(TEST).$(EXE)
	$(RUN) ./$(TEST).$(EXE)

verify:

clean:
	@echo 'Cleaning up...'
	@rm -rf *.$(EXE) *.dwf *.pdb *.mod prof *.o
