# # $Id: try.in,v 1.11 2004/05/08 20:12:55 jmmv Exp $ # Compile and run C/C++ test programs. # # buildtool # Copyright (c) 2003, 2004 Julio M. Merino Vidal. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # 3. Neither the name of the author nor the names of contributors may # be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # ------------------------------------------------------------------------- # # bt_try_init # Initialize the header files for the tests. # BT_INIT_HOOKS+=bt_try_init bt_try_init() { BT_TRY_TESTHDR_C=${BT_WORKDIR}/testhdr.c BT_TRY_TESTHDR_CXX=${BT_WORKDIR}/testhdr.cc rm -f ${BT_TRY_TESTHDR_C} ${BT_TRY_TESTHDR_CXX} touch ${BT_TRY_TESTHDR_C} ${BT_TRY_TESTHDR_CXX} } # ------------------------------------------------------------------------- # # bt_try_finish # Remove the temporary files containing definitions for tests. # BT_FINISH_HOOKS+=bt_try_finish bt_try_finish() { rm -f ${BT_TRY_TESTHDR_C} ${BT_TRY_TESTHDR_CXX} rm -f ${BT_TRY_TESTHDR_C}.old ${BT_TRY_TESTHDR_CXX}.old } # ------------------------------------------------------------------------- # # bt_try_hdr_append_c # Append the contents from stdin to the C test header file. # bt_try_hdr_append_c() { cat >> ${BT_TRY_TESTHDR_C} } # ------------------------------------------------------------------------- # # bt_try_hdr_append_cxx # Append the contents from stdin to the C++ test header file. # bt_try_hdr_append_cxx() { cat >> ${BT_TRY_TESTHDR_CXX} } # ------------------------------------------------------------------------- # # bt_try_hdr_save_c # Save a copy of the current C test header so that it can be modified # before a test and restored afterwards in case of failure. # Should not be needed in most cases. # bt_try_hdr_save_c() { cp ${BT_TRY_TESTHDR_C} ${BT_TRY_TESTHDR_C}.old } # ------------------------------------------------------------------------- # # bt_try_hdr_save_cxx # Save a copy of the current C++ test header so that it can be modified # before a test and restored afterwards in case of failure. # Should not be needed in most cases. # bt_try_hdr_save_cxx() { cp ${BT_TRY_TESTHDR_CXX} ${BT_TRY_TESTHDR_CXX}.old } # ------------------------------------------------------------------------- # # bt_try_hdr_restore_c # Restore a previous copy of the C test header. # bt_try_hdr_restore_c() { mv ${BT_TRY_TESTHDR_C}.old ${BT_TRY_TESTHDR_C} } # ------------------------------------------------------------------------- # # bt_try_hdr_restore_cxx # Restore a previous copy of the C++ test header. # bt_try_hdr_restore_cxx() { mv ${BT_TRY_TESTHDR_CXX}.old ${BT_TRY_TESTHDR_CXX} } # ------------------------------------------------------------------------- # # bt_try_hdr_zap_c # Clear the contents of the C test header file. # bt_try_hdr_zap_c() { rm -f ${BT_TRY_TESTHDR_C} touch ${BT_TRY_TESTHDR_C} } # ------------------------------------------------------------------------- # # bt_try_hdr_zap_cxx # Clear the contents of the C++ test header file. # bt_try_hdr_zap_cxx() { rm -f ${BT_TRY_TESTHDR_CXX} touch ${BT_TRY_TESTHDR_CXX} } # ------------------------------------------------------------------------- # # bt_try_compile_c # Compiles the C program given through standard input. # Returns 0 on success, 1 if compilation failed. # bt_try_compile_c() { btown_try_c compile; } # ------------------------------------------------------------------------- # # bt_try_compile_c # Compiles the C++ program given through standard input. # Returns 0 on success, 1 if compilation failed. # bt_try_compile_cxx() { btown_try_cxx compile; } # ------------------------------------------------------------------------- # # bt_try_run_c # Compiles and executes the C program given through standard input. # If compilation fails, the configuration process is aborted. # Otherwise, the exit status of the program is returned. # bt_try_run_c() { btown_try_c run; } # ------------------------------------------------------------------------- # # bt_try_run_cxx # Compiles and executes the C++ program given through standard input. # If compilation fails, the configuration process is aborted. # Otherwise, the exit status of the program is returned. # bt_try_run_cxx() { btown_try_cxx run; } # ------------------------------------------------------------------------- # # btown_try_c compile|run # Helper function used by bt_try_compile_c and bt_try_run_c. # btown_try_c() { local action=$1 cmd exitstat hdr cp ${BT_TRY_TESTHDR_C} ${BT_WORKDIR}/bt_test.c if [ -n "${BT_INCLUDE_FILES}" ]; then # XXX: Compatibility stuff - to be removed. echo 1>&2 btcmn_warn "usage of BT_INCLUDE_FILES is deprecated!" \ "switch your code to use the bt_try_append_c function" for hdr in ${BT_INCLUDE_FILES}; do echo "#include <${hdr}>" >> ${BT_WORKDIR}/bt_test.cc done fi cat >> ${BT_WORKDIR}/bt_test.c cmd="${BT_PROG_CC} ${BT_FLAGS_CPP} ${BT_FLAGS_CC} ${BT_FLAGS_LD}" cmd+="${BT_LIBS} -o ${BT_WORKDIR}/bt_test ${BT_WORKDIR}/bt_test.c" echo "BUILDING C TEST PROGRAM: ${cmd}" >> $(btcmn_log_name) ${cmd} >> $(btcmn_log_name) 2>&1 if [ $? -eq 0 ]; then rm -f ${BT_WORKDIR}/bt_test.c if [ ${action} = compile ]; then rm -f ${BT_WORKDIR}/bt_test return 0 else ${BT_WORKDIR}/bt_test exitstat=$? rm -f ${BT_WORKDIR}/bt_test return ${exitstat} fi else echo "C TEST PROGRAM FAILED:" >> $(btcmn_log_name) cat ${BT_WORKDIR}/bt_test.c >> $(btcmn_log_name) rm -f ${BT_WORKDIR}/bt_test.c ${BT_WORKDIR}/bt_test if [ ${action} = compile ]; then return 1 else btcmn_err "failed to run the C test program" \ "check $(btcmn_log_name) to see what happened" fi fi } # ------------------------------------------------------------------------- # # btown_try_cxx compile|run # Helper function used by bt_try_compile_cxx and bt_try_run_cxx. # btown_try_cxx() { local action=$1 cmd exitstat hdr cp ${BT_TRY_TESTHDR_CXX} ${BT_WORKDIR}/bt_test.cc if [ -n "${BT_INCLUDE_FILES}" ]; then # XXX: Compatibility stuff - to be removed. echo 1>&2 btcmn_warn "usage of BT_INCLUDE_FILES is deprecated!" \ "switch your code to use the bt_try_append_cxx function" for hdr in ${BT_INCLUDE_FILES}; do echo "#include <${hdr}>" >> ${BT_WORKDIR}/bt_test.cc done fi cat >> ${BT_WORKDIR}/bt_test.cc cmd="${BT_PROG_CXX} ${BT_FLAGS_CPP} ${BT_FLAGS_CXX} ${BT_FLAGS_LD}" cmd+="${BT_LIBS} -o ${BT_WORKDIR}/bt_test ${BT_WORKDIR}/bt_test.cc" echo "BUILDING C++ TEST PROGRAM: ${cmd}" >> $(btcmn_log_name) ${cmd} >> $(btcmn_log_name) 2>&1 if [ $? -eq 0 ]; then rm -f ${BT_WORKDIR}/bt_test.cc if [ ${action} = compile ]; then rm -f ${BT_WORKDIR}/bt_test return 0 else ${BT_WORKDIR}/bt_test exitstat=$? rm -f ${BT_WORKDIR}/bt_test return ${exitstat} fi else echo "C++ TEST PROGRAM FAILED:" >> $(btcmn_log_name) cat ${BT_WORKDIR}/bt_test.cc >> $(btcmn_log_name) rm -f ${BT_WORKDIR}/bt_test.cc ${BT_WORKDIR}/bt_test if [ ${action} = compile ]; then return 1 else btcmn_err "failed to run the C test program" \ "check $(btcmn_log_name) to see what happened" fi fi } # Local Variables: *** # mode: shell-script *** # End: *** # vim: syntax=sh