# Copyright 1996-2022 Cyberbotics Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# determine the OSTYPE

ifeq ($(OS),Windows_NT)
 OSTYPE = windows
endif

ifndef OSTYPE
 OSTYPE = $(shell uname)
endif

ifeq ($(OSTYPE),Linux)
 OSTYPE = linux
endif

ifeq ($(OSTYPE),linux-gnu)
 OSTYPE = linux
endif

ifeq ($(OSTYPE),)
 OSTYPE = windows
endif

ifeq ($(OSTYPE),msys)
 OSTYPE = windows
endif

ifneq ($(findstring MINGW,$(OSTYPE)),)
 OSTYPE = windows
endif

ifneq ($(findstring CYGWIN,$(OSTYPE)),)
 OSTYPE = windows
endif

ifeq ($(OSTYPE),Darwin)
 OSTYPE = darwin
endif

# determine the OSARCH and UBUNTU_VERSION (linux only)
ifeq ($(OSTYPE),linux)
  ifeq ($(filter x86_64, $(shell uname -a)),)
    OSARCH = i386
  else
    OSARCH = x86_64
  endif
  ifeq ($(OSTYPE),linux)
    ifeq ($(SNAP_NAME), webots)
      UBUNTU_VERSION=20.04
    else
      ifneq (, $(shell which lsb_release))
        UBUNTU_VERSION:=$(shell lsb_release -sr)
      endif
    endif
  endif
endif

# OS specific variables

ifeq ($(OSTYPE),darwin)

 SUPPORTED_SDK = 11 12
 MACOSX_MIN_SDK_VERSION = 11

 # cf. https://stackoverflow.com/a/30225756/2210777
 MACOSX_SDK_PATH = $(shell xcrun --sdk macosx --show-sdk-path)
 ifeq ($(wildcard $(MACOSX_SDK_PATH)),)
  $(error The macOS SDK is not found. Please check that XCode and its command line tools are correctly installed.)
 endif
 MACOSX_SDK_VERSION = $(shell egrep -o '10.[0-9][0-9]' <<< $(MACOSX_SDK_PATH) )
 ifneq ($(MACOSX_SDK_VERSION),$(filter $(MACOSX_SDK_VERSION),$(SUPPORTED_SDK)))
  ## Only recent SDKs are working, but this doesn't prevent to use others.
  $(warning Your macOS SDK ("$(MACOSX_SDK_PATH)") is $(MACOSX_SDK_VERSION) while Webots supports the $(SUPPORTED_SDK) SDKs.)
 endif
endif

# extensions and prefixes
STATIC_LIB_EXTENSION = .a
ifeq ($(OSTYPE),windows)
 EXE_EXTENSION = .exe
 SHARED_LIB_EXTENSION = .dll
 WEBOTS_LIB_PATH=$(subst \,/,$(WEBOTS_HOME))/msys64/mingw64/bin
 WEBOTS_CONTROLLER_LIB_PATH=$(subst \,/,$(WEBOTS_HOME))/lib/controller
else
 LIB_PREFIX = lib
 ifeq ($(OSTYPE),linux)
  SHARED_LIB_EXTENSION = .so
  WEBOTS_LIB_PATH=$(WEBOTS_HOME)/lib/webots
  WEBOTS_CONTROLLER_LIB_PATH=$(WEBOTS_HOME)/lib/controller
 else # macOS
  SHARED_LIB_EXTENSION = .dylib
  WEBOTS_LIB_PATH=$(WEBOTS_HOME)/Contents/lib/webots
  WEBOTS_CONTROLLER_LIB_PATH=$(WEBOTS_HOME)/Contents/lib/controller
 endif
endif
