From 4e1825a3fecbf74407d8c54158ec26ba463f7fe3 Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Tue, 9 Nov 2021 11:10:03 -0800 Subject: [PATCH 1/2] Better compiler detection on Linux (#5376) * Better compiler detection on Linux Moving EngineFinder.cmake to cmake/ in the templates Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> * skipping detection if compiler is passed through environment Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> * Fixes condition, needs to be in quotes since is the value of the sttring Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- CMakeLists.txt | 1 + .../DefaultProject/Template/CMakeLists.txt | 3 +- .../Template/cmake/CompilerSettings.cmake | 13 +++++++ .../Template/{ => cmake}/EngineFinder.cmake | 0 .../Platform/Linux/CompilerSettings.cmake | 34 +++++++++++++++++++ Templates/DefaultProject/template.json | 16 +++++++-- .../MinimalProject/Template/CMakeLists.txt | 3 +- .../Template/cmake/CompilerSettings.cmake | 13 +++++++ .../Template/{ => cmake}/EngineFinder.cmake | 0 .../Platform/Linux/CompilerSettings.cmake | 34 +++++++++++++++++++ Templates/MinimalProject/template.json | 16 +++++++-- cmake/CompilerSettings.cmake | 13 +++++++ cmake/Platform/Linux/CompilerSettings.cmake | 34 +++++++++++++++++++ 13 files changed, 174 insertions(+), 6 deletions(-) create mode 100644 Templates/DefaultProject/Template/cmake/CompilerSettings.cmake rename Templates/DefaultProject/Template/{ => cmake}/EngineFinder.cmake (100%) create mode 100644 Templates/DefaultProject/Template/cmake/Platform/Linux/CompilerSettings.cmake create mode 100644 Templates/MinimalProject/Template/cmake/CompilerSettings.cmake rename Templates/MinimalProject/Template/{ => cmake}/EngineFinder.cmake (100%) create mode 100644 Templates/MinimalProject/Template/cmake/Platform/Linux/CompilerSettings.cmake create mode 100644 cmake/CompilerSettings.cmake create mode 100644 cmake/Platform/Linux/CompilerSettings.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index e659270f84..f61a9561e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ include(cmake/Version.cmake) include(cmake/OutputDirectory.cmake) if(NOT PROJECT_NAME) + include(cmake/CompilerSettings.cmake) project(O3DE LANGUAGES C CXX VERSION ${LY_VERSION_STRING} diff --git a/Templates/DefaultProject/Template/CMakeLists.txt b/Templates/DefaultProject/Template/CMakeLists.txt index 76e8f227be..ae4bb662a3 100644 --- a/Templates/DefaultProject/Template/CMakeLists.txt +++ b/Templates/DefaultProject/Template/CMakeLists.txt @@ -10,11 +10,12 @@ if(NOT PROJECT_NAME) cmake_minimum_required(VERSION 3.20) + include(cmake/CompilerSettings.cmake) project(${Name} LANGUAGES C CXX VERSION 1.0.0.0 ) - include(EngineFinder.cmake OPTIONAL) + include(cmake/EngineFinder.cmake OPTIONAL) find_package(o3de REQUIRED) o3de_initialize() else() diff --git a/Templates/DefaultProject/Template/cmake/CompilerSettings.cmake b/Templates/DefaultProject/Template/cmake/CompilerSettings.cmake new file mode 100644 index 0000000000..cf6614e4a5 --- /dev/null +++ b/Templates/DefaultProject/Template/cmake/CompilerSettings.cmake @@ -0,0 +1,13 @@ +# +# Copyright (c) Contributors to the Open 3D Engine Project. +# For complete copyright and license terms please see the LICENSE at the root of this distribution. +# +# SPDX-License-Identifier: Apache-2.0 OR MIT +# +# + +# File to tweak compiler settings before compiler detection happens (before project() is called) +# We dont have PAL enabled at this point, so we can only use pure-CMake variables +if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux") + include(cmake/Platform/${CMAKE_HOST_SYSTEM_NAME}/CompilerSettings.cmake) +endif() diff --git a/Templates/DefaultProject/Template/EngineFinder.cmake b/Templates/DefaultProject/Template/cmake/EngineFinder.cmake similarity index 100% rename from Templates/DefaultProject/Template/EngineFinder.cmake rename to Templates/DefaultProject/Template/cmake/EngineFinder.cmake diff --git a/Templates/DefaultProject/Template/cmake/Platform/Linux/CompilerSettings.cmake b/Templates/DefaultProject/Template/cmake/Platform/Linux/CompilerSettings.cmake new file mode 100644 index 0000000000..9bb629c53b --- /dev/null +++ b/Templates/DefaultProject/Template/cmake/Platform/Linux/CompilerSettings.cmake @@ -0,0 +1,34 @@ +# +# Copyright (c) Contributors to the Open 3D Engine Project. +# For complete copyright and license terms please see the LICENSE at the root of this distribution. +# +# SPDX-License-Identifier: Apache-2.0 OR MIT +# +# + +if(NOT CMAKE_C_COMPILER AND NOT CMAKE_CXX_COMPILER AND NOT "$ENV{CC}" AND NOT "$ENV{CXX}") + set(path_search + /bin + /usr/bin + /usr/local/bin + /sbin + /usr/sbin + /usr/local/sbin + ) + list(TRANSFORM path_search APPEND "/clang-[0-9]*") + file(GLOB clang_versions ${path_search}) + if(clang_versions) + # Find and pick the highest installed version + list(SORT clang_versions COMPARE NATURAL) + list(GET clang_versions 0 clang_higher_version_path) + string(REGEX MATCH "clang-([0-9.]*)" clang_higher_version ${clang_higher_version_path}) + if(CMAKE_MATCH_1) + set(CMAKE_C_COMPILER clang-${CMAKE_MATCH_1}) + set(CMAKE_CXX_COMPILER clang++-${CMAKE_MATCH_1}) + else() + message(FATAL_ERROR "Clang not found, please install clang") + endif() + else() + message(FATAL_ERROR "Clang not found, please install clang") + endif() +endif() diff --git a/Templates/DefaultProject/template.json b/Templates/DefaultProject/template.json index 1e84ea8424..a36926f632 100644 --- a/Templates/DefaultProject/template.json +++ b/Templates/DefaultProject/template.json @@ -181,8 +181,20 @@ "isOptional": false }, { - "file": "EngineFinder.cmake", - "origin": "EngineFinder.cmake", + "file": "cmake/EngineFinder.cmake", + "origin": "cmake/EngineFinder.cmake", + "isTemplated": false, + "isOptional": false + }, + { + "file": "cmake/CompilerSettings.cmake", + "origin": "cmake/CompilerSettings.cmake", + "isTemplated": false, + "isOptional": false + }, + { + "file": "cmake/Platform/Linux/CompilerSettings.cmake", + "origin": "cmake/Platform/Linux/CompilerSettings.cmake", "isTemplated": false, "isOptional": false }, diff --git a/Templates/MinimalProject/Template/CMakeLists.txt b/Templates/MinimalProject/Template/CMakeLists.txt index 76e8f227be..ae4bb662a3 100644 --- a/Templates/MinimalProject/Template/CMakeLists.txt +++ b/Templates/MinimalProject/Template/CMakeLists.txt @@ -10,11 +10,12 @@ if(NOT PROJECT_NAME) cmake_minimum_required(VERSION 3.20) + include(cmake/CompilerSettings.cmake) project(${Name} LANGUAGES C CXX VERSION 1.0.0.0 ) - include(EngineFinder.cmake OPTIONAL) + include(cmake/EngineFinder.cmake OPTIONAL) find_package(o3de REQUIRED) o3de_initialize() else() diff --git a/Templates/MinimalProject/Template/cmake/CompilerSettings.cmake b/Templates/MinimalProject/Template/cmake/CompilerSettings.cmake new file mode 100644 index 0000000000..cf6614e4a5 --- /dev/null +++ b/Templates/MinimalProject/Template/cmake/CompilerSettings.cmake @@ -0,0 +1,13 @@ +# +# Copyright (c) Contributors to the Open 3D Engine Project. +# For complete copyright and license terms please see the LICENSE at the root of this distribution. +# +# SPDX-License-Identifier: Apache-2.0 OR MIT +# +# + +# File to tweak compiler settings before compiler detection happens (before project() is called) +# We dont have PAL enabled at this point, so we can only use pure-CMake variables +if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux") + include(cmake/Platform/${CMAKE_HOST_SYSTEM_NAME}/CompilerSettings.cmake) +endif() diff --git a/Templates/MinimalProject/Template/EngineFinder.cmake b/Templates/MinimalProject/Template/cmake/EngineFinder.cmake similarity index 100% rename from Templates/MinimalProject/Template/EngineFinder.cmake rename to Templates/MinimalProject/Template/cmake/EngineFinder.cmake diff --git a/Templates/MinimalProject/Template/cmake/Platform/Linux/CompilerSettings.cmake b/Templates/MinimalProject/Template/cmake/Platform/Linux/CompilerSettings.cmake new file mode 100644 index 0000000000..9bb629c53b --- /dev/null +++ b/Templates/MinimalProject/Template/cmake/Platform/Linux/CompilerSettings.cmake @@ -0,0 +1,34 @@ +# +# Copyright (c) Contributors to the Open 3D Engine Project. +# For complete copyright and license terms please see the LICENSE at the root of this distribution. +# +# SPDX-License-Identifier: Apache-2.0 OR MIT +# +# + +if(NOT CMAKE_C_COMPILER AND NOT CMAKE_CXX_COMPILER AND NOT "$ENV{CC}" AND NOT "$ENV{CXX}") + set(path_search + /bin + /usr/bin + /usr/local/bin + /sbin + /usr/sbin + /usr/local/sbin + ) + list(TRANSFORM path_search APPEND "/clang-[0-9]*") + file(GLOB clang_versions ${path_search}) + if(clang_versions) + # Find and pick the highest installed version + list(SORT clang_versions COMPARE NATURAL) + list(GET clang_versions 0 clang_higher_version_path) + string(REGEX MATCH "clang-([0-9.]*)" clang_higher_version ${clang_higher_version_path}) + if(CMAKE_MATCH_1) + set(CMAKE_C_COMPILER clang-${CMAKE_MATCH_1}) + set(CMAKE_CXX_COMPILER clang++-${CMAKE_MATCH_1}) + else() + message(FATAL_ERROR "Clang not found, please install clang") + endif() + else() + message(FATAL_ERROR "Clang not found, please install clang") + endif() +endif() diff --git a/Templates/MinimalProject/template.json b/Templates/MinimalProject/template.json index 21608e9204..4260e71527 100644 --- a/Templates/MinimalProject/template.json +++ b/Templates/MinimalProject/template.json @@ -173,8 +173,20 @@ "isOptional": false }, { - "file": "EngineFinder.cmake", - "origin": "EngineFinder.cmake", + "file": "cmake/EngineFinder.cmake", + "origin": "cmake/EngineFinder.cmake", + "isTemplated": false, + "isOptional": false + }, + { + "file": "cmake/CompilerSettings.cmake", + "origin": "cmake/CompilerSettings.cmake", + "isTemplated": false, + "isOptional": false + }, + { + "file": "cmake/Platform/Linux/CompilerSettings.cmake", + "origin": "cmake/Platform/Linux/CompilerSettings.cmake", "isTemplated": false, "isOptional": false }, diff --git a/cmake/CompilerSettings.cmake b/cmake/CompilerSettings.cmake new file mode 100644 index 0000000000..cf6614e4a5 --- /dev/null +++ b/cmake/CompilerSettings.cmake @@ -0,0 +1,13 @@ +# +# Copyright (c) Contributors to the Open 3D Engine Project. +# For complete copyright and license terms please see the LICENSE at the root of this distribution. +# +# SPDX-License-Identifier: Apache-2.0 OR MIT +# +# + +# File to tweak compiler settings before compiler detection happens (before project() is called) +# We dont have PAL enabled at this point, so we can only use pure-CMake variables +if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux") + include(cmake/Platform/${CMAKE_HOST_SYSTEM_NAME}/CompilerSettings.cmake) +endif() diff --git a/cmake/Platform/Linux/CompilerSettings.cmake b/cmake/Platform/Linux/CompilerSettings.cmake new file mode 100644 index 0000000000..9bb629c53b --- /dev/null +++ b/cmake/Platform/Linux/CompilerSettings.cmake @@ -0,0 +1,34 @@ +# +# Copyright (c) Contributors to the Open 3D Engine Project. +# For complete copyright and license terms please see the LICENSE at the root of this distribution. +# +# SPDX-License-Identifier: Apache-2.0 OR MIT +# +# + +if(NOT CMAKE_C_COMPILER AND NOT CMAKE_CXX_COMPILER AND NOT "$ENV{CC}" AND NOT "$ENV{CXX}") + set(path_search + /bin + /usr/bin + /usr/local/bin + /sbin + /usr/sbin + /usr/local/sbin + ) + list(TRANSFORM path_search APPEND "/clang-[0-9]*") + file(GLOB clang_versions ${path_search}) + if(clang_versions) + # Find and pick the highest installed version + list(SORT clang_versions COMPARE NATURAL) + list(GET clang_versions 0 clang_higher_version_path) + string(REGEX MATCH "clang-([0-9.]*)" clang_higher_version ${clang_higher_version_path}) + if(CMAKE_MATCH_1) + set(CMAKE_C_COMPILER clang-${CMAKE_MATCH_1}) + set(CMAKE_CXX_COMPILER clang++-${CMAKE_MATCH_1}) + else() + message(FATAL_ERROR "Clang not found, please install clang") + endif() + else() + message(FATAL_ERROR "Clang not found, please install clang") + endif() +endif() From 289d783f25a61d1789ef443190fb0d8fb4b7d7ab Mon Sep 17 00:00:00 2001 From: Steve Pham <82231385+spham-amzn@users.noreply.github.com> Date: Thu, 11 Nov 2021 09:41:45 -0800 Subject: [PATCH 2/2] Fixes for Project Manager on Linux - Fix error with EngineFinder using the wrong path to locate project.json - Simplified and expanded clang detection - Remove forcing clang-12 for builds and will rely on the new cmake detection of clang by default Signed-off-by: Steve Pham <82231385+spham-amzn@users.noreply.github.com> --- .../Platform/Linux/ProjectBuilderWorker_linux.cpp | 14 -------------- .../Platform/Linux/ProjectUtils_linux.cpp | 15 +++++---------- .../Template/cmake/EngineFinder.cmake | 4 ++-- .../Template/cmake/EngineFinder.cmake | 4 ++-- 4 files changed, 9 insertions(+), 28 deletions(-) diff --git a/Code/Tools/ProjectManager/Platform/Linux/ProjectBuilderWorker_linux.cpp b/Code/Tools/ProjectManager/Platform/Linux/ProjectBuilderWorker_linux.cpp index 2987fc4dc2..fdeaef93bb 100644 --- a/Code/Tools/ProjectManager/Platform/Linux/ProjectBuilderWorker_linux.cpp +++ b/Code/Tools/ProjectManager/Platform/Linux/ProjectBuilderWorker_linux.cpp @@ -23,25 +23,11 @@ namespace O3DE::ProjectManager QString cmakeGenerator = (whichNinjaResult.IsSuccess()) ? "Ninja Multi-Config" : "Unix Makefiles"; bool compileProfileOnBuild = (whichNinjaResult.IsSuccess()); - // On Linux the default compiler is gcc. For O3DE, it is clang, so we need to specify the version of clang that is detected - // in order to get the compiler option. - auto compilerOptionResult = ProjectUtils::FindSupportedCompilerForPlatform(); - if (!compilerOptionResult.IsSuccess()) - { - return AZ::Failure(compilerOptionResult.GetError()); - } - auto clangCompilers = compilerOptionResult.GetValue().split('|'); - AZ_Assert(clangCompilers.length()==2, "Invalid clang compiler pair specification"); - - QString clangCompilerOption = clangCompilers[0]; - QString clangPPCompilerOption = clangCompilers[1]; QString targetBuildPath = QDir(m_projectInfo.m_path).filePath(ProjectBuildPathPostfix); QStringList generateProjectArgs = QStringList{ProjectCMakeCommand, "-B", ProjectBuildPathPostfix, "-S", ".", QString("-G%1").arg(cmakeGenerator), - QString("-DCMAKE_C_COMPILER=").append(clangCompilerOption), - QString("-DCMAKE_CXX_COMPILER=").append(clangPPCompilerOption), QString("-DLY_3RDPARTY_PATH=").append(thirdPartyPath)}; if (!compileProfileOnBuild) { diff --git a/Code/Tools/ProjectManager/Platform/Linux/ProjectUtils_linux.cpp b/Code/Tools/ProjectManager/Platform/Linux/ProjectUtils_linux.cpp index e901d807b4..dc3ec55dee 100644 --- a/Code/Tools/ProjectManager/Platform/Linux/ProjectUtils_linux.cpp +++ b/Code/Tools/ProjectManager/Platform/Linux/ProjectUtils_linux.cpp @@ -17,13 +17,11 @@ namespace O3DE::ProjectManager namespace ProjectUtils { // The list of clang C/C++ compiler command lines to validate on the host Linux system - const QStringList SupportedClangCommands = {"clang-12|clang++-12"}; + const QStringList SupportedClangVersions = {"13", "12", "11", "10", "9", "8", "7", "6.0"}; AZ::Outcome GetCommandLineProcessEnvironment() { QProcessEnvironment currentEnvironment(QProcessEnvironment::systemEnvironment()); - currentEnvironment.insert("CC", "clang-12"); - currentEnvironment.insert("CXX", "clang++-12"); return AZ::Success(currentEnvironment); } @@ -39,16 +37,13 @@ namespace O3DE::ProjectManager } // Look for the first compatible version of clang. The list below will contain the known clang compilers that have been tested for O3DE. - for (const QString& supportClangCommand : SupportedClangCommands) + for (const QString& supportClangVersion : SupportedClangVersions) { - auto clangCompilers = supportClangCommand.split('|'); - AZ_Assert(clangCompilers.length()==2, "Invalid clang compiler pair specification"); - - auto whichClangResult = ProjectUtils::ExecuteCommandResult("which", QStringList{clangCompilers[0]}, QProcessEnvironment::systemEnvironment()); - auto whichClangPPResult = ProjectUtils::ExecuteCommandResult("which", QStringList{clangCompilers[1]}, QProcessEnvironment::systemEnvironment()); + auto whichClangResult = ProjectUtils::ExecuteCommandResult("which", QStringList{QString("clang-%1").arg(supportClangVersion)}, QProcessEnvironment::systemEnvironment()); + auto whichClangPPResult = ProjectUtils::ExecuteCommandResult("which", QStringList{QString("clang++-%1").arg(supportClangVersion)}, QProcessEnvironment::systemEnvironment()); if (whichClangResult.IsSuccess() && whichClangPPResult.IsSuccess()) { - return AZ::Success(supportClangCommand); + return AZ::Success(QString("clang-%1").arg(supportClangVersion)); } } return AZ::Failure(QObject::tr("Clang not found.

" diff --git a/Templates/DefaultProject/Template/cmake/EngineFinder.cmake b/Templates/DefaultProject/Template/cmake/EngineFinder.cmake index 98ad61bae8..15b96eb8a9 100644 --- a/Templates/DefaultProject/Template/cmake/EngineFinder.cmake +++ b/Templates/DefaultProject/Template/cmake/EngineFinder.cmake @@ -13,8 +13,8 @@ include_guard() # Read the engine name from the project_json file -file(READ ${CMAKE_CURRENT_LIST_DIR}/project.json project_json) -set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/project.json) +file(READ ${CMAKE_CURRENT_SOURCE_DIR}/project.json project_json) +set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/project.json) string(JSON LY_ENGINE_NAME_TO_USE ERROR_VARIABLE json_error GET ${project_json} engine) if(json_error) diff --git a/Templates/MinimalProject/Template/cmake/EngineFinder.cmake b/Templates/MinimalProject/Template/cmake/EngineFinder.cmake index 98ad61bae8..15b96eb8a9 100644 --- a/Templates/MinimalProject/Template/cmake/EngineFinder.cmake +++ b/Templates/MinimalProject/Template/cmake/EngineFinder.cmake @@ -13,8 +13,8 @@ include_guard() # Read the engine name from the project_json file -file(READ ${CMAKE_CURRENT_LIST_DIR}/project.json project_json) -set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/project.json) +file(READ ${CMAKE_CURRENT_SOURCE_DIR}/project.json project_json) +set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/project.json) string(JSON LY_ENGINE_NAME_TO_USE ERROR_VARIABLE json_error GET ${project_json} engine) if(json_error)