|
1 | 1 | echo "FDS build target = $FDS_BUILD_TARGET" |
2 | 2 |
|
3 | | -# For following variables 1- indicate availble through |
4 | | -# environment variable FIREMODELS_LIBS_CC, FIREMODELS_LIBS_CXX, and FIREMODELS_LIBS_FC |
5 | | -set_CC=0 |
6 | | -set_CXX=0 |
7 | | -set_FC=0 |
| 3 | +# Initialize variables and check environment variables |
| 4 | +set_compiler_var() { |
| 5 | + local var_name=$1 # Variable name to set (e.g., CC, CXX, FC) |
| 6 | + local env_var=$2 # Environment variable to check (e.g., FIREMODELS_LIBS_CC) |
| 7 | + local set_flag_var=$3 # Flag variable name (e.g., set_CC, set_CXX, set_FC) |
8 | 8 |
|
9 | | -if [ -n "$FIREMODELS_LIBS_CC" ]; then |
10 | | - CC=$FIREMODELS_LIBS_CC |
11 | | - if command -v $CC &> /dev/null; then |
12 | | - set_CC=1 |
13 | | - else |
14 | | - echo "The compiler specified by the FIREMODELS_LIBS_CC environment variable ($CC) is not available on this system. Searching for an alternative compiler." |
15 | | - fi |
16 | | -fi |
| 9 | + if [ -n "${!env_var}" ]; then |
| 10 | + eval "$var_name=${!env_var}" |
| 11 | + if command -v "${!var_name}" &> /dev/null; then |
| 12 | + eval "$set_flag_var=1" |
| 13 | + else |
| 14 | + echo "Warning: ${!env_var} specified by $env_var is not available. Searching for an alternative." |
| 15 | + fi |
| 16 | + fi |
| 17 | +} |
17 | 18 |
|
18 | | -if [ -n "$FIREMODELS_LIBS_CXX" ]; then |
19 | | - CXX=$FIREMODELS_LIBS_CXX |
20 | | - if command -v $CXX &> /dev/null; then |
21 | | - set_CXX=1 |
22 | | - else |
23 | | - echo "The compiler specified by the FIREMODELS_LIBS_CXX environment variable ($CXX) is not available on this system. Searching for an alternative compiler." |
24 | | - fi |
25 | | -fi |
26 | 19 |
|
27 | | -if [ -n "$FIREMODELS_LIBS_FC" ]; then |
28 | | - FC=$FIREMODELS_LIBS_FC |
29 | | - if command -v $FC &> /dev/null; then |
30 | | - set_FC=1 |
31 | | - else |
32 | | - echo "The compiler specified by the FIREMODELS_LIBS_FC environment variable ($FC) is not available on this system. Searching for an alternative compiler." |
33 | | - fi |
34 | | -fi |
| 20 | +# Set compilers based on the build target |
| 21 | +select_compiler() { |
| 22 | + local var_name=$1 # Variable to set (CC, CXX, FC) |
| 23 | + shift |
| 24 | + local compilers=("$@") # List of compilers to check in order |
| 25 | + local set_flag_var="set_$var_name" |
35 | 26 |
|
| 27 | + # Only proceed if the compiler flag is not set |
| 28 | + if [ "$(eval echo \$$set_flag_var)" -eq 0 ]; then |
| 29 | + for compiler in "${compilers[@]}"; do |
| 30 | + if command -v "$compiler" &> /dev/null; then |
| 31 | + # Set the compiler variable |
| 32 | + eval "$var_name=$compiler" |
| 33 | + # Set the flag variable to 1 (indicating the compiler was found) |
| 34 | + eval "$set_flag_var=1" |
| 35 | + return |
| 36 | + fi |
| 37 | + done |
| 38 | + echo "Error: None of the specified compilers (${compilers[*]}) are available for $var_name on this system." |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +} |
36 | 42 |
|
37 | | -if [[ "$FDS_BUILD_TARGET" == *"osx"* ]]; then |
38 | | - # Check for C compiler (mpicc, gcc, clang) |
39 | | - if [ $set_CC -eq 0 ]; then |
40 | | - if command -v mpicc &> /dev/null; then |
41 | | - CC=mpicc |
42 | | - elif command -v clang &> /dev/null; then |
43 | | - CC=clang |
44 | | - elif command -v gcc &> /dev/null; then |
45 | | - CC=gcc |
46 | | - else |
47 | | - echo "Error: Any of mpicc, clang, or gcc compiler is not available on this system." |
48 | | - exit 1 |
49 | | - fi |
50 | | - fi |
51 | 43 |
|
52 | | - # Check for clang C++ compiler (mpicxx, g++, clang ++) |
53 | | - if [ $set_CXX -eq 0 ]; then |
54 | | - if command -v mpicxx &> /dev/null; then |
55 | | - CXX=mpicxx |
56 | | - elif command -v clang++ &> /dev/null; then |
57 | | - CXX=clang++ |
58 | | - elif command -v g++ &> /dev/null; then |
59 | | - CXX=g++ |
60 | | - else |
61 | | - echo "Error: Any of mpicxx, clang++, or g++ compiler is not available on this system." |
62 | | - exit 1 |
63 | | - fi |
64 | | - fi |
| 44 | +# Following variables indicate if compilers are set using environment variables FIREMODELS_LIBS_XXX. |
| 45 | +set_CC=0 |
| 46 | +set_CXX=0 |
| 47 | +set_FC=0 |
65 | 48 |
|
66 | | - # Check for Fortran compiler (gfortran) |
67 | | - if [ $set_FC -eq 0 ]; then |
68 | | - if command -v mpifort &> /dev/null; then |
69 | | - FC=mpifort |
70 | | - elif command -v gfortran &> /dev/null; then |
71 | | - FC=gfortran |
72 | | - else |
73 | | - echo "Error: Any of mpifort or gfortran compiler is not available on this system." |
74 | | - exit 1 |
75 | | - fi |
76 | | - fi |
| 49 | +# Check environment variables for compilers |
| 50 | +set_compiler_var CC FIREMODELS_LIBS_CC set_CC |
| 51 | +set_compiler_var CXX FIREMODELS_LIBS_CXX set_CXX |
| 52 | +set_compiler_var FC FIREMODELS_LIBS_FC set_FC |
77 | 53 |
|
| 54 | +# Determine compiler list based on build target |
| 55 | +if [[ "$FDS_BUILD_TARGET" == *"osx"* ]]; then |
| 56 | + select_compiler CC mpicc clang gcc |
| 57 | + select_compiler CXX mpicxx clang++ g++ |
| 58 | + select_compiler FC mpifort gfortran |
78 | 59 | elif [[ "$FDS_BUILD_TARGET" == *"intel"* ]]; then |
79 | | - # Check for Intel C compiler |
80 | | - if [ $set_CC -eq 0 ]; then |
81 | | - if command -v mpiicx &> /dev/null; then |
82 | | - CC=mpiicx |
83 | | - elif command -v icx &> /dev/null; then |
84 | | - CC=icx |
85 | | - elif command -v mpiicc &> /dev/null; then |
86 | | - CC=mpiicc |
87 | | - elif command -v icc &> /dev/null; then |
88 | | - CC=icc |
89 | | - else |
90 | | - echo "Error: Any of mpiicx, icx, mpiicc, or icc compiler is not available on this system." |
91 | | - exit 1 |
92 | | - fi |
93 | | - fi |
94 | | - |
95 | | - # Check for Intel C++ compiler |
96 | | - if [ $set_CXX -eq 0 ]; then |
97 | | - if command -v mpiicpx &> /dev/null; then |
98 | | - CXX=mpiicpx |
99 | | - elif command -v icpx &> /dev/null; then |
100 | | - CXX=icpx |
101 | | - elif command -v mpiicpc &> /dev/null; then |
102 | | - CXX=mpiicpc |
103 | | - elif command -v icpc &> /dev/null; then |
104 | | - CXX=icpc |
105 | | - else |
106 | | - echo "Error: Any of mpiicpx, icpx, mpiicpc, or icpc compiler is not available on this system." |
107 | | - exit 1 |
108 | | - fi |
109 | | - fi |
110 | | - |
111 | | - # Check for Intel Fortran compiler (ifort). ifx will be added in future. |
112 | | - if [ $set_FC -eq 0 ]; then |
113 | | - if command -v mpiifort &> /dev/null; then |
114 | | - FC=mpiifort |
115 | | - elif command -v ifort &> /dev/null; then |
116 | | - FC=ifort |
117 | | - else |
118 | | - echo "Error: Any of mpiifort, or ifort compiler is not available on this system." |
119 | | - exit 1 |
120 | | - fi |
121 | | - fi |
122 | | -else #gnu |
123 | | - # Check for gnu C compiler (gcc) |
124 | | - if [ $set_CC -eq 0 ]; then |
125 | | - if command -v mpicc &> /dev/null; then |
126 | | - CC=mpicc |
127 | | - elif command -v gcc &> /dev/null; then |
128 | | - CC=gcc |
129 | | - else |
130 | | - echo "Error: Any of mpicc, gcc compiler is not available on this system." |
131 | | - exit 1 |
132 | | - fi |
133 | | - fi |
134 | | - |
135 | | - # Check for Intel C++ compiler (g++) |
136 | | - if [ $set_CXX -eq 0 ]; then |
137 | | - if command -v mpicxx &> /dev/null; then |
138 | | - CXX=mpicxx |
139 | | - elif command -v g++ &> /dev/null; then |
140 | | - CXX=g++ |
141 | | - else |
142 | | - echo "Error: Any of mpicxx, g++ compiler is not available on this system." |
143 | | - exit 1 |
144 | | - fi |
145 | | - fi |
146 | | - |
147 | | - # Check for Fortran compiler (gfortran) |
148 | | - if [ $set_FC -eq 0 ]; then |
149 | | - if command -v mpifort &> /dev/null; then |
150 | | - FC=mpifort |
151 | | - elif command -v gfortran &> /dev/null; then |
152 | | - FC=gfortran |
153 | | - else |
154 | | - echo "Error: Any of mpifort, gfortran compiler is not available on this system." |
155 | | - exit 1 |
156 | | - fi |
157 | | - fi |
| 60 | + select_compiler CC mpiicx icx mpiicc icc |
| 61 | + select_compiler CXX mpiicpx icpx mpiicpc icpc |
| 62 | + select_compiler FC mpiifort ifort |
| 63 | +else # Default to GNU compilers |
| 64 | + select_compiler CC mpicc gcc |
| 65 | + select_compiler CXX mpicxx g++ |
| 66 | + select_compiler FC mpifort gfortran |
158 | 67 | fi |
159 | 68 |
|
| 69 | + |
160 | 70 | echo "Third-party libs C Compiler=$CC" |
161 | 71 | echo "Third-party libs C++ compiler=$CXX" |
162 | 72 | echo "Third-party libs Fortran compiler=$FC" |
163 | 73 |
|
164 | 74 | export CC=$CC |
165 | 75 | export CXX=$CXX |
166 | 76 | export FC=$FC |
| 77 | + |
| 78 | + |
| 79 | + |
0 commit comments