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