
SDKNAME       = bella_scene_sdk
OUTNAME       = console
UNAME         = $(shell uname)

ifeq ($(UNAME), Darwin)

  SDKBASE     = .
  SOEXT       = .dylib
  SDKFNAME    = lib$(SDKNAME)$(SOEXT)
  USDFNAME    = libdl_usd_ms$(SOEXT)
  INCLUDEDIRS = -I$(SDKBASE)/src
  LIBDIR      = $(SDKBASE)/lib
  LIBDIRS     = -L$(LIBDIR)
  OBJDIR      = obj/$(UNAME)
  BINDIR      = bin/$(UNAME)
  OUTPUT      = $(BINDIR)/$(OUTNAME)

  ISYSROOT    = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk

  CC          = clang
  CXX         = clang++

  CCFLAGS     = -arch x86_64\
                -arch arm64\
                -mmacosx-version-min=11.0\
                -isysroot $(ISYSROOT)\
                -fvisibility=hidden\
                -O3\
                $(INCLUDEDIRS)

  CFLAGS      = $(CCFLAGS)\
                -std=c11

  CXXFLAGS    = $(CCFLAGS)\
                -std=c++11
        
  CPPDEFINES  = -DNDEBUG=1\
                -DDL_USE_SHARED

  LIBS        = -l$(SDKNAME)\
                -dl_usd_ms\
                -lm\
                -ldl

  LINKFLAGS   = -mmacosx-version-min=11.0\
                -isysroot $(ISYSROOT)\
                -framework Cocoa\
                -framework IOKit\
                -fvisibility=hidden\
                -O5\
                -rpath @executable_path
else

  SDKBASE     = .
  SOEXT       = .so
  SDKFNAME    = lib$(SDKNAME)$(SOEXT)
  USDFNAME    = libdl_usd_ms$(SOEXT)
  INCLUDEDIRS = -I$(SDKBASE)/src
  LIBDIR      = $(SDKBASE)/lib
  LIBDIRS     = -L$(LIBDIR)
  OBJDIR      = obj/$(UNAME)
  BINDIR      = bin/$(UNAME)
  OUTPUT      = $(BINDIR)/$(OUTNAME)

  CC          = gcc
  CXX         = g++

  CCFLAGS     = -m64\
                -Wall\
                -Werror\
                -fvisibility=hidden\
                -D_FILE_OFFSET_BITS=64\
                -O3\
                $(INCLUDEDIRS)

  CFLAGS      = $(CCFLAGS)\
                -std=c11

  CXXFLAGS    = $(CCFLAGS)\
                -std=c++11\
                -Wno-class-conversion
        
  CPPDEFINES  = -DNDEBUG=1\
                -DDL_USE_SHARED

  LIBS        = -l$(SDKNAME)\
                -dl_usd_ms\
                -lm\
                -ldl\
                -lrt\
                -lpthread

  LINKFLAGS   = -m64\
                -fvisibility=hidden\
                -O3\
                -Wl,-rpath,'$$ORIGIN'\
                -Wl,-rpath,'$$ORIGIN/lib'\
                -Wl,-rpath-link=./lib
endif

OBJS = main.o
OBJ = $(patsubst %,$(OBJDIR)/%,$(OBJS))

$(OBJDIR)/%.o: %.cpp
	@mkdir -p $(@D)
	$(CXX) -c -o $@ $< $(CXXFLAGS) $(CPPDEFINES)

$(OUTPUT): $(OBJ)
	@mkdir -p $(@D)
	$(CXX) -o $@ $^ $(LINKFLAGS) $(LIBDIRS) $(LIBS)
	@cp $(LIBDIR)/$(SDKFNAME) $(BINDIR)/$(SDKFNAME)
	@cp $(LIBDIR)/$(USDFNAME) $(BINDIR)/$(USDFNAME)
	@cp -r $(LIBDIR)/usd $(BINDIR)/usd

.PHONY: clean
clean:
	rm -f $(OBJDIR)/*.o
	rm -f $(OUTPUT)
	rm -f $(BINDIR)/$(SDKFNAME)
	rm -f $(BINDIR)/$(USDFNAME)
	rm -rf $(BINDIR)/usd
