-
Notifications
You must be signed in to change notification settings - Fork 115
/
nrnivmodl.in
executable file
·278 lines (243 loc) · 7.84 KB
/
nrnivmodl.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env bash
set -e
ARCH=@host_cpu@
MODSUBDIR=@modsubdir@
USER_PWD="$PWD"
if [ -z "$NRNHOME" ] ; then
prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
libdir=@libdir@
else
prefix="$NRNHOME"
exec_prefix="${prefix}/${ARCH}"
bindir="${prefix}/bin"
libdir="${prefix}/lib"
fi
if [ -z ${CORENRNHOME+x} ] ; then
# CORENRNHOME wasn't set, use the install prefix
cnrn_prefix=@cnrn_prefix@
else
cnrn_prefix="${CORENRNHOME}"
fi
if [ "${NRNHOME_EXEC}" ] ; then
exec_prefix="${NRNHOME_EXEC}"
bindir="${exec_prefix}/bin"
libdir="${exec_prefix}/lib"
fi
export prefix
export bindir
export libdir
if command -v xcrun >/dev/null 2>&1; then
@NRN_OSX_BUILD_TRUE@export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)
@NRN_OSX_BUILD_TRUE@export MACOSX_DEPLOYMENT_TARGET="@CMAKE_OSX_DEPLOYMENT_TARGET@"
if [ -z "${MACOSX_DEPLOYMENT_TARGET}" ]; then
unset MACOSX_DEPLOYMENT_TARGET
fi
fi
LinkCoreNEURON=false
UserINCFLAGS=""
UserLDFLAGS=""
UserCOREFLAGS=()
UserNMODLBIN=""
UserNMODLFLAGS=""
# - options come first but can be in any order.
while [ "$1" ] ; do
case "$1" in
-coreneuron)
# also run nrnivmodl-core
LinkCoreNEURON=true
shift;;
-incflags)
# extra include flags and paths (NEURON only)
UserINCFLAGS="$2"
# extra include flags and paths for CoreNEURON
UserCOREFLAGS+=(-i "${2}")
shift
shift;;
-loadflags)
# extra link flags, paths, libraries (NEURON only)
UserLDFLAGS="$2"
# extra lin flags, paths. libraries (CoreNEURON)
UserCOREFLAGS+=(-l "${2}")
shift
shift;;
-nmodl)
echo "[NMODL][warning] Code generation with NMODL is pre-alpha, lacks features and is intended only for development use"
UserNMODLBIN="$2"
UserNMODLFLAGS="--neuron $UserNMODLFLAGS"
shift
shift;;
-nmodlflags)
echo "[NMODL][warning] If sympy is enabled, NMODL needs to be found in PYTHONPATH"
UserNMODLFLAGS="$UserNMODLFLAGS $2"
shift
shift;;
-h|--help)
echo "Usage: nrnivmodl [options] [mod files or directories with mod files]"
echo "Options:"
echo " -coreneuron Compile MOD files for CoreNEURON using nrnivmodl-core."
echo " -incflags \"include flags\" Extra include flags and paths when MOD (C++) files are compiled."
echo " -loadflags \"link flags\" Extra link flags, paths, and libraries when MOD (C++) files are linked."
echo " -nmodl /path/to/nmodl Path to the new NMODL transpiler for MOD files (pre-alpha, development use only)."
echo " -nmodlflags \"CLI flags\" Additional CLI flags for new NMODL transpiler"
echo " -h, --help Show this help message and exit."
echo "If no MOD files or directories provided then MOD files from current directory are used."
exit 0;;
-*)
echo "$1 unrecognized, check available CLI options with --help"
exit 1;;
*)
break;;
esac
done
echo "$PWD"
# Mod file paths may contain spaces which make variable lists of those
# hard to manage as space is the item separator. Furthermore, when a
# path is used, sometimes the spaces must be escaped (eg. a Makefile
# dependency pattern, and sometimes it is more useful for readability to
# enclose the path in "". To work around this issue, when creating a list
# of paths, translate the spaces for each item to +++ and after retrieval
# of an item, retranslate back to either a space or an escaped space.
# Only do this for cmake
shopt -s nullglob
# files is the complete list of mod files to process
files=()
cfiles=()
if [ $# -gt 0 ] ; then
for i in "$@" ; do
if [ -d "$i" ] ; then
files+=( "$i"/*.mod )
elif [ "${i##*.}" = "c" ] ; then
cfiles+=( "$i" )
elif [ -e "$i" ] || [ -e "$i.mod" ] ; then
files+=( "$i" )
else
echo "Arg Error: \"$i\" is not a folder or mod file name or prefix"
echo ""
echo -n " Mod file, folder args:"
for j in "$@" ; do
echo -n " \"$j\""
done
echo ""
exit 1
fi
done
else
files=( *.mod )
fi
echo cfiles = $cfiles
base_names=()
for i in "${files[@]}" ; do
base_names+=( "$(basename "$i" .mod)" )
done
echo -n "Mod files:"
for i in "${files[@]}" ; do
dir_name=$(dirname "$i")
echo -n " \"${i%.mod}.mod\""
done
echo ""
echo ""
if [ ! -d "$MODSUBDIR" ] ; then
echo "Creating '$MODSUBDIR' directory for .o files."
echo
mkdir "$MODSUBDIR"
fi
files=( "${files[@]%.mod}" )
cd $MODSUBDIR
mdir="$PWD"
# construct file to be included by makefile to specify mod to c rule when
# executed in $MODSUBDIR (child folder of launch location folder)
# Since $(PWD) may contain spaces in the path name, use the relative path.
# Otherwise, it is very difficult to pass around paths with spaces in
# makefiles. However, to help the user know where things are, when
# compiling use the full path to *.cpp
MODMAKE=makemod2c_inc
> "$MODMAKE"
for i in "${files[@]}" ; do
case "$i" in
/*) f="$i";; # absolute, fine as is
*) f="../$i";; # relative
esac
base_name="$(basename "$f")"
dir_name="$(dirname "$f")"
# Note: indentation for shell lines in make rules must be a tab
f=${f// /\\ }
f=${f//:/\\:}
echo "\
${base_name// /\\ }.cpp: ${f}.mod \$(NOCMODL)
@printf \" -> \$(C_GREEN)NMODL\$(C_RESET) \$<\\\n\"
(cd \"$dir_name\"; @NRN_NOCMODL_SANITIZER_ENVIRONMENT_STRING@ MODLUNIT=\$(NRNUNITS) \$(NOCMODL) \"$base_name.mod\" -o \"$mdir\" $UserNMODLFLAGS)
./${base_name// /\\ }.o: ${base_name// /\\ }.cpp
@printf \" -> \$(C_GREEN)Compiling\$(C_RESET) ${PWD}/\$<\\\n\"
\$(CXXCOMPILE) -I\"$dir_name\" \$(INCLUDES) @CMAKE_CXX_COMPILE_OPTIONS_PIC@ -c \"${PWD}/\$<\" -o \$@
" >> "$MODMAKE"
done
# *.c
cbase_names=()
for i in "${cfiles[@]}" ; do
case "$i" in
/*) f="$i";; # absolute, fine as is
*) f="../$i";; # relative
esac
base_name="$(basename "$f")"
cbase_names+=( "${base_name// /\\ }" )
dir_name="$(dirname "$f")"
# Note: indentation for shell lines in make rules must be a tab
f=${f// /\\ }
f=${f//:/\\:}
echo "\
./${base_name// /\\ }.o: ${i}
@printf \" -> \$(C_GREEN)Compiling\$(C_RESET) \$<\\\n\"
\$(CCOMPILE) -I\"$dir_name\" \$(INCLUDES) @CMAKE_C_COMPILE_OPTIONS_PIC@ -c \$< -o \$@
" >> "$MODMAKE"
done
MODOBJS=
{
echo '#include <stdio.h>
#include "hocdec.h"
extern int nrnmpi_myid;
extern int nrn_nobanner_;
'
for i in "${base_names[@]}" ; do
echo "extern \"C\" void _${i// }_reg(void);"
done
echo ""
echo "extern \"C\" void modl_reg() {"
echo " if (!nrn_nobanner_) if (nrnmpi_myid < 1) {"
printf ' fprintf(stderr, "Additional mechanisms from files\\n");\n'
for i in "${files[@]}"
do
echo ' fprintf(stderr, " \"'"$i"'.mod\"");'
done
printf ' fprintf(stderr, "\\n");\n'
echo " }"
for i in "${base_names[@]}" ; do
echo " _${i// }_reg();"
MODOBJS="$MODOBJS ./${i// /\\ }.o"
done
for i in "${cbase_names[@]}" ; do
MODOBJS="$MODOBJS ./${i// /\\ }.o"
done
echo "}"
} > mod_func.cpp
echo "MODOBJS=$MODOBJS"
# call nrnivmodl-core if CoreNEURON is enabled and requested via CLI
UsingCoreNEURON=false
@CORENEURON_ENABLED_TRUE@UsingCoreNEURON=true
if [ "$LinkCoreNEURON" = true ] ; then
if [ "$UsingCoreNEURON" = true ] ; then
if [ "$#" -gt 1 ] ; then
printf "ERROR : when called with -coreneuron only 1 mod dir is accepted.\n"
exit 1
fi
cd "$USER_PWD"
"${cnrn_prefix}/bin/nrnivmodl-core" "${UserCOREFLAGS[@]}" "$@"
cd "$MODSUBDIR"
else
printf "ERROR : CoreNEURON support is not enabled!\n"
exit 1
fi
fi
make -j 4 -f "${bindir}/nrnmech_makefile" "ROOT=${prefix}" "MODOBJFILES=$MODOBJS" "UserLDFLAGS=$UserLDFLAGS" "UserINCFLAGS=$UserINCFLAGS" "LinkCoreNEURON=$LinkCoreNEURON" "UserNMODLBIN=$UserNMODLBIN" "UserNMODLFLAGS=$UserNMODLFLAGS" special &&
echo "Successfully created $MODSUBDIR/special"