this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Initial work on libxcselect

+396
+1
src/CMakeLists.txt
··· 138 138 #add_subdirectory(external/corecrypto) 139 139 #add_subdirectory(external/security) # work in progress 140 140 add_subdirectory(sandbox) 141 + add_subdirectory(xcselect) 141 142 #add_subdirectory(Cocoa) 142 143 143 144 add_subdirectory(external/file/file)
+31
src/xcselect/CMakeLists.txt
··· 1 + project(xcselect) 2 + 3 + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdinc") 4 + 5 + set(DYLIB_INSTALL_NAME "/usr/lib/libxcselect.dylib") 6 + set(DYLIB_COMPAT_VERSION "1.0.0") 7 + set(DYLIB_CURRENT_VERSION "1.0.0") 8 + 9 + add_darling_library(xcselect libxcselect.c) 10 + target_link_libraries(xcselect system) 11 + 12 + add_darling_executable(xcrun xcrun.c) 13 + target_link_libraries(xcrun system xcselect) 14 + 15 + function(add_shim name) 16 + add_darling_executable("${name}_shim" xcrun-shim.c) 17 + 18 + set_target_properties("${name}_shim" 19 + PROPERTIES 20 + OUTPUT_NAME "${name}" 21 + COMPILE_FLAGS "-DTOOL_NAME=\\\"${name}\\\"" 22 + ) 23 + target_link_libraries("${name}_shim" system xcselect) 24 + install(TARGETS "${name}_shim" DESTINATION libexec/darling/usr/bin) 25 + endfunction(add_shim) 26 + 27 + add_shim(nm) 28 + 29 + install(TARGETS xcselect DESTINATION libexec/darling/usr/lib) 30 + install(TARGETS xcrun DESTINATION libexec/darling/usr/bin) 31 +
+4
src/xcselect/TODO
··· 1 + * Implement `xcode-select` 2 + * Integrate `man` so that it refers to `libxcselect.dylib` for man paths 3 + * Stub out tons of Apple private frameworks to have `xcodebuild` working 4 +
+268
src/xcselect/libxcselect.c
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2017 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + #include <stdlib.h> 20 + #include <fcntl.h> 21 + #include <unistd.h> 22 + #include <stdbool.h> 23 + #include <sys/stat.h> 24 + #include <stdlib.h> 25 + #include <errno.h> 26 + #include <string.h> 27 + #include <dlfcn.h> 28 + #include <stdio.h> 29 + #include "libxcselect.h" 30 + 31 + static char path_buffer[1024]; 32 + 33 + static const char* get_developer_dir_from_file(const char* file) 34 + { 35 + int fd = open(file, O_RDONLY); 36 + if (fd == -1) 37 + return NULL; 38 + 39 + int len = read(fd, path_buffer, sizeof(path_buffer)-1); 40 + if (len <= 0) 41 + { 42 + close(fd); 43 + return NULL; 44 + } 45 + 46 + path_buffer[len] = 0; 47 + close(fd); 48 + 49 + return path_buffer; 50 + } 51 + 52 + const char* get_developer_dir_from_symlink(const char* link) 53 + { 54 + ssize_t len; 55 + 56 + len = readlink(link, path_buffer, sizeof(path_buffer)-1); 57 + if (len <= 0) 58 + return NULL; 59 + 60 + path_buffer[len] = 0; 61 + return path_buffer; 62 + } 63 + 64 + static bool dir_exists(const char* dir) 65 + { 66 + struct stat st; 67 + 68 + if (stat(dir, &st) != 0) 69 + return false; 70 + 71 + return S_ISDIR(st.st_mode); 72 + } 73 + 74 + static bool valid_dev_path(const char* path) 75 + { 76 + char buffer[1024]; 77 + size_t length; 78 + 79 + strcpy(buffer, path); 80 + strcat(buffer, "/"); 81 + 82 + length = strlen(buffer); 83 + strcat(buffer, "usr/lib/libxcrun.dylib"); 84 + if (access(buffer, F_OK) == 0) 85 + return true; 86 + 87 + buffer[length] = 0; 88 + strcat(buffer, "usr/bin/xcrun"); 89 + if (access(buffer, F_OK) == 0) 90 + return true; 91 + 92 + return false; 93 + } 94 + 95 + const char* xcselect_find_developer_contents_from_path(const char* p, bool* is_cmd_line) 96 + { 97 + size_t length; 98 + 99 + if (*p != '/') 100 + { 101 + getcwd(path_buffer, sizeof(path_buffer)); 102 + strcat(path_buffer, "/"); 103 + strcat(path_buffer, p); 104 + } 105 + else 106 + strcpy(path_buffer, p); 107 + 108 + length = strlen(path_buffer); 109 + if (valid_dev_path(path_buffer)) 110 + return path_buffer; 111 + 112 + path_buffer[length++] = '/'; 113 + path_buffer[length] = 0; 114 + 115 + strcat(path_buffer, "Library/Developer/CommandLineTools"); 116 + if (valid_dev_path(path_buffer)) 117 + { 118 + *is_cmd_line = true; 119 + return path_buffer; 120 + } 121 + 122 + path_buffer[length] = 0; 123 + strcat(path_buffer, "CommandLineTools"); 124 + if (valid_dev_path(path_buffer)) 125 + { 126 + *is_cmd_line = true; 127 + return path_buffer; 128 + } 129 + 130 + path_buffer[length] = 0; 131 + strcat(path_buffer, "Contents/Developer"); 132 + if (valid_dev_path(path_buffer)) 133 + return path_buffer; 134 + 135 + return NULL; 136 + } 137 + 138 + const char* xcselect_get_developer_dir_path(bool* is_cmd_line) 139 + { 140 + const char* p; 141 + char* slash; 142 + 143 + *is_cmd_line = false; 144 + 145 + p = getenv("DEVELOPER_DIR"); 146 + if (p) 147 + { 148 + p = xcselect_find_developer_contents_from_path(p, is_cmd_line); 149 + if (p) 150 + goto have_path; 151 + } 152 + 153 + p = get_developer_dir_from_symlink("/var/db/xcode_select_link"); 154 + if (p) 155 + goto have_path; 156 + 157 + p = get_developer_dir_from_symlink("/usr/share/xcode-select/xcode_dir_link"); 158 + if (p) 159 + goto have_path; 160 + 161 + p = get_developer_dir_from_file("/usr/share/xcode-select/xcode_dir_path"); 162 + if (p) 163 + goto have_path; 164 + 165 + if (dir_exists("/Applications/Xcode.app")) 166 + { 167 + p = "/Applications/Xcode.app/Contents/Developer"; 168 + goto have_path; 169 + } 170 + else if (dir_exists("/Library/Developer/CommandLineTools")) 171 + { 172 + p = "/Library/Developer/CommandLineTools"; 173 + goto have_path; 174 + } 175 + 176 + return NULL; 177 + 178 + have_path: 179 + slash = strrchr(p, '/'); 180 + if (slash != NULL) 181 + { 182 + if (strcmp(slash+1, "CommandLineTools") == 0) 183 + *is_cmd_line = true; 184 + } 185 + 186 + return p; 187 + } 188 + 189 + 190 + int xcselect_invoke_xcrun(const char* tool, int argc, char* argv[], int flags) 191 + { 192 + const char* dev_dir; 193 + bool is_cmdline; 194 + 195 + dev_dir = xcselect_get_developer_dir_path(&is_cmdline); 196 + 197 + if (dev_dir != NULL) 198 + { 199 + char* buf = (char*) malloc(2048); 200 + size_t length; 201 + void* lib; 202 + 203 + if (is_cmdline && (flags & XCSELECT_FLAG_REQUIRE_XCODE)) 204 + { 205 + fprintf(stderr, "xcrun: tool '%s' requires Xcode installation, command line tools are insufficient.\n", tool); 206 + exit(1); 207 + } 208 + 209 + strcpy(buf, dev_dir); 210 + strcat(buf, "/"); 211 + length = strlen(buf); 212 + 213 + strcat(buf, "usr/lib/libxcrun.dylib"); 214 + lib = dlopen(buf, RTLD_LAZY); 215 + 216 + if (lib != NULL) 217 + { 218 + void(*fn)(const char*, int, char**, const char*); 219 + 220 + *((void**)&fn) = dlsym(lib, "xcrun_main"); 221 + if (!fn) 222 + { 223 + fprintf(stderr, "xcrun: broken libxcrun.dylib at '%s'\n", buf); 224 + } 225 + else 226 + { 227 + fn(getprogname(), argc, argv, dev_dir); 228 + } 229 + } 230 + else 231 + { 232 + char** argv2; 233 + int j = 0; 234 + 235 + buf[length] = 0; 236 + strcat(buf, "usr/bin/xcrun"); 237 + 238 + argv2 = (char**) __builtin_alloca((argc+2) * sizeof(char*)); 239 + argv2[j++] = buf; 240 + 241 + if (tool != NULL) 242 + argv2[j++] = (char*) tool; 243 + 244 + for (int i = 0; i < argc; i++, j++) 245 + argv2[j] = argv[i]; 246 + 247 + execv(buf, argv2); 248 + fprintf(stderr, "xcrun: developer path '%s' is invalid, failed to execute '%s': %s\n", 249 + dev_dir, buf, strerror(errno)); 250 + } 251 + } 252 + else if (dir_exists("/usr/libexec/DeveloperTools") && tool != NULL) 253 + { 254 + char* buf = __builtin_alloca(strlen(tool) + 30); 255 + strcpy(buf, "/usr/libexec/DeveloperTools/"); 256 + strcat(buf, tool); 257 + 258 + execv(buf, argv); 259 + fprintf(stderr, "xcrun: failed to exec '%s': %s\n", buf, strerror(errno)); 260 + } 261 + else 262 + { 263 + fprintf(stderr, "xcrun: cannot find developer tools, set DEVELOPER_DIR if you are using a non-standard location.\n"); 264 + } 265 + 266 + exit(1); 267 + } 268 +
+27
src/xcselect/libxcselect.h
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2017 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + #ifndef _LIBXCSELECT_H_ 20 + #define _LIBXCSELECT_H_ 21 + 22 + #define XCSELECT_FLAG_REQUIRE_XCODE 1 23 + 24 + int xcselect_invoke_xcrun(const char* tool, int argc, char* argv[], int flags); 25 + 26 + #endif 27 +
+27
src/xcselect/xcrun-shim.c
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2017 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + #include "libxcselect.h" 20 + #include <stdlib.h> 21 + #include <string.h> 22 + 23 + int main(int argc, const char** argv) 24 + { 25 + return xcselect_invoke_xcrun(TOOL_NAME, argc - 1, &argv[1], 0); 26 + } 27 +
+38
src/xcselect/xcrun.c
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2017 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + #include "libxcselect.h" 20 + #include <stdlib.h> 21 + #include <string.h> 22 + 23 + int main(int argc, const char** argv) 24 + { 25 + const char* tool; 26 + const char* pname = getprogname(); 27 + 28 + if (*pname == '/') 29 + tool = pname + 1; 30 + else 31 + tool = pname; 32 + 33 + if (strcasecmp(tool, "xcrun") == 0) 34 + tool = NULL; 35 + 36 + return xcselect_invoke_xcrun(tool, argc - 1, &argv[1], 0); 37 + } 38 +