Coverage for python/lsst/sconsUtils/state.py: 35%

298 statements  

« prev     ^ index     » next       coverage.py v7.17.5, created at 2026-11-26 06:02 +0000

1# This file is part of sconsUtils. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22"""Global state for sconsUtils. 

23 

24This module acts like a singleton, holding all global state for sconsUtils. 

25This includes the primary Environment object (`lsst.sconsUtils.state.env`), 

26the message log (`lsst.sconsUtils.state.log`), the command-line variables 

27object (`lsst.sconsUtils.state.state.opts`), and a dictionary of command-line 

28targets used to setup aliases, default targets, and dependencies 

29(`lsst.sconsUtils.state.targets`). All four of these variables are aliased 

30to the main `lsst.sconsUtils` scope, so there should be no need for users to 

31deal with the state module directly. 

32 

33These are all initialized when the module is imported, but may be modified 

34by other code (particularly `lsst.sconsUtils.dependencies.configure`). 

35""" 

36 

37import os 

38import re 

39import shlex 

40from configparser import ConfigParser 

41 

42import SCons.Conftest 

43import SCons.Script 

44 

45from . import eupsForScons 

46from .utils import conda_compilers, get_conda_prefix, use_conda_compilers 

47 

48SCons.Script.EnsureSConsVersion(2, 1, 0) 

49 

50"""A dictionary of SCons aliases and targets. 

51 

52These are used to setup aliases, default targets, and dependencies by 

53`lsst.sconsUtils.scripts.BasicSConstruct.finish`. 

54While one can still use env.Alias to setup aliases (and should for "install"), 

55putting targets here will generally provide better build-time dependency 

