15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 112b730f8bSJeremy 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 19ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object for error handling 20ee5a26f2SJeremy L Thompson @param[in] source_file_path Absolute path to source file 21ea61e9acSJeremy L Thompson @param[out] is_valid Boolean flag indicating if file can be opened 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 29ea61e9acSJeremy L Thompson // Create temporary file path without name, if needed 30ee5a26f2SJeremy L Thompson char *source_file_path_only; 31ee5a26f2SJeremy L Thompson char *last_colon = strrchr(source_file_path, ':'); 321c66c397SJeremy L Thompson 33ee5a26f2SJeremy L Thompson if (last_colon) { 34ee5a26f2SJeremy L Thompson size_t source_file_path_length = (last_colon - source_file_path + 1); 35ee5a26f2SJeremy L Thompson 362b730f8bSJeremy L Thompson CeedCall(CeedCalloc(source_file_path_length, &source_file_path_only)); 37d602d780SJeremy L Thompson memcpy(source_file_path_only, source_file_path, source_file_path_length - 1); 38ee5a26f2SJeremy L Thompson } else { 39ee5a26f2SJeremy L Thompson source_file_path_only = (char *)source_file_path; 40ee5a26f2SJeremy L Thompson } 41ee5a26f2SJeremy L Thompson 42ee5a26f2SJeremy L Thompson // Debug 4323d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking for source file: "); 4413f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", source_file_path_only); 45ee5a26f2SJeremy L Thompson 46ee5a26f2SJeremy L Thompson // Check for valid file path 47ee5a26f2SJeremy L Thompson FILE *source_file; 48ee5a26f2SJeremy L Thompson source_file = fopen(source_file_path_only, "rb"); 491c66c397SJeremy L Thompson *is_valid = source_file; 50ee5a26f2SJeremy L Thompson 51ee5a26f2SJeremy L Thompson if (*is_valid) { 52ee5a26f2SJeremy L Thompson // Debug 5323d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Found JiT source file: "); 5413f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", source_file_path_only); 55ee5a26f2SJeremy L Thompson fclose(source_file); 56ee5a26f2SJeremy L Thompson } 57ee5a26f2SJeremy L Thompson 58ee5a26f2SJeremy L Thompson // Free temp file path, if used 592b730f8bSJeremy L Thompson if (last_colon) CeedCall(CeedFree(&source_file_path_only)); 60ee5a26f2SJeremy L Thompson return CEED_ERROR_SUCCESS; 61ee5a26f2SJeremy L Thompson } 62ee5a26f2SJeremy L Thompson 63ee5a26f2SJeremy L Thompson /** 64*509d4af6SJeremy L Thompson @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`. 65*509d4af6SJeremy L Thompson This also updates the `num_file_paths` and `source_file_paths`. 66*509d4af6SJeremy L Thompson Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree(). 673d3250a0SJeremy L Thompson 68ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object for error handling 693d3250a0SJeremy L Thompson @param[in] source_file_path Absolute path to source file 70*509d4af6SJeremy L Thompson @param[in,out] num_file_paths Number of files already included 71*509d4af6SJeremy L Thompson @param[in,out] file_paths Paths of files already included 723d3250a0SJeremy L Thompson @param[out] buffer String buffer for source file contents 733d3250a0SJeremy L Thompson 743d3250a0SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 753d3250a0SJeremy L Thompson 763d3250a0SJeremy L Thompson @ref Backend 773d3250a0SJeremy L Thompson **/ 78*509d4af6SJeremy L Thompson int CeedLoadSourceToInitializedBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_files, char ***file_paths, char **buffer) { 793d3250a0SJeremy L Thompson FILE *source_file; 803d3250a0SJeremy L Thompson long file_size, file_offset = 0; 813d3250a0SJeremy L Thompson char *temp_buffer; 823d3250a0SJeremy L Thompson 833d3250a0SJeremy L Thompson // Debug 8423d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n"); 8523d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: "); 8613f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", source_file_path); 8723d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current buffer:\n"); 8813f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", *buffer); 893d3250a0SJeremy L Thompson 903d3250a0SJeremy L Thompson // Read file to temporary buffer 913d3250a0SJeremy L Thompson source_file = fopen(source_file_path, "rb"); 926574a04fSJeremy L Thompson CeedCheck(source_file, ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s", source_file_path); 933d3250a0SJeremy L Thompson // -- Compute size of source 943d3250a0SJeremy L Thompson fseek(source_file, 0L, SEEK_END); 953d3250a0SJeremy L Thompson file_size = ftell(source_file); 963d3250a0SJeremy L Thompson rewind(source_file); 973d3250a0SJeremy L Thompson // -- Allocate memory for entire source file 982b730f8bSJeremy L Thompson CeedCall(CeedCalloc(file_size + 1, &temp_buffer)); 993d3250a0SJeremy L Thompson // -- Copy the file into the buffer 1003d3250a0SJeremy L Thompson if (1 != fread(temp_buffer, file_size, 1, source_file)) { 1013d3250a0SJeremy L Thompson // LCOV_EXCL_START 1023d3250a0SJeremy L Thompson fclose(source_file); 1032b730f8bSJeremy L Thompson CeedCall(CeedFree(&temp_buffer)); 1042b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s", source_file_path); 1053d3250a0SJeremy L Thompson // LCOV_EXCL_STOP 1063d3250a0SJeremy L Thompson } 1073d3250a0SJeremy L Thompson fclose(source_file); 1083d3250a0SJeremy L Thompson 1093d3250a0SJeremy L Thompson // Search for headers to include 1103d3250a0SJeremy L Thompson const char *first_hash = strchr(temp_buffer, '#'); 1111c66c397SJeremy L Thompson 1123d3250a0SJeremy L Thompson while (first_hash) { 113*509d4af6SJeremy L Thompson // -- Check for 'pragma' keyword 114*509d4af6SJeremy L Thompson const char *next_m = strchr(first_hash, 'm'); 115*509d4af6SJeremy L Thompson char keyword[8] = ""; 116*509d4af6SJeremy L Thompson 117*509d4af6SJeremy L Thompson if (next_m && next_m - first_hash >= 5) memcpy(keyword, &next_m[-4], 6); 118*509d4af6SJeremy L Thompson bool is_hash_pragma = !strcmp(keyword, "pragma"); 119*509d4af6SJeremy L Thompson 120*509d4af6SJeremy L Thompson // ---- Spaces allowed in '# pragma' 121*509d4af6SJeremy L Thompson if (next_m) { 122*509d4af6SJeremy L Thompson for (CeedInt i = 1; first_hash - next_m + i < -5; i++) { 123*509d4af6SJeremy L Thompson is_hash_pragma &= first_hash[i] == ' '; 124*509d4af6SJeremy L Thompson } 125*509d4af6SJeremy L Thompson } 126*509d4af6SJeremy L Thompson if (is_hash_pragma) { 127*509d4af6SJeremy L Thompson // -- Check if '#pragma once' 128*509d4af6SJeremy L Thompson char *next_o = strchr(first_hash, 'o'); 129*509d4af6SJeremy L Thompson char *next_new_line = strchr(first_hash, '\n'); 130*509d4af6SJeremy L Thompson bool is_pragma_once = next_o && (next_new_line - next_o > 0) && !strncmp(next_o, "once", 4); 131*509d4af6SJeremy L Thompson 132*509d4af6SJeremy L Thompson // -- Copy into buffer, omitting last line if #pragma once 133*509d4af6SJeremy L Thompson long current_size = strlen(*buffer); 134*509d4af6SJeremy L Thompson long copy_size = first_hash - &temp_buffer[file_offset] + (is_pragma_once ? 0 : (next_new_line - first_hash + 1)); 135*509d4af6SJeremy L Thompson 136*509d4af6SJeremy L Thompson CeedCall(CeedRealloc(current_size + copy_size + 2, buffer)); 137*509d4af6SJeremy L Thompson memcpy(&(*buffer)[current_size], "\n", 2); 138*509d4af6SJeremy L Thompson memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 139*509d4af6SJeremy L Thompson memcpy(&(*buffer)[current_size + copy_size], "", 1); 140*509d4af6SJeremy L Thompson 141*509d4af6SJeremy L Thompson file_offset = strchr(first_hash, '\n') - temp_buffer + 1; 142*509d4af6SJeremy L Thompson } 143*509d4af6SJeremy L Thompson 1443d3250a0SJeremy L Thompson // -- Check for 'include' keyword 1453d3250a0SJeremy L Thompson const char *next_e = strchr(first_hash, 'e'); 1461c66c397SJeremy L Thompson 147c9c2c079SJeremy L Thompson if (next_e && next_e - first_hash >= 7) memcpy(keyword, &next_e[-6], 7); 1483d3250a0SJeremy L Thompson bool is_hash_include = !strcmp(keyword, "include"); 1491c66c397SJeremy L Thompson 1503d3250a0SJeremy L Thompson // ---- Spaces allowed in '# include <header.h>' 151c9c2c079SJeremy L Thompson if (next_e) { 152c9c2c079SJeremy L Thompson for (CeedInt i = 1; first_hash - next_e + i < -6; i++) { 1533d3250a0SJeremy L Thompson is_hash_include &= first_hash[i] == ' '; 154c9c2c079SJeremy L Thompson } 155c9c2c079SJeremy L Thompson } 1563d3250a0SJeremy L Thompson if (is_hash_include) { 1573d3250a0SJeremy L Thompson // -- Copy into buffer all preceding # 1583d3250a0SJeremy L Thompson long current_size = strlen(*buffer); 1593d3250a0SJeremy L Thompson long copy_size = first_hash - &temp_buffer[file_offset]; 1601c66c397SJeremy L Thompson 1612b730f8bSJeremy L Thompson CeedCall(CeedRealloc(current_size + copy_size + 2, buffer)); 162d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size], "\n", 2); 163d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 164d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + copy_size], "", 1); 1653d3250a0SJeremy L Thompson // -- Load local "header.h" 1663d3250a0SJeremy L Thompson char *next_quote = strchr(first_hash, '"'); 1673d3250a0SJeremy L Thompson char *next_new_line = strchr(first_hash, '\n'); 1682b730f8bSJeremy L Thompson bool is_local_header = is_hash_include && next_quote && (next_new_line - next_quote > 0); 169c9c2c079SJeremy L Thompson char *next_left_chevron = strchr(first_hash, '<'); 170*509d4af6SJeremy L Thompson bool is_ceed_header = next_left_chevron && (next_new_line - next_left_chevron > 0) && 1712b730f8bSJeremy L Thompson (!strncmp(next_left_chevron, "<ceed/jit-source/", 17) || !strncmp(next_left_chevron, "<ceed/types.h>", 14) || 1722b730f8bSJeremy L Thompson !strncmp(next_left_chevron, "<ceed/ceed-f32.h>", 17) || !strncmp(next_left_chevron, "<ceed/ceed-f64.h>", 17)); 1731c66c397SJeremy L Thompson 174c9c2c079SJeremy L Thompson if (is_local_header || is_ceed_header) { 1753d3250a0SJeremy L Thompson // ---- Build source path 176430da97aSJeremy L Thompson bool is_included = false; 1773d3250a0SJeremy L Thompson char *include_source_path; 1781c66c397SJeremy L Thompson 179c9c2c079SJeremy L Thompson if (is_local_header) { 1803d3250a0SJeremy L Thompson long root_length = strrchr(source_file_path, '/') - source_file_path; 1813d3250a0SJeremy L Thompson long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1; 1821c66c397SJeremy L Thompson 1832b730f8bSJeremy L Thompson CeedCall(CeedCalloc(root_length + include_file_name_len + 2, &include_source_path)); 184d602d780SJeremy L Thompson memcpy(include_source_path, source_file_path, root_length + 1); 1852b730f8bSJeremy L Thompson memcpy(&include_source_path[root_length + 1], &next_quote[1], include_file_name_len); 186d602d780SJeremy L Thompson memcpy(&include_source_path[root_length + include_file_name_len + 1], "", 1); 187c9c2c079SJeremy L Thompson } else { 188c9c2c079SJeremy L Thompson char *next_right_chevron = strchr(first_hash, '>'); 189c9c2c079SJeremy L Thompson char *ceed_relative_path; 190c9c2c079SJeremy L Thompson long ceed_relative_path_length = next_right_chevron - next_left_chevron - 1; 1911c66c397SJeremy L Thompson 1922b730f8bSJeremy L Thompson CeedCall(CeedCalloc(ceed_relative_path_length + 1, &ceed_relative_path)); 193c9c2c079SJeremy L Thompson memcpy(ceed_relative_path, &next_left_chevron[1], ceed_relative_path_length); 19422070f95SJeremy L Thompson CeedCall(CeedGetJitAbsolutePath(ceed, ceed_relative_path, (const char **)&include_source_path)); 1952b730f8bSJeremy L Thompson CeedCall(CeedFree(&ceed_relative_path)); 196c9c2c079SJeremy L Thompson } 1973d3250a0SJeremy L Thompson // ---- Recursive call to load source to buffer 198*509d4af6SJeremy L Thompson for (CeedInt i = 0; i < *num_files; i++) is_included |= !strcmp(include_source_path, (*file_paths)[i]); 199430da97aSJeremy L Thompson if (!is_included) { 20023d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "JiT Including: %s\n", include_source_path); 201*509d4af6SJeremy L Thompson CeedCall(CeedLoadSourceToInitializedBuffer(ceed, include_source_path, num_files, file_paths, buffer)); 202*509d4af6SJeremy L Thompson CeedCall(CeedRealloc(*num_files + 1, file_paths)); 203*509d4af6SJeremy L Thompson CeedCall(CeedStringAllocCopy(include_source_path, &(*file_paths)[*num_files])); 204430da97aSJeremy L Thompson (*num_files)++; 205430da97aSJeremy L Thompson } 2062b730f8bSJeremy L Thompson CeedCall(CeedFree(&include_source_path)); 2073d3250a0SJeremy L Thompson } 2083d3250a0SJeremy L Thompson file_offset = strchr(first_hash, '\n') - temp_buffer + 1; 2093d3250a0SJeremy L Thompson } 2103d3250a0SJeremy L Thompson // -- Next hash 2113d3250a0SJeremy L Thompson first_hash = strchr(&first_hash[1], '#'); 2123d3250a0SJeremy L Thompson } 2133d3250a0SJeremy L Thompson // Copy rest of source file into buffer 2143d3250a0SJeremy L Thompson long current_size = strlen(*buffer); 2153d3250a0SJeremy L Thompson long copy_size = strlen(&temp_buffer[file_offset]); 2161c66c397SJeremy L Thompson 2172b730f8bSJeremy L Thompson CeedCall(CeedRealloc(current_size + copy_size + 2, buffer)); 218d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size], "\n", 2); 219d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 220d602d780SJeremy L Thompson memcpy(&(*buffer)[current_size + copy_size + 1], "", 1); 2213d3250a0SJeremy L Thompson 2223d3250a0SJeremy L Thompson // Cleanup 2232b730f8bSJeremy L Thompson CeedCall(CeedFree(&temp_buffer)); 2243d3250a0SJeremy L Thompson 2253d3250a0SJeremy L Thompson // Debug 22623d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n"); 22723d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Current source file: "); 22813f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", source_file_path); 22923d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Final buffer:\n"); 23013f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", *buffer); 2313d3250a0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2323d3250a0SJeremy L Thompson } 2333d3250a0SJeremy L Thompson 2343d3250a0SJeremy L Thompson /** 235*509d4af6SJeremy L Thompson @brief Load source file into initialized string buffer, including full text of local files in place of `#include "local.h"`. 236*509d4af6SJeremy L Thompson This also initializes and populates the `num_file_paths` and `source_file_paths`. 237*509d4af6SJeremy L Thompson Callers are responsible freeing all filepath strings and the string buffer with @ref CeedFree(). 238430da97aSJeremy L Thompson 239430da97aSJeremy L Thompson @param[in] ceed `Ceed` object for error handling 240430da97aSJeremy L Thompson @param[in] source_file_path Absolute path to source file 241*509d4af6SJeremy L Thompson @param[in,out] num_file_paths Number of files already included 242*509d4af6SJeremy L Thompson @param[in,out] file_paths Paths of files already included 243430da97aSJeremy L Thompson @param[out] buffer String buffer for source file contents 244430da97aSJeremy L Thompson 245430da97aSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 246430da97aSJeremy L Thompson 247430da97aSJeremy L Thompson @ref Backend 248430da97aSJeremy L Thompson **/ 249*509d4af6SJeremy L Thompson int CeedLoadSourceAndInitializeBuffer(Ceed ceed, const char *source_file_path, CeedInt *num_file_paths, char ***file_paths, char **buffer) { 250*509d4af6SJeremy L Thompson // Ensure defaults were set 251*509d4af6SJeremy L Thompson *num_file_paths = 0; 252*509d4af6SJeremy L Thompson *file_paths = NULL; 253430da97aSJeremy L Thompson 254*509d4af6SJeremy L Thompson // Initialize 255*509d4af6SJeremy L Thompson CeedCall(CeedCalloc(1, buffer)); 256*509d4af6SJeremy L Thompson 257*509d4af6SJeremy L Thompson // And load source 258*509d4af6SJeremy L Thompson CeedCall(CeedLoadSourceToInitializedBuffer(ceed, source_file_path, num_file_paths, file_paths, buffer)); 259430da97aSJeremy L Thompson return CEED_ERROR_SUCCESS; 260430da97aSJeremy L Thompson } 261430da97aSJeremy L Thompson 262430da97aSJeremy L Thompson /** 263ea61e9acSJeremy L Thompson @brief Initialize and load source file into string buffer, including full text of local files in place of `#include "local.h"`. 264*509d4af6SJeremy L Thompson User @ref CeedLoadSourceAndInitializeBuffer() and @ref CeedLoadSourceToInitializedBuffer() if loading multiple source files into the same buffer. 265*509d4af6SJeremy L Thompson Caller is responsible for freeing the string buffer with @ref CeedFree(). 2663d3250a0SJeremy L Thompson 267ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object for error handling 2683d3250a0SJeremy L Thompson @param[in] source_file_path Absolute path to source file 2693d3250a0SJeremy L Thompson @param[out] buffer String buffer for source file contents 2703d3250a0SJeremy L Thompson 2713d3250a0SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2723d3250a0SJeremy L Thompson 2733d3250a0SJeremy L Thompson @ref Backend 2743d3250a0SJeremy L Thompson **/ 2752b730f8bSJeremy L Thompson int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, char **buffer) { 276*509d4af6SJeremy L Thompson char **file_paths = NULL; 277*509d4af6SJeremy L Thompson CeedInt num_file_paths = 0; 2783d3250a0SJeremy L Thompson 279*509d4af6SJeremy L Thompson // Load 280*509d4af6SJeremy L Thompson CeedCall(CeedLoadSourceAndInitializeBuffer(ceed, source_file_path, &num_file_paths, &file_paths, buffer)); 281*509d4af6SJeremy L Thompson 282*509d4af6SJeremy L Thompson // Cleanup 283*509d4af6SJeremy L Thompson for (CeedInt i = 0; i < num_file_paths; i++) CeedCall(CeedFree(&file_paths[i])); 284*509d4af6SJeremy L Thompson CeedCall(CeedFree(&file_paths)); 2853d3250a0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2863d3250a0SJeremy L Thompson } 287437930d1SJeremy L Thompson 288437930d1SJeremy L Thompson /** 289437930d1SJeremy L Thompson @brief Build an absolute filepath from a base filepath and an absolute filepath. 2904385fb7fSSebastian Grimberg 291ca94c3ddSJeremy L Thompson This helps construct source file paths for @ref CeedLoadSourceToBuffer(). 2924385fb7fSSebastian Grimberg 293ca94c3ddSJeremy L Thompson Note: Caller is responsible for freeing the string buffer with @ref CeedFree(). 294437930d1SJeremy L Thompson 295ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object for error handling 296437930d1SJeremy L Thompson @param[in] base_file_path Absolute path to current file 297437930d1SJeremy L Thompson @param[in] relative_file_path Relative path to target file 298437930d1SJeremy L Thompson @param[out] new_file_path String buffer for absolute path to target file 299437930d1SJeremy L Thompson 300437930d1SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 301437930d1SJeremy L Thompson 302437930d1SJeremy L Thompson @ref Backend 303437930d1SJeremy L Thompson **/ 3042b730f8bSJeremy L Thompson int CeedPathConcatenate(Ceed ceed, const char *base_file_path, const char *relative_file_path, char **new_file_path) { 305437930d1SJeremy L Thompson char *last_slash = strrchr(base_file_path, '/'); 3062b730f8bSJeremy L Thompson size_t base_length = (last_slash - base_file_path + 1), relative_length = strlen(relative_file_path), 307437930d1SJeremy L Thompson new_file_path_length = base_length + relative_length + 1; 308437930d1SJeremy L Thompson 3092b730f8bSJeremy L Thompson CeedCall(CeedCalloc(new_file_path_length, new_file_path)); 310d602d780SJeremy L Thompson memcpy(*new_file_path, base_file_path, base_length); 311d602d780SJeremy L Thompson memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length); 312437930d1SJeremy L Thompson return CEED_ERROR_SUCCESS; 313437930d1SJeremy L Thompson } 3146eb0d8b4SJeremy L Thompson 3156eb0d8b4SJeremy L Thompson /** 316032e71eaSJeremy L Thompson @brief Find the relative filepath to an installed JiT file 3176eb0d8b4SJeremy L Thompson 318032e71eaSJeremy L Thompson @param[in] absolute_file_path Absolute path to installed JiT file 31991e18578SJeremy L Thompson @param[out] relative_file_path Relative path to installed JiT file, a substring of the absolute path 3206eb0d8b4SJeremy L Thompson 3216eb0d8b4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3226eb0d8b4SJeremy L Thompson 3236eb0d8b4SJeremy L Thompson @ref Backend 3246eb0d8b4SJeremy L Thompson **/ 3252b730f8bSJeremy L Thompson int CeedGetJitRelativePath(const char *absolute_file_path, const char **relative_file_path) { 326032e71eaSJeremy L Thompson *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source"); 3276574a04fSJeremy L Thompson CeedCheck(*relative_file_path, NULL, CEED_ERROR_MAJOR, "Couldn't find relative path including 'ceed/jit-source' for: %s", absolute_file_path); 3286eb0d8b4SJeremy L Thompson return CEED_ERROR_SUCCESS; 3296eb0d8b4SJeremy L Thompson } 330032e71eaSJeremy L Thompson 331032e71eaSJeremy L Thompson /** 332032e71eaSJeremy L Thompson @brief Build an absolute filepath to a JiT file 333032e71eaSJeremy L Thompson 334ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object for error handling 335032e71eaSJeremy L Thompson @param[in] relative_file_path Relative path to installed JiT file 33691e18578SJeremy L Thompson @param[out] absolute_file_path String buffer for absolute path to target file, to be freed by caller 337032e71eaSJeremy L Thompson 338032e71eaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 339032e71eaSJeremy L Thompson 340032e71eaSJeremy L Thompson @ref Backend 341032e71eaSJeremy L Thompson **/ 342f8608ea8SJed Brown int CeedGetJitAbsolutePath(Ceed ceed, const char *relative_file_path, const char **absolute_file_path) { 3436155f12fSJeremy L Thompson Ceed ceed_parent; 344032e71eaSJeremy L Thompson 345032e71eaSJeremy L Thompson // Debug 34623d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- Ceed JiT ----------\n"); 34723d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Relative JiT source file: "); 34813f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", relative_file_path); 349032e71eaSJeremy L Thompson 3502b730f8bSJeremy L Thompson CeedCall(CeedGetParent(ceed, &ceed_parent)); 3516155f12fSJeremy L Thompson for (CeedInt i = 0; i < ceed_parent->num_jit_source_roots; i++) { 352ee5a26f2SJeremy L Thompson bool is_valid; 353ee5a26f2SJeremy L Thompson 354032e71eaSJeremy L Thompson // Debug 35523d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Checking JiT root: "); 35613f886e9SJeremy L Thompson CeedDebug(ceed, "%s\n", ceed_parent->jit_source_roots[i]); 357032e71eaSJeremy L Thompson 358ee5a26f2SJeremy L Thompson // Build and check absolute path with current root 35922070f95SJeremy L Thompson CeedCall(CeedPathConcatenate(ceed, ceed_parent->jit_source_roots[i], relative_file_path, (char **)absolute_file_path)); 3602b730f8bSJeremy L Thompson CeedCall(CeedCheckFilePath(ceed, *absolute_file_path, &is_valid)); 361032e71eaSJeremy L Thompson 3622b730f8bSJeremy L Thompson if (is_valid) return CEED_ERROR_SUCCESS; 36391e18578SJeremy L Thompson // LCOV_EXCL_START 3642b730f8bSJeremy L Thompson else CeedCall(CeedFree(absolute_file_path)); 36591e18578SJeremy L Thompson // LCOV_EXCL_STOP 366032e71eaSJeremy L Thompson } 367032e71eaSJeremy L Thompson // LCOV_EXCL_START 3682b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't find matching JiT source file: %s", relative_file_path); 369032e71eaSJeremy L Thompson // LCOV_EXCL_STOP 370032e71eaSJeremy L Thompson } 371