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> 11*6eb0d8b4SJeremy 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); 1053d3250a0SJeremy L Thompson CeedChk(ierr); 1063d3250a0SJeremy L Thompson ierr = CeedFree(&include_source_path); CeedChk(ierr); 1073d3250a0SJeremy L Thompson } 1083d3250a0SJeremy L Thompson file_offset = strchr(first_hash, '\n') - temp_buffer + 1; 1093d3250a0SJeremy L Thompson } 1103d3250a0SJeremy L Thompson // -- Next hash 1113d3250a0SJeremy L Thompson first_hash = strchr(&first_hash[1], '#'); 1123d3250a0SJeremy L Thompson } 1133d3250a0SJeremy L Thompson // Copy rest of source file into buffer 1143d3250a0SJeremy L Thompson long current_size = strlen(*buffer); 1153d3250a0SJeremy L Thompson long copy_size = strlen(&temp_buffer[file_offset]); 1163d3250a0SJeremy L Thompson ierr = CeedRealloc(current_size + copy_size + 2, buffer); CeedChk(ierr); 1173d3250a0SJeremy L Thompson strncpy(&(*buffer)[current_size], "\n", 2); 1183d3250a0SJeremy L Thompson strncpy(&(*buffer)[current_size + 1], &temp_buffer[file_offset], copy_size); 1193d3250a0SJeremy L Thompson strncpy(&(*buffer)[current_size + copy_size + 1], "", 1); 1203d3250a0SJeremy L Thompson 1213d3250a0SJeremy L Thompson // Cleanup 1223d3250a0SJeremy L Thompson ierr = CeedFree(&temp_buffer); CeedChk(ierr); 1233d3250a0SJeremy L Thompson 1243d3250a0SJeremy L Thompson // Debug 1253d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "---------- Ceed JiT ----------\n"); 1263d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Current source file: "); 1273d3250a0SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", source_file_path); 1283d3250a0SJeremy L Thompson CeedDebug256(ceed, 1, "Final buffer:\n"); 1293d3250a0SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", *buffer); 1303d3250a0SJeremy L Thompson 1313d3250a0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1323d3250a0SJeremy L Thompson } 1333d3250a0SJeremy L Thompson 1343d3250a0SJeremy L Thompson /** 1353d3250a0SJeremy L Thompson @brief Initalize and load source file into string buffer, including full text 1363d3250a0SJeremy L Thompson of local files in place of `#include "local.h"`. 1373d3250a0SJeremy L Thompson Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 1383d3250a0SJeremy L Thompson 1393d3250a0SJeremy L Thompson @param ceed A Ceed object for error handling 1403d3250a0SJeremy L Thompson @param[in] source_file_path Absolute path to source file 1413d3250a0SJeremy L Thompson @param[out] buffer String buffer for source file contents 1423d3250a0SJeremy L Thompson 1433d3250a0SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1443d3250a0SJeremy L Thompson 1453d3250a0SJeremy L Thompson @ref Backend 1463d3250a0SJeremy L Thompson **/ 1473d3250a0SJeremy L Thompson int CeedLoadSourceToBuffer(Ceed ceed, const char *source_file_path, 1483d3250a0SJeremy L Thompson char **buffer) { 1493d3250a0SJeremy L Thompson int ierr; 1503d3250a0SJeremy L Thompson 1513d3250a0SJeremy L Thompson // Initalize buffer 1523d3250a0SJeremy L Thompson ierr = CeedCalloc(1, buffer); CeedChk(ierr); 1533d3250a0SJeremy L Thompson 1543d3250a0SJeremy L Thompson // Load to initalized buffer 1553d3250a0SJeremy L Thompson ierr = CeedLoadSourceToInitalizedBuffer(ceed, source_file_path, buffer); 1563d3250a0SJeremy L Thompson CeedChk(ierr); 1573d3250a0SJeremy L Thompson 1583d3250a0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1593d3250a0SJeremy L Thompson } 160437930d1SJeremy L Thompson 161437930d1SJeremy L Thompson /** 162*6eb0d8b4SJeremy L Thompson @brief Get root of search path for installed files for JiT 163*6eb0d8b4SJeremy L Thompson 164*6eb0d8b4SJeremy L Thompson @param ceed A Ceed object for error handling 165*6eb0d8b4SJeremy L Thompson @param[out] jit_source_root String for search path root 166*6eb0d8b4SJeremy L Thompson 167*6eb0d8b4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 168*6eb0d8b4SJeremy L Thompson 169*6eb0d8b4SJeremy L Thompson @ref Backend 170*6eb0d8b4SJeremy L Thompson **/ 171*6eb0d8b4SJeremy L Thompson int CeedGetJitSourceRoot(Ceed ceed, const char **jit_source_root) { 172*6eb0d8b4SJeremy L Thompson CeedDebug256(ceed, 1, "JiT Source Root: "); 173*6eb0d8b4SJeremy L Thompson CeedDebug256(ceed, 255, "%s\n", ceed->jit_source_root); 174*6eb0d8b4SJeremy L Thompson *jit_source_root = ceed->jit_source_root; 175*6eb0d8b4SJeremy L Thompson return CEED_ERROR_SUCCESS; 176*6eb0d8b4SJeremy L Thompson } 177*6eb0d8b4SJeremy L Thompson 178*6eb0d8b4SJeremy L Thompson /** 179*6eb0d8b4SJeremy L Thompson @brief Find the relative filepath to an installed JiT file 180*6eb0d8b4SJeremy L Thompson 181*6eb0d8b4SJeremy L Thompson @param[in] absolute_file_path Absolute path to installed JiT file 182*6eb0d8b4SJeremy L Thompson @param[out] relative_file_path Relative path to installed JiT file 183*6eb0d8b4SJeremy L Thompson 184*6eb0d8b4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 185*6eb0d8b4SJeremy L Thompson 186*6eb0d8b4SJeremy L Thompson @ref Backend 187*6eb0d8b4SJeremy L Thompson **/ 188*6eb0d8b4SJeremy L Thompson int CeedGetJitRelativePath(const char *absolute_file_path, 189*6eb0d8b4SJeremy L Thompson const char **relative_file_path) { 190*6eb0d8b4SJeremy L Thompson *(relative_file_path) = strstr(absolute_file_path, "ceed-jit-source"); 191*6eb0d8b4SJeremy L Thompson 192*6eb0d8b4SJeremy L Thompson if (!*relative_file_path) 193*6eb0d8b4SJeremy L Thompson // LCOV_EXCL_START 194*6eb0d8b4SJeremy L Thompson return CeedError(NULL, CEED_ERROR_MAJOR, 195*6eb0d8b4SJeremy L Thompson "Couldn't find relative path including " 196*6eb0d8b4SJeremy L Thompson "'ceed-jit-source' for: %s", absolute_file_path); 197*6eb0d8b4SJeremy L Thompson // LCOV_EXCL_STOP 198*6eb0d8b4SJeremy L Thompson 199*6eb0d8b4SJeremy L Thompson return CEED_ERROR_SUCCESS; 200*6eb0d8b4SJeremy L Thompson } 201*6eb0d8b4SJeremy L Thompson 202*6eb0d8b4SJeremy L Thompson /** 203437930d1SJeremy L Thompson @brief Build an absolute filepath from a base filepath and an absolute filepath. 204437930d1SJeremy L Thompson This helps construct source file paths for `CeedLoadSourceToBuffer()`. 205437930d1SJeremy L Thompson Note: Caller is responsible for freeing the string buffer with `CeedFree()`. 206437930d1SJeremy L Thompson 207437930d1SJeremy L Thompson @param ceed A Ceed object for error handling 208437930d1SJeremy L Thompson @param[in] base_file_path Absolute path to current file 209437930d1SJeremy L Thompson @param[in] relative_file_path Relative path to target file 210437930d1SJeremy L Thompson @param[out] new_file_path String buffer for absolute path to target file 211437930d1SJeremy L Thompson 212437930d1SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 213437930d1SJeremy L Thompson 214437930d1SJeremy L Thompson @ref Backend 215437930d1SJeremy L Thompson **/ 216437930d1SJeremy L Thompson int CeedPathConcatenate(Ceed ceed, const char *base_file_path, 217437930d1SJeremy L Thompson const char *relative_file_path, char **new_file_path) { 218437930d1SJeremy L Thompson int ierr; 219437930d1SJeremy L Thompson char *last_slash = strrchr(base_file_path, '/'); 220437930d1SJeremy L Thompson size_t base_length = (last_slash - base_file_path + 1), 221437930d1SJeremy L Thompson relative_length = strlen(relative_file_path), 222437930d1SJeremy L Thompson new_file_path_length = base_length + relative_length + 1; 223437930d1SJeremy L Thompson 224437930d1SJeremy L Thompson ierr = CeedCalloc(new_file_path_length, new_file_path); CeedChk(ierr); 225437930d1SJeremy L Thompson memcpy(*new_file_path, base_file_path, base_length); 226437930d1SJeremy L Thompson memcpy(&((*new_file_path)[base_length]), relative_file_path, relative_length); 227437930d1SJeremy L Thompson 228437930d1SJeremy L Thompson return CEED_ERROR_SUCCESS; 229437930d1SJeremy L Thompson } 230*6eb0d8b4SJeremy L Thompson 231*6eb0d8b4SJeremy L Thompson /** 232*6eb0d8b4SJeremy L Thompson @brief Build an absolute filepath to an installed JiT file 233*6eb0d8b4SJeremy L Thompson 234*6eb0d8b4SJeremy L Thompson @param ceed A Ceed object for error handling 235*6eb0d8b4SJeremy L Thompson @param[in] relative_file_path Relative path to installed JiT file 236*6eb0d8b4SJeremy L Thompson @param[out] new_file_path String buffer for absolute path to target file 237*6eb0d8b4SJeremy L Thompson 238*6eb0d8b4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 239*6eb0d8b4SJeremy L Thompson 240*6eb0d8b4SJeremy L Thompson @ref Backend 241*6eb0d8b4SJeremy L Thompson **/ 242*6eb0d8b4SJeremy L Thompson int CeedGetInstalledJitPath(Ceed ceed, const char *relative_file_path, 243*6eb0d8b4SJeremy L Thompson char **jit_file_path) { 244*6eb0d8b4SJeremy L Thompson int ierr; 245*6eb0d8b4SJeremy L Thompson const char *jit_source_root; 246*6eb0d8b4SJeremy L Thompson 247*6eb0d8b4SJeremy L Thompson ierr = CeedGetJitSourceRoot(ceed, &jit_source_root); CeedChk(ierr); 248*6eb0d8b4SJeremy L Thompson 249*6eb0d8b4SJeremy L Thompson char *last_slash = strrchr(jit_source_root, '/'); 250*6eb0d8b4SJeremy L Thompson size_t base_length = (last_slash - jit_source_root + 1), 251*6eb0d8b4SJeremy L Thompson relative_length = strlen(relative_file_path), 252*6eb0d8b4SJeremy L Thompson new_file_path_length = base_length + relative_length + 1; 253*6eb0d8b4SJeremy L Thompson 254*6eb0d8b4SJeremy L Thompson ierr = CeedCalloc(new_file_path_length, jit_file_path); CeedChk(ierr); 255*6eb0d8b4SJeremy L Thompson memcpy(*jit_file_path, jit_source_root, base_length); 256*6eb0d8b4SJeremy L Thompson memcpy(&((*jit_file_path)[base_length]), relative_file_path, relative_length); 257*6eb0d8b4SJeremy L Thompson 258*6eb0d8b4SJeremy L Thompson return CEED_ERROR_SUCCESS; 259*6eb0d8b4SJeremy L Thompson } 260