g4edge / pyg4ometry

Python code for rapid creation and conversion of radiation transport Monte Carlo (Geant4 and Fluka) geometries
https://pyg4ometry.readthedocs.io
GNU General Public License v3.0
14 stars 13 forks source link

Generate ANTLR parsers at build time #42

Open gipert opened 1 year ago

gipert commented 1 year ago

Instead of generating these files manually once in a while and fixing antlr4-python3-runtime to that version in pyproject.toml, we could let CMake do that automatically at build (actually configure) time. This could be achieved by:

  1. add antlr4-tools to build-system.requires in pyproject.toml
  2. write a shell script that produces the files and moves them to a certain location, for example:

    #!/bin/sh
    
    for f in *.g4; do
       antlr4 "$f" -Werror -Dlanguage=Python3 -visitor || exit 1
    done
    
    mv *.py "$1" || exit 1

    in cmake/tools/antlr

  3. remove everything from the various *Expression folders and just leave a CMakeLists.txt there listing, for example:

    message(STATUS "Generating parser for fluka/RegionExpression")
    
    execute_process(COMMAND
     ${CMAKE_SOURCE_DIR}/cmake/tools/path/to/script.sh ${CMAKE_CURRENT_SOURCE_DIR}
     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/cmake/tools/RegionExpression
     COMMAND_ERROR_IS_FATAL ANY
    )

    and add the directory in src/pyg4ometry/CMakeLists.txt

  4. set a loose version bound to antlr4-python3-runtime in proyect.dependencies (pyproject.toml)
stewartboogert commented 1 year ago

I have realised there is another antler generation required for FLUKA as well as GDML. Did you notice this @gipert?