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 86eb0d8b4SJeremy L Thompson #include <ceed-impl.h> 9*2b730f8bSJeremy L Thompson #include <ceed/backend.h> 10*2b730f8bSJeremy L Thompson #include <ceed/ceed.h> 11*2b730f8bSJeremy L Thompson #include <ceed/jit-tools.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 // Sometimes we have path/to/file.h:function_name 29ee5a26f2SJeremy L Thompson // Create tempory file path without name, if needed 30ee5a26f2SJeremy L Thompson char *source_file_path_only; 31ee5a26f2SJeremy L Thompson char *last_colon = strrchr(source_file_path, ':'); 32ee5a26f2SJeremy L Thompson if (last_colon) { 33ee5a26f2SJeremy L Thompson size_t source_file_path_length = (last_colon - source_file_path + 1); 34ee5a26f2SJeremy L Thompson 35*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(source_file_path_length, &source_file_path_only)); 36d602d780SJeremy L Thompson memcpy(source_file_path_only, source_file_path, source_file_path_length - 1); 37ee5a26f2SJeremy L Thompson } else { 38ee5a26f2SJeremy L Thompson source_file_path_only = (char *)source_file_path; 39ee5a26f2SJeremy L Thompson } 40ee5a26f2SJeremy L Thompson 41ee5a26f2SJeremy L Thompson // Debug 42ee5a26f2SJeremy L Thompson CeedDebug256(ceed, 1, "Checking for source file: "); 4313f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", source_file_path_only); 44ee5a26f2SJeremy L Thompson 45ee5a26f2SJeremy L Thompson // Check for valid file path 46ee5a26f2SJeremy L Thompson FILE *source_file; 47ee5a26f2SJeremy L Thompson source_file = fopen(source_file_path_only, "rb"); 48ee5a26f2SJeremy L Thompson *is_valid = !!source_file; 49ee5a26f2SJeremy L Thompson 50ee5a26f2SJeremy L Thompson if (*is_valid) { 51ee5a26f2SJeremy L Thompson // Debug 52ee5a26f2SJeremy L Thompson CeedDebug256(ceed, 1, "Found JiT source file: "); 5313f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", source_file_path_only); 54ee5a26f2SJeremy L Thompson 55ee5a26f2SJeremy L Thompson fclose(source_file); 56ee5a26f2SJeremy L Thompson } 57ee5a26f2SJeremy L Thompson 58ee5a26f2SJeremy L Thompson // Free temp file path, if used 59*2b730f8bSJeremy L Thompson if (last_colon) CeedCall(CeedFree(&source_file_path_only)); 60ee5a26f2SJeremy L Thompson 61ee5a26f2SJeremy L Thompson return CEED_ERROR_SUCCESS; 62ee5a26f2SJeremy L Thompson } 63ee5a26f2SJeremy L Thompson 64ee5a26f2SJeremy L Thompson /** 65f6af633fSnbeams @brief Load source file into initialized string buffer, including full text 663d3250a0SJeremy L Thompson of local files in place of `#include "local.h"` 673d3250a0SJeremy L Thompson 683d3250a0SJeremy L Thompson @param ceed A Ceed object for error handling 693d3250a0SJeremy L Thompson @param[in] source_file_path Absolute path to source file 703d3250a0SJeremy L Thompson @param[out] buffer String buffer for source file contents 713d3250a0SJeremy L Thompson 723d3250a0SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 733d3250a0SJeremy L Thompson 743d3250a0SJeremy L Thompson @ref Backend 753d3250a0SJeremy L Thompson **/ 76*2b730f8bSJeremy L Thompson int CeedLoadSourceToInitializedBuffer(Ceed ceed, const char *source_file_path, char **buffer) { 773d3250a0SJeremy L Thompson FILE *source_file; 783d3250a0SJeremy L Thompson long file_size, file_offset = 0; 793d3250a0SJeremy L Thompson char *temp_buffer; 803d3250a0SJeremy L Thompson 813d3250a0SJeremy L Thompson // Debug 823d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 833d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Current source file: "); 8413f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", source_file_path); 853d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Current buffer:\n"); 8613f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", *buffer); 873d3250a0SJeremy L Thompson 883d3250a0SJeremy L Thompson // Read file to temporary buffer 893d3250a0SJeremy L Thompson source_file = fopen(source_file_path, "rb"); 90*2b730f8bSJeremy L Thompson if (!source_file) { 913d3250a0SJeremy L Thompson // LCOV_EXCL_START 92*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s", source_file_path); 933d3250a0SJeremy L Thompson // LCOV_EXCL_STOP 94*2b730f8bSJeremy L Thompson } 953d3250a0SJeremy L Thompson // -- Compute size of source 963d3250a0SJeremy L Thompson fseek(source_file, 0L, SEEK_END); 973d3250a0SJeremy L Thompson file_size = ftell(source_file); 983d3250a0SJeremy L Thompson rewind(source_file); 993d3250a0SJeremy L Thompson // -- Allocate memory for entire source file 100*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(file_size + 1, &temp_buffer)); 1013d3250a0SJeremy L Thompson // -- Copy the file into the buffer 1023d3250a0SJeremy L Thompson if (1 != fread(temp_buffer, file_size, 1, source_file)) { 1033d3250a0SJeremy L Thompson // LCOV_EXCL_START 1043d3250a0SJeremy L Thompson fclose(source_file); 105*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&temp_buffer)); 106*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s", source_file_path); 1073d3250a0SJeremy L Thompson // LCOV_EXCL_STOP 1083d3250a0SJeremy L Thompson } 1093d3250a0SJeremy L Thompson fclose(source_file); 1103d3250a0SJeremy L Thompson 1113d3250a0SJeremy L Thompson // Search for headers to include 1123d3250a0SJeremy L Thompson const char *first_hash = strchr(temp_buffer, '#'); 1133d3250a0SJeremy L Thompson while (first_hash) { 1143d3250a0SJeremy L Thompson // -- Check for 'include' keyword 1153d3250a0SJeremy L Thompson const char *next_e = strchr(first_hash, 'e'); 1163d3250a0SJeremy L Thompson char keyword[8] = ""; 117c9c2c079SJeremy L Thompson if (next_e && next_e - first_hash >= 7) memcpy(keyword, &next_e[-6], 7); 1183d3250a0SJeremy L Thompson bool is_hash_include = !strcmp(keyword, "include"); 1193d3250a0SJeremy L Thompson // ---- Spaces allowed in '# include <header.h>' 120c9c2c079SJeremy L Thompson if (next_e) { 121c9c2c079SJeremy L Thompson for (CeedInt i = 1; first_hash - next_e + i < -6; i++) { 1223d3250a0SJeremy L Thompson is_hash_include &= first_hash[i] == ' '; 123c9c2c079SJeremy L Thompson } 124c9c2c079SJeremy L Thompson } 1253d3250a0SJeremy L Thompson if (is_hash_include) { 1263d3250a0SJeremy L Thompson // -- Copy into buffer all preceding # 1273d3250a0SJeremy L Thompson long current_size = strlen(*buffer); 1283d3250a0SJeremy L Thompson long copy_size = first_hash - &temp_buffer[file_offset]; 129*2b730f8bSJeremy L Thompson CeedCall(CeedRealloc(current_size + copy_size + 2, buffer)); 130d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size], "\n", 2); 131d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 132d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + copy_size], "", 1); 1333d3250a0SJeremy L Thompson // -- Load local "header.h" 1343d3250a0SJeremy L Thompson char *next_quote = strchr(first_hash, '"'); 1353d3250a0SJeremy L Thompson char *next_new_line = strchr(first_hash, '\n'); 136*2b730f8bSJeremy L Thompson bool is_local_header = is_hash_include && next_quote && (next_new_line - next_quote > 0); 137c9c2c079SJeremy L Thompson char *next_left_chevron = strchr(first_hash, '<'); 138*2b730f8bSJeremy L Thompson bool is_ceed_header = is_hash_include && next_left_chevron && (next_new_line - next_left_chevron > 0) && 139*2b730f8bSJeremy L Thompson (!strncmp(next_left_chevron, "<ceed/jit-source/", 17) || !strncmp(next_left_chevron, "<ceed/types.h>", 14) || 140*2b730f8bSJeremy L Thompson !strncmp(next_left_chevron, "<ceed/ceed-f32.h>", 17) || !strncmp(next_left_chevron, "<ceed/ceed-f64.h>", 17)); 141c9c2c079SJeremy L Thompson if (is_local_header || is_ceed_header) { 1423d3250a0SJeremy L Thompson // ---- Build source path 1433d3250a0SJeremy L Thompson char *include_source_path; 144c9c2c079SJeremy L Thompson if (is_local_header) { 1453d3250a0SJeremy L Thompson long root_length = strrchr(source_file_path, '/') - source_file_path; 1463d3250a0SJeremy L Thompson long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1; 147*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(root_length + include_file_name_len + 2, &include_source_path)); 148d602d780SJeremy L Thompson memcpy(include_source_path, source_file_path, root_length + 1); 149*2b730f8bSJeremy L Thompson memcpy(&include_source_path[root_length + 1], &next_quote[1], include_file_name_len); 150d602d780SJeremy L Thompson memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1); 151c9c2c079SJeremy L Thompson } else { 152c9c2c079SJeremy L Thompson char *next_right_chevron = strchr(first_hash, '>'); 153c9c2c079SJeremy L Thompson char *ceed_relative_path; 154c9c2c079SJeremy L Thompson long ceed_relative_path_length = next_right_chevron - next_left_chevron - 1; 155*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(ceed_relative_path_length + 1, &ceed_relative_path)); 156c9c2c079SJeremy L Thompson memcpy(ceed_relative_path, &next_left_chevron[1], ceed_relative_path_length); 157*2b730f8bSJeremy L Thompson CeedCall(CeedGetJitAbsolutePath(ceed, ceed_relative_path, &include_source_path)); 158*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&ceed_relative_path)); 159c9c2c079SJeremy L Thompson } 1603d3250a0SJeremy L Thompson // ---- Recursive call to load source to buffer 161a0154adeSJed Brown CeedDebug256(ceed, 2, "JiT Including: %s\n", include_source_path); 162*2b730f8bSJeremy L Thompson CeedCall(CeedLoadSourceToInitializedBuffer(ceed, include_source_path, buffer)); 163*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&include_source_path)); 1643d3250a0SJeremy L Thompson } 1653d3250a0SJeremy L Thompson file_offset = strchr(first_hash, '\n') - temp_buffer + 1; 1663d3250a0SJeremy L Thompson } 1673d3250a0SJeremy L Thompson // -- Next hash 1683d3250a0SJeremy L Thompson first_hash = strchr(&first_hash[1], '#'); 1693d3250a0SJeremy L Thompson } 1703d3250a0SJeremy L Thompson // Copy rest of source file into buffer 1713d3250a0SJeremy L Thompson long current_size = strlen(*buffer); 1723d3250a0SJeremy L Thompson long copy_size = strlen(&temp_buffer[file_offset]); 173*2b730f8bSJeremy L Thompson CeedCall(CeedRealloc(current_size + copy_size + 2, buffer)); 174d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size], "\n", 2); 175d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 176d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + copy_size + 1], "", 1); 1773d3250a0SJeremy L Thompson 1783d3250a0SJeremy L Thompson // Cleanup 179*2b730f8bSJeremy L Thompson CeedCall(CeedFree(&temp_buffer)); 1803d3250a0SJeremy L Thompson 1813d3250a0SJeremy L Thompson // Debug 1823d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 1833d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Current source file: "); 18413f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", source_file_path); 1853d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Final buffer:\n"); 18613f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", *buffer); 1873d3250a0SJeremy L Thompson 1883d3250a0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1893d3250a0SJeremy L Thompson } 1903d3250a0SJeremy L Thompson 1913d3250a0SJeremy L Thompson /** 1923d3250a0SJeremy L Thompson @brief Initalize and load source file into string buffer, including full text 1933d3250a0SJeremy L Thompson of local files in place of `#include "local.h"`. 1943d3250a0SJeremy L Thompson Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 1953d3250a0SJeremy L Thompson 1963d3250a0SJeremy L Thompson @param ceed A Ceed object for error handling 1973d3250a0SJeremy L Thompson @param[in] source_file_path Absolute path to source file 1983d3250a0SJeremy L Thompson @param[out] buffer String buffer for source file contents 1993d3250a0SJeremy L Thompson 2003d3250a0SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2013d3250a0SJeremy L Thompson 2023d3250a0SJeremy L Thompson @ref Backend 2033d3250a0SJeremy L Thompson **/ 204*2b730f8bSJeremy L Thompson int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, char **buffer) { 2053d3250a0SJeremy L Thompson // Initalize buffer 206*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, buffer)); 2073d3250a0SJeremy L Thompson 2083d3250a0SJeremy L Thompson // Load to initalized buffer 209*2b730f8bSJeremy L Thompson CeedCall(CeedLoadSourceToInitializedBuffer(ceed, source_file_path, buffer)); 2103d3250a0SJeremy L Thompson 2113d3250a0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2123d3250a0SJeremy L Thompson } 213437930d1SJeremy L Thompson 214437930d1SJeremy L Thompson /** 215437930d1SJeremy L Thompson @brief Build an absolute filepath from a base filepath and an absolute filepath. 216437930d1SJeremy L Thompson This helps construct source file paths for `CeedLoadSourceToBuffer()`. 217437930d1SJeremy L Thompson Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 218437930d1SJeremy L Thompson 219437930d1SJeremy L Thompson @param ceed A Ceed object for error handling 220437930d1SJeremy L Thompson @param[in] base_file_path Absolute path to current file 221437930d1SJeremy L Thompson @param[in] relative_file_path Relative path to target file 222437930d1SJeremy L Thompson @param[out] new_file_path String buffer for absolute path to target file 223437930d1SJeremy L Thompson 224437930d1SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 225437930d1SJeremy L Thompson 226437930d1SJeremy L Thompson @ref Backend 227437930d1SJeremy L Thompson **/ 228*2b730f8bSJeremy L Thompson int CeedPathConcatenate(Ceed ceed, const char *base_file_path, const char *relative_file_path, char **new_file_path) { 229437930d1SJeremy L Thompson char *last_slash = strrchr(base_file_path, '/'); 230*2b730f8bSJeremy L Thompson size_t base_length = (last_slash - base_file_path + 1), relative_length = strlen(relative_file_path), 231437930d1SJeremy L Thompson new_file_path_length = base_length + relative_length + 1; 232437930d1SJeremy L Thompson 233*2b730f8bSJeremy L Thompson CeedCall(CeedCalloc(new_file_path_length, new_file_path)); 234d602d780SJeremy L Thompson memcpy(*new_file_path, base_file_path, base_length); 235d602d780SJeremy L Thompson memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length); 236437930d1SJeremy L Thompson 237437930d1SJeremy L Thompson return CEED_ERROR_SUCCESS; 238437930d1SJeremy L Thompson } 2396eb0d8b4SJeremy L Thompson 2406eb0d8b4SJeremy L Thompson /** 241032e71eaSJeremy L Thompson @brief Find the relative filepath to an installed JiT file 2426eb0d8b4SJeremy L Thompson 243032e71eaSJeremy L Thompson @param[in] absolute_file_path Absolute path to installed JiT file 244032e71eaSJeremy L Thompson @param[out] relative_file_path Relative path to installed JiT file 2456eb0d8b4SJeremy L Thompson 2466eb0d8b4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2476eb0d8b4SJeremy L Thompson 2486eb0d8b4SJeremy L Thompson @ref Backend 2496eb0d8b4SJeremy L Thompson **/ 250*2b730f8bSJeremy L Thompson int CeedGetJitRelativePath(const char *absolute_file_path, const char **relative_file_path) { 251032e71eaSJeremy L Thompson *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source"); 2526eb0d8b4SJeremy L Thompson 253*2b730f8bSJeremy L Thompson if (!*relative_file_path) { 254032e71eaSJeremy L Thompson // LCOV_EXCL_START 255*2b730f8bSJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, "Couldn't find relative path including 'ceed/jit-source' for: %s", absolute_file_path); 256032e71eaSJeremy L Thompson // LCOV_EXCL_STOP 257*2b730f8bSJeremy L Thompson } 2586eb0d8b4SJeremy L Thompson return CEED_ERROR_SUCCESS; 2596eb0d8b4SJeremy L Thompson } 260032e71eaSJeremy L Thompson 261032e71eaSJeremy L Thompson /** 262032e71eaSJeremy L Thompson @brief Build an absolute filepath to a JiT file 263032e71eaSJeremy L Thompson 264032e71eaSJeremy L Thompson @param ceed A Ceed object for error handling 265032e71eaSJeremy L Thompson @param[in] relative_file_path Relative path to installed JiT file 266032e71eaSJeremy L Thompson @param[out] absolute_file_path String buffer for absolute path to target file 267032e71eaSJeremy L Thompson 268032e71eaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 269032e71eaSJeremy L Thompson 270032e71eaSJeremy L Thompson @ref Backend 271032e71eaSJeremy L Thompson **/ 272*2b730f8bSJeremy L Thompson int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path, char **absolute_file_path) { 2736155f12fSJeremy L Thompson Ceed ceed_parent; 274032e71eaSJeremy L Thompson 275032e71eaSJeremy L Thompson // Debug 276032e71eaSJeremy L Thompson CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 277032e71eaSJeremy L Thompson CeedDebug256(ceed, 1, "Relative JiT source file: "); 27813f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", relative_file_path); 279032e71eaSJeremy L Thompson 280*2b730f8bSJeremy L Thompson CeedCall(CeedGetParent(ceed, &ceed_parent)); 2816155f12fSJeremy L Thompson for (CeedInt i = 0; i < ceed_parent->num_jit_source_roots; i++) { 282ee5a26f2SJeremy L Thompson bool is_valid; 283ee5a26f2SJeremy L Thompson 284032e71eaSJeremy L Thompson // Debug 285032e71eaSJeremy L Thompson CeedDebug256(ceed, 1, "Checking JiT root: "); 28613f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", ceed_parent->jit_source_roots[i]); 287032e71eaSJeremy L Thompson 288ee5a26f2SJeremy L Thompson // Build and check absolute path with current root 289*2b730f8bSJeremy L Thompson CeedCall(CeedPathConcatenate(ceed, ceed_parent->jit_source_roots[i], relative_file_path, absolute_file_path)); 290*2b730f8bSJeremy L Thompson CeedCall(CeedCheckFilePath(ceed, *absolute_file_path, &is_valid)); 291032e71eaSJeremy L Thompson 292*2b730f8bSJeremy L Thompson if (is_valid) return CEED_ERROR_SUCCESS; 293*2b730f8bSJeremy L Thompson else CeedCall(CeedFree(absolute_file_path)); 294032e71eaSJeremy L Thompson } 295032e71eaSJeremy L Thompson 296032e71eaSJeremy L Thompson // LCOV_EXCL_START 297*2b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't find matching JiT source file: %s", relative_file_path); 298032e71eaSJeremy L Thompson // LCOV_EXCL_STOP 299032e71eaSJeremy L Thompson } 300