# Copyright 2021 DeepMind Technologies Limited
#
# 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
#
#     https://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.

cmake_minimum_required(VERSION 3.16)

# INTERPROCEDURAL_OPTIMIZATION is enforced when enabled.
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
# Default to GLVND if available.
set(CMAKE_POLICY_DEFAULT_CMP0072 NEW)

# This line has to appear before 'PROJECT' in order to be able to disable incremental linking
set(MSVC_INCREMENTAL_DEFAULT ON)

project(
  mujoco_samples
  VERSION 4.1.1
  DESCRIPTION "MuJoCo samples binaries"
  HOMEPAGE_URL "https://mujoco.org"
)

enable_language(C)
enable_language(CXX)
if(APPLE)
  enable_language(OBJC)
  enable_language(OBJCXX)
endif()

# Check if we are building as standalone project.
set(SAMPLE_STANDALONE OFF)
set(_INSTALL_SAMPLES ON)
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  set(SAMPLE_STANDALONE ON)
  # If standalone, do not install the samples.
  set(_INSTALL_SAMPLES OFF)
endif()

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../cmake")

if(SAMPLE_STANDALONE)
  include(SampleOptions)
elseif(APPLE)
  add_compile_options(${MUJOCO_MACOS_COMPILE_OPTIONS})
endif()
include(SampleDependencies)

set(MUJOCO_SAMPLE_COMPILE_OPTIONS "${AVX_COMPILE_OPTIONS}" "${EXTRA_COMPILE_OPTIONS}")
set(MUJOCO_SAMPLE_LINK_OPTIONS "${EXTRA_LINK_OPTIONS}")

if(MUJOCO_HARDEN)
  if(WIN32)
    set(MUJOCO_SAMPLE_LINK_OPTIONS "${MUJOCO_SAMPLE_LINK_OPTIONS}" -Wl,/DYNAMICBASE)
  else()
    set(MUJOCO_SAMPLE_COMPILE_OPTIONS "${MUJOCO_SAMPLE_COMPILE_OPTIONS}" -fPIE)
    if(APPLE)
      set(MUJOCO_SAMPLE_LINK_OPTIONS "${MUJOCO_SAMPLE_LINK_OPTIONS}" -Wl,-pie)
    else()
      set(MUJOCO_SAMPLE_LINK_OPTIONS "${MUJOCO_SAMPLE_LINK_OPTIONS}" -pie)
    endif()
  endif()
endif()

# Build sample binaries
add_executable(compile compile.cc)
target_compile_options(compile PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
target_link_libraries(compile Threads::Threads)
target_link_options(
  compile PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS} ${MUJOCO_MACOS_LINK_OPTIONS}
)

# Build sample binaries
add_executable(dependencies dependencies.cc)
target_compile_options(dependencies PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
target_link_libraries(dependencies Threads::Threads)
target_link_options(
  dependencies PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS} ${MUJOCO_MACOS_LINK_OPTIONS}
)

add_executable(testspeed testspeed.cc)
target_compile_options(testspeed PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
target_link_libraries(testspeed Threads::Threads)
target_link_options(
  testspeed PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS} ${MUJOCO_MACOS_LINK_OPTIONS}
)

target_link_libraries(compile mujoco::mujoco)
target_link_libraries(dependencies mujoco::mujoco)
target_link_libraries(testspeed mujoco::mujoco)

# Build samples that require GLFW.

add_executable(basic basic.cc)
target_compile_options(basic PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
target_link_libraries(
  basic
  mujoco::mujoco
  glfw
  Threads::Threads
)
target_link_options(
  basic PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS} ${MUJOCO_MACOS_LINK_OPTIONS}
)

if(MUJOCO_USE_FILAMENT AND NOT SAMPLE_STANDALONE)
  add_executable(render render.cc)
  target_compile_options(render PUBLIC ${MUJOCO_SAMPLE_COMPILE_OPTIONS})
  if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    target_compile_options(render PRIVATE -Wno-gnu-anonymous-struct -Wno-nested-anon-types)
  endif()
  target_link_libraries(
    render
    mujoco::mujoco
    mujoco::filament
    lodepng
    Threads::Threads
  )
  target_link_options(render PRIVATE ${MUJOCO_SAMPLE_LINK_OPTIONS})
  if(APPLE AND DEFINED MUJOCO_MACOSX_VERSION_MIN)
    # Omit -Wl,-no_weak_imports: Filament uses guarded @available(...) checks.
    target_link_options(render PRIVATE -mmacosx-version-min=${MUJOCO_MACOSX_VERSION_MIN})
  endif()

  # Deploy Filament assets (compiled shaders, IBL) next to the binary so the
  # Filament backend can load them at runtime. On macOS framework builds, render
  # is embedded in MuJoCo.app and libmujoco_filament loads assets directly from
  # the embedded mujoco.framework/Versions/A/Resources/assets.
  if(NOT (APPLE AND MUJOCO_BUILD_MACOS_FRAMEWORKS))
    add_dependencies(render mujoco_filament_assets)
    add_custom_command(
      TARGET render POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_BINARY_DIR}/src/render/filament/assets
        $<TARGET_FILE_DIR:render>/assets
      COMMENT "Deploying Filament assets for render sample")
  endif()
endif()

if(APPLE AND MUJOCO_BUILD_MACOS_FRAMEWORKS)
  embed_in_bundle(basic simulate)
  embed_in_bundle(compile simulate)
  embed_in_bundle(dependencies simulate)
  embed_in_bundle(testspeed simulate)
  if(MUJOCO_USE_FILAMENT AND NOT SAMPLE_STANDALONE)
    embed_in_bundle(render simulate)
  endif()
endif()

# Do not install if macOS Bundles are created as RPATH is managed manually there.
if(APPLE AND MUJOCO_BUILD_MACOS_FRAMEWORKS)
  set(_INSTALL_SAMPLES OFF)
endif()

if(_INSTALL_SAMPLES)

  include(TargetAddRpath)

  # Add support to RPATH for the samples.
  set(MUJOCO_INSTALL_SAMPLES
      basic
      compile
      dependencies
      testspeed
  )
  if(MUJOCO_USE_FILAMENT AND NOT SAMPLE_STANDALONE)
    list(APPEND MUJOCO_INSTALL_SAMPLES render)
  endif()

  target_add_rpath(
    TARGETS
    ${MUJOCO_INSTALL_SAMPLES}
    INSTALL_DIRECTORY
    "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}"
    LIB_DIRS
    "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}"
    DEPENDS
    MUJOCO_ENABLE_RPATH
  )

  install(
    TARGETS ${MUJOCO_INSTALL_SAMPLES}
    EXPORT ${PROJECT_NAME}
    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT samples
    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT samples
    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT samples
    BUNDLE DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT samples
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT samples
  )

  if(NOT MUJOCO_SAMPLES_USE_SYSTEM_GLFW)
    # We downloaded GLFW. Depending if it is a static or shared LIBRARY we might
    # need to install it.
    get_target_property(MJ_GLFW_LIBRARY_TYPE glfw TYPE)
    if(MJ_GLFW_LIBRARY_TYPE STREQUAL SHARED_LIBRARY)
      install(
        TARGETS glfw
        EXPORT ${PROJECT_NAME}
        RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT samples
        LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT samples
        ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT samples
        PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT samples
      )
    endif()
  endif()
endif()
