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