From e474cb7f671d796179fa73378598959ad01bdc49 Mon Sep 17 00:00:00 2001 From: ksuprynowicz Date: Sun, 24 Oct 2021 15:47:17 +0200 Subject: [PATCH] Added CMake variables for optimization and architecture-specific options --- CMakeLists.txt | 58 ++++++++++++++++++++++++++++++ cmake/compiler.cmake | 2 ++ cmake/ports/bullet3/portfile.cmake | 26 +++++++++++++- cmake/ports/opus/portfile.cmake | 28 ++++++++++++++- 4 files changed, 112 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8abcce4489..820ba2aa32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,64 @@ if ((NOT "${RELEASE_TYPE}" STREQUAL "PRODUCTION") AND (NOT "${RELEASE_TYPE}" STR set(RELEASE_TYPE "DEV") endif() +# VIRCADIA_OPTIMIZE +# Variable determining vircadia optimization. If not set, it defaults to true. +# It's used to determine build flags for main codebase and for VCPKG dependencies. +# Should be set to false to get completely unoptimized build for easier line-by-line debugging + +if(NOT DEFINED VIRCADIA_OPTIMIZE) + message("Enabling code optimization for Vircadia and compiled dependencies") + set(VIRCADIA_OPTIMIZE true CACHE BOOL "Enable code optimization for Vircadia and compiled dependencies") +endif() + +#compiler needs to be detected before building VCPKG dependencies +set(CMAKE_PLATFORM_INFO_DIR "${CMAKE_CURRENT_BINARY_DIR}") +include(CMakeDetermineCXXCompiler) + +set(VIRCADIA_OPTIMIZE_FLAGS "") + +if(VIRCADIA_OPTIMIZE) + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/_env/VIRCADIA_OPTIMIZE.txt" "${VIRCADIA_OPTIMIZE}") + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + message("Clang compiler detected, adding -O3 -fPIC flags") + set(VIRCADIA_OPTIMIZE_FLAGS "-O3 -fPIC") + elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU") + message("GCC compiler detected, adding -O3 -fPIC flags") + set(VIRCADIA_OPTIMIZE_FLAGS "-O3 -fPIC") + elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + message("MSVC compiler detected, adding /O2 flag") + set(VIRCADIA_OPTIMIZE_FLAGS "/O2") + endif() +endif() +MESSAGE(STATUS "VIRCADIA_OPTIMIZE: ${VIRCADIA_OPTIMIZE}") + +# VIRCADIA_CPU_ARCHITECTURE +# Variable determining CPU architecture for which Vircadia will be built. +# If defined, it's appended to CXXFLAGS and CFLAGS for both Vircadia and VCPKG dependencies + +#Assume -march=native for compilers that allow it if architecture is not specified +if(NOT DEFINED VIRCADIA_CPU_ARCHITECTURE) + if(VIRCADIA_OPTIMIZE AND ( (CMAKE_CXX_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "GNU") ) ) + message("Optimization is enabled, but architecture is not specified. Assuming native build") + set(VIRCADIA_CPU_ARCHITECTURE "-march=native -mtune=native" CACHE STRING "Specify architecture dependent compiler flags here") + endif() +endif() + +if(DEFINED VIRCADIA_CPU_ARCHITECTURE) + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/_env/VIRCADIA_CPU_ARCHITECTURE.txt" "${VIRCADIA_CPU_ARCHITECTURE}") + set(VIRCADIA_OPTIMIZE_FLAGS "${VIRCADIA_OPTIMIZE_FLAGS} ${VIRCADIA_CPU_ARCHITECTURE}") + message("Adding CPU architecture flags: ${VIRCADIA_CPU_ARCHITECTURE}") + MESSAGE(STATUS "VIRCADIA_CPU_ARCHITECTURE: ${VIRCADIA_CPU_ARCHITECTURE}") +endif() + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VIRCADIA_OPTIMIZE_FLAGS}") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VIRCADIA_OPTIMIZE_FLAGS}") + +set(ENV{CXXFLAGS} "$ENV{CXXFLAGS} ${VIRCADIA_OPTIMIZE_FLAGS}") +set(ENV{CFLAGS} "$ENV{CFLAGS} ${VIRCADIA_OPTIMIZE_FLAGS}") + +message($ENV{CXXFLAGS}) + if (HIFI_ANDROID) execute_process( COMMAND ${HIFI_PYTHON_EXEC} ${CMAKE_CURRENT_SOURCE_DIR}/prebuild.py --release-type ${RELEASE_TYPE} --android ${HIFI_ANDROID_APP} --build-root ${CMAKE_BINARY_DIR} diff --git a/cmake/compiler.cmake b/cmake/compiler.cmake index 45cef27727..5108253403 100644 --- a/cmake/compiler.cmake +++ b/cmake/compiler.cmake @@ -110,3 +110,5 @@ if (APPLE) set(CMAKE_OSX_SYSROOT ${_OSX_DESIRED_SDK_PATH}/MacOSX${OSX_SDK}.sdk) endif () endif () + +message("CXXFLAGS: ${CXXFLAGS}") diff --git a/cmake/ports/bullet3/portfile.cmake b/cmake/ports/bullet3/portfile.cmake index cda39ed349..8fc34179b6 100644 --- a/cmake/ports/bullet3/portfile.cmake +++ b/cmake/ports/bullet3/portfile.cmake @@ -15,6 +15,14 @@ include(vcpkg_common_functions) +if(EXISTS "${VCPKG_ROOT_DIR}/_env/VIRCADIA_OPTIMIZE.txt") + file(READ "${VCPKG_ROOT_DIR}/_env/VIRCADIA_OPTIMIZE.txt" VIRCADIA_OPTIMIZE) +endif() +if(EXISTS "${VCPKG_ROOT_DIR}/_env/VIRCADIA_CPU_ARCHITECTURE.txt") + file(READ "${VCPKG_ROOT_DIR}/_env/VIRCADIA_CPU_ARCHITECTURE.txt" VIRCADIA_CPU_ARCHITECTURE) +endif() + + if (VCPKG_LIBRARY_LINKAGE STREQUAL dynamic) message(WARNING "Dynamic not supported, building static") set(VCPKG_LIBRARY_LINKAGE static) @@ -30,6 +38,21 @@ vcpkg_from_github( PATCHES "bullet-git-fix-build-clang-8.patch" ) + +if(VIRCADIA_OPTIMIZE) + set(VIRCADIA_BULLET_OPTIONS "-DCMAKE_BUILD_TYPE=Release") +else() + set(VIRCADIA_BULLET_OPTIONS "-DCMAKE_BUILD_TYPE=RelWithDebInfo") +endif() + +set(VIRCADIA_BULLET_OPTIONS "${VIRCADIA_BULLET_OPTIONS}") + +if(DEFINED VIRCADIA_CPU_ARCHITECTURE) + set(VIRCADIA_BULLET_OPTIONS "${VIRCADIA_BULLET_OPTIONS} -DCMAKE_CXX_FLAGS=\"${VIRCADIA_CPU_ARCHITECTURE}\" -DCMAKE_C_FLAGS=\"${VIRCADIA_CPU_ARCHITECTURE}\" ") +endif() + +message("Optimization options for Bullet: ${VIRCADIA_BULLET_OPTIONS}") + vcpkg_configure_cmake( SOURCE_PATH ${SOURCE_PATH} OPTIONS @@ -46,6 +69,7 @@ vcpkg_configure_cmake( -DBUILD_UNIT_TESTS=OFF -DBUILD_SHARED_LIBS=ON -DINSTALL_LIBS=ON + ${VIRCADIA_BULLET_OPTIONS} ) vcpkg_install_cmake() @@ -58,4 +82,4 @@ file(REMOVE_RECURSE ${CURRENT_PACKAGES_DIR}/include/bullet/BulletInverseDynamics vcpkg_copy_pdbs() # Handle copyright -file(INSTALL ${SOURCE_PATH}/LICENSE.txt DESTINATION ${CURRENT_PACKAGES_DIR}/share/bullet3 RENAME copyright) \ No newline at end of file +file(INSTALL ${SOURCE_PATH}/LICENSE.txt DESTINATION ${CURRENT_PACKAGES_DIR}/share/bullet3 RENAME copyright) diff --git a/cmake/ports/opus/portfile.cmake b/cmake/ports/opus/portfile.cmake index e5518351f8..238f481690 100644 --- a/cmake/ports/opus/portfile.cmake +++ b/cmake/ports/opus/portfile.cmake @@ -1,5 +1,12 @@ include(vcpkg_common_functions) +if(EXISTS "${VCPKG_ROOT_DIR}/_env/VIRCADIA_OPTIMIZE.txt") + file(READ "${VCPKG_ROOT_DIR}/_env/VIRCADIA_OPTIMIZE.txt" VIRCADIA_OPTIMIZE) +endif() +if(EXISTS "${VCPKG_ROOT_DIR}/_env/VIRCADIA_CPU_ARCHITECTURE.txt") + file(READ "${VCPKG_ROOT_DIR}/_env/VIRCADIA_CPU_ARCHITECTURE.txt" VIRCADIA_CPU_ARCHITECTURE) +endif() + vcpkg_from_github( OUT_SOURCE_PATH SOURCE_PATH @@ -12,7 +19,26 @@ vcpkg_from_github( HEAD_REF master) -vcpkg_configure_cmake(SOURCE_PATH ${SOURCE_PATH} PREFER_NINJA) +if(VIRCADIA_OPTIMIZE) + set(VIRCADIA_OPUS_OPTIONS "-DCMAKE_BUILD_TYPE=Release") +else() + set(VIRCADIA_OPUS_OPTIONS "-DCMAKE_BUILD_TYPE=RelWithDebInfo") +endif() + +set(VIRCADIA_OPUS_OPTIONS "${VIRCADIA_OPUS_OPTIONS}") + +if(DEFINED VIRCADIA_CPU_ARCHITECTURE) + set(VIRCADIA_OPUS_OPTIONS "${VIRCADIA_OPUS_OPTIONS} -DCMAKE_CXX_FLAGS=\"${VIRCADIA_CPU_ARCHITECTURE}\" -DCMAKE_C_FLAGS=\"${VIRCADIA_CPU_ARCHITECTURE}\" ") +endif() + +message("Optimization options for Opus: ${VIRCADIA_OPUS_OPTIONS}") + +vcpkg_configure_cmake( + SOURCE_PATH ${SOURCE_PATH} + PREFER_NINJA + OPTIONS ${VIRCADIA_OPUS_OPTIONS} +) + vcpkg_install_cmake() vcpkg_fixup_cmake_targets(CONFIG_PATH lib/cmake/Opus) vcpkg_copy_pdbs()