Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
third time's the charm. It was already done properly for smpicc, so just port it...
[simgrid.git] / src / smpi / smpif90.in
1 #!/usr/bin/env sh
2
3 # Copyright (c) 2012-2020. The SimGrid Team. All rights reserved.
4
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the license (GNU LGPL) which comes with this package.
7
8 SIMGRID_VERSION="@SIMGRID_VERSION_STRING@"
9 SIMGRID_GITHASH="@SIMGRID_GITHASH@"
10
11 REAL_FORTRAN_COMPILER=@SMPI_Fortran_COMPILER@
12
13 INCLUDEARGS="@includeflag@"
14 CMAKE_LINKARGS="-L@libdir@"
15
16 @SMPITOOLS_SH@
17
18 list_set FFLAGS @SMPI_Fortran_FLAGS@
19 list_set LINKARGS "-lsimgrid" @SMPI_Fortran_LIBS@ "-lm"
20 if [ "x${SMPI_PRETEND_CC}" = "x" ]; then
21    list_add LINKARGS "-shared"
22    if [ "x@APPLE@" != "x1" ]; then
23        list_add LINKARGS "-Wl,-z,defs"
24    else
25        list_add LINKARGS "-Wl,-undefined,error"
26    fi
27 else
28    echo "Warning: smpif90 pretends to be a regular compiler (SMPI_PRETEND_CC is set). Produced binaries will not be usable in SimGrid."
29 fi
30 list_set TMPFILES
31 main_name=main
32
33 cleanup () {
34     eval $(list_get TMPFILES)
35     rm -f "$@"
36 }
37 trap 'cleanup' EXIT
38
39 filter_and_compile() {
40     list_add TMPFILES "${TMPFILE}"
41     #replace "program main_name by subroutine user_main (and the end clause as well)"
42     sed 's/^\([[:space:]]*\)\([eE][nN][dD] \)\{0,1\}[pP][rR][oO][gG][rR][aA][mM][^a-zA-Z0-9]*\([a-zA-Z0-9_]*\)/\1\2subroutine user_main /g' "${ARG}" >> "${TMPFILE}"
43     SRCFILE="${TMPFILE}"
44     list_add CMDLINE "${SRCFILE}"
45 }
46
47 NEEDS_OUTPUT=1
48
49 list_set CMDLINE "${REAL_FORTRAN_COMPILER}"
50 list_add_not_empty CMDLINE "${FFLAGS}"
51 while [ $# -gt 0 ]; do
52     ARG="$1"
53     shift
54     case "${ARG}" in
55         -c)
56             CMAKE_LINKARGS=""
57             LINKARGS=""
58             list_add CMDLINE "-c"
59             ;;
60         *.f90)
61             FILENAME=`basename ${ARG}`
62             TMPFILE=$(mymktemp "${ARG}" ".f90")
63             ORIGFILE="${FILENAME%.f90}"
64             filter_and_compile
65             ;;
66         *.F90)
67             FILENAME=`basename ${ARG}`
68             TMPFILE=$(mymktemp "${ARG}" ".F90")
69             ORIGFILE="${FILENAME%.F90}"
70             filter_and_compile
71             ;;
72         '-version' | '--version')
73             printf '%b\n' "$SIMGRID_VERSION"
74             exit 0
75             ;;
76         "-git-version" | "--git-version")
77             printf '%b\n' "$SIMGRID_GITHASH"
78             exit 0
79             ;;
80         '-compiler-version' | '--compiler-version')
81             ${REAL_FORTRAN_COMPILER} --version
82             ;;
83         -o)
84             list_add CMDLINE "-o$1"
85             NEEDS_OUTPUT=0
86             shift
87             ;;
88         '-show'|'-compile-info'|'-link-info')
89             # Dry run displaying commands instead of executing them. Useful to cmake
90             show=1
91             ;;
92         *)
93             list_add CMDLINE "${ARG}"
94             ;;
95     esac
96 done
97
98 if [ $NEEDS_OUTPUT -ne 0 ]; then
99    list_add CMDLINE "-o${ORIGFILE}.o"
100 fi
101
102 list_add_not_empty CMDLINE ${INCLUDEARGS}
103 list_add_not_empty CMDLINE ${CMAKE_LINKARGS}
104 list_add_not_empty CMDLINE "${LINKARGS}"
105
106 eval $(list_get CMDLINE)
107 if [ "x$VERBOSE" = x1 -o "x$show" = x1 ] ; then
108   echo "$@"
109   [ "x$show" = x1 ] && exit 0
110 fi
111 "$@"