From 59bb1cdbf75dc65bd575c2849a2dd59fe794a380 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Tue, 3 Sep 2024 11:59:42 -0700 Subject: [PATCH 1/2] `ParmParse`: Prefix to `FILE` For CI/CD workflows and out-of-source tests we often want to include dependent inputs files via `FILE = `. For development, we often want to run in temporary run directories but want to avoid having to copy over the latest inputs file from a source directory. Now, the environment variable `AMREX_INPUTS_FILE_PREFIX` can be set to prefix every `FILE = ` with a custom path. We will use this in the CTests integration of WarpX. --- Src/Base/AMReX_ParmParse.H | 3 +++ Src/Base/AMReX_ParmParse.cpp | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Src/Base/AMReX_ParmParse.H b/Src/Base/AMReX_ParmParse.H index cc9588793d..802997b6e9 100644 --- a/Src/Base/AMReX_ParmParse.H +++ b/Src/Base/AMReX_ParmParse.H @@ -60,6 +60,9 @@ class RealVect; // '\n's. The "FILE = " definition is special. Rather than just // adding this entry to the database, it reads the contents of // into the database. +// For CI/CD workflows and out-of-source tests, the environment variable +// AMREX_INPUTS_FILE_PREFIX can be set to prefix every FILE = +// with a custom path. // // ParmParse stores all entries in a static table which is built the // first time a ParmParse object is constructed (usually in main()). diff --git a/Src/Base/AMReX_ParmParse.cpp b/Src/Base/AMReX_ParmParse.cpp index df1e18e9b0..88adac3395 100644 --- a/Src/Base/AMReX_ParmParse.cpp +++ b/Src/Base/AMReX_ParmParse.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -407,6 +408,14 @@ read_file (const char* fname, ParmParse::Table& tab) // if ( fname != nullptr && fname[0] != 0 ) { + std::string filename = fname; + + // optional prefix to search files in + char const *amrex_inputs_file_prefix = std::getenv("AMREX_INPUTS_FILE_PREFIX"); + if (amrex_inputs_file_prefix != nullptr) { + filename = std::string(amrex_inputs_file_prefix) + filename; + } + #ifdef AMREX_USE_MPI if (ParallelDescriptor::Communicator() == MPI_COMM_NULL) { @@ -415,7 +424,6 @@ read_file (const char* fname, ParmParse::Table& tab) #endif Vector fileCharPtr; - std::string filename = fname; ParallelDescriptor::ReadAndBcastFile(filename, fileCharPtr); std::istringstream is(fileCharPtr.data()); From 5e4908cf2fdcd9153f0257b8016ce3fa8c847053 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Wed, 4 Sep 2024 18:11:00 -0700 Subject: [PATCH 2/2] Append a Trailing `/` to Prefix --- Src/Base/AMReX_ParmParse.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Src/Base/AMReX_ParmParse.cpp b/Src/Base/AMReX_ParmParse.cpp index 88adac3395..2e3efff7a6 100644 --- a/Src/Base/AMReX_ParmParse.cpp +++ b/Src/Base/AMReX_ParmParse.cpp @@ -411,9 +411,14 @@ read_file (const char* fname, ParmParse::Table& tab) std::string filename = fname; // optional prefix to search files in - char const *amrex_inputs_file_prefix = std::getenv("AMREX_INPUTS_FILE_PREFIX"); - if (amrex_inputs_file_prefix != nullptr) { - filename = std::string(amrex_inputs_file_prefix) + filename; + char const *amrex_inputs_file_prefix_c = std::getenv("AMREX_INPUTS_FILE_PREFIX"); + if (amrex_inputs_file_prefix_c != nullptr) { + // we expect a directory path as the prefix: append a trailing "/" if missing + auto amrex_inputs_file_prefix = std::string(amrex_inputs_file_prefix_c); + if (amrex_inputs_file_prefix.back() != '/') { + amrex_inputs_file_prefix += "/"; + } + filename = amrex_inputs_file_prefix + filename; } #ifdef AMREX_USE_MPI