Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake: Add option to always automatically regenerate ncgen source #2822

Merged
merged 5 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ endif(NOT ENABLE_V2_API)

# Option to build utilities
option(NETCDF_BUILD_UTILITIES "Build ncgen, ncgen3, ncdump." ON)
option(NETCDF_GENERATE_NCGEN "Automatically regenerate ncgen C files if required" OFF)


# Option to use MMAP
option(NETCDF_ENABLE_MMAP "Use MMAP." ON)
Expand Down
47 changes: 26 additions & 21 deletions ncgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,32 @@ ENDIF()
SET(CLEANFILES c0.nc c0_64.nc c0_4.nc c0_4c.nc ref_camrun.c)
SET_DIRECTORY_PROPERTIES(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEANFILES}")

# These rules are used if someone wants to rebuild ncgeny.c or ncgenl.c
# Otherwise never invoked, but records how to do it.
# BTW: note that renaming is essential because otherwise
# autoconf will forcibly delete files of the name *.tab.*

ADD_CUSTOM_COMMAND(
OUTPUT ncgentab.h
COMMAND flex -Pncg -8 ncgen.l
COMMAND rm -f ncgenl.c
COMMAND mv lex.ncg.c ncgenl.c

COMMAND bison -pncg -t -d ncgen.y
COMMAND rm -f ncgeny.c ncgeny.h
COMMAND mv ncgen.tab.c ncgeny.c
COMMAND mv ncgen.tab.h ncgeny.h
COMMAND mv ncgeny.h ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND mv ncgeny.c ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND mv ncgenl.c ${CMAKE_CURRENT_SOURCE_DIR}

)
ADD_CUSTOM_TARGET(makeparser DEPENDS ncgentab.h)
# With this option enabled, automatically generate the parser source
# files from the yacc input files when they're changed. They don't
# change very often, so this option is off by default.
if (NETCDF_GENERATE_NCGEN)
find_program(FLEX flex REQUIRED)
find_program(BISON bison REQUIRED)

add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/ncgeny.h ${CMAKE_CURRENT_SOURCE_DIR}/ncgeny.c ${CMAKE_CURRENT_SOURCE_DIR}/ncgenl.c
DEPENDS ncgen.y ncgen.l
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}

COMMAND ${FLEX} -Pncg -8 ncgen/ncgen.l
COMMAND mv lex.ncg.c "${CMAKE_CURRENT_SOURCE_DIR}/ncgenl.c"

COMMAND ${BISON} -pncg -t -d ncgen/ncgen.y
COMMAND mv ncgen.tab.c "${CMAKE_CURRENT_SOURCE_DIR}/ncgeny.c"
COMMAND mv ncgen.tab.h "${CMAKE_CURRENT_SOURCE_DIR}/ncgeny.h"

# Fix the `#line` preprocessor directives with the correct source paths
COMMAND sed -i s/ncgen.tab/ncgeny/ "${CMAKE_CURRENT_SOURCE_DIR}/ncgeny.c"
COMMAND sed -i s/lex.ncg/ncgenl/ "${CMAKE_CURRENT_SOURCE_DIR}/ncgenl.c"
COMMAND sed -i s/ncgen.tab/ncgeny/ "${CMAKE_CURRENT_SOURCE_DIR}/ncgeny.h"
VERBATIM
)
endif()

## Specify files to be distributed by 'make dist'
FILE(GLOB CUR_EXTRA_DIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.c ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.sh)
Expand Down
Loading
Loading