| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
destdir="ann-cctbx/" |
| 4 |
incdir="include" |
| 5 |
srcdir="src" |
| 6 |
dstdir="self_include" |
| 7 |
|
| 8 |
|
| 9 |
################################## |
| 10 |
# Apply the cctbx transformation # |
| 11 |
################################## |
| 12 |
|
| 13 |
cat <<"EOF" | python3 - |
| 14 |
import os |
| 15 |
import copy |
| 16 |
|
| 17 |
annlib_under_build = "ann-cctbx" |
| 18 |
annlib_adaptbx_dir = "." |
| 19 |
annlib_under_build_include = os.path.join(annlib_under_build, "include") |
| 20 |
annlib_dir = "." |
| 21 |
|
| 22 |
|
| 23 |
def yield_includes(search_mode): |
| 24 |
annlib_mode_include = os.path.join( |
| 25 |
annlib_under_build_include, "ANN" + search_mode.upper() |
| 26 |
) |
| 27 |
if not os.path.isdir(annlib_under_build_include): |
| 28 |
os.makedirs(annlib_under_build_include) |
| 29 |
if not os.path.isdir(annlib_mode_include): |
| 30 |
os.makedirs(annlib_mode_include) |
| 31 |
|
| 32 |
annlib_include = os.path.join(annlib_dir, "include") |
| 33 |
ANN_include = os.path.join(annlib_include, "ANN") |
| 34 |
for item in os.listdir(ANN_include): |
| 35 |
yield { |
| 36 |
"src": os.path.join(ANN_include, item), |
| 37 |
"dst": os.path.join(annlib_mode_include, item), |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
def yield_src_includes(search_mode): |
| 42 |
annlib_mode_src = os.path.join(annlib_under_build, search_mode) |
| 43 |
if not os.path.isdir(annlib_mode_src): |
| 44 |
os.makedirs(annlib_mode_src) |
| 45 |
annlib_src = os.path.join(annlib_dir, "src") |
| 46 |
for item in os.listdir(annlib_src): |
| 47 |
if item.find(".h") > 0: |
| 48 |
yield { |
| 49 |
"src": os.path.join(annlib_src, item), |
| 50 |
"dst": os.path.join(annlib_mode_src, item), |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
def yield_src(search_mode): |
| 55 |
annlib_mode_src = os.path.join(annlib_under_build, search_mode) |
| 56 |
if not os.path.isdir(annlib_mode_src): |
| 57 |
os.makedirs(annlib_mode_src) |
| 58 |
annlib_src = os.path.join(annlib_dir, "src") |
| 59 |
for item in os.listdir(annlib_src): |
| 60 |
if item.find(".cpp") > 0: |
| 61 |
yield { |
| 62 |
"src": os.path.join(annlib_src, item), |
| 63 |
"dst": os.path.join(annlib_mode_src, item), |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
def process_includes(lines, search_mode): |
| 68 |
# Rule 1. The header guards should reflect the search_mode |
| 69 |
guard_line_no = 0 |
| 70 |
while lines[guard_line_no].find("#ifndef") < 0: |
| 71 |
guard_line_no += 1 |
| 72 |
for i in [guard_line_no, guard_line_no + 1]: |
| 73 |
lines[i] = lines[i].replace("ANN", "ANN" + search_mode.upper()) |
| 74 |
|
| 75 |
# Rule 2. Include path should reflect search mode instance |
| 76 |
line_no = 0 |
| 77 |
while line_no < len(lines): |
| 78 |
if lines[line_no].find("#include") >= 0 and lines[line_no].find("ANN/") >= 0: |
| 79 |
lines[line_no] = lines[line_no].replace( |
| 80 |
"ANN/", "ANN" + search_mode.upper() + "/" |
| 81 |
) |
| 82 |
line_no += 1 |
| 83 |
|
| 84 |
# Rule 3. Enclose functions in namespace |
| 85 |
# 3a. Opening of the namespace |
| 86 |
line_no = 0 |
| 87 |
last_include = 0 |
| 88 |
while line_no < len(lines): |
| 89 |
if lines[line_no].find("#include") >= 0: |
| 90 |
last_include = copy.copy(line_no) |
| 91 |
line_no += 1 |
| 92 |
lines.insert(last_include + 1, "namespace ann" + search_mode + " {\n") |
| 93 |
|
| 94 |
# 3b. special case for the file "ANN.h" where opening { is inside if clause |
| 95 |
line_no = 0 |
| 96 |
last_include = 0 |
| 97 |
while line_no < len(lines): |
| 98 |
if lines[line_no].find("<cvalues>") >= 0: |
| 99 |
last_include = copy.copy(line_no) |
| 100 |
break |
| 101 |
line_no += 1 |
| 102 |
if last_include > 0: |
| 103 |
lines.insert(last_include + 1, "namespace ann" + search_mode + " {\n") |
| 104 |
|
| 105 |
# 3c. Closing of the namespace |
| 106 |
line_no = 0 |
| 107 |
last_endif = 0 |
| 108 |
while line_no < len(lines): |
| 109 |
if lines[line_no].find("#endif") >= 0: |
| 110 |
last_endif = copy.copy(line_no) |
| 111 |
line_no += 1 |
| 112 |
lines.insert(last_endif, "} //namespace ann" + search_mode + "\n") |
| 113 |
|
| 114 |
# Rule 4. Define ANN_ALLOW_SELF_MATCH |
| 115 |
line_no = 0 |
| 116 |
while line_no < len(lines): |
| 117 |
if ( |
| 118 |
lines[line_no].find("ANNbool") >= 0 |
| 119 |
and lines[line_no].find("ANN_ALLOW_SELF_MATCH") >= 0 |
| 120 |
): |
| 121 |
bool_result = {"self_include": "ANNtrue", "self_exclude": "ANNfalse"} |
| 122 |
if lines[line_no].find(bool_result[search_mode]) < 0: |
| 123 |
code_result = { |
| 124 |
"self_include": "const ANNbool ANN_ALLOW_SELF_MATCH = ANNtrue;\n", |
| 125 |
"self_exclude": "const ANNbool ANN_ALLOW_SELF_MATCH = ANNfalse;\n", |
| 126 |
} |
| 127 |
lines[line_no] = code_result[search_mode] |
| 128 |
break |
| 129 |
line_no += 1 |
| 130 |
return |
| 131 |
|
| 132 |
|
| 133 |
def process_src_includes(lines, search_mode): |
| 134 |
process_includes(lines, search_mode) |
| 135 |
# Rule 5. Header guards for priority queues |
| 136 |
guard_line_no = 0 |
| 137 |
while lines[guard_line_no].find("#ifndef") < 0: |
| 138 |
guard_line_no += 1 |
| 139 |
for i in [guard_line_no, guard_line_no + 1]: |
| 140 |
lines[i] = lines[i].replace("PR_Q", "ANN" + search_mode.upper() + "_PR_Q") |
| 141 |
|
| 142 |
|
| 143 |
def process_src(lines, search_mode, unit): |
| 144 |
if unit in [ |
| 145 |
"ANN.cpp", |
| 146 |
"bd_tree.cpp", |
| 147 |
"brute.cpp", |
| 148 |
"kd_tree.cpp", |
| 149 |
"kd_util.cpp", |
| 150 |
"perf.cpp", |
| 151 |
]: |
| 152 |
# Rule 2. Include path should reflect search mode instance |
| 153 |
line_no = 0 |
| 154 |
while line_no < len(lines): |
| 155 |
if ( |
| 156 |
lines[line_no].find("#include") >= 0 |
| 157 |
and lines[line_no].find("ANN/") >= 0 |
| 158 |
): |
| 159 |
lines[line_no] = lines[line_no].replace( |
| 160 |
"ANN/", "ANN" + search_mode.upper() + "/" |
| 161 |
) |
| 162 |
line_no += 1 |
| 163 |
|
| 164 |
if unit in [ |
| 165 |
"ANN.cpp", |
| 166 |
"kd_fix_rad_search.cpp", |
| 167 |
"kd_pr_search.cpp", |
| 168 |
"kd_search.cpp", |
| 169 |
"kd_split.cpp", |
| 170 |
"kd_tree.cpp", |
| 171 |
"kd_util.cpp", |
| 172 |
"perf.cpp", |
| 173 |
]: |
| 174 |
# Rule 6. Enclosure within namespace |
| 175 |
# 3a. Opening of the namespace |
| 176 |
line_no = 0 |
| 177 |
last_include = 0 |
| 178 |
while line_no < len(lines): |
| 179 |
if lines[line_no].find("#include") >= 0: |
| 180 |
last_include = copy.copy(line_no) |
| 181 |
line_no += 1 |
| 182 |
lines.insert(last_include + 1, "namespace ann" + search_mode + " {\n") |
| 183 |
|
| 184 |
# 6c. Closing of the namespace |
| 185 |
lines.append("} //namespace ann" + search_mode + "\n") |
| 186 |
|
| 187 |
if unit in [ |
| 188 |
"bd_fix_rad_search.cpp", |
| 189 |
"bd_pr_search.cpp", |
| 190 |
"bd_search.cpp", |
| 191 |
"bd_tree.cpp", |
| 192 |
"brute.cpp", |
| 193 |
"kd_dump.cpp", |
| 194 |
]: |
| 195 |
# Rule 7. Namespace using clause |
| 196 |
line_no = 0 |
| 197 |
last_include = 0 |
| 198 |
while line_no < len(lines): |
| 199 |
if lines[line_no].find("#include") >= 0: |
| 200 |
last_include = copy.copy(line_no) |
| 201 |
line_no += 1 |
| 202 |
lines.insert(last_include + 1, "using namespace ann" + search_mode + ";\n") |
| 203 |
|
| 204 |
|
| 205 |
print("Generating C++ files for self-inclusive approximate nearest neighbour") |
| 206 |
for search_mode in ["self_exclude", "self_include"]: |
| 207 |
for item in yield_includes(search_mode): |
| 208 |
# print item |
| 209 |
with open(item["src"], "r") as G: |
| 210 |
lines = G.readlines() |
| 211 |
process_includes(lines, search_mode) |
| 212 |
with open(item["dst"], "w") as F: |
| 213 |
F.write("".join(lines)) |
| 214 |
|
| 215 |
for item in yield_src_includes(search_mode): |
| 216 |
# print item |
| 217 |
with open(item["src"], "r") as G: |
| 218 |
lines = G.readlines() |
| 219 |
process_src_includes(lines, search_mode) |
| 220 |
with open(item["dst"], "w") as F: |
| 221 |
F.write("".join(lines)) |
| 222 |
|
| 223 |
for item in yield_src(search_mode): |
| 224 |
# print item |
| 225 |
with open(item["src"], "r") as G: |
| 226 |
lines = G.readlines() |
| 227 |
process_src(lines, search_mode, os.path.basename(item["src"])) |
| 228 |
with open(item["dst"], "w") as F: |
| 229 |
F.write("".join(lines)) |
| 230 |
EOF |
| 231 |
|
| 232 |
|
| 233 |
FILE=ann-cctbx/Makefile.am |
| 234 |
echo "Saved cctbx automake in ${FILE}" |
| 235 |
dd status=none of=${FILE} << "EOF" |
| 236 |
annselfincdir = $(includedir)/ANNSELF_INCLUDE |
| 237 |
|
| 238 |
annselfinc_HEADERS = \ |
| 239 |
include/ANNSELF_INCLUDE/ANN.h \ |
| 240 |
include/ANNSELF_INCLUDE/ANNx.h \ |
| 241 |
include/ANNSELF_INCLUDE/ANNperf.h |
| 242 |
|
| 243 |
annselfexcdir = $(includedir)/ANNSELF_EXCLUDE |
| 244 |
|
| 245 |
annselfexc_HEADERS = \ |
| 246 |
include/ANNSELF_EXCLUDE/ANN.h \ |
| 247 |
include/ANNSELF_EXCLUDE/ANNx.h \ |
| 248 |
include/ANNSELF_EXCLUDE/ANNperf.h |
| 249 |
|
| 250 |
AM_CPPFLAGS = -I$(top_srcdir)/ann-cctbx/include |
| 251 |
|
| 252 |
lib_LTLIBRARIES = libann-cctbx.la |
| 253 |
|
| 254 |
libann_cctbx_la_SOURCES = \ |
| 255 |
self_include/ANN.h \ |
| 256 |
self_include/ANN.cpp \ |
| 257 |
self_include/ANNx.h \ |
| 258 |
self_include/ANNperf.h \ |
| 259 |
self_include/bd_fix_rad_search.cpp \ |
| 260 |
self_include/bd_pr_search.cpp \ |
| 261 |
self_include/bd_search.cpp \ |
| 262 |
self_include/bd_tree.cpp \ |
| 263 |
self_include/bd_tree.h \ |
| 264 |
self_include/brute.cpp \ |
| 265 |
self_include/kd_dump.cpp \ |
| 266 |
self_include/kd_fix_rad_search.cpp \ |
| 267 |
self_include/kd_pr_search.cpp \ |
| 268 |
self_include/kd_pr_search.h \ |
| 269 |
self_include/kd_search.cpp \ |
| 270 |
self_include/kd_search.h \ |
| 271 |
self_include/kd_split.cpp \ |
| 272 |
self_include/kd_split.h \ |
| 273 |
self_include/kd_tree.cpp \ |
| 274 |
self_include/kd_tree.h \ |
| 275 |
self_include/kd_util.cpp \ |
| 276 |
self_include/kd_util.h \ |
| 277 |
self_include/perf.cpp \ |
| 278 |
self_include/pr_queue.h \ |
| 279 |
self_include/pr_queue_k.h \ |
| 280 |
self_exclude/ANN.h \ |
| 281 |
self_exclude/ANN.cpp \ |
| 282 |
self_exclude/ANNx.h \ |
| 283 |
self_exclude/ANNperf.h \ |
| 284 |
self_exclude/bd_fix_rad_search.cpp \ |
| 285 |
self_exclude/bd_pr_search.cpp \ |
| 286 |
self_exclude/bd_search.cpp \ |
| 287 |
self_exclude/bd_tree.cpp \ |
| 288 |
self_exclude/bd_tree.h \ |
| 289 |
self_exclude/brute.cpp \ |
| 290 |
self_exclude/kd_dump.cpp \ |
| 291 |
self_exclude/kd_fix_rad_search.cpp \ |
| 292 |
self_exclude/kd_pr_search.cpp \ |
| 293 |
self_exclude/kd_pr_search.h \ |
| 294 |
self_exclude/kd_search.cpp \ |
| 295 |
self_exclude/kd_search.h \ |
| 296 |
self_exclude/kd_split.cpp \ |
| 297 |
self_exclude/kd_split.h \ |
| 298 |
self_exclude/kd_tree.cpp \ |
| 299 |
self_exclude/kd_tree.h \ |
| 300 |
self_exclude/kd_util.cpp \ |
| 301 |
self_exclude/kd_util.h \ |
| 302 |
self_exclude/perf.cpp \ |
| 303 |
self_exclude/pr_queue.h \ |
| 304 |
self_exclude/pr_queue_k.h |
| 305 |
|
| 306 |
libann_cctbx_la_LDFLAGS = -no-undefined -version-info 0:0:0 |
| 307 |
EOF |