1111# help@hdfgroup.org.
1212# #
1313
14+ # Constants definitions
15+ EXIT_SUCCESS=0
16+ EXIT_FAILURE=1
17+
18+ # Variables to control final behavior of script
19+ do_show=" no"
20+ do_compile=" yes"
21+ do_link=" yes"
22+
23+ # Add the HDF5 library directory to the rpath of the
24+ # built executable/library by default
25+ add_rpath=" yes"
26+
27+ # Variables for arguments passed to pkg-config and compiler
28+ pc_args=" ${HDF5_PKG_CONFIG_ARGS} "
29+ compile_args=" "
30+ prefix=" "
31+ libdir=" "
32+
33+ # Variables for flags passed to pkg-config and compiler
34+ pc_flags=" "
35+
36+ status=$EXIT_SUCCESS
37+
38+ # Determine name and location of script
1439prg=$0
1540if [ ! -e " $prg " ]; then
1641 case $prg in
2146dir=$(
2247 cd -P -- " $( dirname -- " $prg " ) /.." && pwd -P
2348) || exit
24- prg=$dir /bin/$( basename -- " $prg " ) || exit
2549
26- # printf '%s\n' "$prg"
27- # printf 'dir is %s\n' "$dir"
50+ prog_name=$( basename -- " $prg " ) || exit
51+ prg=$dir /bin/$prog_name
52+
53+ prefix=$dir
2854
29- pc_args=" "
55+ libdir=$dir /@HDF5_INSTALL_LIB_DIR@
56+
57+ export PKG_CONFIG_PATH=$libdir /pkgconfig
3058
3159# Show the configuration summary of the library recorded in the
3260# libhdf5.settings file residing in the lib directory.
@@ -38,50 +66,214 @@ showconfigure()
3866
3967usage () {
4068 # "How-to use" message.
41- echo " usage: $prg [OPTIONS] <pkg-config line>"
69+ echo " usage: $prog_name [OPTIONS] <compile line>"
4270 echo " OPTIONS:"
43- echo " -help This help message."
44- echo " -show Show the commands without executing them"
45- echo " -showconfig Show the HDF5 library configuration summary"
71+ echo " -help | --help | -h This help message."
72+ echo " -echo Show all the shell commands executed"
73+ echo " -show Show the commands without executing them"
74+ echo " -showconfig Show the HDF5 library configuration summary"
75+ echo " -shlib Compile with shared HDF5 libraries [default for"
76+ echo " hdf5 built with shared libraries]"
77+ echo " -noshlib Compile with static HDF5 libraries [default for"
78+ echo " hdf5 built without shared libraries]"
79+ echo " -nohl By default, $prog_name builds and links against the"
80+ echo " high-level HDF5 library, if it is available."
81+ echo " Specifying the -nohl option disables this behavior."
82+ echo " -norpath By default, $prog_name adds the library directory"
83+ echo " of the HDF5 installation to the rpath of the resulting"
84+ echo " executable/library when linking against shared HDF5"
85+ echo " libraries. If this behavior is not desired or is"
86+ echo " problematic for a particular compiler/linker, specifying"
87+ echo " the -norpath option disables this behavior."
88+ echo " -c Compile and assemble without linking. When combined"
89+ echo " with -show, displays only the compile flags without"
90+ echo " displaying the linking flags. Otherwise, passes the"
91+ echo " '-c' flag to the compiler, where it's assumed that"
92+ echo " the flag will have the behavior of 'compile and"
93+ echo " assemble without linking'. Note that this may not"
94+ echo " be the case for some compilers."
4695 echo " "
47- echo " <pkg-config line> - the pkg-config compile line options for the compiler"
48- echo " that was used to compile HDF5. "
49- echo " Use pkg-config --help for more information "
50- echo " on which options are available. $prg passes pkg-config options "
51- echo " through as those options use double-underscores ."
96+ echo " <compile line> - the normal compile line options for your compiler. "
97+ echo " $prog_name uses the same compiler you used to compile"
98+ echo " HDF5, unless an alternate compiler is specified at "
99+ echo " configuration time. Check with your compiler's man "
100+ echo " pages for more information on which options are needed ."
52101 echo " "
53- echo " NOTE: pkg-config is required to be installed on your system and"
54- echo " using --static requires a static version of the C runtime library"
55- echo " to be have been installed on your system."
102+ echo " All options specific to $prog_name should appear BEFORE"
103+ echo " any options specified for <compile line>."
104+ echo " "
105+ echo " You can override whether or not to use shared or static HDF5 libraries"
106+ echo " by setting the following environment variable:"
107+ echo " "
108+ echo " HDF5_USE_SHLIB=[yes|no] - use shared or static version of the HDF5 library."
109+ echo " precedence is given to the -shlib/-noshlib options."
110+ echo " [default: yes except when built with only"
111+ echo " static libraries]"
112+ echo " "
113+ echo " $prog_name uses pkg-config to determine additional flags to pass to the"
114+ echo " compiler. You can specify any options to pass to pkg-config by setting the"
115+ echo " following environment variable:"
116+ echo " "
117+ echo " HDF5_PKG_CONFIG_ARGS"
56118 echo " "
57- exit $EXIT_FAILURE
58119}
59120
60- export PKG_CONFIG_PATH=$dir /@HDF5_INSTALL_LIB_DIR@/pkgconfig
121+ if test $# -eq 0; then
122+ # No parameters specified, issue usage statement and exit.
123+ usage
124+ exit $EXIT_FAILURE
125+ fi
126+
127+ # If a shared library is available, the default will be to use it. If the only
128+ # available library is static, it will be used by default. The user can
129+ # override either default, although choosing an unavailable library will result
130+ # in errors from h5cc.
131+ SHARED_AVAILABLE=" @BUILD_SHARED_LIBS@"
132+ STATIC_AVAILABLE=" @BUILD_STATIC_LIBS@"
133+ if test " ${SHARED_AVAILABLE} " = " ON" ; then
134+ USE_SHARED_LIB=" ${HDF5_USE_SHLIB:- yes} "
135+ else
136+ USE_SHARED_LIB=" ${HDF5_USE_SHLIB:- no} "
137+ fi
138+
139+ # If the high-level library is available, the default will be to build and
140+ # link against it.
141+ USE_HL_LIB=" yes"
142+ HL_AVAILABLE=" @HDF5_BUILD_HL_LIB@"
143+ if test " ${HL_AVAILABLE} " = " ON" ; then
144+ HL_LIB_NAME=" @_PKG_CONFIG_HL_LIBNAME@"
145+ else
146+ USE_HL_LIB=" no"
147+ fi
61148
62149for arg in $@ ; do
63150 case " $arg " in
151+ -help|--help|-h)
152+ usage
153+ exit $EXIT_SUCCESS
154+ ;;
155+ -echo)
156+ shift
157+ set -x
158+ ;;
159+ -show)
160+ shift
161+ # Delay showing command-line until arguments have been processed
162+ do_show=" yes"
163+ ;;
64164 -showconfig)
65165 showconfigure
66166 exit $status
67167 ;;
68- -show )
168+ -shlib )
69169 shift
70- echo @_PKG_CONFIG_COMPILER@ " $@ " ` pkg-config $pc_args --define-variable=prefix=$dir --cflags --libs @_PKG_CONFIG_LIBNAME@`
71- exit $status
170+ USE_SHARED_LIB=" yes"
72171 ;;
73- -help)
74- usage
75- exit $status
172+ -noshlib)
173+ shift
174+ USE_SHARED_LIB=" no"
175+ ;;
176+ -nohl)
177+ shift
178+ USE_HL_LIB=" no"
76179 ;;
77- --* )
78- # gather pkg-config specific options
79- pc_args=" $pc_args $arg "
180+ -norpath)
181+ shift
182+ add_rpath=" no"
183+ ;;
184+ -c)
185+ compile_args=" ${compile_args: +${compile_args} } $arg "
186+ shift
187+
188+ do_link=" no"
80189 ;;
81190 * )
82- @_PKG_CONFIG_COMPILER@ " $@ " ` pkg-config $pc_args --define-variable=prefix=$dir --cflags --libs @_PKG_CONFIG_LIBNAME@`
83- status=$?
84- exit $status
191+ # For now, any unrecognized arguments are passed directly on to the
192+ # compiler to simplify handling of them. It's assumed that any
193+ # h5cc-specific options come before these arguments. If this proves
194+ # to be insufficient, arguments should be stored somewhere and
195+ # shifted off the command-line.
85196 ;;
86197 esac
87198done
199+
200+ # Check availability of default or requested HDF5 library type
201+ if test " ${USE_SHARED_LIB} " = " yes" -a " ${SHARED_AVAILABLE} " ! = " ON" ; then
202+ echo " error: linking against shared HDF5 libraries was requested, but shared HDF5 libraries aren't available" >&2
203+ exit $EXIT_FAILURE
204+ elif test " ${USE_SHARED_LIB} " = " no" -a " ${STATIC_AVAILABLE} " ! = " ON" ; then
205+ echo " error: linking against static HDF5 libraries was requested, but static HDF5 libraries aren't available" >&2
206+ exit $EXIT_FAILURE
207+ fi
208+
209+ # Add in relevant flags to pkg-config command
210+ pc_args=" ${pc_args: +${pc_args} } --define-variable=prefix=$prefix "
211+
212+ if test " $do_compile " = " yes" ; then
213+ pc_args=" ${pc_args: +${pc_args} } --cflags"
214+ fi
215+
216+ if test " $do_link " = " yes" ; then
217+ pc_args=" ${pc_args: +${pc_args} } --libs"
218+ fi
219+
220+ if test " $USE_SHARED_LIB " = " no" ; then
221+ pc_args=" ${pc_args: +${pc_args} } --static"
222+ fi
223+
224+ pc_args=" ${pc_args: +${pc_args} } @_PKG_CONFIG_LIBNAME@"
225+
226+ # Build and link against high-level library,
227+ # unless requested not to.
228+ if test " ${USE_HL_LIB} " = " yes" ; then
229+ pc_args=" ${pc_args: +${pc_args} }${HL_LIB_NAME} "
230+ fi
231+
232+ # Evaluate pkg-config command for later use
233+ pc_flags=" ` pkg-config $pc_args ` "
234+ if test $? -ne 0; then
235+ echo " pkg-config failed with arguments: $pc_args " >&2
236+ exit $EXIT_FAILURE
237+ fi
238+
239+ # If linking against static HDF5 libraries, fixup the
240+ # flags generated by pkg-config to point to the static
241+ # libraries
242+ if test " $do_link " = " yes" -a " $USE_SHARED_LIB " = " no" ; then
243+ # Escape '/' in libdir path to not conflict with sed delimiter
244+ escaped_libdir=$( echo " $libdir " | sed ' s/\//\\\//g' )
245+
246+ # Replace flags of form '-lhdf5*' with path to matching static library
247+ edited_pc_flags=$( echo " $pc_flags " | sed -E ' s/-l(hdf5[a-z0-9_]*)/' " ${escaped_libdir} \/" ' lib\1.a/g' )
248+ if test $? -ne 0; then
249+ echo " couldn't edit flags to compile against static HDF5 libraries" >&2
250+ exit $EXIT_FAILURE
251+ fi
252+
253+ pc_flags=$edited_pc_flags
254+ fi
255+
256+ # If linking against shared HDF5 libraries, add the
257+ # HDF5 library directory to the rpath of the executable
258+ # if not requested not to.
259+ if test " $do_link " = " yes" -a " $USE_SHARED_LIB " = " yes" -a " $add_rpath " = " yes" ; then
260+ # Simply assume that the -Wl,-rpath,<dir> form is acceptable
261+ # for the compiler and -rpath is a linker option. Add
262+ # compiler/linker-specific logic as needed.
263+ compile_args=" ${compile_args: +${compile_args} } -Wl,-rpath,$libdir "
264+ fi
265+
266+ # If -show was passed, only output the command-line, don't execute it
267+ if test " $do_show " = " yes" ; then
268+ echo @_PKG_CONFIG_COMPILER@ ${compile_args: +" ${compile_args} " } " $@ " " $pc_flags "
269+ exit $EXIT_SUCCESS
270+ fi
271+
272+ @_PKG_CONFIG_COMPILER@ $compile_args " $@ " $pc_flags
273+ status=$?
274+
275+ if test $status -ne 0; then
276+ echo " compile failed with command: @_PKG_CONFIG_COMPILER@ $compile_args $@ $pc_flags " >&2
277+ fi
278+
279+ exit $status
0 commit comments