Skip to content

Commit c4cc1eb

Browse files
committed
Improve windows builds and cafrun wrapper
- Fixes #457 - Incorporate most of @jbmaggard's patches for windows builds
1 parent 95093f3 commit c4cc1eb

File tree

6 files changed

+112
-23
lines changed

6 files changed

+112
-23
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ function(add_caf_test name num_caf_img test_target)
606606
endif()
607607
endif()
608608
set(test_parameters -np ${num_caf_img} ${test_parameters})
609-
add_test(NAME ${name} COMMAND "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/cafrun" ${test_parameters} "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${test_target}")
609+
add_test(NAME ${name} COMMAND "bash" "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/cafrun" ${test_parameters} "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${test_target}")
610610
set_property(TEST ${name} PROPERTY PASS_REGULAR_EXPRESSION "Test passed.")
611611
endfunction(add_caf_test)
612612

prerequisites/install-functions/build_opencoarrays.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ build_opencoarrays()
1313
if [[ -z ${MPIFC:-} || -z ${MPICC:-} ]]; then
1414
emergency "build_opencoarrays.sh: empty \${MPIFC}=${MPIFC:-} or \${MPICC}=${MPICC:-}"
1515
fi
16-
MPIFC_show=($($MPIFC -show))
17-
MPICC_show=($($MPICC -show))
16+
MPIFC_show=($("$MPIFC" -show))
17+
MPICC_show=($("$MPICC" -show))
1818
if [[ "${MPIFC_show[0]}" != *gfortran* || "${MPICC_show[0]}" != *gcc* ]]; then
1919
emergency "build_opencoarrays.sh: MPI doesn't wrap gfortran/gcc: \${MPIFC_show}=${MPIFC_show[*]}, \${MPICC_show}=${MPICC_show[*]}"
2020
fi

prerequisites/install-functions/find_or_install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ find_or_install()
212212

213213
# Check consistency of MPIFC, if set, and user-specified Fortran compiler
214214
if [[ ! -z ${MPIFC:-} && ! -z "${arg_f:-}" ]]; then
215-
MPIFC_wraps=$(${MPIFC} --version)
215+
MPIFC_wraps=$("${MPIFC}" --version)
216216
compiler=$(${arg_f} --version)
217217
if [[ "${MPIFC_wraps}" != "${compiler}" ]]; then
218218
emergency "Specified MPI ${MPIFC_wraps} wraps a compiler other than the specified Fortran compiler ${compiler}"

src/extensions/caf.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ cmd="${__base}"
122122
# mac OS doesn't have readlink -f, so need to crawl symlinks manually.
123123
# See: https://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
124124
current_dir="${PWD}"
125-
cd "${__dir}"
125+
cd "${__dir}" || exit 5
126126
target_file="${__file}"
127127
max_iter=1000
128128
iter=0
129129
while [[ -L "${target_file}" ]]; do
130130
target_file="$(readlink "${target_file}")"
131-
cd "$(dirname "${target_file}")"
131+
cd "$(dirname "${target_file}")" || exit 5
132132
target_file="$(basename "${target_file}")"
133133
if ((++iter >= max_iter)); then
134134
echo "Failed to compute OpenCoarrays instalation prefix!" >&2
@@ -142,7 +142,7 @@ cd "${__dir}"
142142
# shellcheck disable=SC2034
143143
prefix="${true_dir%/bin}"
144144
# echo "Install prefix is ${prefix}" # for debugging
145-
cd "${current_dir}"
145+
cd "${current_dir}" || exit 5
146146
# echo "Current directory is $(pwd)"
147147
# echo "This script is in ${__dir}"
148148

src/extensions/cafrun.in

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@
3434
# 2. The environment variable "FC" is used to determine the identity fo the Fortran compiler/linker.
3535
# 3. If "FC" is empty, a default value of "mpifort" is used.
3636

37+
# Exit on error. Append "|| true" if you expect an error.
38+
set -o errexit
39+
# Exit on error inside any functions or subshells.
40+
set -o errtrace
41+
# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
42+
set -o nounset
43+
# Catch the error in case mysqldump fails (but gzip succeeds) in `mysqldump |gzip`
44+
set -o pipefail
45+
# Turn on traces, useful while debugging but commented out by default
46+
# set -o xtrace
47+
3748

3849
#---------------------
3950
# Configured variables
@@ -44,8 +55,8 @@
4455
#
4556

