Diana Picus | 3b2ef82 | 2016-10-13 16:53:18 +0300 | [diff] [blame^] | 1 | import os |
| 2 | |
| 3 | from linaropy.git.worktree import Worktree |
| 4 | |
| 5 | |
| 6 | class LLVMSubproject(object): |
| 7 | """ |
| 8 | Class that keeps track of everything related to an LLVM subproject (clang, |
| 9 | lld, compiler-rt etc): repo URL, location preferred by CMake, |
| 10 | CMake variable for adding or removing it from the build etc. |
| 11 | """ |
| 12 | |
| 13 | def __init__(self, cmake_path, cmake_var): |
| 14 | """Create an LLVMSubproject with the provided info. |
| 15 | |
| 16 | Parameters |
| 17 | ---------- |
| 18 | cmake_path |
| 19 | Path relative to the LLVM source root where this subproject should |
| 20 | live so that CMake can automatically pick it up during the build. |
| 21 | cmake_var |
| 22 | The name of the CMake variable that can be used to enable/disable |
| 23 | building this subproject. |
| 24 | """ |
| 25 | self.cmake_path = cmake_path |
| 26 | self.cmake_var = cmake_var |
| 27 | |
| 28 | def get_cmake_path(self, llvm_source_directory): |
| 29 | """ |
| 30 | Get the path where this subproject should live in the given LLVM tree so |
| 31 | that CMake can pick it up by default. |
| 32 | """ |
| 33 | return os.path.join(llvm_source_directory, self.cmake_path) |
| 34 | |
| 35 | @classmethod |
| 36 | def get_all_subprojects(cls): |
| 37 | """ |
| 38 | Return a dictionary of all the LLVM subprojects. |
| 39 | At the moment, llvm itself is not part of the subprojects, because it |
| 40 | always needs to be there and everything is relative to it. |
| 41 | """ |
| 42 | return { |
| 43 | "clang": |
| 44 | cls(os.path.join("tools", "clang"), "LLVM_TOOL_CLANG_BUILD"), |
| 45 | "compiler-rt": |
| 46 | cls(os.path.join("projects", "compiler-rt"), |
| 47 | "LLVM_TOOL_COMPILER_RT_BUILD"), |
| 48 | "libcxx": |
| 49 | cls(os.path.join("projects", "libcxx"), |
| 50 | "LLVM_TOOL_LIBCXX_BUILD"), |
| 51 | "libcxxabi": |
| 52 | cls(os.path.join("projects", "libcxxabi"), |
| 53 | "LLVM_TOOL_LIBCXXABI_BUILD"), |
| 54 | "libunwind": |
| 55 | cls(os.path.join("projects", "libunwind"), |
| 56 | "LLVM_TOOL_LIBUNWIND_BUILD"), |
| 57 | "lld": |
| 58 | cls(os.path.join("tools", "lld"), "LLVM_TOOL_LLD_BUILD"), |
| 59 | "lldb": |
| 60 | cls(os.path.join("tools", "lldb"), "LLVM_TOOL_LLDB_BUILD"), |
| 61 | "test-suite": |
| 62 | cls(os.path.join("projects", "test-suite"), None), |
| 63 | } |
| 64 | |
| 65 | |
| 66 | class LLVMSourceConfig(object): |
| 67 | """Class for managing an LLVM source tree. |
| 68 | |
| 69 | It keeps track of which subprojects are enabled in a given tree and provides |
| 70 | functionality for adding / removing them. |
| 71 | """ |
| 72 | |
| 73 | def __init__(self, proj, sourcePath, |
| 74 | subprojs=LLVMSubproject.get_all_subprojects()): |
| 75 | """ Create a source configuration. |
| 76 | |
| 77 | Parameters |
| 78 | ---------- |
| 79 | proj : Proj |
| 80 | Temporary project directory (used mostly for logging). |
| 81 | sourcePath |
| 82 | Must point to a valid LLVM source tree. |
| 83 | subprojs : dictionary |
| 84 | Dictionary containing a number of LLVMSubproject objects. |
| 85 | By default, this contains all the LLVM subprojects as returned by |
| 86 | LLVMSubproject.get_all_subprojects(), but any subset would work just |
| 87 | as well. |
| 88 | The keys will be used to identify the subprojects in any of this |
| 89 | class's methods. It is an error to invoke them with a subproj that |
| 90 | does not exist in this dictionary. |
| 91 | """ |
| 92 | sourcePath = str(sourcePath) |
| 93 | if not os.path.isdir(sourcePath): |
| 94 | raise EnvironmentError("Invalid path to LLVM source tree") |
| 95 | |
| 96 | self.proj = proj |
| 97 | self.llvmSourceTree = Worktree(proj, sourcePath) |
| 98 | self.subprojs = subprojs |
| 99 | |
| 100 | def get_enabled_subprojects(self): |
| 101 | """Get a list of the subprojects enabled in this configuration.""" |
| 102 | enabled = [] |
| 103 | for subproj in self.subprojs: |
| 104 | if self.__is_enabled(subproj): |
| 105 | enabled.append(subproj) |
| 106 | return enabled |
| 107 | |
| 108 | def update(self, add={}, remove=[]): |
| 109 | """Update the configuration by adding/removing subprojects. |
| 110 | |
| 111 | Parameters |
| 112 | ---------- |
| 113 | add : dictionary |
| 114 | A map of (subproject name, repo) to add to the config. The order in |
| 115 | which the subprojects are added is unspecified. Subprojects that are |
| 116 | already enabled in the config are ignored. |
| 117 | remove: list |
| 118 | A list of subproject names to remove from the config. The order in |
| 119 | which the subprojects are removed is unspecified. Duplicates and |
| 120 | subprojects that aren't enabled in the config are ignored. A |
| 121 | subproject may be removed even if it is in an invalid state. |
| 122 | |
| 123 | For both add and remove, the subproject name must exist in the |
| 124 | dictionary that the config was initialized with and the repo must be a |
| 125 | valid GitRepo object. |
| 126 | |
| 127 | TODO: If adding/removing a project fails, this function should try to |
| 128 | recover. |
| 129 | """ |
| 130 | |
| 131 | # Make the inputs friendly |
| 132 | if add is None: |
| 133 | add = {} |
| 134 | |
| 135 | if remove is None: |
| 136 | remove = [] |
| 137 | |
| 138 | for subproj in add.keys(): |
| 139 | if subproj in remove: |
| 140 | raise ValueError("Can't add and remove {} at the same time" |
| 141 | .format(subproj)) |
| 142 | |
| 143 | for (subproj, repo) in add.items(): |
| 144 | self.__add_subproject(subproj, repo) |
| 145 | |
| 146 | for subproj in remove: |
| 147 | self.__remove_subproject(subproj) |
| 148 | |
| 149 | def __get_subproj_object(self, subprojName): |
| 150 | """Get the LLVMSubproject object corresponding to subprojName.""" |
| 151 | if not subprojName in self.subprojs.keys(): |
| 152 | raise ValueError("Unknown llvm subproject {0}".format(subprojName)) |
| 153 | return self.subprojs[subprojName] |
| 154 | |
| 155 | def __get_subproj_cmake_path(self, subprojName): |
| 156 | """Get the full path to subprojName in this source tree.""" |
| 157 | subproj = self.__get_subproj_object(subprojName) |
| 158 | return subproj.get_cmake_path(self.llvmSourceTree.repodir) |
| 159 | |
| 160 | def __is_enabled(self, subprojName): |
| 161 | """ |
| 162 | Check if subproj is enabled in this configuration. A subproj is |
| 163 | considered to be enabled if it is a worktree on the correct branch. |
| 164 | """ |
| 165 | try: |
| 166 | # If this succeeds, the subproject has already been added. |
| 167 | existing = Worktree(self.proj, |
| 168 | self.__get_subproj_cmake_path(subprojName)) |
| 169 | except EnvironmentError: |
| 170 | # If it's not a worktree (for whatever reason), it's not enabled. |
| 171 | return False |
| 172 | |
| 173 | # If it is a worktree, but on the wrong branch, it is not enabled. |
| 174 | return existing.getbranch() == self.llvmSourceTree.getbranch() |
| 175 | |
| 176 | # TODO: add_subproject, remove_subproject and is_enabled should live in |
| 177 | # another object (AddRemoveStrategy?) that would know what we want to add |
| 178 | # (worktrees, links, whatever) |
| 179 | def __add_subproject(self, subprojName, subprojRepo): |
| 180 | """Add a given subproject to this configuration. |
| 181 | |
| 182 | This will make sure the subproject's sources are visible in the proper |
| 183 | place in the LLVM source tree that the configuration was created with. |
| 184 | |
| 185 | We currently achieve this by creating a worktree for the subproject, but |
| 186 | if more flexibility is needed we can add a config option. The branch |
| 187 | that the worktree will be created with is the same branch that the |
| 188 | existing LLVM source tree is on, and it will be tracking a branch |
| 189 | corresponding to the one that the LLVM branch was forked from. |
| 190 | """ |
| 191 | path = self.__get_subproj_cmake_path(subprojName) |
| 192 | |
| 193 | if self.__is_enabled(subprojName): |
| 194 | # Subproject has already been added, nothing to do. |
| 195 | return |
| 196 | |
| 197 | if os.path.exists(path): |
| 198 | raise EnvironmentError( |
| 199 | "{} already exists but is not a valid subproject directory." |
| 200 | .format(path) + |
| 201 | "Please make sure it is a worktree on the same branch as LLVM.") |
| 202 | |
| 203 | # Create a new worktree |
| 204 | branch = self.llvmSourceTree.getbranch() |
| 205 | if subprojRepo.branchexists(branch): |
| 206 | Worktree.create(self.proj, subprojRepo, path, branch) |
| 207 | else: |
| 208 | trackedBranch = "master" # TODO: track proper branch |
| 209 | Worktree.create( |
| 210 | self.proj, |
| 211 | subprojRepo, |
| 212 | path, |
| 213 | branch, |
| 214 | trackedBranch) |
| 215 | |
| 216 | def __remove_subproject(self, subprojName): |
| 217 | """Remove a given subproject from this build configuration.""" |
| 218 | path = self.__get_subproj_cmake_path(subprojName) |
| 219 | |
| 220 | if not os.path.isdir(path): |
| 221 | return |
| 222 | |
| 223 | worktree = Worktree(self.proj, path) |
| 224 | worktree.clean(False) |