diff --git a/code/code_gen/build_info_gen.py b/code/code_gen/build_info_gen.py new file mode 100644 index 0000000..7a97275 --- /dev/null +++ b/code/code_gen/build_info_gen.py @@ -0,0 +1,74 @@ +import sys +import os +import datetime +import subprocess as sp + +# Add the directory containing this script to sys.path for local imports +script_dir = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, script_dir) + +from lark import Lark +import code_gen +import post + +def escape_fortran_string(s): + # Escape double quotes by doubling them + escaped = s.replace('"', '""') + # Split by newline and format as Fortran concatenation with char(10) + lines = escaped.splitlines() + if not lines: + return '""' + formatted_lines = [f'"{line}"' for line in lines] + return ' // char(10) // &\n '.join(formatted_lines) + +def main(): + if len(sys.argv) < 2: + print("Usage: python3 build_info_gen.py ") + sys.exit(1) + + terms_file = sys.argv[1] + with open(terms_file, 'r', encoding='utf-8') as f: + terms_raw = f.read() + + # Generate Version Info + vinfo = code_gen.VersionInfo(terms_raw) + build_info_str = vinfo.fparams() + + # Generate LaTeX equations + parser = Lark(code_gen.calc_grammar, + parser='lalr', + lexer_callbacks={ + 'ESCAPED_STRING': code_gen.tok_to_str, + 'INT': code_gen.tok_to_int, + 'BOOL': code_gen.tok_to_bool + }) + tree = parser.parse(terms_raw) + ir1 = post.Stage1(tree) + ir2 = post.Stage2(ir1) + ir3 = post.Stage3(ir2) + ir4 = post.Stage4(ir3) + + latex_lines = ["{"] + for avg in ir4.averaged.values(): + latex_lines.append(f' "{avg.name}" : r"${avg.latex}$",') + latex_lines.append("}") + latex_str = "\n".join(latex_lines) + + # Format for Fortran + fortran_build_info = escape_fortran_string(build_info_str) + fortran_latex = escape_fortran_string(latex_str) + + # Write m_build_info.f90 + fortran_code = f"""module m_build_info + implicit none + character(len=*), parameter :: build_info_str = & + {fortran_build_info} + character(len=*), parameter :: latex_equations_str = & + {fortran_latex} +end module m_build_info +""" + # Print to stdout so it can be redirected + print(fortran_code) + +if __name__ == "__main__": + main() diff --git a/code/makefile b/code/makefile index cb95099..289d54b 100644 --- a/code/makefile +++ b/code/makefile @@ -28,18 +28,16 @@ MODULES += \ m_calculate.o\ m_arrays.o\ m_terms.o\ + m_build_info.o\ post.o # Objects OBJ = post_dns.o -# Data Objects -ODATA = build_info.odata latex_equations.odata - Post_Cbar_FSD_Incomp : x-edge-cold-bc-uPrime-hybrid -x-edge-cold-bc-uPrime-hybrid : ${MODULES} ${OBJ} print_build_info.o ${ODATA} - ${FC} -o x-edge-cold-bc-uPrime-hybrid ${flags} ${MODULES} ${OBJ} print_build_info.o ${ODATA} +x-edge-cold-bc-uPrime-hybrid : ${MODULES} ${OBJ} + ${FC} -o x-edge-cold-bc-uPrime-hybrid ${flags} ${MODULES} ${OBJ} ${OBJ} : ${MODULES} @@ -50,7 +48,7 @@ m_terms.f90 : code_gen/code_gen.py ${TERMSPEC} code_gen/resources/m_template.py ${FC} -c ${flags} $< cleanAll: clean - rm -f m_terms.f90 + rm -f m_terms.f90 m_build_info.f90 clean: rm -f *.o *.odata *.mod x-edge-cold-bc-uPrime-hybrid test_calculate test_compact build_info.txt latex_equations.txt @@ -71,14 +69,5 @@ test_compact : Compact.o test_compact.o test_compact.o : Compact.o -print_build_info.o : print_build_info.c - ${CC} -c print_build_info.c - -%.odata : %.txt - ${LD} -r -b binary -o $@ $< - -build_info.txt : ${TERMSPEC} - ${PYTHON} code_gen/code_gen.py -b ${TERMSPEC} > $@ - -latex_equations.txt : ${TERMSPEC} - ${PYTHON} code_gen/code_gen.py -x ${TERMSPEC} > $@ +m_build_info.f90 : code_gen/build_info_gen.py ${TERMSPEC} code_gen/code_gen.py code_gen/post.py + ${PYTHON} code_gen/build_info_gen.py ${TERMSPEC} > $@ diff --git a/code/post_dns.f90 b/code/post_dns.f90 index aabefd4..86f3cbb 100644 --- a/code/post_dns.f90 +++ b/code/post_dns.f90 @@ -6,30 +6,11 @@ USE m_openmpi USE post USE Compact - - interface - - function build_info_length() - integer :: build_info_length - end function build_info_length - - function latex_length() - integer :: latex_length - end function latex_length - - end interface + USE m_build_info + IMPLICIT NONE CHARACTER(100) :: num1char = "" - character (len=8000) :: input_string = "" - - character (len=8000) :: latex_string = "" - - call cstr(input_string) - - call eqnstr(latex_string) - - !First, make sure the right number of inputs have been provided IF(COMMAND_ARGUMENT_COUNT() > 1) THEN @@ -48,9 +29,9 @@ WRITE(*,*) " -v print version information" WRITE(*,*) " -x print latex equations" ELSE IF(num1char=="-v") THEN - WRITE(*,*) input_string(1:len_trim(input_string)) + WRITE(*,*) trim(build_info_str) ELSE IF(num1char=="-x") THEN - WRITE(*,*) latex_string(1:len_trim(latex_string)) + WRITE(*,*) trim(latex_equations_str) ELSE WRITE(*,*) 'ERROR, OPTION NOT SUPPORTED ', num1char END IF @@ -61,7 +42,7 @@ CALL m_openmpi_init - if (iammaster) write(*,*) input_string(1:len_trim(input_string)) + if (iammaster) write(*,*) trim(build_info_str) CALL main diff --git a/code/print_build_info.c b/code/print_build_info.c deleted file mode 100644 index a3bdc87..0000000 --- a/code/print_build_info.c +++ /dev/null @@ -1,43 +0,0 @@ -extern const char _binary_build_info_txt_start []; -extern const char _binary_build_info_txt_end []; -extern const char _binary_build_info_txt_size []; - -extern const char _binary_latex_equations_txt_start []; -extern const char _binary_latex_equations_txt_end []; -extern const char _binary_latex_equations_txt_size []; - -int build_info_length_() -{ - return ((char*)_binary_build_info_txt_end - (char*)_binary_build_info_txt_start); -} - -int latex_length_() -{ - return ((char*)_binary_latex_equations_txt_end - (char*)_binary_latex_equations_txt_start); -} - -void string_copy ( char *s, unsigned long b, const char *p_start , const char *p_end ) -{ - int count = 0; - const char* p = p_start; - - while ( p != p_end && count < b ) - { - s[count++] = *p++; - } - - while ( count < b ) - { - s[count++] = ' '; - } -} - -void cstr_( char *s, unsigned long b ) -{ - string_copy(s, b, _binary_build_info_txt_start, _binary_build_info_txt_end); -} - -void eqnstr_( char *s, unsigned long b ) -{ - string_copy(s, b, _binary_latex_equations_txt_start, _binary_latex_equations_txt_end); -}