瀏覽代碼

Better compiler detection on Linux (#5376)

* Better compiler detection on Linux
Moving EngineFinder.cmake to cmake/ in the templates

Signed-off-by: Esteban Papp <[email protected]>

* skipping detection if compiler is passed through environment

Signed-off-by: Esteban Papp <[email protected]>

* Fixes condition, needs to be in quotes since is the value of the sttring

Signed-off-by: Esteban Papp <[email protected]>
Esteban Papp 3 年之前
父節點
當前提交
61fa2eac32

+ 1 - 0
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}

+ 2 - 1
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()

+ 13 - 0
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()

+ 0 - 0
Templates/DefaultProject/Template/EngineFinder.cmake → Templates/DefaultProject/Template/cmake/EngineFinder.cmake


+ 34 - 0
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()

+ 14 - 2
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
         },

+ 2 - 1
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()

+ 13 - 0
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()

+ 0 - 0
Templates/MinimalProject/Template/EngineFinder.cmake → Templates/MinimalProject/Template/cmake/EngineFinder.cmake


+ 34 - 0
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()

+ 14 - 2
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
         },

+ 13 - 0
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()

+ 34 - 0
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()