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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
#!/usr/bin/env python3
# This script will generate interfacing headers from the official headers of solvers.
# Note that since this is not an actual parser for the headers this is not a foolproof
# method. Please make sure that the generated file do not contain multiple definitions
# of the same identifier. This generally suggest a ifdef statement was read ignored.
#
# Generally this script goes over the header files given as "source_names" and looks
# for declarations of macros, enumerated types, and typedefinitions. It then compares
# each of these definitions to see if they are used in the files declared in "usage".
# Additional control is added by the "ignore" and "include" fields to correct
# the generated header file.
import os
import re
import sys
from itertools import chain
from pathlib import Path
from typing import List, Optional
STDCALL_PREPEND = """#if !defined(WIN32) && !defined(WIN64)
#define __cdecl
#define __stdcall
#endif
"""
def find_file(file_name: str, search_path: List[Path]) -> Optional[Path]:
for p in search_path:
path = p / file_name
if path.exists():
return path
return None
def remove_comments(code: str):
code = re.sub(r"/\*(.|\n)*?\*/", "", code)
code = re.sub(r"//.*", "", code)
return code
# Generator for the definitions in the header of the given Path.
def definitions(path: Path):
with path.open() as file:
end = True
buffer = ""
for line in file.readlines():
buffer += line
if line.endswith("\\\n"):
buffer = buffer[:-2]
continue
# macro definitions
match = re.search(r"#define\s+(\w+).*[ \t]+\S+[^\n]*\n", buffer)
if match:
text = remove_comments(match[0])
yield (match[1], text)
buffer = ""
continue
buffer = ""
txt = path.read_text()
# macro definitions
for match in re.findall(r"^\s*(enum\s+(\w+)([^;]|\n)*;)", txt, flags=re.MULTILINE):
text = remove_comments(match[0]) + "\n"
yield (match[1], text)
# type definitions
for match in re.findall(r"(typedef\s+.+\s+\*?\s*(\w+)\s*;)", txt):
text = remove_comments(match[0]) + "\n"
yield (match[1], text)
if __name__ == "__main__":
if len(sys.argv) <= 1:
print(
"Header generator usage:\n\tgen_header.py <solver> [search_location...]",
file=sys.stderr,
)
exit(1)
solver = sys.argv[1].upper()
root = Path(os.path.realpath(__file__)).parent.parent.parent
# Set addition search paths provided from command line
search_path = []
for i in range(2, len(sys.argv)):
search_path.append(Path(sys.argv[i]).resolve())
search_path.append(Path(sys.argv[i]).resolve() / "include")
# Set all solver specific settings
output = root / "include/minizinc/_thirdparty"
prepend = ""
ignore = set() # identifiers to be ignore (e.g., platform dependent definitions)
include = (
set()
) # identifiers that must be included (e.g., identifiers used by included macros)
if solver == "CPLEX":
output /= "cplex_interface.h"
source_names = ["ilcplex/cpxconst.h"]
prepend = """#ifdef _MSC_VER
typedef __int64 CPXLONG;
#else
typedef long long CPXLONG;
#endif
#ifdef _WIN32
#define CPXPUBLIC __stdcall
#else
#define CPXPUBLIC
#endif
"""
ignore = {"CPXLONG", "CPXPUBLIC"}
usage = [
root / "solvers/MIP/MIP_cplex_solverfactory.cpp",
root / "solvers/MIP/MIP_cplex_wrap.cpp",
root / "include/minizinc/solvers/MIP/MIP_cplex_solverfactory.hh",
root / "include/minizinc/solvers/MIP/MIP_cplex_wrap.hh",
]
for ver in [
"1210",
"129",
"128",
"1271",
"127",
"1263",
"1262",
"1261",
"126",
"201",
]:
paths = [
f"/opt/ibm/ILOG/CPLEX_Studio{ver}",
f"/opt/IBM/ILOG/CPLEX_Studio{ver}",
f"C:\\Program Files\\IBM\\ILOG\\CPLEX_Studio{ver}",
f"C:\\Program Files (x86)\\IBM\\ILOG\\CPLEX_Studio{ver}",
f"~/Applications/IBM/ILOG/CPLEX_Studio{ver}",
f"/Applications/IBM/ILOG/CPLEX_Studio{ver}",
f"/Applications/CPLEX_Studio{ver}",
]
search_path.extend([Path(p) / "include" for p in paths if Path(p).exists()])
search_path.extend(
[Path(p) / "cplex/include" for p in paths if Path(p).exists()]
)
elif solver == "GUROBI":
output /= "gurobi_interface.h"
source_names = ["gurobi_c.h"]
prepend = STDCALL_PREPEND
usage = [
root / "solvers/MIP/MIP_gurobi_solverfactory.cpp",
root / "solvers/MIP/MIP_gurobi_wrap.cpp",
root / "include/minizinc/solvers/MIP/MIP_gurobi_solverfactory.hh",
root / "include/minizinc/solvers/MIP/MIP_gurobi_wrap.hh",
]
for ver in [
"913",
"912",
"911",
"910",
"903",
"902",
"901",
"900",
"811",
"810",
"801",
"752",
"702",
]:
paths = [
f"/opt/gurobi{ver}/linux64",
f"C:\\gurobi{ver}\\win64",
f"C:\\gurobi{ver}\\win32",
f"/Library/gurobi{ver}/mac64",
f"/Library/gurobi{ver}/macos_universal2",
]
search_path.extend([Path(p) / "include" for p in paths if Path(p).exists()])
elif solver == "SCIP":
output /= "scip_interface.h"
source_names = [
"scip/def.h",
"scip/type_scip.h",
"scip/type_retcode.h",
"scip/type_message.h",
"scip/type_prob.h",
"scip/type_var.h",
"scip/type_stat.h",
"scip/type_sol.h",
"scip/type_event.h",
"scip/type_cons.h",
"scip/type_paramset.h",
"scip/type_lp.h",
"scip/cons_orbitope.h",
"symmetry/type_symmetry.h"
]
include = {
"SCIP_Retcode",
"SCIP_Vartype",
"SCIP_Status",
"SCIP_Objsense",
"SCIP_BoundType",
"SCIP_OrbitopeType",
}
prepend = """#if !defined(_MSC_VER) || _MSC_VER > 1600
#ifdef __cplusplus
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
#else
#define PRIx64 "llx"
#endif
#ifdef SCIP_DEBUG
#define SCIPdebugMessage printf("[%s:%d] debug: ", __FILE__, __LINE__), printf
#else
#define SCIPdebugMessage while( FALSE ) printf
#endif
"""
usage = [
root / "solvers/MIP/MIP_scip_solverfactory.cpp",
root / "solvers/MIP/MIP_scip_wrap.cpp",
root / "include/minizinc/solvers/MIP/MIP_scip_solverfactory.hh",
root / "include/minizinc/solvers/MIP/MIP_scip_wrap.hh",
]
elif solver == "XPRESS":
output /= "xpress_interface.h"
source_names = ["xprb.h", "xprs.h"]
prepend = """#ifdef _WIN32
#define XPRS_CC __stdcall
#else
#define XPRS_CC
#endif
#if defined(_WIN32) || defined(_WIN64)
#define XB_CC __stdcall
#else
#define XB_CC
#endif
#if defined(_WIN32)
#define XPRSint64 __int64
#elif defined(__LP64__) || defined(_LP64) || defined(__ILP64__) || defined(_ILP64)
#define XPRSint64 long
#else
#define XPRSint64 long long
#endif
"""
ignore = {"XB_CC", "XPRS_CC", "XPRSint64"}
include = {
"XB_INFINITY",
"XB_PL",
"XB_BV",
"XB_UI",
"XB_L",
"XB_G",
"XB_E",
"XB_MAXIM",
"XB_MINIM",
"XB_LP",
"XB_MPS",
"XB_XO_SOL",
"XB_MIP_NOT_LOADED",
"XB_MIP_NO_SOL_FOUND",
"XB_MIP_INFEAS",
"XB_MIP_OPTIMAL",
"XB_MIP_UNBOUNDED",
}
usage = [
root / "solvers/MIP/MIP_xpress_solverfactory.cpp",
root / "solvers/MIP/MIP_xpress_wrap.cpp",
root / "include/minizinc/solvers/MIP/MIP_xpress_solverfactory.hh",
root / "include/minizinc/solvers/MIP/MIP_xpress_wrap.hh",
]
search_path.extend(
[
"/opt/xpressmp/include",
"C:\\xpressmp\\Applications\\FICO Xpress\\xpressmp\\include",
]
)
else:
print(
f"ERROR: solver `{sys.argv[1]}' unknown. Unable to generate headers",
file=sys.stderr,
)
exit(1)
# Find Source Files
source_files = []
for name in source_names:
f = find_file(name, search_path)
if f is None:
print(
f"ERROR: Unable to locate source file `{name}'. Did you forget to specify a search location?",
file=sys.stderr,
)
exit(1)
source_files.append(f)
# Filter definitions on usage and place in generated header file
combined_usage = "\n".join([f.read_text() for f in usage])
with output.open("w") as out:
out.write(prepend)
for (ident, define) in chain(*[definitions(f) for f in source_files]):
if ident in include or (ident not in ignore and ident in combined_usage):
out.write(define)
|