4657
caf_version='@CAF_VERSION@'
47-
CAFRUN='@MPIEXEC@'
48-
if [[ ${CAFRUN} == @*@ ]]; then
58+
CAFRUN="@MPIEXEC@"
59+
if [[ "${CAFRUN}" == @*@ ]]; then
4960
CAFRUN=mpiexec
5061
fi
5162
have_failed_img=@HAVE_FAILED_IMG@
@@ -56,17 +67,82 @@ numproc_flag='@MPIEXEC_NUMPROC_FLAG@'
5667
if [[ ${numproc_flag} == @*@ ]]; then
5768
numproc_flag='-np'
5869
fi
59-
preflags='@MPIEXEC_PREFLAGS@'
60-
if [[ ${preflags} == @*@ ]]; then
70+
preflags="@MPIEXEC_PREFLAGS@"
71+
if [[ "${preflags}" == @*@ ]]; then
6172
unset preflags
6273
fi
63-
postflags='@MPIEXEC_POSTFLAGS@'
64-
if [[ ${postflags} == @*@ ]]; then
74+
postflags="@MPIEXEC_POSTFLAGS@"
75+
if [[ "${postflags}" == @*@ ]]; then
6576
unset postflags
6677
fi
78+
#-------------------------
79+
# End configured variables
80+
#-------------------------
81+
82+
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
83+
__i_am_main_script="0" # false
84+
85+
# shellcheck disable=SC2154
86+
if [[ "${__usage+x}" ]]; then
87+
if [[ "${BASH_SOURCE[1]}" == "${0}" ]]; then
88+
__i_am_main_script="1" # true
89+
fi
90+
__caf_tmp_source_idx=1
91+
fi
92+
else
93+
# shellcheck disable=SC2034
94+
__i_am_main_script="1" # true
95+
fi
96+
97+
# Set magic variables for current file, directory, os, etc.
98+
__dir="$(cd "$(dirname "${BASH_SOURCE[${__caf_tmp_source_idx:-0}]}")" && pwd)"
99+
__file="${__dir}/$(basename "${BASH_SOURCE[${__caf_tmp_source_idx:-0}]}")"
100+
# shellcheck disable=SC2034
101+
__base="$(basename "${__file}")"
102+
cmd="${__base}"
103+
104+
# Set installation prefix. Compute this dynamically assuming this script is in a bin/ subdir
105+
# Dereference symbolic links and canonicalize (i.e., abs path) prefix in case stow, homebrew, etc being used
106+
# mac OS doesn't have readlink -f, so need to crawl symlinks manually.
107+
# See: https://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
108+
current_dir="${PWD}"
109+
cd "${__dir}" || exit 5
110+
target_file="${__file}"
111+
max_iter=1000
112+
iter=0
113+
while [[ -L "${target_file}" ]]; do
114+
target_file="$(readlink "${target_file}")"
115+
cd "$(dirname "${target_file}")" || exit 5
116+
target_file="$(basename "${target_file}")"
117+
if ((++iter >= max_iter)); then
118+
echo "Failed to compute OpenCoarrays instalation prefix!" >&2
119+
echo "Likely cause: circular symlink cycles." >&2
120+
echo "Aborting!" >&2
121+
exit 200
122+
fi
123+
done
124+
true_dir="$(pwd -P)"
125+
true_dir="${true_dir%/}"
126+
# shellcheck disable=SC2034
127+
prefix="${true_dir%/bin}"
128+
# echo "Install prefix is ${prefix}" # for debugging
129+
cd "${current_dir}" || exit 5
130+
# echo "Current directory is $(pwd)"
131+
# echo "This script is in ${__dir}"
132+
133+
# Error tracing
134+
# requires `set -o errtrace`
135+
__caf_err_report() {
136+
local error_code
137+
error_code=${?}
138+
echo "Error in ${__file} in function ${1} on line ${2}. Please report this error at http://bit.ly/OpenCoarrays-new-issue" >&2
139+
exit ${error_code}
140+
}
141+
# Always provide an error backtrace
142+
trap '__caf_err_report "${FUNCNAME:-.}" ${LINENO}' ERR
143+
67144

