Skip to content

Commit

Permalink
"Simplify" XGetopt usage
Browse files Browse the repository at this point in the history
When "getopt()" is not available, various of the netcdf-c utilities
use XGetopt instead. This occurs primarily when building under Window,
so the build changes are restricted to CMake.

This PR tries to isolate XGetopt.c to the libdispatch directory
and then builds the various utilities using this cliche:
````
IF(USE_X_GETOPT)
  SET(XGETOPTSRC "${CMAKE_CURRENT_SOURCE_DIR}/../libdispatch/XGetopt.c")
ENDIF()
````

This avoids the need to copy XGetopt.c to all the directories that
use it.
  • Loading branch information
DennisHeimbigner committed Apr 9, 2023
1 parent 43abd69 commit 3765d86
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 63 deletions.
12 changes: 0 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1249,18 +1249,6 @@ IF(MSVC)
OPTION(ENABLE_XGETOPT "Enable bundled XGetOpt instead of external getopt()." ON)
IF(ENABLE_XGETOPT)
SET(USE_X_GETOPT ON CACHE BOOL "")
# Copy XGetopt.c to everywhere it is needed. Avoids
# inconsistent code
FILE(COPY ${netCDF_SOURCE_DIR}/libsrc/XGetopt.c
DESTINATION ${netCDF_BINARY_DIR}/ncgen3/)
FILE(COPY ${netCDF_SOURCE_DIR}/libsrc/XGetopt.c
DESTINATION ${netCDF_BINARY_DIR}/ncgen/)
FILE(COPY ${netCDF_SOURCE_DIR}/libsrc/XGetopt.c
DESTINATION ${netCDF_BINARY_DIR}/ncdump/)
FILE(COPY ${netCDF_SOURCE_DIR}/libsrc/XGetopt.c
DESTINATION ${netCDF_BINARY_DIR}/nczarr_test/)
FILE(COPY ${netCDF_SOURCE_DIR}/libsrc/XGetopt.c
DESTINATION ${netCDF_BINARY_DIR}/ncdap_test/)
ENDIF()
ENDIF()

Expand Down
2 changes: 1 addition & 1 deletion libdispatch/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if REGEDIT
libdispatch_la_SOURCES += dreg.c
endif

EXTRA_DIST=CMakeLists.txt ncsettings.hdr utf8proc_data.c
EXTRA_DIST = CMakeLists.txt ncsettings.hdr utf8proc_data.c XGetopt.c

# Build ncsettings.c as follows:
# 1. copy ncsettings.hdr to ncsettings.c
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion libsrc/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ noinst_LTLIBRARIES = libnetcdf3.la
# with m4), but they are included in the distribution so that the user
# does not have to have m4.
MAINTAINERCLEANFILES = $(man_MANS) attr.c ncx.c putget.c
EXTRA_DIST = attr.m4 ncx.m4 putget.m4 $(man_MANS) CMakeLists.txt XGetopt.c
EXTRA_DIST = attr.m4 ncx.m4 putget.m4 $(man_MANS) CMakeLists.txt

# This tells make how to turn .m4 files into .c files.
.m4.c:
Expand Down
54 changes: 24 additions & 30 deletions ncdump/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,19 @@

SET(RCMERGE OFF)

SET(ncdump_FILES ncdump.c vardata.c dumplib.c indent.c nctime0.c utils.c nciter.c)
SET(nccopy_FILES nccopy.c nciter.c chunkspec.c utils.c dimmap.c list.c)
SET(ocprint_FILES ocprint.c)
SET(ncvalidator_FILES ncvalidator.c)
SET(printfqn_FILES printfqn.c)
SET(ncpathcvt_FILES ncpathcvt.c)
SET(ncfilteravail_FILES ncfilteravail.c)
SET(nchdf5version_FILES nchdf5version.c)

IF(USE_X_GETOPT)
SET(ncdump_FILES ${ncdump_FILES} XGetopt.c)
SET(nccopy_FILES ${nccopy_FILES} XGetopt.c)
SET(ocprint_FILES ${ocprint_FILES} XGetopt.c)
SET(ncvalidator_FILES ${ncvalidator_FILES} XGetopt.c)
SET(printfqn_FILES ${printfqn_FILES} XGetopt.c)
SET(ncpathcvt_FILES ${ncpathcvt_FILES} XGetopt.c)
SET(ncfilteravail_FILES ${ncfilteravail_FILES} XGetopt.c)
ENDIF(USE_X_GETOPT)
SET(XGETOPTSRC "${CMAKE_CURRENT_SOURCE_DIR}/../libdispatch/XGetopt.c")
ENDIF()

SET(ncdump_FILES ncdump.c vardata.c dumplib.c indent.c nctime0.c utils.c nciter.c ${XGETOPTSRC})
SET(nccopy_FILES nccopy.c nciter.c chunkspec.c utils.c dimmap.c list.c ${XGETOPTSRC})
SET(ocprint_FILES ocprint.c ${XGETOPTSRC})
SET(ncvalidator_FILES ncvalidator.c ${XGETOPTSRC})
SET(printfqn_FILES printfqn.c ${XGETOPTSRC})
SET(ncpathcvt_FILES ncpathcvt.c ${XGETOPTSRC})
SET(ncfilteravail_FILES ncfilteravail.c ${XGETOPTSRC})
SET(nchdf5version_FILES nchdf5version.c)

