1 // Copyright (c) 2017-2024, 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-impl.h> 9 #include <ceed.h> 10 #include <ceed/backend.h> 11 #include <ceed/jit-tools.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[in] ceed `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 opened 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 // Sometimes we have path/to/file.h:function_name 29 // Create temporary file path without name, if needed 30 char *source_file_path_only; 31 char *last_colon = strrchr(source_file_path, ':'); 32 33 if (last_colon) { 34 size_t source_file_path_length = (last_colon - source_file_path + 1); 35 36 CeedCall(CeedCalloc(source_file_path_length, &source_file_path_only)); 37 memcpy(source_file_path_only, source_file_path, source_file_path_length - 1); 38 } else { 39 source_file_path_only = (char *)source_file_path; 40 } 41 42 // Debug 43 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking for source file: "); 44 CeedDebug(ceed, "%s\n", source_file_path_only); 45 46 // Check for valid file path 47 FILE *source_file; 48 source_file = fopen(source_file_path_only, "rb"); 49 *is_valid = source_file; 50 51 if (*is_valid) { 52 // Debug 53 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Found JiT source file: "); 54 CeedDebug(ceed, "%s\n", source_file_path_only); 55 fclose(source_file); 56 } 57 58 // Free temp file path, if used 59 if (last_colon) CeedCall(CeedFree(&source_file_path_only)); 60 return CEED_ERROR_SUCCESS; 61 } 62 63 /** 64 @brief Normalize a filepath 65 66 @param[in] ceed `Ceed` object for error handling 67 @param[in] source_file_path Absolute path to source file 68 @param[out] normalized_source_file_path Normalized filepath 69 70 @return An error code: 0 - success, otherwise - failure 71 72 @ref Backend 73 **/ 74 static int CeedNormalizePath(Ceed ceed, const char *source_file_path, char **normalized_source_file_path) { 75 CeedCall(CeedStringAllocCopy(source_file_path, normalized_source_file_path)); 76 77 char *first_dot = strchr(*normalized_source_file_path, '.'); 78 79 while (first_dot) { 80 char *search_from = first_dot + 1; 81 char keyword[5] = ""; 82 83 // -- Check for /./ and covert to / 84 if (first_dot != *normalized_source_file_path && strlen(first_dot) > 2) memcpy(keyword, &first_dot[-1], 3); 85 bool is_here = !strcmp(keyword, "/./"); 86 87 if (is_here) { 88 for (CeedInt i = 0; first_dot[i - 1]; i++) first_dot[i] = first_dot[i + 2]; 89 search_from = first_dot; 90 } else { 91 // -- Check for /foo/../ and convert to / 92 if (first_dot != *normalized_source_file_path && strlen(first_dot) > 3) memcpy(keyword, &first_dot[-1], 4); 93 bool is_up_one = !strcmp(keyword, "/../"); 94 95 if (is_up_one) { 96 char *last_slash = &first_dot[-2]; 97 98 while (last_slash[0] != '/' && last_slash != *normalized_source_file_path) last_slash--; 99 CeedCheck(last_slash != *normalized_source_file_path, ceed, CEED_ERROR_MAJOR, "Malformed source path %s", source_file_path); 100 for (CeedInt i = 0; first_dot[i + 1]; i++) last_slash[i] = first_dot[i + 2]; 101 search_from = last_slash; 102 } 103 } 104 first_dot = strchr(search_from, '.'); 105 } 106 return CEED_ERROR_SUCCESS; 107 } 108 109 /** 110 @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`. 111 This also updates the `num_file_paths` and `source_file_paths`. 112 Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree(). 113 114 @param[in] ceed `Ceed` object for error handling 115 @param[in] source_file_path Absolute path to source file 116 @param[in,out] num_file_paths Number of files already included 117 @param[in,out] file_paths Paths of files already included 118 @param[out] buffer String buffer for source file contents 119 120 @return An error code: 0 - success, otherwise - failure 121 122 @ref Backend 123 **/ 124 int CeedLoadSourceToInitializedBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_file_paths, char ***file_paths, char **buffer) { 125 FILE *source_file; 126 long file_size, file_offset = 0; 127 char *temp_buffer; 128 129 // Debug 130 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n"); 131 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: "); 132 CeedDebug(ceed, "%s\n", source_file_path); 133 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current buffer:\n"); 134 CeedDebug(ceed, "%s\n", *buffer); 135 136 // Read file to temporary buffer 137 source_file = fopen(source_file_path, "rb"); 138 CeedCheck(source_file, ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s", source_file_path); 139 // -- Compute size of source 140 fseek(source_file, 0L, SEEK_END); 141 file_size = ftell(source_file); 142 rewind(source_file); 143 // -- Allocate memory for entire source file 144 CeedCall(CeedCalloc(file_size + 1, &temp_buffer)); 145 // -- Copy the file into the buffer 146 if (1 != fread(temp_buffer, file_size, 1, source_file)) { 147 // LCOV_EXCL_START 148 fclose(source_file); 149 CeedCall(CeedFree(&temp_buffer)); 150 return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s", source_file_path); 151 // LCOV_EXCL_STOP 152 } 153 fclose(source_file); 154 155 // Search for headers to include 156 const char *first_hash = strchr(temp_buffer, '#'); 157 158 while (first_hash) { 159 // -- Check for 'pragma' keyword 160 const char *next_m = strchr(first_hash, 'm'); 161 char keyword[8] = ""; 162 163 if (next_m && next_m - first_hash >= 5) memcpy(keyword, &next_m[-4], 6); 164 bool is_hash_pragma = !strcmp(keyword, "pragma"); 165 166 // ---- Spaces allowed in '# pragma' 167 if (next_m) { 168 for (CeedInt i = 1; first_hash - next_m + i < -5; i++) { 169 is_hash_pragma &= first_hash[i] == ' '; 170 } 171 } 172 if (is_hash_pragma) { 173 // -- Check if '#pragma once' 174 char *next_o = strchr(first_hash, 'o'); 175 char *next_new_line = strchr(first_hash, '\n'); 176 bool is_pragma_once = next_o && (next_new_line - next_o > 0) && !strncmp(next_o, "once", 4); 177 178 // -- Copy into buffer, omitting last line if #pragma once 179 long current_size = strlen(*buffer); 180 long copy_size = first_hash - &temp_buffer[file_offset] + (is_pragma_once ? 0 : (next_new_line - first_hash + 1)); 181 182 CeedCall(CeedRealloc(current_size + copy_size + 2, buffer)); 183 memcpy(&(*buffer)[current_size], "\n", 2); 184 memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 185 memcpy(&(*buffer)[current_size + copy_size], "", 1); 186 187 file_offset = strchr(first_hash, '\n') - temp_buffer + 1; 188 } 189 190 // -- Check for 'include' keyword 191 const char *next_e = strchr(first_hash, 'e'); 192 193 if (next_e && next_e - first_hash >= 7) memcpy(keyword, &next_e[-6], 7); 194 bool is_hash_include = !strcmp(keyword, "include"); 195 196 // ---- Spaces allowed in '# include <header.h>' 197 if (next_e) { 198 for (CeedInt i = 1; first_hash - next_e + i < -6; i++) { 199 is_hash_include &= first_hash[i] == ' '; 200 } 201 } 202 if (is_hash_include) { 203 // -- Copy into buffer all preceding # 204 long current_size = strlen(*buffer); 205 long copy_size = first_hash - &temp_buffer[file_offset]; 206 207 CeedCall(CeedRealloc(current_size + copy_size + 2, buffer)); 208 memcpy(&(*buffer)[current_size], "\n", 2); 209 memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 210 memcpy(&(*buffer)[current_size + copy_size], "", 1); 211 // -- Load local "header.h" 212 char *next_quote = strchr(first_hash, '"'); 213 char *next_new_line = strchr(first_hash, '\n'); 214 bool is_local_header = is_hash_include && next_quote && (next_new_line - next_quote > 0); 215 char *next_left_chevron = strchr(first_hash, '<'); 216 bool is_ceed_header = next_left_chevron && (next_new_line - next_left_chevron > 0) && 217 (!strncmp(next_left_chevron, "<ceed/jit-source/", 17) || !strncmp(next_left_chevron, "<ceed/types.h>", 14) || 218 !strncmp(next_left_chevron, "<ceed/ceed-f32.h>", 17) || !strncmp(next_left_chevron, "<ceed/ceed-f64.h>", 17)); 219 220 if (is_local_header || is_ceed_header) { 221 // ---- Build source path 222 bool is_included = false; 223 char *include_source_path; 224 225 if (is_local_header) { 226 long root_length = strrchr(source_file_path, '/') - source_file_path; 227 long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1; 228 229 CeedCall(CeedCalloc(root_length + include_file_name_len + 2, &include_source_path)); 230 memcpy(include_source_path, source_file_path, root_length + 1); 231 memcpy(&include_source_path[root_length + 1], &next_quote[1], include_file_name_len); 232 memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1); 233 } else { 234 char *next_right_chevron = strchr(first_hash, '>'); 235 char *ceed_relative_path; 236 long ceed_relative_path_length = next_right_chevron - next_left_chevron - 1; 237 238 CeedCall(CeedCalloc(ceed_relative_path_length + 1, &ceed_relative_path)); 239 memcpy(ceed_relative_path, &next_left_chevron[1], ceed_relative_path_length); 240 CeedCall(CeedGetJitAbsolutePath(ceed, ceed_relative_path, (const char **)&include_source_path)); 241 CeedCall(CeedFree(&ceed_relative_path)); 242 } 243 // ---- Recursive call to load source to buffer 244 char *normalized_include_source_path; 245 246 CeedCall(CeedNormalizePath(ceed, include_source_path, &normalized_include_source_path)); 247 for (CeedInt i = 0; i < *num_file_paths; i++) is_included |= !strcmp(normalized_include_source_path, (*file_paths)[i]); 248 if (!is_included) { 249 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "JiT Including: %s\n", normalized_include_source_path); 250 CeedCall(CeedLoadSourceToInitializedBuffer(ceed, normalized_include_source_path, num_file_paths, file_paths, buffer)); 251 CeedCall(CeedRealloc(*num_file_paths + 1, file_paths)); 252 CeedCall(CeedStringAllocCopy(normalized_include_source_path, &(*file_paths)[*num_file_paths])); 253 (*num_file_paths)++; 254 } 255 CeedCall(CeedFree(&include_source_path)); 256 CeedCall(CeedFree(&normalized_include_source_path)); 257 } 258 file_offset = strchr(first_hash, '\n') - temp_buffer + 1; 259 } 260 // -- Next hash 261 first_hash = strchr(&first_hash[1], '#'); 262 } 263 // Copy rest of source file into buffer 264 long current_size = strlen(*buffer); 265 long copy_size = strlen(&temp_buffer[file_offset]); 266 267 CeedCall(CeedRealloc(current_size + copy_size + 2, buffer)); 268 memcpy(&(*buffer)[current_size], "\n", 2); 269 memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 270 memcpy(&(*buffer)[current_size + copy_size + 1], "", 1); 271 272 // Cleanup 273 CeedCall(CeedFree(&temp_buffer)); 274 275 // Debug 276 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n"); 277 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: "); 278 CeedDebug(ceed, "%s\n", source_file_path); 279 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Final buffer:\n"); 280 CeedDebug(ceed, "%s\n", *buffer); 281 return CEED_ERROR_SUCCESS; 282 } 283 284 /** 285 @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`. 286 This also initializes and populates the `num_file_paths` and `source_file_paths`. 287 Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree(). 288 289 @param[in] ceed `Ceed` object for error handling 290 @param[in] source_file_path Absolute path to source file 291 @param[in,out] num_file_paths Number of files already included 292 @param[in,out] file_paths Paths of files already included 293 @param[out] buffer String buffer for source file contents 294 295 @return An error code: 0 - success, otherwise - failure 296 297 @ref Backend 298 **/ 299 int CeedLoadSourceAndInitializeBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_file_paths, char ***file_paths, char **buffer) { 300 // Ensure defaults were set 301 *num_file_paths = 0; 302 *file_paths = NULL; 303 304 // Initialize 305 CeedCall(CeedCalloc(1, buffer)); 306 307 // And load source 308 CeedCall(CeedLoadSourceToInitializedBuffer(ceed, source_file_path, num_file_paths, file_paths, buffer)); 309 return CEED_ERROR_SUCCESS; 310 } 311 312 /** 313 @brief Initialize and load source file into string buffer, including full text of local files in place of `#include "local.h"`. 314 User @ref CeedLoadSourceAndInitializeBuffer() and @ref CeedLoadSourceToInitializedBuffer() if loading multiple source files into the same buffer. 315 Caller is responsible for freeing the string buffer with @ref CeedFree(). 316 317 @param[in] ceed `Ceed` object for error handling 318 @param[in] source_file_path Absolute path to source file 319 @param[out] buffer String buffer for source file contents 320 321 @return An error code: 0 - success, otherwise - failure 322 323 @ref Backend 324 **/ 325 int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, char **buffer) { 326 char **file_paths = NULL; 327 CeedInt num_file_paths = 0; 328 329 // Load 330 CeedCall(CeedLoadSourceAndInitializeBuffer(ceed, source_file_path, &num_file_paths, &file_paths, buffer)); 331 332 // Cleanup 333 for (CeedInt i = 0; i < num_file_paths; i++) CeedCall(CeedFree(&file_paths[i])); 334 CeedCall(CeedFree(&file_paths)); 335 return CEED_ERROR_SUCCESS; 336 } 337 338 /** 339 @brief Build an absolute filepath from a base filepath and an absolute filepath. 340 341 This helps construct source file paths for @ref CeedLoadSourceToBuffer(). 342 343 Note: Caller is responsible for freeing the string buffer with @ref CeedFree(). 344 345 @param[in] ceed `Ceed` object for error handling 346 @param[in] base_file_path Absolute path to current file 347 @param[in] relative_file_path Relative path to target file 348 @param[out] new_file_path String buffer for absolute path to target file 349 350 @return An error code: 0 - success, otherwise - failure 351 352 @ref Backend 353 **/ 354 int CeedPathConcatenate(Ceed ceed, const char *base_file_path, const char *relative_file_path, char **new_file_path) { 355 char *last_slash = strrchr(base_file_path, '/'); 356 size_t base_length = (last_slash - base_file_path + 1), relative_length = strlen(relative_file_path), 357 new_file_path_length = base_length + relative_length + 1; 358 359 CeedCall(CeedCalloc(new_file_path_length, new_file_path)); 360 memcpy(*new_file_path, base_file_path, base_length); 361 memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length); 362 return CEED_ERROR_SUCCESS; 363 } 364 365 /** 366 @brief Find the relative filepath to an installed JiT file 367 368 @param[in] absolute_file_path Absolute path to installed JiT file 369 @param[out] relative_file_path Relative path to installed JiT file, a substring of the absolute path 370 371 @return An error code: 0 - success, otherwise - failure 372 373 @ref Backend 374 **/ 375 int CeedGetJitRelativePath(const char *absolute_file_path, const char **relative_file_path) { 376 *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source"); 377 CeedCheck(*relative_file_path, NULL, CEED_ERROR_MAJOR, "Couldn't find relative path including 'ceed/jit-source' for: %s", absolute_file_path); 378 return CEED_ERROR_SUCCESS; 379 } 380 381 /** 382 @brief Build an absolute filepath to a JiT file 383 384 @param[in] ceed `Ceed` object for error handling 385 @param[in] relative_file_path Relative path to installed JiT file 386 @param[out] absolute_file_path String buffer for absolute path to target file, to be freed by caller 387 388 @return An error code: 0 - success, otherwise - failure 389 390 @ref Backend 391 **/ 392 int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path, const char **absolute_file_path) { 393 Ceed ceed_parent; 394 395 // Debug 396 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n"); 397 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Relative JiT source file: "); 398 CeedDebug(ceed, "%s\n", relative_file_path); 399 400 CeedCall(CeedGetParent(ceed, &ceed_parent)); 401 for (CeedInt i = 0; i < ceed_parent->num_jit_source_roots; i++) { 402 bool is_valid; 403 404 // Debug 405 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking JiT root: "); 406 CeedDebug(ceed, "%s\n", ceed_parent->jit_source_roots[i]); 407 408 // Build and check absolute path with current root 409 CeedCall(CeedPathConcatenate(ceed, ceed_parent->jit_source_roots[i], relative_file_path, (char **)absolute_file_path)); 410 CeedCall(CeedCheckFilePath(ceed, *absolute_file_path, &is_valid)); 411 412 if (is_valid) return CEED_ERROR_SUCCESS; 413 // LCOV_EXCL_START 414 else CeedCall(CeedFree(absolute_file_path)); 415 // LCOV_EXCL_STOP 416 } 417 // LCOV_EXCL_START 418 return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't find matching JiT source file: %s", relative_file_path); 419 // LCOV_EXCL_STOP 420 } 421