[Lldb-commits] [lldb] [lldb][test] Improve toolchain detection in Makefile.rules (PR #102185)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Mon Aug 26 08:18:54 PDT 2024


================
@@ -96,16 +98,120 @@ def getArchSpec(self, architecture):
         """
         return ["ARCH=" + architecture] if architecture else []
 
-    def getCCSpec(self, compiler):
+    def getToolchainSpec(self, compiler):
         """
-        Helper function to return the key-value string to specify the compiler
+        Helper function to return the key-value strings to specify the toolchain
         used for the make system.
         """
         cc = compiler if compiler else None
         if not cc and configuration.compiler:
             cc = configuration.compiler
+
         if cc:
-            return ['CC="%s"' % cc]
+            exe_ext = ""
+            if lldbplatformutil.getHostPlatform() == "windows":
+                exe_ext = ".exe"
+
+            cc = cc.strip()
+            cc_path = pathlib.Path(cc)
+            cc = cc_path.as_posix()
+
+            # We can get CC compiler string in the following formats:
+            #  [<tool>] <compiler>    - such as 'xrun clang', 'xrun /usr/bin/clang' & etc
+            #
+            # Where <compiler> could contain the following parts:
+            #   <simple-name>[.<exe-ext>]                           - sucn as 'clang', 'clang.exe' ('clamg-cl.exe'?)
+            #   <target-triple>-<simple-name>[.<exe-ext>]           - such as 'armv7-linux-gnueabi-gcc'
+            #   <path>/<simple-name>[.<exe-ext>]                    - such as '/usr/bin/clang', 'c:\path\to\compiler\clang,exe'
+            #   <path>/<target-triple>-<simple-name>[.<exe-ext>]    - such as '/usr/bin/clang', 'c:\path\to\compiler\clang,exe'
+
+            cc_ext = cc_path.suffix
+            # Compiler name without extension
+            cc_name = cc_path.stem.split(" ")[-1]
+
+            # A kind of compiler (canonical name): clang, gcc, cc & etc.
+            cc_type = cc_name
+            # A triple prefix of compiler name: <armv7-none-linux-gnu->gcc
+            cc_prefix = ""
+            if not "clang-cl" in cc_name and not "llvm-gcc" in cc_name:
+                cc_name_parts = cc_name.split("-")
+                cc_type = cc_name_parts[-1]
+                if len(cc_name_parts) > 1:
+                    cc_prefix = "-".join(cc_name_parts[:-1]) + "-"
+
+            # A kind of C++ compiler.
+            cxx_types = {
+                "icc": "icpc",
+                "llvm-gcc": "llvm-g++",
+                "gcc": "g++",
+                "cc": "c++",
+                "clang": "clang++",
+            }
+            cxx_type = cxx_types.get(cc_type, cc_type)
+
+            cc_dir = cc_path.parent
+
+            def getLlvmUtil(util_name):
+                llvm_tools_dir = os.getenv("LLVM_TOOLS_DIR", cc_dir.as_posix())
+                return llvm_tools_dir + "/" + util_name + exe_ext
+
+            def getToolchainUtil(util_name):
+                return (cc_dir / (cc_prefix + util_name + cc_ext)).as_posix()
+
+            cxx = getToolchainUtil(cxx_type)
+
+            util_names = {
+                "OBJCOPY": "objcopy",
+                "STRIP": "objcopy",
+                "ARCHIVER": "ar",
+                "DWP": "dwp",
+            }
+            utils = []
+
+            # Note: LLVM_AR is currently required by API TestBSDArchives.py tests.
+            # Assembly a full path to llvm-ar for given LLVM_TOOLS_DIR;
+            # otherwise assume we have llvm-ar at the same place where is CC specified.
+            if not os.getenv("LLVM_AR"):
+                llvm_ar = getLlvmUtil("llvm-ar")
+                utils.extend(['LLVM_AR="%s"' % llvm_ar])
+
+            if not lldbplatformutil.platformIsDarwin():
+                if os.getenv("USE_LLVM_TOOLS"):
+                    # Use the llvm project tool instead of the system defaults.
+                    for var, name in util_names.items():
+                        utils.append('%s="%s"' % (var, getLlvmUtil("llvm-" + name)))
+                    utils.extend(['AR="%s"' % llvm_ar])
+                elif cc_type in ["clang", "cc", "gcc"]:
+                    util_paths = {}
+                    # Assembly a toolchain side tool cmd based on passed CC.
+                    for var, name in util_names.items():
+                        # Do not override explicity specified tool from the cmd line.
+                        if not os.getenv(var):
+                            util_paths[var] = getToolchainUtil(name)
+                        else:
+                            util_paths[var] = os.getenv(var)
+                    utils.extend(['AR="%s"' % util_paths["ARCHIVER"]])
+
+                    # Look for llvm-dwp or gnu dwp
+                    if not lldbutil.which(util_paths["DWP"]):
+                        util_paths["DWP"] = getToolchainUtil("llvm-dwp")
+                    if not lldbutil.which(util_paths["DWP"]):
+                        util_paths["DWP"] = lldbutil.which("llvm-dwp")
+                    if not util_paths["DWP"]:
+                        util_paths["DWP"] = lldbutil.which("dwp")
+                        if not util_paths["DWP"]:
+                            del util_paths["DWP"]
+
+                    for var, path in util_paths.items():
+                        utils.append('%s="%s"' % (var, path))
+            else:
+                utils.extend(['AR="%slibtool"' % os.getenv("CROSS_COMPILE", "")])
+
+            return [
+                'CC="%s"' % cc,
+                'CCC="%s"' % cc_type,
+                'CXX="%s"' % cxx,
----------------
labath wrote:

I.e., by replacing this (and others) with
```suggestion
                'CC=%s' % cc,
                'CCC=%s' % cc_type,
                'CXX=%s' % cxx,
```

https://github.com/llvm/llvm-project/pull/102185


More information about the lldb-commits mailing list