68145
usage() {
69-
cmd="$(basename "${0}")"
70146
echo ""
71147
echo " ${cmd} - Coarray Fortran executable launcher for OpenCoarrays"
72148
echo ""
@@ -75,7 +151,7 @@ usage() {
75151
echo " Options:"
76152
echo " --help, -h Show this help message"
77153
echo " --version, -v, -V Report version and copyright information"
78-
echo " --wraps, -w, Report the name of the wrapped compiler"
154+
echo " --wraps, -w, Report info about the wrapped MPI launcher"
79155
echo " -np <N>, Number of images, N, to execute, N must be a positive integer"
80156
echo " --reenable-auto-cleanup Turn off failed images support (if library support is present)"
81157
echo " This option re-enables MPI auto cleanup, which is disabled by"
@@ -84,6 +160,9 @@ usage() {
84160
echo " failed image support is present, OpenCoarrays triggers cleanup"
85161
echo " explicitly when a failed/stopped image is encountered in an"
86162
echo " image control statement without a \`stat=\` clause."
163+
ehco " --show, -s, Show the command that the wrapper will execute. You can pass"
164+
echo " this as the first argument and then the additional arguments"
165+
echo " that you're planning to pass to perform a dry run."
87166
echo ""
88167
echo " Example usage:"
89168
echo ""

src/mpi/CMakeLists.txt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ CHECK_SYMBOL_EXISTS(SIGKILL "signal.h" HAVE_SIGKILL)
4949
if(NOT HAVE_SIGKILL) # try -D_POSIX, needed for mingw-w64, maybe others, see #435
5050
# https://github.com/sourceryinstitute/OpenCoarrays/issues/435#issuecomment-323592433
5151
list( APPEND CMAKE_REQUIRED_DEFINITIONS -D_POSIX)
52-
CHECK_SYMBOL_EXISTS(SIGKILL "signal.h" HAVE_SIGKILL)
53-
if(HAVE_SIGKILL)
52+
CHECK_SYMBOL_EXISTS(SIGKILL "signal.h" HAVE_SIGKILL2)
53+
if(HAVE_SIGKILL2)
54+
set(HAVE_SIGKILL ${HAVE_SIGKILL2})
5455
add_definitions(-D_POSIX)
5556
endif()
5657
endif()
@@ -115,11 +116,18 @@ endif()
115116
#---------------------------------------------------
116117
# Windows Intel MPI compatibility, see GH issue #435
117118
#---------------------------------------------------
118-
CHECK_SYMBOL_EXISTS(I_MPI_VERSION mpi.h HAVE_Intel_MPI)
119+
set(old_cmake_required_includes "${CMAKE_REQUIRED_INCLUDES}")
120+
if(CMAKE_REQUIRED_INCLUDES)
121+
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES};${MPI_C_INCLUDE_PATH})
122+
else()
123+
set(CMAKE_REQUIRED_INCLUDES ${MPI_C_INCLUDE_PATH})
124+
endif()
125+
CHECK_INCLUDE_FILES("mpi.h" HAVE_MPI_H)
126+
CHECK_SYMBOL_EXISTS(I_MPI_VERSION "mpi.h" HAVE_Intel_MPI)
119127
if(HAVE_Intel_MPI AND WIN32)
120128
add_definitions(-DUSE_GCC)
121129
endif()
122-
130+
set(CMAKE_REQUIRED_INCLUDES ${old_cmake_required_includes})
123131

124132
add_library(opencoarrays_mod OBJECT ../extensions/opencoarrays.F90)
125133
set_target_properties(opencoarrays_mod
@@ -148,10 +156,12 @@ set_target_properties ( caf_mpi
148156
)
149157

150158
# Create a symlink in the include dir
151-
add_custom_command(TARGET caf_mpi
152-
POST_BUILD
153-
COMMAND ${CMAKE_COMMAND} -E create_symlink "./${mod_dir_tail}/opencoarrays.mod" "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR}/opencoarrays.mod"
154-
COMMENT "Creating symlink ${CMAKE_INSTALL_INCLUDEDIR}/opencoarrays.mod --> ${CMAKE_INSTALL_INCLUDEDIR}/${mod_dir_tail}/opencoarrays.mod")
159+
if(UNIX)
160+
add_custom_command(TARGET caf_mpi
161+
POST_BUILD
162+
COMMAND ${CMAKE_COMMAND} -E create_symlink "./${mod_dir_tail}/opencoarrays.mod" "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR}/opencoarrays.mod"
163+
COMMENT "Creating symlink ${CMAKE_INSTALL_INCLUDEDIR}/opencoarrays.mod --> ${CMAKE_INSTALL_INCLUDEDIR}/${mod_dir_tail}/opencoarrays.mod")
164+
endif()
155165

156166
install(DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR}/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
157167
FILES_MATCHING PATTERN "*.mod")

0 commit comments

Comments
 (0)