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 66 @param[in] ceed `Ceed` object for error handling 67 @param[in] source_file_path Absolute path to source file 68 @param[in] depth Depth of recursion 69 @param[in,out] num_files Number of files already included 70 @param[in,out] filepaths Paths of files already included 71 @param[out] buffer String buffer for source file contents 72 73 @return An error code: 0 - success, otherwise - failure 74 75 @ref Backend 76 **/ 77 static int CeedLoadSourceToInitializedBuffer_Private(Ceed ceed, const char *source_file_path, CeedInt depth, CeedInt *num_files, char ***filepaths, 78 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 'include' keyword 114 const char *next_e = strchr(first_hash, 'e'); 115 char keyword[8] = ""; 116 117 if (next_e && next_e - first_hash >= 7) memcpy(keyword, &next_e[-6], 7); 118 bool is_hash_include = !strcmp(keyword, "include"); 119 120 // ---- Spaces allowed in '# include <header.h>' 121 if (next_e) { 122 for (CeedInt i = 1; first_hash - next_e + i < -6; i++) { 123 is_hash_include &= first_hash[i] == ' '; 124 } 125 } 126 if (is_hash_include) { 127 // -- Copy into buffer all preceding # 128 long current_size = strlen(*buffer); 129 long copy_size = first_hash - &temp_buffer[file_offset]; 130 131 CeedCall(CeedRealloc(current_size + copy_size + 2, buffer)); 132 memcpy(&(*buffer)[current_size], "\n", 2); 133 memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 134 memcpy(&(*buffer)[current_size + copy_size], "", 1); 135 // -- Load local "header.h" 136 char *next_quote = strchr(first_hash, '"'); 137 char *next_new_line = strchr(first_hash, '\n'); 138 bool is_local_header = is_hash_include && next_quote && (next_new_line - next_quote > 0); 139 char *next_left_chevron = strchr(first_hash, '<'); 140 bool is_ceed_header = is_hash_include && next_left_chevron && (next_new_line - next_left_chevron > 0) && 141 (!strncmp(next_left_chevron, "<ceed/jit-source/", 17) || !strncmp(next_left_chevron, "<ceed/types.h>", 14) || 142 !strncmp(next_left_chevron, "<ceed/ceed-f32.h>", 17) || !strncmp(next_left_chevron, "<ceed/ceed-f64.h>", 17)); 143 144 if (is_local_header || is_ceed_header) { 145 // ---- Build source path 146 bool is_included = false; 147 char *include_source_path; 148 149 if (is_local_header) { 150 long root_length = strrchr(source_file_path, '/') - source_file_path; 151 long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1; 152 153 CeedCall(CeedCalloc(root_length + include_file_name_len + 2, &include_source_path)); 154 memcpy(include_source_path, source_file_path, root_length + 1); 155 memcpy(&include_source_path[root_length + 1], &next_quote[1], include_file_name_len); 156 memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1); 157 } else { 158 char *next_right_chevron = strchr(first_hash, '>'); 159 char *ceed_relative_path; 160 long ceed_relative_path_length = next_right_chevron - next_left_chevron - 1; 161 162 CeedCall(CeedCalloc(ceed_relative_path_length + 1, &ceed_relative_path)); 163 memcpy(ceed_relative_path, &next_left_chevron[1], ceed_relative_path_length); 164 CeedCall(CeedGetJitAbsolutePath(ceed, ceed_relative_path, (const char **)&include_source_path)); 165 CeedCall(CeedFree(&ceed_relative_path)); 166 } 167 // ---- Recursive call to load source to buffer 168 for (CeedInt i = 0; i < *num_files; i++) is_included |= !strcmp(include_source_path, (*filepaths)[i]); 169 if (!is_included) { 170 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "JiT Including: %s\n", include_source_path); 171 CeedCall(CeedLoadSourceToInitializedBuffer_Private(ceed, include_source_path, depth + 1, num_files, filepaths, buffer)); 172 CeedCall(CeedRealloc(*num_files + 1, filepaths)); 173 CeedCall(CeedStringAllocCopy(include_source_path, &(*filepaths)[*num_files])); 174 (*num_files)++; 175 } 176 CeedCall(CeedFree(&include_source_path)); 177 } 178 file_offset = strchr(first_hash, '\n') - temp_buffer + 1; 179 } 180 // -- Next hash 181 first_hash = strchr(&first_hash[1], '#'); 182 } 183 // Copy rest of source file into buffer 184 long current_size = strlen(*buffer); 185 long copy_size = strlen(&temp_buffer[file_offset]); 186 187 CeedCall(CeedRealloc(current_size + copy_size + 2, buffer)); 188 memcpy(&(*buffer)[current_size], "\n", 2); 189 memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 190 memcpy(&(*buffer)[current_size + copy_size + 1], "", 1); 191 192 // Cleanup 193 CeedCall(CeedFree(&temp_buffer)); 194 if (depth == 0) { 195 for (CeedInt i = 0; i < *num_files; i++) CeedCall(CeedFree(&(*filepaths)[i])); 196 CeedCall(CeedFree(filepaths)); 197 } 198 199 // Debug 200 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n"); 201 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: "); 202 CeedDebug(ceed, "%s\n", source_file_path); 203 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Final buffer:\n"); 204 CeedDebug(ceed, "%s\n", *buffer); 205 return CEED_ERROR_SUCCESS; 206 } 207 208 /** 209 @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"` 210 211 @param[in] ceed `Ceed` object for error handling 212 @param[in] source_file_path Absolute path to source file 213 @param[out] buffer String buffer for source file contents 214 215 @return An error code: 0 - success, otherwise - failure 216 217 @ref Backend 218 **/ 219 int CeedLoadSourceToInitializedBuffer(Ceed ceed, const char *source_file_path, char **buffer) { 220 char **filepaths = NULL; 221 CeedInt num_files = 0; 222 223 CeedCall(CeedLoadSourceToInitializedBuffer_Private(ceed, source_file_path, 0, &num_files, &filepaths, buffer)); 224 return CEED_ERROR_SUCCESS; 225 } 226 227 /** 228 @brief Initialize and load source file into string buffer, including full text of local files in place of `#include "local.h"`. 229 230 Note: Caller is responsible for freeing the string buffer with @ref CeedFree(). 231 232 @param[in] ceed `Ceed` object for error handling 233 @param[in] source_file_path Absolute path to source file 234 @param[out] buffer String buffer for source file contents 235 236 @return An error code: 0 - success, otherwise - failure 237 238 @ref Backend 239 **/ 240 int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, char **buffer) { 241 // Initialize buffer 242 CeedCall(CeedCalloc(1, buffer)); 243 244 // Load to initalized buffer 245 CeedCall(CeedLoadSourceToInitializedBuffer(ceed, source_file_path, buffer)); 246 return CEED_ERROR_SUCCESS; 247 } 248 249 /** 250 @brief Build an absolute filepath from a base filepath and an absolute filepath. 251 252 This helps construct source file paths for @ref CeedLoadSourceToBuffer(). 253 254 Note: Caller is responsible for freeing the string buffer with @ref CeedFree(). 255 256 @param[in] ceed `Ceed` object for error handling 257 @param[in] base_file_path Absolute path to current file 258 @param[in] relative_file_path Relative path to target file 259 @param[out] new_file_path String buffer for absolute path to target file 260 261 @return An error code: 0 - success, otherwise - failure 262 263 @ref Backend 264 **/ 265 int CeedPathConcatenate(Ceed ceed, const char *base_file_path, const char *relative_file_path, char **new_file_path) { 266 char *last_slash = strrchr(base_file_path, '/'); 267 size_t base_length = (last_slash - base_file_path + 1), relative_length = strlen(relative_file_path), 268 new_file_path_length = base_length + relative_length + 1; 269 270 CeedCall(CeedCalloc(new_file_path_length, new_file_path)); 271 memcpy(*new_file_path, base_file_path, base_length); 272 memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length); 273 return CEED_ERROR_SUCCESS; 274 } 275 276 /** 277 @brief Find the relative filepath to an installed JiT file 278 279 @param[in] absolute_file_path Absolute path to installed JiT file 280 @param[out] relative_file_path Relative path to installed JiT file, a substring of the absolute path 281 282 @return An error code: 0 - success, otherwise - failure 283 284 @ref Backend 285 **/ 286 int CeedGetJitRelativePath(const char *absolute_file_path, const char **relative_file_path) { 287 *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source"); 288 CeedCheck(*relative_file_path, NULL, CEED_ERROR_MAJOR, "Couldn't find relative path including 'ceed/jit-source' for: %s", absolute_file_path); 289 return CEED_ERROR_SUCCESS; 290 } 291 292 /** 293 @brief Build an absolute filepath to a JiT file 294 295 @param[in] ceed `Ceed` object for error handling 296 @param[in] relative_file_path Relative path to installed JiT file 297 @param[out] absolute_file_path String buffer for absolute path to target file, to be freed by caller 298 299 @return An error code: 0 - success, otherwise - failure 300 301 @ref Backend 302 **/ 303 int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path, const char **absolute_file_path) { 304 Ceed ceed_parent; 305 306 // Debug 307 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n"); 308 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Relative JiT source file: "); 309 CeedDebug(ceed, "%s\n", relative_file_path); 310 311 CeedCall(CeedGetParent(ceed, &ceed_parent)); 312 for (CeedInt i = 0; i < ceed_parent->num_jit_source_roots; i++) { 313 bool is_valid; 314 315 // Debug 316 CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking JiT root: "); 317 CeedDebug(ceed, "%s\n", ceed_parent->jit_source_roots[i]); 318 319 // Build and check absolute path with current root 320 CeedCall(CeedPathConcatenate(ceed, ceed_parent->jit_source_roots[i], relative_file_path, (char **)absolute_file_path)); 321 CeedCall(CeedCheckFilePath(ceed, *absolute_file_path, &is_valid)); 322 323 if (is_valid) return CEED_ERROR_SUCCESS; 324 // LCOV_EXCL_START 325 else CeedCall(CeedFree(absolute_file_path)); 326 // LCOV_EXCL_STOP 327 } 328 // LCOV_EXCL_START 329 return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't find matching JiT source file: %s", relative_file_path); 330 // LCOV_EXCL_STOP 331 } 332