ADD_EXECUTABLE(ncdump ${ncdump_FILES})
ADD_EXECUTABLE(nccopy ${nccopy_FILES})
ADD_EXECUTABLE(ncvalidator ${ncvalidator_FILES})
Expand Down Expand Up @@ -181,21 +175,21 @@ endif()
build_bin_test_no_prefix(tst_h_rdc0)
build_bin_test_no_prefix(tst_unicode)
build_bin_test_no_prefix(tst_vlen_data)
add_bin_test_no_prefix(tst_create_files)
add_bin_test_no_prefix(tst_opaque_data)
add_bin_test_no_prefix(tst_string_data)
add_bin_test_no_prefix(tst_comp2)
add_bin_test_no_prefix(tst_nans)
add_bin_test_no_prefix(tst_h_scalar)
add_bin_test_no_prefix(tst_compress)
add_bin_test_no_prefix(tst_chunking)
add_bin_test_no_prefix(tst_group_data)
add_bin_test_no_prefix(tst_enum_data)
add_bin_test_no_prefix(tst_enum_undef)
add_bin_test_no_prefix(tst_comp)
build_bin_test_no_prefix(tst_create_files)
build_bin_test_no_prefix(tst_opaque_data)
build_bin_test_no_prefix(tst_string_data)
build_bin_test_no_prefix(tst_comp2)
build_bin_test_no_prefix(tst_nans)
build_bin_test_no_prefix(tst_h_scalar)
build_bin_test_no_prefix(tst_compress)
build_bin_test_no_prefix(tst_chunking)
build_bin_test_no_prefix(tst_group_data)
build_bin_test_no_prefix(tst_enum_data)
build_bin_test_no_prefix(tst_enum_undef)
build_bin_test_no_prefix(tst_comp)
# Add this test by hand, as it is also called from a script.
# Editing the script would break autotools compatibility.
add_bin_test_no_prefix(tst_special_atts)
build_bin_test_no_prefix(tst_special_atts)
ENDIF(USE_HDF5)

# Base tests
Expand Down
10 changes: 5 additions & 5 deletions ncgen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ IF(BUILD_SHARED_LIBS AND WIN32)
remove_definitions(-DDLL_NETCDF)
ENDIF()

IF(USE_X_GETOPT)
SET(XGETOPTSRC "${CMAKE_CURRENT_SOURCE_DIR}/../libdispatch/XGetopt.c")
ENDIF()

SET(ncgen_FILES bindata.c bytebuffer.c cdata.c cvt.c data.c
debug.c dump.c escapes.c f77data.c genbin.c
genc.c genchar.c generate.c generr.c genf77.c
genj.c genlib.c getfill.c jdata.c list.c
main.c ncgeny.c semantics.c
util.c bytebuffer.h data.h debug.h dump.h
generate.h generr.h genlib.h includes.h list.h
ncgen.h ncgeny.h util.h)
ncgen.h ncgeny.h util.h ${XGETOPTSRC})

# Obsolete
SET(OBSOLETE odom.c odom.h jdatastd.c jdatajni.c genjni.c cdfdata.c cmldata.c)

IF(USE_X_GETOPT)
SET(ncgen_FILES ${ncgen_FILES} XGetopt.c)
ENDIF()

# don't add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
Expand Down
7 changes: 4 additions & 3 deletions ncgen3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
# 2015, 2016, 2017, 2018
# University Corporation for Atmospheric Research/Unidata.

# See netcdf-c/COPYRIGHT file for more info.
SET(ncgen3_FILES main.c load.c escapes.c getfill.c init.c genlib.c ncgeny.c)

IF(USE_X_GETOPT)
SET(ncgen3_FILES ${ncgen3_FILES} XGetopt.c)
SET(XGETOPTSRC "${CMAKE_CURRENT_SOURCE_DIR}/../libdispatch/XGetopt.c")
ENDIF()

# See netcdf-c/COPYRIGHT file for more info.
SET(ncgen3_FILES main.c load.c escapes.c getfill.c init.c genlib.c ncgeny.c ${XGETOPTSRC})

FILE(GLOB COPY_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.nc ${CMAKE_CURRENT_SOURCE_DIR}/*.sh ${CMAKE_CURRENT_SOURCE_DIR}/*.cdl)
FILE(COPY ${COPY_FILES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ FILE_PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE)

Expand Down
18 changes: 7 additions & 11 deletions nczarr_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ ${CMAKE_CURRENT_SOURCE_DIR}/ref*.zmap)

FILE(COPY ${COPY_FILES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ FILE_PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE)

IF(USE_X_GETOPT)
SET(XGETOPTSRC "${CMAKE_CURRENT_SOURCE_DIR}/../libdispatch/XGetopt.c")
ENDIF()

IF(ENABLE_TESTS)

SET(COMMONSRC ut_util.c ut_test.c)
IF(USE_X_GETOPT)
SET(COMMONSRC ${COMMONSRC} XGetopt.c)
ENDIF()
SET(COMMONSRC ut_util.c ut_test.c ${XGETOPTSRC})

SET(TSTCOMMONSRC tst_utils.c)
IF(USE_X_GETOPT)
SET(TSTCOMMONSRC ${TSTCOMMONSRC} XGetopt.c)
ENDIF()
SET(TSTCOMMONSRC tst_utils.c ${XGETOPTSRC})

# Base tests
# The tests are set up as a combination of shell scripts and executables that
Expand Down Expand Up @@ -91,9 +89,7 @@ IF(ENABLE_TESTS)
endif()

SET(ncdumpchunks_SOURCE ncdumpchunks.c)
IF(USE_X_GETOPT)
SET(ncdumpchunks_SOURCE ${ncdumpchunks_SOURCE} XGetopt.c)
ENDIF()
SET(ncdumpchunks_SOURCE ${ncdumpchunks_SOURCE} ${XGETOPTSRC})
BUILD_BIN_TEST(ncdumpchunks ${ncdumpchunks_SOURCE})
TARGET_INCLUDE_DIRECTORIES(ncdumpchunks PUBLIC ../libnczarr)

Expand Down

0 comments on commit 3765d86

Please sign in to comment.