56handling (like ensuring everything is built before we try to install, and 

57making sure SCons doesn't rebuild the world before installing). 

58 

59Users can add additional keys to the dictionary if desired. 

60 

61Targets should be added by calling extend() or using ``+=`` on the dict 

62values, to keep the lists of targets from turning into lists-of-lists. 

63""" 

64targets = { 

65 "doc": [], 

66 "tests": [], 

67 "lib": [], 

68 "python": [], 

69 "examples": [], 

70 "include": [], 

71 "version": [], 

72 "shebang": [], 

73 "pkginfo": [], 

74 "scripts": [], 

75} 

76 

77env = None 

78log = None 

79opts = None 

80 

81 

82def _initOptions(): 

83 SCons.Script.AddOption( 

84 "--checkDependencies", 

85 dest="checkDependencies", 

86 action="store_true", 

87 default=False, 

88 help="Verify dependencies with autoconf-style tests.", 

89 ) 

90 SCons.Script.AddOption( 

91 "--filterWarn", 

92 dest="filterWarn", 

93 action="store_true", 

94 default=False, 

95 help="Filter out a class of warnings deemed irrelevant", 

96 ) 

97 SCons.Script.AddOption( 

98 "--force", 

99 dest="force", 

100 action="store_true", 

101 default=False, 

102 help="Set to force possibly dangerous behaviours", 

103 ) 

104 SCons.Script.AddOption( 

105 "--linkFarmDir", 

106 dest="linkFarmDir", 

107 action="store", 

108 default=None, 

109 help="The directory of symbolic links needed to build and use the package", 

110 ) 

111 SCons.Script.AddOption( 

112 "--prefix", dest="prefix", action="store", default=False, help="Specify the install destination" 

113 ) 

114 SCons.Script.AddOption( 

115 "--setenv", 

116 dest="setenv", 

117 action="store_true", 

118 default=False, 

119 help="Treat arguments such as Foo=bar as defining construction variables", 

120 ) 

121 SCons.Script.AddOption( 

122 "--tag", dest="tag", action="store", default=None, help="Declare product with this eups tag" 

123 ) 

124 SCons.Script.AddOption( 

125 "--verbose", 

126 dest="verbose", 

127 action="store_true", 

128 default=False, 

129 help="Print additional messages for debugging.", 

130 ) 

131 SCons.Script.AddOption( 

132 "--traceback", 

133 dest="traceback", 

134 action="store_true", 

135 default=False, 

136 help="Print full exception tracebacks when errors occur.", 

137 ) 

138 SCons.Script.AddOption( 

139 "--no-eups", 

140 dest="no_eups", 

141 action="store_true", 

142 default=False, 

143 help="Do not use EUPS for configuration", 

144 ) 

145 SCons.Script.AddOption( 

146 "--no-tests", 

147 dest="no_tests", 

148 action="store_true", 

149 default=False, 

150 help=( 

151 "Do not build or run tests (by clearing the 'tests' target). " 

152 "Library targets in the test directory may still be built." 

153 ), 

154 ) 

155 

156 

157def _initLog(): 

158 from . import utils 

159 

160 global log 

161 log = utils.Log() 

162 

163 

164def _get_macosx_deployment_target_default(): 

165 """Determine the default deployment target on macOS. 

166 

167 Returns 

168 ------- 

169 target : `str` 

170 The default target for this platform. Can be returned even 

171 if not running on macOS. 

172 """ 

173 uname = os.uname() 

174 if uname.sysname == "Darwin" and uname.machine == "arm64": 174 ↛ 175line 174 didn't jump to line 175 because the condition on line 174 was never true

175 return "11.0" 

176 return "10.9" 

177 

178 

179def _initVariables(): 

180 files = [] 

181 if "optfile" in SCons.Script.ARGUMENTS: 181 ↛ 182line 181 didn't jump to line 182 because the condition on line 181 was never true

182 configfile = SCons.Script.ARGUMENTS["optfile"] 

183 if configfile not in files: 

184 files.append(configfile) 

185 for file in files: 185 ↛ 186line 185 didn't jump to line 186 because the loop on line 185 never started

186 if not os.path.isfile(file): 

187 log.warn(f"Warning: Will ignore non-existent options file, {file}") 

188 if "optfile" not in SCons.Script.ARGUMENTS: 188 ↛ 191line 188 didn't jump to line 191 because the condition on line 188 was always true

189 files.append("buildOpts.py") 

190 global opts 

191 opts = SCons.Script.Variables(files) 

192 opts.AddVariables( 

193 ("archflags", "Extra architecture specification to add to CC/LINK flags (e.g. -m32)", ""), 

194 ("cc", "Choose the compiler to use", ""), 

195 SCons.Script.BoolVariable("debug", "Set to enable debugging flags (use --debug)", True), 

196 ("eupsdb", "Specify which element of EUPS_PATH should be used", None), 

197 ("flavor", "Set the build flavor", None), 

198 SCons.Script.BoolVariable("force", "Set to force possibly dangerous behaviours", False), 

199 ("optfile", "Specify a file to read default options from", None), 

200 ("prefix", "Specify the install destination", None), 

201 SCons.Script.EnumVariable( 

202 "opt", "Set the optimisation level", 3, allowed_values=("g", "0", "1", "2", "3") 

203 ), 

204 SCons.Script.EnumVariable( 

205 "profile", "Compile/link for profiler", 0, allowed_values=("0", "1", "pg", "gcov") 

206 ), 

207 ("version", "Specify the version to declare", None), 

208 ("baseversion", "Specify the current base version", None), 

209 ("optFiles", "Specify a list of files that SHOULD be optimized", None), 

210 ("noOptFiles", "Specify a list of files that should NOT be optimized", None), 

211 ( 

212 "macosx_deployment_target", 

213 "Deployment target for Mac OS X", 

214 _get_macosx_deployment_target_default(), 

215 ), 

216 ) 

217 

218 

219def _initEnvironment(): 

220 """Construction and basic setup of the state.env variable.""" 

221 

222 ourEnv = {} 

223 preserveVars = [ 

224 "DYLD_LIBRARY_PATH", 

225 "EUPS_DIR", 

226 "EUPS_LOCK_PID", 

227 "EUPS_PATH", 

228 "EUPS_SHELL", 

229 "EUPS_USERDATA", 

230 "LD_LIBRARY_PATH", 

231 "PATH", 

232 "SHELL", 

233 "TEMP", 

234 "TERM", 

235 "TMP", 

236 "TMPDIR", 

237 "XPA_PORT", 

238 "CONDA_BUILD_SYSROOT", 

239 "SDKROOT", 

240 "GCC_COLORS", 

241 ] 

242 

243 codeCheckerVars = [ 

244 "LD_PRELOAD", 

245 "CC_LOGGER_FILE", 

246 "CC_LOGGER_GCC_LIKE", 

247 "CC_LIB_DIR", 

248 "CC_DATA_FILES_DIR", 

249 "CC_LOGGER_BIN", 

250 ] 

251 

252 # The list of implicit multithreading environment variables here 

253 # is taken from lsst.utils.disable_implicit_threading(), but 

254 # has to be put here for dependency ordering reasons, and 

255 # to ensure these are set prior to any instantiation of pytest 

256 # or any code that may implicitly spawn threads. 

257 implicitMultithreadingVars = [ 

258 "OPENBLAS_NUM_THREADS", 

259 "GOTO_NUM_THREADS", 

260 "OMP_NUM_THREADS", 

261 "MKL_NUM_THREADS", 

262 "MKL_DOMAIN_NUM_THREADS", 

263 "MPI_NUM_THREADS", 

264 "NUMEXPR_NUM_THREADS", 

265 "NUMEXPR_MAX_THREADS", 

266 ] 

267 

268 for key in preserveVars: 

269 if key in os.environ: 

270 ourEnv[key] = os.environ[key] 

271 

272 # check if running in CodeChecker environment 

273 if "CC_LOGGER_BIN" in os.environ: 273 ↛ 274line 273 didn't jump to line 274 because the condition on line 273 was never true

274 for key in codeCheckerVars: 

275 if key in os.environ: 

276 ourEnv[key] = os.environ[key] 

277 

278 # Turn off implicit multithreading. 

279 for key in implicitMultithreadingVars: 

280 ourEnv[key] = "1" 

281 

282 # Find and propagate EUPS environment variables. 

283 cfgPath = [] 

284 for k in os.environ: 

285 m = re.search(r"^(?P<name>\w+)_DIR(?P<extra>_EXTRA)?$", k) 

286 if not m: 

287 continue 

288 cfgPath.append(os.path.join(os.environ[k], "ups")) 

289 cfgPath.append(os.path.join(os.environ[k], "configs")) 

290 if m.group("extra"): 290 ↛ 291line 290 didn't jump to line 291 because the condition on line 290 was never true

291 cfgPath.append(os.environ[k]) 

292 else: 

293 cfgPath.append(os.path.join(os.environ[k], "ups")) 

294 p = m.group("name") 

295 varname = eupsForScons.utils.setupEnvNameFor(p) 

296 if varname in os.environ: 296 ↛ 284line 296 didn't jump to line 284 because the condition on line 296 was always true

297 ourEnv[varname] = os.environ[varname] 

298 ourEnv[k] = os.environ[k] 

299 

300 # add <build root>/ups directory to the configuration search path 

301 # this allows the .cfg file for the package being built to be found without 

302 # requiring <product name>_DIR to be in the env 

303 cfgPath.append(os.path.join(SCons.Script.Dir("#").abspath, "ups")) 

304 

305 # 

306 # Add any values marked as export=FOO=XXX[,GOO=YYY] to ourEnv 

307 # 

308 exportVal = SCons.Script.ARGUMENTS.pop("export", None) 

309 if exportVal: 309 ↛ 310line 309 didn't jump to line 310 because the condition on line 309 was never true

310 for kv in exportVal.split(","): 

311 k, v = kv.split("=") 

312 ourEnv[k] = v 

313 global env 

314 sconsUtilsPath, thisFile = os.path.split(__file__) 

315 toolPath = os.path.join(sconsUtilsPath, "tools") 

316 env = SCons.Script.Environment(ENV=ourEnv, variables=opts, toolpath=[toolPath], tools=["default", "cuda"]) 

317 env.cfgPath = cfgPath 

318 # 

319 # We don't want "lib" inserted at the beginning of loadable module names; 

320 # we'll import them under their given names. 

321 # 

322 env["LDMODULEPREFIX"] = "" 

323 if env["PLATFORM"] == "darwin": 323 ↛ 324line 323 didn't jump to line 324 because the condition on line 323 was never true

324 env["LDMODULESUFFIX"] = ".so" 

325 if not re.search(r"-install_name", str(env["SHLINKFLAGS"])): 

326 env.Append(SHLINKFLAGS=["-install_name", "${TARGET.file}"]) 

327 if not re.search(r"-headerpad_max_install_names", str(env["SHLINKFLAGS"])): 

328 env.Append(SHLINKFLAGS=["-Wl,-headerpad_max_install_names"]) 

329 # 

330 # We want to be explicit about the OS X version we're targeting 

331 # 

332 env["ENV"]["MACOSX_DEPLOYMENT_TARGET"] = env["macosx_deployment_target"] 

333 # This flag is required for std::variant and std::filesystem 

334 # on deployment platforms < 10.13 and 10.15 

335 for flag in ("-D_LIBCPP_DISABLE_AVAILABILITY=1",): 

336 env["CFLAGS"].append(flag) 

337 env["CXXFLAGS"].append(flag) 

338 log.info("Setting OS X binary compatibility level: {}".format(env["ENV"]["MACOSX_DEPLOYMENT_TARGET"])) 

339 # 

340 # For XCode 7.3 we need to explicitly add a trailing slash to library 

341 # paths. This does not hurt things on older XCodes. We can remove this 

342 # once XCode is fixed. See Apple radar rdr://25313838 

343 # 

344 env["LIBDIRSUFFIX"] = "/" 

345 

346 if use_conda_compilers(): 346 ↛ 373line 346 didn't jump to line 373 because the condition on line 346 was always true

347 _conda_prefix = get_conda_prefix() 

348 if "LDFLAGS" in os.environ: 348 ↛ 349line 348 didn't jump to line 349 because the condition on line 348 was never true

349 LDFLAGS = shlex.split(os.environ["LDFLAGS"]) # respects quoting! 

350 LDFLAGS = [v for v in LDFLAGS if v[0:2] != "-L"] 

351 # this one breaks some linking in the eups build 

352 LDFLAGS = [v for v in LDFLAGS if v != "-Wl,-dead_strip_dylibs"] 

353 env.Append(LIBPATH=[f"{_conda_prefix}/lib"]) 

354 env.Append(LINKFLAGS=LDFLAGS) 

355 env.Append(SHLINKFLAGS=LDFLAGS) 

356 

357 if "CFLAGS" in os.environ: 357 ↛ 358line 357 didn't jump to line 358 because the condition on line 357 was never true

358 CFLAGS = shlex.split(os.environ["CFLAGS"]) # respects quoting! 

359 CFLAGS = [v for v in CFLAGS if v[0:2] != "-I"] 

360 env.Append(CCFLAGS=CFLAGS) 

361 

362 if "CXXFLAGS" in os.environ: 362 ↛ 363line 362 didn't jump to line 363 because the condition on line 362 was never true

363 CXXFLAGS = shlex.split(os.environ["CXXFLAGS"]) # respects quoting! 

364 CXXFLAGS = [v for v in CXXFLAGS if v[0:2] != "-I"] 

365 CXXFLAGS = [v for v in CXXFLAGS if v[0:5] != "-std="] # we let LSST set this 

366 CXXFLAGS = [v for v in CXXFLAGS if v not in CFLAGS] # conda puts in duplicates 

367 env.Append(CXXFLAGS=CXXFLAGS) 

368 

369 # 

370 # Remove valid options from the arguments 

371 # 

372 # SCons Variables do not behave like dicts 

373 for opt in opts.keys(): 

374 try: 

375 del SCons.Script.ARGUMENTS[opt] 

376 except KeyError: 

377 pass 

378 # 

379 # Process those arguments 

380 # 

381 for k in ("force", "prefix"): # these may now be set as options instead of variables 

382 if SCons.Script.GetOption(k): 382 ↛ 383line 382 didn't jump to line 383 because the condition on line 382 was never true

383 env[k] = SCons.Script.GetOption(k) 

384 

385 if env["debug"]: 385 ↛ 394line 385 didn't jump to line 394 because the condition on line 385 was always true

386 env.Append(CCFLAGS=["-g"]) 

387 

388 # 

389 # determine if EUPS is present 

390 # 

391 

392 # --no-eups overides probing 

393 # XXX is it possible to test python snippets as a scons action? 

394 if SCons.Script.GetOption("no_eups"): 394 ↛ 395line 394 didn't jump to line 395 because the condition on line 394 was never true

395 env["no_eups"] = True 

396 else: 

397 env["no_eups"] = not eupsForScons.haveEups() 

398 

399 if not env.GetOption("no_progress"): 399 ↛ 408line 399 didn't jump to line 408 because the condition on line 399 was always true

400 if env["no_eups"]: 400 ↛ 401line 400 didn't jump to line 401 because the condition on line 400 was never true

401 log.info("EUPS integration: disabled") 

402 else: 

403 log.info("EUPS integration: enabled") 

404 

405 # 

406 # Find the eups path, replace 'flavor' in favor of 'PLATFORM' if needed. 

407 # 

408 eupsPath = None 

409 try: 

410 db = env["eupsdb"] 

411 if "EUPS_PATH" not in os.environ: 

412 raise RuntimeError("You can't use eupsdb=XXX without an EUPS_PATH set") 

413 eupsPath = None 

414 for d in os.environ["EUPS_PATH"].split(":"): 

415 if re.search(rf"/{db}$|^{db}/|/{db}/", d): 

416 eupsPath = d 

417 break 

418 if not eupsPath: 

419 raise RuntimeError(f'I cannot find DB "{db}" in $EUPS_PATH') 

420 except KeyError: 

421 if "EUPS_PATH" in os.environ: 421 ↛ 423line 421 didn't jump to line 423 because the condition on line 421 was always true

422 eupsPath = os.environ["EUPS_PATH"].split(":")[0] 

423 env["eupsPath"] = eupsPath 

424 try: 

425 env["PLATFORM"] = env["flavor"] 

426 del env["flavor"] 

427 except KeyError: 

428 pass 

429 # 

430 # Check arguments 

431 # 

432 errorStr = "" 

433 # 

434 # Process otherwise unknown arguments. If setenv is true, 

435 # set construction variables; otherwise generate an error 

436 # 

437 if SCons.Script.GetOption("setenv"): 437 ↛ 438line 437 didn't jump to line 438 because the condition on line 437 was never true

438 for key in SCons.Script.ARGUMENTS: 

439 env[key] = SCons.Script.Split(SCons.Script.ARGUMENTS[key]) 

440 else: 

441 for key in SCons.Script.ARGUMENTS: 441 ↛ 442line 441 didn't jump to line 442 because the loop on line 441 never started

442 errorStr += f" {key}={SCons.Script.ARGUMENTS[key]}" 

443 if errorStr: 443 ↛ 444line 443 didn't jump to line 444 because the condition on line 443 was never true

444 log.fail(f"Unprocessed arguments:{errorStr}") 

445 # 

446 # We need a binary name, not just "Posix" 

447 # 

448 env["eupsFlavor"] = eupsForScons.flavor() 

449 

450 # Forward environment variables that should always be set to allow 

451 # our code to run even outside of tests. Executables would normally 

452 # pick up the linker environment variables via libraryLoaderEnvironment(). 

453 for envvar in ["PYTHONPATH", "HTTP_PROXY", "HTTPS_PROXY"]: 

454 if envvar in os.environ: 

455 env.AppendENVPath(envvar, os.environ[envvar]) 

456 

457 

458_configured = False 

459 

460 

461def _configureCommon(): 

462 """Configuration checks for the compiler, platform, and standard 

463 libraries.""" 

464 global _configured 

465 if _configured: 

466 return 

467 _configured = True 

468 

469 def ClassifyCc(context): 

470 """Return a pair of string identifying the compiler in use. 

471 

472 Parameters 

473 ---------- 

474 context : context 

475 Context. 

476 

477 Returns 

478 ------- 

479 compiler : `str` 

480 Compiler to use, or "unknown". 

481 version : `str` 

482 Compiler version or "unknown". 

483 """ 

484 versionNameList = ( 

485 (r"gcc(?:\-.+)? +\(.+\) +([0-9.a-zA-Z]+)", "gcc"), 

486 (r"gnu-cc(?:\-.+)? +\(.+\) +([0-9.a-zA-Z]+)", "gcc"), # catch the conda-build compiler on linux 

487 (r"\(GCC\) +([0-9.a-zA-Z]+) ", "gcc"), 

488 (r"LLVM +version +([0-9.a-zA-Z]+) ", "clang"), # clang on Mac 

489 (r"clang +version +([0-9.a-zA-Z]+) ", "clang"), # clang on linux or clang w/ conda on Mac 

490 (r"\(ICC\) +([0-9.a-zA-Z]+) ", "icc"), 

491 (r"cc \(Ubuntu +([0-9\~\-.a-zA-Z]+)\)", "gcc"), # gcc on Ubuntu (not always caught by #3 above) 

492 ) 

493 

494 context.Message("Checking who built the CC compiler...") 

495 result = context.TryAction(SCons.Script.Action(r"$CC --version > $TARGET")) 

496 ccVersDumpOK, ccVersDump = result[0:2] 

497 if ccVersDumpOK: 

498 for reStr, compilerName in versionNameList: 

499 match = re.search(reStr, ccVersDump) 

500 if match: 

501 compilerVersion = match.groups()[0] 

502 context.Result(f"{compilerName}={compilerVersion}") 

503 return (compilerName, compilerVersion) 

504 context.Result("unknown") 

505 return ("unknown", "unknown") 

506 

507 if env.GetOption("clean") or env.GetOption("no_exec") or env.GetOption("help"): 

508 env.whichCc = "unknown" # who cares? We're cleaning/not execing, not building 

509 else: 

510 if use_conda_compilers(): 

511 # conda-build expects you to use the compilers as-is 

512 env["CC"], env["CXX"] = conda_compilers() 

513 

514 conf = env.Configure(custom_tests={"ClassifyCc": ClassifyCc}) 

515 env.whichCc, env.ccVersion = conf.ClassifyCc() 

516 if not env.GetOption("no_progress"): 

517 log.info(f"CC is **CONDA** {env.whichCc} version {env.ccVersion}") 

518 conf.Finish() 

519 else: 

520 if env["cc"] != "": 

521 CC = CXX = None 

522 if re.search(r"^gcc(-\d+(\.\d+)*)?( |$)", env["cc"]): 

523 CC = env["cc"] 

524 CXX = re.sub(r"^gcc", "g++", CC) 

525 elif re.search(r"^icc( |$)", env["cc"]): 

526 CC = env["cc"] 

527 CXX = re.sub(r"^icc", "icpc", CC) 

528 elif re.search(r"^clang( |$)", env["cc"]): 

529 CC = env["cc"] 

530 CXX = re.sub(r"^clang", "clang++", CC) 

531 elif re.search(r"^cc( |$)", env["cc"]): 

532 CC = env["cc"] 

533 CXX = re.sub(r"^cc", "c++", CC) 

534 else: 

535 log.fail(f"Unrecognised compiler: {env['cc']}") 

536 env0 = SCons.Script.Environment() 

537 if CC and env["CC"] == env0["CC"]: 

538 env["CC"] = CC 

539 if CC and env["CXX"] == env0["CXX"]: 

540 env["CXX"] = CXX 

541 conf = env.Configure(custom_tests={"ClassifyCc": ClassifyCc}) 

542 env.whichCc, env.ccVersion = conf.ClassifyCc() 

543 

544 # If we have picked up a default compiler called gcc that is really 

545 # clang, we call it clang to avoid confusion (gcc on macOS has 

546 # subtly different options) 

547 if not env["cc"] and env.whichCc == "clang" and env["CC"] == "gcc": 

548 env["CC"] = "clang" 

549 env["CXX"] = "clang++" 

550 

551 if not env.GetOption("no_progress"): 

552 log.info(f"CC is {env.whichCc} version {env.ccVersion}") 

553 conf.Finish() 

554 

555 # 

556 # Compiler flags, including CCFLAGS for C and C++ and CXXFLAGS for C++ only 

557 # 

558 ARCHFLAGS = os.environ.get("ARCHFLAGS", env.get("archflags")) 

559 if ARCHFLAGS: 

560 env.Append(CCFLAGS=ARCHFLAGS.split()) 

561 env.Append(LINKFLAGS=ARCHFLAGS.split()) 

562 # We'll add warning and optimisation options last 

563 if env["profile"] == "1" or env["profile"] == "pg": 

564 env.Append(CCFLAGS=["-pg"]) 

565 env.Append(LINKFLAGS=["-pg"]) 

566 elif env["profile"] == "gcov": 

567 env.Append(CCFLAGS="--coverage") 

568 env.Append(LINKFLAGS="--coverage") 

569 

570 # 

571 # Enable C++20 support 

572 # 

573 if not (env.GetOption("clean") or env.GetOption("help") or env.GetOption("no_exec")): 

574 if not env.GetOption("no_progress"): 

575 log.info("Checking for C++20 support") 

576 conf = env.Configure() 

577 for cpp20Arg in (f"-std={val}" for val in ("c++20",)): 

578 conf.env = env.Clone() 

579 conf.env.Append(CXXFLAGS=cpp20Arg) 

580 if conf.CheckCXX(): 

581 env.Append(CXXFLAGS=cpp20Arg) 

582 if not env.GetOption("no_progress"): 

583 log.info(f"C++20 supported with {cpp20Arg!r}") 

584 break 

585 else: 

586 log.fail(f"C++20 extensions could not be enabled for compiler {env.whichCc!r}") 

587 conf.Finish() 

588 

589 # 

590 # Byte order 

591 # 

592 import socket 

593 

594 if socket.htons(1) != 1: 

595 env.Append(CCFLAGS=["-DLSST_LITTLE_ENDIAN=1"]) 

596 # 

597 # If we're linking to libraries that themselves linked to 

598 # shareable libraries we need to do something special. 

599 # 

600 if re.search(r"^(Linux|Linux64)$", env["eupsFlavor"]) and "LD_LIBRARY_PATH" in os.environ: 

601 env.Append(LINKFLAGS=["-Wl,-rpath-link"]) 

602 env.Append(LINKFLAGS=[f"-Wl,{os.environ['LD_LIBRARY_PATH']}"]) 

603 # 

604 # Set the optimization level. 

605 # 

606 if env["opt"]: 

607 env["CCFLAGS"] = [o for o in env["CCFLAGS"] if not re.search(r"^-O(\d|s|g|fast)$", o)] 

608 env.MergeFlags(f"-O{env['opt']}") 

609 # 

610 # Set compiler-specific warning flags. 

611 # 

612 if env.whichCc == "clang": 

613 env.Append(CCFLAGS=["-Wall"]) 

614 env["CCFLAGS"] = [o for o in env["CCFLAGS"] if not re.search(r"^-mno-fused-madd$", o)] 

615 

616 ignoreWarnings = { 

617 "unused-function": "boost::regex has functions in anon namespaces in headers", 

618 } 

619 filterWarnings = { 

620 "attributes": "clang pretends to be g++, but complains about g++ attributes such as flatten", 

621 "char-subscripts": "seems innocous enough, and is used by boost", 

622 "constant-logical-operand": "Used by eigen 2.0.15. Should get this fixed", 

623 "format-security": "format string is not a string literal", 

624 "mismatched-tags": "mixed class and struct. Used by gcc 4.2 RTL and eigen 2.0.15", 

625 "parentheses": "equality comparison with extraneous parentheses", 

626 "shorten-64-to-32": "implicit conversion loses integer precision", 

627 "self-assign": "x = x", 

628 "unused-local-typedefs": "unused typedef", # lots from boost 

629 "unknown-pragmas": "unknown pragma ignored", 

630 "deprecated-register": "register is deprecated", 

631 } 

632 for k in ignoreWarnings: 

633 env.Append(CCFLAGS=[f"-Wno-{k}"]) 

634 if env.GetOption("filterWarn"): 

635 for k in filterWarnings: 

636 env.Append(CCFLAGS=[f"-Wno-{k}"]) 

637 elif env.whichCc == "gcc": 

638 env.Append(CCFLAGS=["-Wall"]) 

639 env.Append(CCFLAGS=["-Wno-unknown-pragmas"]) # we don't want complaints about icc/clang pragmas 

640 env.Append(CCFLAGS=["-Wno-unused-local-typedefs"]) # boost generates a lot of these 

641 elif env.whichCc == "icc": 

642 env.Append(CCFLAGS=["-Wall"]) 

643 filterWarnings = { 

644 21: "type qualifiers are meaningless in this declaration", 

645 68: "integer conversion resulted in a change of sign", 

646 111: "statement is unreachable", 

647 191: "type qualifier is meaningless on cast type", 

648 193: 'zero used for undefined preprocessing identifier "SYMB"', 

649 279: "controlling expression is constant", 

650 304: 'access control not specified ("public" by default)', # comes from boost 

651 383: "value copied to temporary, reference to temporary used", 

652 # 424: 'Extra ";" ignored', 

653 444: 'destructor for base class "CLASS" is not virtual', 

654 981: "operands are evaluated in unspecified order", 

655 1418: "external function definition with no prior declaration", 

656 1419: "external declaration in primary source file", 

657 1572: "floating-point equality and inequality comparisons are unreliable", 

658 1720: 'function "FUNC" has no corresponding member operator delete' 

659 "(to be called if an exception is thrown during initialization of an allocated object)", 

660 2259: 'non-pointer conversion from "int" to "float" may lose significant bits', 

661 } 

662 if env.GetOption("filterWarn"): 

663 env.Append(CCFLAGS=["-wd{}".format(",".join([str(k) for k in filterWarnings]))]) 

664 # Workaround intel bug; cf. RHL's intel bug report 580167 

665 env.Append(LINKFLAGS=["-Wl,-no_compact_unwind", "-wd,11015"]) 

666 # 

667 # Disable link-time-optimization on GCC, for compatibility with conda 

668 # binaries 

669 # 

670 if env.whichCc == "gcc": 

671 env.Append(CCFLAGS=["-fno-lto"]) 

672 env.Append(LINKFLAGS=["-fno-lto"]) 

673 

674 

675def _saveState(): 

676 """Save state such as optimization level used. 

677 

678 Notes 

679 ----- 

680 The scons mailing lists were unable to tell RHL how to get this back 

681 from ``.sconsign.dblite``. 

682 """ 

683 

684 if env.GetOption("clean"): 

685 return 

686 

687 config = ConfigParser() 

688 config.add_section("Build") 

689 config.set("Build", "cc", env.whichCc) 

690 if env["opt"]: 

691 config.set("Build", "opt", env["opt"]) 

692 

693 try: 

694 confFile = os.path.join(env.Dir(env["CONFIGUREDIR"]).abspath, "build.cfg") 

695 with open(confFile, "w") as configfile: 

696 config.write(configfile) 

697 except Exception as e: 

698 log.warn(f"Unexpected exception in _saveState: {e}") 

699 

700 

701_initOptions() 

702_initLog() 

703_initVariables() 

704_initEnvironment()