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 /** 173d3250a0SJeremy L Thompson @brief Load source file into initalized string buffer, including full text 183d3250a0SJeremy L Thompson of local files in place of `#include "local.h"` 193d3250a0SJeremy L Thompson 203d3250a0SJeremy L Thompson @param ceed A Ceed object for error handling 213d3250a0SJeremy L Thompson @param[in] source_file_path Absolute path to source file 223d3250a0SJeremy L Thompson @param[out] buffer String buffer for source file contents 233d3250a0SJeremy L Thompson 243d3250a0SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 253d3250a0SJeremy L Thompson 263d3250a0SJeremy L Thompson @ref Backend 273d3250a0SJeremy L Thompson **/ 283d3250a0SJeremy L Thompson static inline int CeedLoadSourceToInitalizedBuffer(Ceed ceed, 293d3250a0SJeremy L Thompson const char *source_file_path, char **buffer) { 303d3250a0SJeremy L Thompson int ierr; 313d3250a0SJeremy L Thompson FILE *source_file; 323d3250a0SJeremy L Thompson long file_size, file_offset = 0; 333d3250a0SJeremy L Thompson char *temp_buffer; 343d3250a0SJeremy L Thompson 353d3250a0SJeremy L Thompson // Debug 363d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 373d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Current source file: "); 383d3250a0SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", source_file_path); 393d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Current buffer:\n"); 403d3250a0SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", *buffer); 413d3250a0SJeremy L Thompson 423d3250a0SJeremy L Thompson // Read file to temporary buffer 433d3250a0SJeremy L Thompson source_file = fopen(source_file_path, "rb"); 443d3250a0SJeremy L Thompson if (!source_file) 453d3250a0SJeremy L Thompson // LCOV_EXCL_START 463d3250a0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't open source file: %s", 473d3250a0SJeremy L Thompson source_file_path); 483d3250a0SJeremy L Thompson // LCOV_EXCL_STOP 493d3250a0SJeremy L Thompson // -- Compute size of source 503d3250a0SJeremy L Thompson fseek(source_file, 0L, SEEK_END); 513d3250a0SJeremy L Thompson file_size = ftell(source_file); 523d3250a0SJeremy L Thompson rewind(source_file); 533d3250a0SJeremy L Thompson // -- Allocate memory for entire source file 543d3250a0SJeremy L Thompson ierr = CeedCalloc(file_size + 1, &temp_buffer); CeedChk(ierr); 553d3250a0SJeremy L Thompson // -- Copy the file into the buffer 563d3250a0SJeremy L Thompson if (1 != fread(temp_buffer, file_size, 1, source_file)) { 573d3250a0SJeremy L Thompson // LCOV_EXCL_START 583d3250a0SJeremy L Thompson fclose(source_file); 593d3250a0SJeremy L Thompson ierr = CeedFree(&temp_buffer); CeedChk(ierr); 603d3250a0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_MAJOR, "Couldn't read source file: %s", 613d3250a0SJeremy L Thompson source_file_path); 623d3250a0SJeremy L Thompson // LCOV_EXCL_STOP 633d3250a0SJeremy L Thompson } 643d3250a0SJeremy L Thompson fclose(source_file); 653d3250a0SJeremy L Thompson 663d3250a0SJeremy L Thompson // Search for headers to include 673d3250a0SJeremy L Thompson const char *first_hash = strchr(temp_buffer, '#'); 683d3250a0SJeremy L Thompson while (first_hash) { 693d3250a0SJeremy L Thompson // -- Check for 'include' keyword 703d3250a0SJeremy L Thompson const char *next_e = strchr(first_hash, 'e'); 713d3250a0SJeremy L Thompson char keyword[8] = ""; 723d3250a0SJeremy L Thompson if (next_e) 733d3250a0SJeremy L Thompson strncpy(keyword, &next_e[-6], 7); 743d3250a0SJeremy L Thompson bool is_hash_include = !strcmp(keyword, "include"); 753d3250a0SJeremy L Thompson // ---- Spaces allowed in '# include <header.h>' 763d3250a0SJeremy L Thompson if (next_e) 773d3250a0SJeremy L Thompson for (CeedInt i = 1; first_hash - next_e + i < -6; i++) 783d3250a0SJeremy L Thompson is_hash_include &= first_hash[i] == ' '; 793d3250a0SJeremy L Thompson if (is_hash_include) { 803d3250a0SJeremy L Thompson // -- Copy into buffer all preceding # 813d3250a0SJeremy L Thompson long current_size = strlen(*buffer); 823d3250a0SJeremy L Thompson long copy_size = first_hash - &temp_buffer[file_offset]; 833d3250a0SJeremy L Thompson ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr); 843d3250a0SJeremy L Thompson strncpy(&(*buffer)[current_size], "\n", 2); 853d3250a0SJeremy L Thompson strncpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 863d3250a0SJeremy L Thompson strncpy(&(*buffer)[current_size + copy_size], "", 1); 873d3250a0SJeremy L Thompson // -- Load local "header.h" 883d3250a0SJeremy L Thompson char *next_quote = strchr(first_hash, '"'); 893d3250a0SJeremy L Thompson char *next_new_line = strchr(first_hash, '\n'); 903d3250a0SJeremy L Thompson bool is_local_header = is_hash_include && next_quote 913d3250a0SJeremy L Thompson && (next_new_line - next_quote > 0); 923d3250a0SJeremy L Thompson if (is_local_header) { 933d3250a0SJeremy L Thompson // ---- Build source path 943d3250a0SJeremy L Thompson char *include_source_path; 953d3250a0SJeremy L Thompson long root_length = strrchr(source_file_path, '/') - source_file_path; 963d3250a0SJeremy L Thompson long include_file_name_len = strchr(&next_quote[1], '"') - next_quote - 1; 973d3250a0SJeremy L Thompson ierr = CeedCalloc(root_length + include_file_name_len + 2, 983d3250a0SJeremy L Thompson &include_source_path); CeedChk(ierr); 993d3250a0SJeremy L Thompson strncpy(include_source_path, source_file_path, root_length + 1); 1003d3250a0SJeremy L Thompson strncpy(&include_source_path[root_length + 1], &next_quote[1], 1013d3250a0SJeremy L Thompson include_file_name_len); 1023d3250a0SJeremy L Thompson strncpy(&include_source_path[root_length + include_file_name_len + 1], "", 1); 1033d3250a0SJeremy L Thompson // ---- Recursive call to load source to buffer 1043d3250a0SJeremy L Thompson ierr = CeedLoadSourceToInitalizedBuffer(ceed, include_source_path, buffer); 105*a0154adeSJed Brown CeedDebug256(ceed, 2, "JiT Including: %s\n", include_source_path); 1063d3250a0SJeremy L Thompson CeedChk(ierr); 1073d3250a0SJeremy L Thompson ierr = CeedFree(&include_source_path); CeedChk(ierr); 1083d3250a0SJeremy L Thompson } 1093d3250a0SJeremy L Thompson file_offset = strchr(first_hash, '\n') - temp_buffer + 1; 1103d3250a0SJeremy L Thompson } 1113d3250a0SJeremy L Thompson // -- Next hash 1123d3250a0SJeremy L Thompson first_hash = strchr(&first_hash[1], '#'); 1133d3250a0SJeremy L Thompson } 1143d3250a0SJeremy L Thompson // Copy rest of source file into buffer 1153d3250a0SJeremy L Thompson long current_size = strlen(*buffer); 1163d3250a0SJeremy L Thompson long copy_size = strlen(&temp_buffer[file_offset]); 1173d3250a0SJeremy L Thompson ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr); 1183d3250a0SJeremy L Thompson strncpy(&(*buffer)[current_size], "\n", 2); 1193d3250a0SJeremy L Thompson strncpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 1203d3250a0SJeremy L Thompson strncpy(&(*buffer)[current_size + copy_size + 1], "", 1); 1213d3250a0SJeremy L Thompson 1223d3250a0SJeremy L Thompson // Cleanup 1233d3250a0SJeremy L Thompson ierr = CeedFree(&temp_buffer); CeedChk(ierr); 1243d3250a0SJeremy L Thompson 1253d3250a0SJeremy L Thompson // Debug 1263d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 1273d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Current source file: "); 1283d3250a0SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", source_file_path); 1293d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Final buffer:\n"); 1303d3250a0SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", *buffer); 1313d3250a0SJeremy L Thompson 1323d3250a0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1333d3250a0SJeremy L Thompson } 1343d3250a0SJeremy L Thompson 1353d3250a0SJeremy L Thompson /** 1363d3250a0SJeremy L Thompson @brief Initalize and load source file into string buffer, including full text 1373d3250a0SJeremy L Thompson of local files in place of `#include "local.h"`. 1383d3250a0SJeremy L Thompson Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 1393d3250a0SJeremy L Thompson 1403d3250a0SJeremy L Thompson @param ceed A Ceed object for error handling 1413d3250a0SJeremy L Thompson @param[in] source_file_path Absolute path to source file 1423d3250a0SJeremy L Thompson @param[out] buffer String buffer for source file contents 1433d3250a0SJeremy L Thompson 1443d3250a0SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1453d3250a0SJeremy L Thompson 1463d3250a0SJeremy L Thompson @ref Backend 1473d3250a0SJeremy L Thompson **/ 1483d3250a0SJeremy L Thompson int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, 1493d3250a0SJeremy L Thompson char **buffer) { 1503d3250a0SJeremy L Thompson int ierr; 1513d3250a0SJeremy L Thompson 1523d3250a0SJeremy L Thompson // Initalize buffer 1533d3250a0SJeremy L Thompson ierr = CeedCalloc(1, buffer); CeedChk(ierr); 1543d3250a0SJeremy L Thompson 1553d3250a0SJeremy L Thompson // Load to initalized buffer 1563d3250a0SJeremy L Thompson ierr = CeedLoadSourceToInitalizedBuffer(ceed, source_file_path, buffer); 1573d3250a0SJeremy L Thompson CeedChk(ierr); 1583d3250a0SJeremy L Thompson 1593d3250a0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1603d3250a0SJeremy L Thompson } 161437930d1SJeremy L Thompson 162437930d1SJeremy L Thompson /** 1636eb0d8b4SJeremy L Thompson @brief Get root of search path for installed files for JiT 1646eb0d8b4SJeremy L Thompson 1656eb0d8b4SJeremy L Thompson @param ceed A Ceed object for error handling 1666eb0d8b4SJeremy L Thompson @param[out] jit_source_root String for search path root 1676eb0d8b4SJeremy L Thompson 1686eb0d8b4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1696eb0d8b4SJeremy L Thompson 1706eb0d8b4SJeremy L Thompson @ref Backend 1716eb0d8b4SJeremy L Thompson **/ 1726eb0d8b4SJeremy L Thompson int CeedGetJitSourceRoot(Ceed ceed, const char **jit_source_root) { 1736eb0d8b4SJeremy L Thompson CeedDebug256(ceed, 1, "JiT Source Root: "); 1746eb0d8b4SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", ceed->jit_source_root); 1756eb0d8b4SJeremy L Thompson *jit_source_root = ceed->jit_source_root; 1766eb0d8b4SJeremy L Thompson return CEED_ERROR_SUCCESS; 1776eb0d8b4SJeremy L Thompson } 1786eb0d8b4SJeremy L Thompson 1796eb0d8b4SJeremy L Thompson /** 1806eb0d8b4SJeremy L Thompson @brief Find the relative filepath to an installed JiT file 1816eb0d8b4SJeremy L Thompson 1826eb0d8b4SJeremy L Thompson @param[in] absolute_file_path Absolute path to installed JiT file 1836eb0d8b4SJeremy L Thompson @param[out] relative_file_path Relative path to installed JiT file 1846eb0d8b4SJeremy L Thompson 1856eb0d8b4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1866eb0d8b4SJeremy L Thompson 1876eb0d8b4SJeremy L Thompson @ref Backend 1886eb0d8b4SJeremy L Thompson **/ 1896eb0d8b4SJeremy L Thompson int CeedGetJitRelativePath(const char *absolute_file_path, 1906eb0d8b4SJeremy L Thompson const char **relative_file_path) { 191*a0154adeSJed Brown *(relative_file_path) = strstr(absolute_file_path, "ceed/jit-source"); 1926eb0d8b4SJeremy L Thompson 1936eb0d8b4SJeremy L Thompson if (!*relative_file_path) 1946eb0d8b4SJeremy L Thompson // LCOV_EXCL_START 1956eb0d8b4SJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, 1966eb0d8b4SJeremy L Thompson "Couldn't find relative path including " 197*a0154adeSJed Brown "'ceed/jit-source' for: %s", absolute_file_path); 1986eb0d8b4SJeremy L Thompson // LCOV_EXCL_STOP 1996eb0d8b4SJeremy L Thompson 2006eb0d8b4SJeremy L Thompson return CEED_ERROR_SUCCESS; 2016eb0d8b4SJeremy L Thompson } 2026eb0d8b4SJeremy L Thompson 2036eb0d8b4SJeremy L Thompson /** 204437930d1SJeremy L Thompson @brief Build an absolute filepath from a base filepath and an absolute filepath. 205437930d1SJeremy L Thompson This helps construct source file paths for `CeedLoadSourceToBuffer()`. 206437930d1SJeremy L Thompson Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 207437930d1SJeremy L Thompson 208437930d1SJeremy L Thompson @param ceed A Ceed object for error handling 209437930d1SJeremy L Thompson @param[in] base_file_path Absolute path to current file 210437930d1SJeremy L Thompson @param[in] relative_file_path Relative path to target file 211437930d1SJeremy L Thompson @param[out] new_file_path String buffer for absolute path to target file 212437930d1SJeremy L Thompson 213437930d1SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 214437930d1SJeremy L Thompson 215437930d1SJeremy L Thompson @ref Backend 216437930d1SJeremy L Thompson **/ 217437930d1SJeremy L Thompson int CeedPathConcatenate(Ceed ceed, const char *base_file_path, 218437930d1SJeremy L Thompson const char *relative_file_path, char **new_file_path) { 219437930d1SJeremy L Thompson int ierr; 220437930d1SJeremy L Thompson char *last_slash = strrchr(base_file_path, '/'); 221437930d1SJeremy L Thompson size_t base_length = (last_slash - base_file_path + 1), 222437930d1SJeremy L Thompson relative_length = strlen(relative_file_path), 223437930d1SJeremy L Thompson new_file_path_length = base_length + relative_length + 1; 224437930d1SJeremy L Thompson 225437930d1SJeremy L Thompson ierr = CeedCalloc(new_file_path_length, new_file_path); CeedChk(ierr); 226437930d1SJeremy L Thompson memcpy(*new_file_path, base_file_path, base_length); 227437930d1SJeremy L Thompson memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length); 228437930d1SJeremy L Thompson 229437930d1SJeremy L Thompson return CEED_ERROR_SUCCESS; 230437930d1SJeremy L Thompson } 2316eb0d8b4SJeremy L Thompson 2326eb0d8b4SJeremy L Thompson /** 2336eb0d8b4SJeremy L Thompson @brief Build an absolute filepath to an installed JiT file 2346eb0d8b4SJeremy L Thompson 2356eb0d8b4SJeremy L Thompson @param ceed A Ceed object for error handling 2366eb0d8b4SJeremy L Thompson @param[in] relative_file_path Relative path to installed JiT file 2376eb0d8b4SJeremy L Thompson @param[out] new_file_path String buffer for absolute path to target file 2386eb0d8b4SJeremy L Thompson 2396eb0d8b4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2406eb0d8b4SJeremy L Thompson 2416eb0d8b4SJeremy L Thompson @ref Backend 2426eb0d8b4SJeremy L Thompson **/ 2436eb0d8b4SJeremy L Thompson int CeedGetInstalledJitPath(Ceed ceed, const char *relative_file_path, 2446eb0d8b4SJeremy L Thompson char **jit_file_path) { 2456eb0d8b4SJeremy L Thompson int ierr; 2466eb0d8b4SJeremy L Thompson const char *jit_source_root; 2476eb0d8b4SJeremy L Thompson 2486eb0d8b4SJeremy L Thompson ierr = CeedGetJitSourceRoot(ceed, &jit_source_root); CeedChk(ierr); 2496eb0d8b4SJeremy L Thompson 2506eb0d8b4SJeremy L Thompson char *last_slash = strrchr(jit_source_root, '/'); 2516eb0d8b4SJeremy L Thompson size_t base_length = (last_slash - jit_source_root + 1), 2526eb0d8b4SJeremy L Thompson relative_length = strlen(relative_file_path), 2536eb0d8b4SJeremy L Thompson new_file_path_length = base_length + relative_length + 1; 2546eb0d8b4SJeremy L Thompson 2556eb0d8b4SJeremy L Thompson ierr = CeedCalloc(new_file_path_length, jit_file_path); CeedChk(ierr); 2566eb0d8b4SJeremy L Thompson memcpy(*jit_file_path, jit_source_root, base_length); 2576eb0d8b4SJeremy L Thompson memcpy(&((*jit_file_path)[base_length]), relative_file_path, relative_length); 2586eb0d8b4SJeremy L Thompson 2596eb0d8b4SJeremy L Thompson return CEED_ERROR_SUCCESS; 2606eb0d8b4SJeremy L Thompson } 261