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 / smpiff.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: smpiff 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_f77() {
40     list_add TMPFILES "${TMPFILE}"
41     #replace "program main_name by subroutine user_main (and the end clause as well)"
42     if [ $TRACE_CALL_LOCATION -gt 0 ]; then
43       echo "#include \"@includedir@/smpi/smpi_extended_traces_fortran.h\"" > ${TMPFILE}
44       echo "#line 1 \"${ARG}\"" >> ${TMPFILE}
45     fi
46     sed 's/^[[:space:]]\{6\}[[:space:]]*\([eE][nN][dD] \)\{0,1\}[pP][rR][oO][gG][rR][aA][mM][^a-zA-Z0-9]*\([a-zA-Z0-9_]*\)/      \1subroutine user_main /g' "${ARG}" >> "${TMPFILE}"
47     SRCFILE="${TMPFILE}"
48     list_add CMDLINE "${SRCFILE}"
49 }
50 filter_and_compile_f90() {
51     list_add TMPFILES "${TMPFILE}"
52     #replace "program main_name by subroutine user_main (and the end clause as well)"
53     if [ $TRACE_CALL_LOCATION -gt 0 ]; then
54       echo "#include \"@includedir@/smpi/smpi_extended_traces_fortran.h\"" > ${TMPFILE}
55       echo "#line 1 \"${ARG}\"" >> ${TMPFILE}
56     fi
57     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}"
58     SRCFILE="${TMPFILE}"
59     list_add CMDLINE "${SRCFILE}"
60 }
61 TRACE_CALL_LOCATION=0
62 NEEDS_OUTPUT=1
63
64 list_set CMDLINE "${REAL_FORTRAN_COMPILER}"
65 list_add_not_empty CMDLINE "${FFLAGS}"
66 while [ $# -gt 0 ]; do
67     ARG="$1"
68     shift
69     case "${ARG}" in
70         -c)
71             CMAKE_LINKARGS=""
72             LINKARGS=""
73             list_add CMDLINE "-c"
74             ;;
75         *.f)
76             FILENAME=`basename ${ARG}`
77             TMPFILE=$(mymktemp "${ARG}" ".f")
78             ORIGFILE="${FILENAME%.f}"
79             filter_and_compile_f77
80             ;;
81         *.F)$
82             FILENAME=`basename ${ARG}`
83             TMPFILE=$(mymktemp "${ARG}" ".F")
84             ORIGFILE="${FILENAME%.F}"
85             filter_and_compile_f77
86             ;;
87         *.f90)
88             FILENAME=`basename ${ARG}`
89             TMPFILE=$(mymktemp "${ARG}" ".f90")
90             ORIGFILE="${FILENAME%.f90}"
91             filter_and_compile_f90
92             ;;
93         *.F90)$
94             FILENAME=`basename ${ARG}`
95             TMPFILE=$(mymktemp "${ARG}" ".F90")
96             ORIGFILE="${FILENAME%.F90}"
97             filter_and_compile_f90
98             ;;
99         '-version' | '--version')
100             printf '%b\n' "$SIMGRID_VERSION"
101             exit 0
102             ;;
103         "-git-version" | "--git-version")
104             printf '%b\n' "$SIMGRID_GITHASH"
105             exit 0
106             ;;
107         '-compiler-version' | '--compiler-version')
108             ${REAL_FORTRAN_COMPILER} --version
109             ;;
110         '-trace-call-location')
111             TRACE_CALL_LOCATION=1
112             # This should be list_add FFLAGS but it's not possible
113             # anymore: FFLAGS was already moved into CMDLINE above.
114             list_add_not_empty CMDLINE "-ffixed-line-length-none" "-cpp"
115             ;;
116         -o)
117             list_add CMDLINE "-o$1"
118             NEEDS_OUTPUT=0
119             shift
120             ;;
121         '-show'|'-compile-info'|'-link-info')
122             # Dry run displaying commands instead of executing them. Useful to cmake
123             show=1
124             ;;
125         *)
126             list_add CMDLINE "${ARG}"
127             ;;
128     esac
129 done
130
131 if [ $NEEDS_OUTPUT -ne 0 ]; then
132    list_add CMDLINE "-o${ORIGFILE}.o"
133 fi
134
135 list_add_not_empty CMDLINE ${INCLUDEARGS}
136 list_add_not_empty CMDLINE ${CMAKE_LINKARGS}
137 list_add_not_empty CMDLINE "${LINKARGS}"
138
139 eval $(list_get CMDLINE)
140 if [ "x$VERBOSE" = x1 -o "x$show" = x1 ] ; then
141   echo "$@"
142   [ "x$show" = x1 ] && exit 0
143 fi
144 "$@"