1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 #include <ceed/ceed.h> 9 #include <ceed/backend.h> 10 #include <ceed/jit-tools.h> 11 #include <ceed-impl.h> 12 #include <stdbool.h> 13 #include <stdio.h> 14 #include <string.h> 15 16 /** 17 @brief Check if valid file exists at path given 18 19 @param ceed A Ceed object for error handling 20 @param[in] source_file_path Absolute path to source file 21 @param[out] is_valid Boolean flag indicating if file can be opend 22 23 @return An error code: 0 - success, otherwise - failure 24 25 @ref Backend 26 **/ 27 int CeedCheckFilePath(Ceed ceed, const char *source_file_path, bool *is_valid) { 28 int ierr; 29 30 // Sometimes we have path/to/file.h:function_name 31 // Create tempory file path without name, if needed 32 char *source_file_path_only; 33 char *last_colon = strrchr(source_file_path, ':'); 34 if (last_colon) { 35 size_t source_file_path_length = (last_colon - source_file_path + 1); 36 37 ierr = CeedCalloc(source_file_path_length, &source_file_path_only); 38 CeedChk(ierr); 39 memcpy(source_file_path_only, source_file_path, source_file_path_length - 1); 40 } else { 41 source_file_path_only = (char *)source_file_path; 42 } 43 44 // Debug 45 CeedDebug256(ceed, 1, "Checking for source file: "); 46 CeedDebug(ceed, "%s\n", source_file_path_only); 47 48 // Check for valid file path 49 FILE *source_file; 50 source_file = fopen(source_file_path_only, "rb"); 51 *is_valid = !!source_file; 52 53 if (*is_valid) { 54 // Debug 55 CeedDebug256(ceed, 1, "Found JiT source file: "); 56 CeedDebug(ceed, "%s\n", source_file_path_only); 57 58 fclose(source_file); 59 } 60 61 // Free temp file path, if used 62 if (last_colon) { 63 ierr = CeedFree(&source_file_path_only); CeedChk(ierr); 64 } 65 66 return CEED_ERROR_SUCCESS; 67 } 68 69 /** 70 @brief Load source file into initialized string buffer, including full text 71 of local files in place of `#include "local.h"` 72 73 @param ceed A Ceed object for error handling 74 @param[in] source_file_path Absolute path to source file 75 @param[out] buffer String buffer for source file contents 76 77 @return An error code: 0 - success, otherwise - failure 78 79 @ref Backend 80 **/ 81 int CeedLoadSourceToInitializedBuffer(Ceed ceed, 82 const char *source_file_path, char **buffer) { 83 int ierr; 84 FILE *source_file; 85 long file_size, file_offset = 0; 86 char *temp_buffer; 87 88 // Debug 89 CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 90 CeedDebug256(ceed, 1, "Current source file: "); 91 CeedDebug(ceed, "%s\n", source_file_path); 92 CeedDebug256(ceed, 1, "Current buffer:\n"); 93 CeedDebug(ceed, "%s\n", *buffer); 94 95 // Read file to temporary buffer 96 source_file = fopen(source_file_path, "rb"); 97 if (!source_file) 98 // LCOV_EXCL_START 99 return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s", 100 source_file_path); 101 // LCOV_EXCL_STOP 102 // -- Compute size of source 103 fseek(source_file, 0L, SEEK_END); 104 file_size = ftell(source_file); 105 rewind(source_file); 106 // -- Allocate memory for entire source file 107 ierr = CeedCalloc(file_size + 1, &temp_buffer); CeedChk(ierr); 108 // -- Copy the file into the buffer 109 if (1 != fread(temp_buffer, file_size, 1, source_file)) { 110 // LCOV_EXCL_START 111 fclose(source_file); 112 ierr = CeedFree(&temp_buffer); CeedChk(ierr); 113 return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s", 114 source_file_path); 115 // LCOV_EXCL_STOP 116 } 117 fclose(source_file); 118 119 // Search for headers to include 120 const char *first_hash = strchr(temp_buffer, '#'); 121 while (first_hash) { 122 // -- Check for 'include' keyword 123 const char *next_e = strchr(first_hash, 'e'); 124 char keyword[8] = ""; 125 if (next_e && next_e - first_hash >= 7) memcpy(keyword, &next_e[-6], 7); 126 bool is_hash_include = !strcmp(keyword, "include"); 127 // ---- Spaces allowed in '# include <header.h>' 128 if (next_e) { 129 for (CeedInt i = 1; first_hash - next_e + i < -6; i++) { 130 is_hash_include &= first_hash[i] == ' '; 131 } 132 } 133 if (is_hash_include) { 134 // -- Copy into buffer all preceding # 135 long current_size = strlen(*buffer); 136 long copy_size = first_hash - &temp_buffer[file_offset]; 137 ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr); 138 memcpy(&(*buffer)[current_size], "\n", 2); 139 memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 140 memcpy(&(*buffer)[current_size + copy_size], "", 1); 141 // -- Load local "header.h" 142 char *next_quote = strchr(first_hash, '"'); 143 char *next_new_line = strchr(first_hash, '\n'); 144 bool is_local_header = is_hash_include && next_quote && 145 (next_new_line - next_quote > 0); 146 char *next_left_chevron = strchr(first_hash, '<'); 147 bool is_ceed_header = is_hash_include && next_left_chevron && 148 (next_new_line - next_left_chevron > 0) && 149 (!strncmp(next_left_chevron, "<ceed/jit-source/", 17) || 150 !strncmp(next_left_chevron, "<ceed/types.h>", 14) || 151 !strncmp(next_left_chevron, "<ceed/ceed-f32.h>", 17) || 152 !strncmp(next_left_chevron, "<ceed/ceed-f64.h>", 17)); 153 if (is_local_header || is_ceed_header) { 154 // ---- Build source path 155 char *include_source_path; 156 if (is_local_header) { 157 long root_length = strrchr(source_file_path, '/') - source_file_path; 158 long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1; 159 ierr = CeedCalloc(root_length + include_file_name_len + 2, 160 &include_source_path); CeedChk(ierr); 161 memcpy(include_source_path, source_file_path, root_length + 1); 162 memcpy(&include_source_path[root_length + 1], &next_quote[1], 163 include_file_name_len); 164 memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1); 165 } else { 166 char *next_right_chevron = strchr(first_hash, '>'); 167 char *ceed_relative_path; 168 long ceed_relative_path_length = next_right_chevron - next_left_chevron - 1; 169 ierr = CeedCalloc(ceed_relative_path_length + 1, &ceed_relative_path); 170 CeedChk(ierr); 171 memcpy(ceed_relative_path, &next_left_chevron[1], ceed_relative_path_length); 172 ierr = CeedGetJitAbsolutePath(ceed, ceed_relative_path, &include_source_path); 173 CeedChk(ierr); 174 } 175 // ---- Recursive call to load source to buffer 176 CeedDebug256(ceed, 2, "JiT Including: %s\n", include_source_path); 177 CeedChk(ierr); 178 ierr = CeedLoadSourceToInitializedBuffer(ceed, include_source_path, buffer); 179 CeedChk(ierr); 180 ierr = CeedFree(&include_source_path); CeedChk(ierr); 181 } 182 file_offset = strchr(first_hash, '\n') - temp_buffer + 1; 183 } 184 // -- Next hash 185 first_hash = strchr(&first_hash[1], '#'); 186 } 187 // Copy rest of source file into buffer 188 long current_size = strlen(*buffer); 189 long copy_size = strlen(&temp_buffer[file_offset]); 190 ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr); 191 memcpy(&(*buffer)[current_size], "\n", 2); 192 memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 193 memcpy(&(*buffer)[current_size + copy_size + 1], "", 1); 194 195 // Cleanup 196 ierr = CeedFree(&temp_buffer); CeedChk(ierr); 197 198 // Debug 199 CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 200 CeedDebug256(ceed, 1, "Current source file: "); 201 CeedDebug(ceed, "%s\n", source_file_path); 202 CeedDebug256(ceed, 1, "Final buffer:\n"); 203 CeedDebug(ceed, "%s\n", *buffer); 204 205 return CEED_ERROR_SUCCESS; 206 } 207 208 /** 209 @brief Initalize and load source file into string buffer, including full text 210 of local files in place of `#include "local.h"`. 211 Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 212 213 @param ceed A Ceed object for error handling 214 @param[in] source_file_path Absolute path to source file 215 @param[out] buffer String buffer for source file contents 216 217 @return An error code: 0 - success, otherwise - failure 218 219 @ref Backend 220 **/ 221 int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, 222 char **buffer) { 223 int ierr; 224 225 // Initalize buffer 226 ierr = CeedCalloc(1, buffer); CeedChk(ierr); 227 228 // Load to initalized buffer 229 ierr = CeedLoadSourceToInitializedBuffer(ceed, source_file_path, buffer); 230 CeedChk(ierr); 231 232 return CEED_ERROR_SUCCESS; 233 } 234 235 /** 236 @brief Build an absolute filepath from a base filepath and an absolute filepath. 237 This helps construct source file paths for `CeedLoadSourceToBuffer()`. 238 Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 239 240 @param ceed A Ceed object for error handling 241 @param[in] base_file_path Absolute path to current file 242 @param[in] relative_file_path Relative path to target file 243 @param[out] new_file_path String buffer for absolute path to target file 244 245 @return An error code: 0 - success, otherwise - failure 246 247 @ref Backend 248 **/ 249 int CeedPathConcatenate(Ceed ceed, const char *base_file_path, 250 const char *relative_file_path, char **new_file_path) { 251 int ierr; 252 char *last_slash = strrchr(base_file_path, '/'); 253 size_t base_length = (last_slash - base_file_path + 1), 254 relative_length = strlen(relative_file_path), 255 new_file_path_length = base_length + relative_length + 1; 256 257 ierr = CeedCalloc(new_file_path_length, new_file_path); CeedChk(ierr); 258 memcpy(*new_file_path, base_file_path, base_length); 259 memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length); 260 261 return CEED_ERROR_SUCCESS; 262 } 263 264 /** 265 @brief Find the relative filepath to an installed JiT file 266 267 @param[in] absolute_file_path Absolute path to installed JiT file 268 @param[out] relative_file_path Relative path to installed JiT file 269 270 @return An error code: 0 - success, otherwise - failure 271 272 @ref Backend 273 **/ 274 int CeedGetJitRelativePath(const char *absolute_file_path, 275 const char **relative_file_path) { 276 *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source"); 277 278 if (!*relative_file_path) 279 // LCOV_EXCL_START 280 return CeedError(NULL, CEED_ERROR_MAJOR, 281 "Couldn't find relative path including " 282 "'ceed/jit-source' for: %s", absolute_file_path); 283 // LCOV_EXCL_STOP 284 285 return CEED_ERROR_SUCCESS; 286 } 287 288 /** 289 @brief Build an absolute filepath to a JiT file 290 291 @param ceed A Ceed object for error handling 292 @param[in] relative_file_path Relative path to installed JiT file 293 @param[out] absolute_file_path String buffer for absolute path to target file 294 295 @return An error code: 0 - success, otherwise - failure 296 297 @ref Backend 298 **/ 299 int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path, 300 char **absolute_file_path) { 301 int ierr; 302 Ceed ceed_parent; 303 304 // Debug 305 CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 306 CeedDebug256(ceed, 1, "Relative JiT source file: "); 307 CeedDebug(ceed, "%s\n", relative_file_path); 308 309 310 ierr = CeedGetParent(ceed, &ceed_parent); CeedChk(ierr); 311 for (CeedInt i = 0; i < ceed_parent->num_jit_source_roots; i++) { 312 bool is_valid; 313 314 // Debug 315 CeedDebug256(ceed, 1, "Checking JiT root: "); 316 CeedDebug(ceed, "%s\n", ceed_parent->jit_source_roots[i]); 317 318 // Build and check absolute path with current root 319 ierr = CeedPathConcatenate(ceed, ceed_parent->jit_source_roots[i], 320 relative_file_path, absolute_file_path); 321 CeedChk(ierr); 322 ierr = CeedCheckFilePath(ceed, *absolute_file_path, &is_valid); CeedChk(ierr); 323 324 if (is_valid) { 325 return CEED_ERROR_SUCCESS; 326 } else { 327 ierr = CeedFree(absolute_file_path); CeedChk(ierr); 328 } 329 } 330 331 // LCOV_EXCL_START 332 return CeedError(ceed, CEED_ERROR_MAJOR, 333 "Couldn't find matching JiT source file: %s", 334 relative_file_path); 335 // LCOV_EXCL_STOP 336 } 337