13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 33d3250a0SJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 53d3250a0SJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 73d3250a0SJeremy L Thompson 83d3250a0SJeremy L Thompson #include <ceed/ceed.h> 93d3250a0SJeremy L Thompson #include <ceed/backend.h> 103d3250a0SJeremy L Thompson #include <ceed/jit-tools.h> 116eb0d8b4SJeremy L Thompson #include <ceed-impl.h> 123d3250a0SJeremy L Thompson #include <stdbool.h> 133d3250a0SJeremy L Thompson #include <stdio.h> 143d3250a0SJeremy L Thompson #include <string.h> 153d3250a0SJeremy L Thompson 163d3250a0SJeremy L Thompson /** 17ee5a26f2SJeremy L Thompson @brief Check if valid file exists at path given 18ee5a26f2SJeremy L Thompson 19ee5a26f2SJeremy L Thompson @param ceed A Ceed object for error handling 20ee5a26f2SJeremy L Thompson @param[in] source_file_path Absolute path to source file 21ee5a26f2SJeremy L Thompson @param[out] is_valid Boolean flag indicating if file can be opend 22ee5a26f2SJeremy L Thompson 23ee5a26f2SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 24ee5a26f2SJeremy L Thompson 25ee5a26f2SJeremy L Thompson @ref Backend 26ee5a26f2SJeremy L Thompson **/ 27ee5a26f2SJeremy L Thompson int CeedCheckFilePath(Ceed ceed, const char *source_file_path, bool *is_valid) { 28ee5a26f2SJeremy L Thompson int ierr; 29ee5a26f2SJeremy L Thompson 30ee5a26f2SJeremy L Thompson // Sometimes we have path/to/file.h:function_name 31ee5a26f2SJeremy L Thompson // Create tempory file path without name, if needed 32ee5a26f2SJeremy L Thompson char *source_file_path_only; 33ee5a26f2SJeremy L Thompson char *last_colon = strrchr(source_file_path, ':'); 34ee5a26f2SJeremy L Thompson if (last_colon) { 35ee5a26f2SJeremy L Thompson size_t source_file_path_length = (last_colon - source_file_path + 1); 36ee5a26f2SJeremy L Thompson 37ee5a26f2SJeremy L Thompson ierr = CeedCalloc(source_file_path_length, &source_file_path_only); 38ee5a26f2SJeremy L Thompson CeedChk(ierr); 39d602d780SJeremy L Thompson memcpy(source_file_path_only, source_file_path, source_file_path_length - 1); 40ee5a26f2SJeremy L Thompson } else { 41ee5a26f2SJeremy L Thompson source_file_path_only = (char *)source_file_path; 42ee5a26f2SJeremy L Thompson } 43ee5a26f2SJeremy L Thompson 44ee5a26f2SJeremy L Thompson // Debug 45ee5a26f2SJeremy L Thompson CeedDebug256(ceed, 1, "Checking for source file: "); 46ee5a26f2SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", source_file_path_only); 47ee5a26f2SJeremy L Thompson 48ee5a26f2SJeremy L Thompson // Check for valid file path 49ee5a26f2SJeremy L Thompson FILE *source_file; 50ee5a26f2SJeremy L Thompson source_file = fopen(source_file_path_only, "rb"); 51ee5a26f2SJeremy L Thompson *is_valid = !!source_file; 52ee5a26f2SJeremy L Thompson 53ee5a26f2SJeremy L Thompson if (*is_valid) { 54ee5a26f2SJeremy L Thompson // Debug 55ee5a26f2SJeremy L Thompson CeedDebug256(ceed, 1, "Found JiT source file: "); 56ee5a26f2SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", source_file_path_only); 57ee5a26f2SJeremy L Thompson 58ee5a26f2SJeremy L Thompson fclose(source_file); 59ee5a26f2SJeremy L Thompson } 60ee5a26f2SJeremy L Thompson 61ee5a26f2SJeremy L Thompson // Free temp file path, if used 62ee5a26f2SJeremy L Thompson if (last_colon) { 63ee5a26f2SJeremy L Thompson ierr = CeedFree(&source_file_path_only); CeedChk(ierr); 64ee5a26f2SJeremy L Thompson } 65ee5a26f2SJeremy L Thompson 66ee5a26f2SJeremy L Thompson return CEED_ERROR_SUCCESS; 67ee5a26f2SJeremy L Thompson } 68ee5a26f2SJeremy L Thompson 69ee5a26f2SJeremy L Thompson /** 70*f6af633fSnbeams @brief Load source file into initialized string buffer, including full text 713d3250a0SJeremy L Thompson of local files in place of `#include "local.h"` 723d3250a0SJeremy L Thompson 733d3250a0SJeremy L Thompson @param ceed A Ceed object for error handling 743d3250a0SJeremy L Thompson @param[in] source_file_path Absolute path to source file 753d3250a0SJeremy L Thompson @param[out] buffer String buffer for source file contents 763d3250a0SJeremy L Thompson 773d3250a0SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 783d3250a0SJeremy L Thompson 793d3250a0SJeremy L Thompson @ref Backend 803d3250a0SJeremy L Thompson **/ 81*f6af633fSnbeams int CeedLoadSourceToInitializedBuffer(Ceed ceed, 823d3250a0SJeremy L Thompson const char *source_file_path, char **buffer) { 833d3250a0SJeremy L Thompson int ierr; 843d3250a0SJeremy L Thompson FILE *source_file; 853d3250a0SJeremy L Thompson long file_size, file_offset = 0; 863d3250a0SJeremy L Thompson char *temp_buffer; 873d3250a0SJeremy L Thompson 883d3250a0SJeremy L Thompson // Debug 893d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 903d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Current source file: "); 913d3250a0SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", source_file_path); 923d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Current buffer:\n"); 933d3250a0SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", *buffer); 943d3250a0SJeremy L Thompson 953d3250a0SJeremy L Thompson // Read file to temporary buffer 963d3250a0SJeremy L Thompson source_file = fopen(source_file_path, "rb"); 973d3250a0SJeremy L Thompson if (!source_file) 983d3250a0SJeremy L Thompson // LCOV_EXCL_START 993d3250a0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s", 1003d3250a0SJeremy L Thompson source_file_path); 1013d3250a0SJeremy L Thompson // LCOV_EXCL_STOP 1023d3250a0SJeremy L Thompson // -- Compute size of source 1033d3250a0SJeremy L Thompson fseek(source_file, 0L, SEEK_END); 1043d3250a0SJeremy L Thompson file_size = ftell(source_file); 1053d3250a0SJeremy L Thompson rewind(source_file); 1063d3250a0SJeremy L Thompson // -- Allocate memory for entire source file 1073d3250a0SJeremy L Thompson ierr = CeedCalloc(file_size + 1, &temp_buffer); CeedChk(ierr); 1083d3250a0SJeremy L Thompson // -- Copy the file into the buffer 1093d3250a0SJeremy L Thompson if (1 != fread(temp_buffer, file_size, 1, source_file)) { 1103d3250a0SJeremy L Thompson // LCOV_EXCL_START 1113d3250a0SJeremy L Thompson fclose(source_file); 1123d3250a0SJeremy L Thompson ierr = CeedFree(&temp_buffer); CeedChk(ierr); 1133d3250a0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s", 1143d3250a0SJeremy L Thompson source_file_path); 1153d3250a0SJeremy L Thompson // LCOV_EXCL_STOP 1163d3250a0SJeremy L Thompson } 1173d3250a0SJeremy L Thompson fclose(source_file); 1183d3250a0SJeremy L Thompson 1193d3250a0SJeremy L Thompson // Search for headers to include 1203d3250a0SJeremy L Thompson const char *first_hash = strchr(temp_buffer, '#'); 1213d3250a0SJeremy L Thompson while (first_hash) { 1223d3250a0SJeremy L Thompson // -- Check for 'include' keyword 1233d3250a0SJeremy L Thompson const char *next_e = strchr(first_hash, 'e'); 1243d3250a0SJeremy L Thompson char keyword[8] = ""; 1253d3250a0SJeremy L Thompson if (next_e) 126d602d780SJeremy L Thompson memcpy(keyword, &next_e[-6], 7); 1273d3250a0SJeremy L Thompson bool is_hash_include = !strcmp(keyword, "include"); 1283d3250a0SJeremy L Thompson // ---- Spaces allowed in '# include <header.h>' 1293d3250a0SJeremy L Thompson if (next_e) 1303d3250a0SJeremy L Thompson for (CeedInt i = 1; first_hash - next_e + i < -6; i++) 1313d3250a0SJeremy L Thompson is_hash_include &= first_hash[i] == ' '; 1323d3250a0SJeremy L Thompson if (is_hash_include) { 1333d3250a0SJeremy L Thompson // -- Copy into buffer all preceding # 1343d3250a0SJeremy L Thompson long current_size = strlen(*buffer); 1353d3250a0SJeremy L Thompson long copy_size = first_hash - &temp_buffer[file_offset]; 1363d3250a0SJeremy L Thompson ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr); 137d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size], "\n", 2); 138d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 139d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + copy_size], "", 1); 1403d3250a0SJeremy L Thompson // -- Load local "header.h" 1413d3250a0SJeremy L Thompson char *next_quote = strchr(first_hash, '"'); 1423d3250a0SJeremy L Thompson char *next_new_line = strchr(first_hash, '\n'); 1433d3250a0SJeremy L Thompson bool is_local_header = is_hash_include && next_quote 1443d3250a0SJeremy L Thompson && (next_new_line - next_quote > 0); 1453d3250a0SJeremy L Thompson if (is_local_header) { 1463d3250a0SJeremy L Thompson // ---- Build source path 1473d3250a0SJeremy L Thompson char *include_source_path; 1483d3250a0SJeremy L Thompson long root_length = strrchr(source_file_path, '/') - source_file_path; 1493d3250a0SJeremy L Thompson long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1; 1503d3250a0SJeremy L Thompson ierr = CeedCalloc(root_length + include_file_name_len + 2, 1513d3250a0SJeremy L Thompson &include_source_path); CeedChk(ierr); 152d602d780SJeremy L Thompson memcpy(include_source_path, source_file_path, root_length + 1); 153d602d780SJeremy L Thompson memcpy(&include_source_path[root_length + 1], &next_quote[1], 1543d3250a0SJeremy L Thompson include_file_name_len); 155d602d780SJeremy L Thompson memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1); 1563d3250a0SJeremy L Thompson // ---- Recursive call to load source to buffer 157*f6af633fSnbeams ierr = CeedLoadSourceToInitializedBuffer(ceed, include_source_path, buffer); 158a0154adeSJed Brown CeedDebug256(ceed, 2, "JiT Including: %s\n", include_source_path); 1593d3250a0SJeremy L Thompson CeedChk(ierr); 1603d3250a0SJeremy L Thompson ierr = CeedFree(&include_source_path); CeedChk(ierr); 1613d3250a0SJeremy L Thompson } 1623d3250a0SJeremy L Thompson file_offset = strchr(first_hash, '\n') - temp_buffer + 1; 1633d3250a0SJeremy L Thompson } 1643d3250a0SJeremy L Thompson // -- Next hash 1653d3250a0SJeremy L Thompson first_hash = strchr(&first_hash[1], '#'); 1663d3250a0SJeremy L Thompson } 1673d3250a0SJeremy L Thompson // Copy rest of source file into buffer 1683d3250a0SJeremy L Thompson long current_size = strlen(*buffer); 1693d3250a0SJeremy L Thompson long copy_size = strlen(&temp_buffer[file_offset]); 1703d3250a0SJeremy L Thompson ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr); 171d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size], "\n", 2); 172d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 173d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + copy_size + 1], "", 1); 1743d3250a0SJeremy L Thompson 1753d3250a0SJeremy L Thompson // Cleanup 1763d3250a0SJeremy L Thompson ierr = CeedFree(&temp_buffer); CeedChk(ierr); 1773d3250a0SJeremy L Thompson 1783d3250a0SJeremy L Thompson // Debug 1793d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 1803d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Current source file: "); 1813d3250a0SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", source_file_path); 1823d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Final buffer:\n"); 1833d3250a0SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", *buffer); 1843d3250a0SJeremy L Thompson 1853d3250a0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1863d3250a0SJeremy L Thompson } 1873d3250a0SJeremy L Thompson 1883d3250a0SJeremy L Thompson /** 1893d3250a0SJeremy L Thompson @brief Initalize and load source file into string buffer, including full text 1903d3250a0SJeremy L Thompson of local files in place of `#include "local.h"`. 1913d3250a0SJeremy L Thompson Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 1923d3250a0SJeremy L Thompson 1933d3250a0SJeremy L Thompson @param ceed A Ceed object for error handling 1943d3250a0SJeremy L Thompson @param[in] source_file_path Absolute path to source file 1953d3250a0SJeremy L Thompson @param[out] buffer String buffer for source file contents 1963d3250a0SJeremy L Thompson 1973d3250a0SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1983d3250a0SJeremy L Thompson 1993d3250a0SJeremy L Thompson @ref Backend 2003d3250a0SJeremy L Thompson **/ 2013d3250a0SJeremy L Thompson int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, 2023d3250a0SJeremy L Thompson char **buffer) { 2033d3250a0SJeremy L Thompson int ierr; 2043d3250a0SJeremy L Thompson 2053d3250a0SJeremy L Thompson // Initalize buffer 2063d3250a0SJeremy L Thompson ierr = CeedCalloc(1, buffer); CeedChk(ierr); 2073d3250a0SJeremy L Thompson 2083d3250a0SJeremy L Thompson // Load to initalized buffer 209*f6af633fSnbeams ierr = CeedLoadSourceToInitializedBuffer(ceed, source_file_path, buffer); 2103d3250a0SJeremy L Thompson CeedChk(ierr); 2113d3250a0SJeremy L Thompson 2123d3250a0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2133d3250a0SJeremy L Thompson } 214437930d1SJeremy L Thompson 215437930d1SJeremy L Thompson /** 216437930d1SJeremy L Thompson @brief Build an absolute filepath from a base filepath and an absolute filepath. 217437930d1SJeremy L Thompson This helps construct source file paths for `CeedLoadSourceToBuffer()`. 218437930d1SJeremy L Thompson Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 219437930d1SJeremy L Thompson 220437930d1SJeremy L Thompson @param ceed A Ceed object for error handling 221437930d1SJeremy L Thompson @param[in] base_file_path Absolute path to current file 222437930d1SJeremy L Thompson @param[in] relative_file_path Relative path to target file 223437930d1SJeremy L Thompson @param[out] new_file_path String buffer for absolute path to target file 224437930d1SJeremy L Thompson 225437930d1SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 226437930d1SJeremy L Thompson 227437930d1SJeremy L Thompson @ref Backend 228437930d1SJeremy L Thompson **/ 229437930d1SJeremy L Thompson int CeedPathConcatenate(Ceed ceed, const char *base_file_path, 230437930d1SJeremy L Thompson const char *relative_file_path, char **new_file_path) { 231437930d1SJeremy L Thompson int ierr; 232437930d1SJeremy L Thompson char *last_slash = strrchr(base_file_path, '/'); 233437930d1SJeremy L Thompson size_t base_length = (last_slash - base_file_path + 1), 234437930d1SJeremy L Thompson relative_length = strlen(relative_file_path), 235437930d1SJeremy L Thompson new_file_path_length = base_length + relative_length + 1; 236437930d1SJeremy L Thompson 237437930d1SJeremy L Thompson ierr = CeedCalloc(new_file_path_length, new_file_path); CeedChk(ierr); 238d602d780SJeremy L Thompson memcpy(*new_file_path, base_file_path, base_length); 239d602d780SJeremy L Thompson memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length); 240437930d1SJeremy L Thompson 241437930d1SJeremy L Thompson return CEED_ERROR_SUCCESS; 242437930d1SJeremy L Thompson } 2436eb0d8b4SJeremy L Thompson 2446eb0d8b4SJeremy L Thompson /** 245032e71eaSJeremy L Thompson @brief Find the relative filepath to an installed JiT file 2466eb0d8b4SJeremy L Thompson 247032e71eaSJeremy L Thompson @param[in] absolute_file_path Absolute path to installed JiT file 248032e71eaSJeremy L Thompson @param[out] relative_file_path Relative path to installed JiT file 2496eb0d8b4SJeremy L Thompson 2506eb0d8b4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2516eb0d8b4SJeremy L Thompson 2526eb0d8b4SJeremy L Thompson @ref Backend 2536eb0d8b4SJeremy L Thompson **/ 254032e71eaSJeremy L Thompson int CeedGetJitRelativePath(const char *absolute_file_path, 255032e71eaSJeremy L Thompson const char **relative_file_path) { 256032e71eaSJeremy L Thompson *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source"); 2576eb0d8b4SJeremy L Thompson 258032e71eaSJeremy L Thompson if (!*relative_file_path) 259032e71eaSJeremy L Thompson // LCOV_EXCL_START 260032e71eaSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, 261032e71eaSJeremy L Thompson "Couldn't find relative path including " 262032e71eaSJeremy L Thompson "'ceed/jit-source' for: %s", absolute_file_path); 263032e71eaSJeremy L Thompson // LCOV_EXCL_STOP 2646eb0d8b4SJeremy L Thompson 2656eb0d8b4SJeremy L Thompson return CEED_ERROR_SUCCESS; 2666eb0d8b4SJeremy L Thompson } 267032e71eaSJeremy L Thompson 268032e71eaSJeremy L Thompson /** 269032e71eaSJeremy L Thompson @brief Build an absolute filepath to a JiT file 270032e71eaSJeremy L Thompson 271032e71eaSJeremy L Thompson @param ceed A Ceed object for error handling 272032e71eaSJeremy L Thompson @param[in] relative_file_path Relative path to installed JiT file 273032e71eaSJeremy L Thompson @param[out] absolute_file_path String buffer for absolute path to target file 274032e71eaSJeremy L Thompson 275032e71eaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 276032e71eaSJeremy L Thompson 277032e71eaSJeremy L Thompson @ref Backend 278032e71eaSJeremy L Thompson **/ 279032e71eaSJeremy L Thompson int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path, 280032e71eaSJeremy L Thompson char **absolute_file_path) { 281032e71eaSJeremy L Thompson int ierr; 2826155f12fSJeremy L Thompson Ceed ceed_parent; 283032e71eaSJeremy L Thompson 284032e71eaSJeremy L Thompson // Debug 285032e71eaSJeremy L Thompson CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 286032e71eaSJeremy L Thompson CeedDebug256(ceed, 1, "Relative JiT source file: "); 287032e71eaSJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", relative_file_path); 288032e71eaSJeremy L Thompson 289032e71eaSJeremy L Thompson 2906155f12fSJeremy L Thompson ierr = CeedGetParent(ceed, &ceed_parent); CeedChk(ierr); 2916155f12fSJeremy L Thompson for (CeedInt i = 0; i < ceed_parent->num_jit_source_roots; i++) { 292ee5a26f2SJeremy L Thompson bool is_valid; 293ee5a26f2SJeremy L Thompson 294032e71eaSJeremy L Thompson // Debug 295032e71eaSJeremy L Thompson CeedDebug256(ceed, 1, "Checking JiT root: "); 2966155f12fSJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", ceed_parent->jit_source_roots[i]); 297032e71eaSJeremy L Thompson 298ee5a26f2SJeremy L Thompson // Build and check absolute path with current root 2996155f12fSJeremy L Thompson ierr = CeedPathConcatenate(ceed, ceed_parent->jit_source_roots[i], 300032e71eaSJeremy L Thompson relative_file_path, absolute_file_path); 301032e71eaSJeremy L Thompson CeedChk(ierr); 302ee5a26f2SJeremy L Thompson ierr = CeedCheckFilePath(ceed, *absolute_file_path, &is_valid); CeedChk(ierr); 303032e71eaSJeremy L Thompson 304ee5a26f2SJeremy L Thompson if (is_valid) { 305032e71eaSJeremy L Thompson return CEED_ERROR_SUCCESS; 306032e71eaSJeremy L Thompson } else { 307032e71eaSJeremy L Thompson ierr = CeedFree(absolute_file_path); CeedChk(ierr); 308032e71eaSJeremy L Thompson } 309032e71eaSJeremy L Thompson } 310032e71eaSJeremy L Thompson 311032e71eaSJeremy L Thompson // LCOV_EXCL_START 312032e71eaSJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, 313032e71eaSJeremy L Thompson "Couldn't find matching JiT source file: %s", 314032e71eaSJeremy L Thompson relative_file_path); 315032e71eaSJeremy L Thompson // LCOV_EXCL_STOP 316032e71eaSJeremy L Thompson } 317