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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
|
# SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
# SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
import importlib
import logging
import subprocess
import os
import sys
import shlex
import jinja2
import json
import copy
import dune
from dune.packagemetadata import (
getDunePyDir, Description,
getBuildMetaData, getCMakeFlags, defaultCMakeFlags,
getExternalPythonModules
)
from dune.common import comm
from dune.common.locking import Lock, LOCK_EX, LOCK_SH
from dune.common.utility import buffer_to_str, isString, reload_module
from dune.generator.exceptions import CompileError
from dune.generator.remove import removeGenerated
logger = logging.getLogger(__name__)
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
cxxFlags = None
noDepCheck = False
def deprecationMessage(dune_py_dir):
print(f"Using a pre-existing old style dune-py with a newer version of dune-common. Remove dune-py folder `{dune_py_dir}` and re-run Python script!")
print(f"It might also be required to execute 'cmake .' in the 'dune-common' build directory")
print(f"It is possible to continue to use the old version by typing 'export DUNE_PY_USE_CMAKEBUILDER=1'")
sys.exit(1)
def getCMakeCommand():
try:
return os.environ['DUNE_CMAKE']
except KeyError:
return 'cmake'
def getDefaultBuildArgs():
try:
return shlex.split(os.environ['DUNE_BUILD_FLAGS'])
except KeyError:
return None
class Builder:
@staticmethod
def sync_dir(dir_path):
"""
Execute fsync on a directory ensuring it is synced to disk
:param str dir_path: The directory to sync
:raise OSError: If fail opening the directory
"""
dir_fd = os.open(dir_path, os.O_DIRECTORY)
try:
os.fsync(dir_fd)
except OSError as e:
# On some filesystem doing a fsync on a directory
# raises an EINVAL error. Ignoring it is usually safe.
if e.errno != errno.EINVAL:
raise
finally:
os.close(dir_fd)
@staticmethod
def generate_dunepy_from_template(dunepy_dir,force=False):
# Extract the raw data dictionary
metaData = getBuildMetaData()
modules = metaData.combine_across_modules("MODULENAME")
builddirs = metaData.zip_across_modules("DEPS", "DEPBUILDDIRS")
# add dune modules which where available during the build of a
# python module but don't provide their own python module
for k,v in builddirs.items():
if k not in modules and not v.endswith("NOTFOUND"):
modules += [k]
modules.sort()
logger.debug("Found the following Dune module dependencies for dune-py: " + str(modules))
# for an existing dune-py compare module dependencies to check if rebuild required
if os.path.exists(os.path.join(dunepy_dir,"dune.module")):
description = Description(os.path.join(dunepy_dir,"dune.module"))
deps = description.depends
force = not modules == [d[0] for d in deps] # ignore version number in dependency for now
# todo also check if location of modules have changed to find
# modules moved from build to install or vice versa - or tag that some other way
# For now we simply always force a rebuild of the dune-py
# module which might lead to more work than required
force = True
else:
force = True
if force:
# Remove any remnants of an old dune-py installation
# if os.path.exists(dunepy_dir):
# shutil.rmtree(dunepy_dir)
# only remove cache
try:
os.remove(os.path.join(dunepy_dir, 'CMakeCache.txt'))
except FileNotFoundError:
pass
# Gather and reorganize meta data context that is used to write dune-py
context = {}
context["modules"] = modules
context["builddirs"] = builddirs
context["install_prefix"] = metaData.unique_value_across_modules("INSTALL_PREFIX")
context["cmake_flags"] = getCMakeFlags().copy()
context["dunepy_dir"] = dunepy_dir
# to use the default launcher we move the CMAKE_CXX_FLAGS to DEFAULT_CXXFLAGS
# to get the compile command without CXX_FLAGS we then remove them
try:
cxxflags = context["cmake_flags"].pop("CMAKE_CXX_FLAGS")
context["DEFAULT_CXXFLAGS"] = cxxflags
except KeyError:
pass
# we also need to make sure that no 'build type' (is set to 'release' in the packaging framework skbuild)
# Note: this means the value can not be set by the user
# perhaps we can find a better solution here but for now
# if we keep it the launcher will not work with packages
try:
context["cmake_flags"].pop("CMAKE_BUILD_TYPE")
except KeyError:
pass
# Find the correct template path
path, _ = os.path.split(__file__)
template_path = os.path.join(path, "template")
# Run the template through Jinja2
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(template_path),
keep_trailing_newline=True,
)
for root, _, files in os.walk(template_path):
# fix issues due to template taken from build dir
if root.endswith("CMakeFiles") or root.endswith("src_dir"): continue
for template_file in files:
full_template_file = os.path.join(root, template_file)
relative_template_file = os.path.relpath(full_template_file, start=template_path)
gen_file = os.path.join(dunepy_dir, relative_template_file.split(".template")[0])
# we don't want to remove CMakeLists.txt in the generated folder if it already exists
if root.endswith('generated') and os.path.exists(gen_file): continue
if gen_file.endswith(".cmake") or gen_file.endswith("Makefile"): continue # issues due to template taken from build dir
os.makedirs(os.path.split(gen_file)[0], exist_ok=True)
# make sure the launcher can be executed
def opener(path,flags):
if "compiler_launcher" in path:
return os.open(path, flags, 0o774)
else:
return os.open(path, flags, 0o664)
with open(gen_file, "w", opener=opener) as outfile:
outfile.write(env.get_template(relative_template_file).render(**context))
os.fsync(outfile) # make sure files are correctly synced before calling make or cmake
logger.debug("Context used to configure dune-py: {}".format(context))
# return force variable because this might be needed in the overloaded versions
return force
@staticmethod
def build_dunepy_from_template(dunepy_dir,force=False):
force = Builder.generate_dunepy_from_template(dunepy_dir,force)
if force:
# configure dune-py
Builder.callCMake(["cmake"]+defaultCMakeFlags()+["."],
cwd=dunepy_dir,
infoTxt="Configuring dune-py with CMake",
active=True, # print details anyway
)
return force
def __init__(self, force=False, saveOutput=False):
self.force = force
self.skipTargetAll = False
if saveOutput is True or saveOutput.lower() == "write":
self.savedOutput = [open("generatorCompiler.out","w+"), open("generatorCompiler.err","w+")]
elif saveOutput.lower() == "append":
self.savedOutput = [open("generatorCompiler.out","a+"), open("generatorCompiler.err","a+")]
elif saveOutput.lower() == "console" or \
saveOutput.lower() == "terminal" or \
saveOutput.lower() == "console_nocmake":
self.savedOutput = [sys.stdout, sys.stderr]
if saveOutput.lower() == "console_nocmake":
self.skipTargetAll = True
else:
self.savedOutput = None
self.dune_py_dir = getDunePyDir()
self.build_args = getDefaultBuildArgs()
self.generated_dir = os.path.join(self.dune_py_dir, 'python', 'dune', 'generated')
self.initialized = False
self.externalPythonModules = copy.deepcopy(getExternalPythonModules())
def cacheExternalModules(self):
"""Store external modules in dune-py"""
externalModulesPath = os.path.join(self.dune_py_dir, ".externalmodules.json")
with open(externalModulesPath, "w") as externalModulesFile:
json.dump(self.externalPythonModules, externalModulesFile)
def initialize(self):
# check if we need to initialize dune-py either because
# this is the first call to load or because an external module with metadata has been registered
if self.initialized and self.externalPythonModules == getExternalPythonModules():
return False
self.externalPythonModules = copy.deepcopy(getExternalPythonModules())
if comm.rank == 0:
logger.debug("(Re-)Initializing JIT compilation module")
os.makedirs(self.dune_py_dir, exist_ok=True)
# need to lock here so that multiple processes don't try to
# generate a new dune-py at the same time
with Lock(os.path.join(self.dune_py_dir, '..', 'lock-module.lock'), flags=LOCK_EX):
# check for existence of tag file - this is removed if a
# new dune package is added
tagfile = os.path.join(self.dune_py_dir, ".noconfigure")
if not os.path.isfile(tagfile):
logger.info('Generating dune-py module in ' + self.dune_py_dir)
# create module cache for external modules that have been registered with dune-py
self.cacheExternalModules()
# create dune-py module
self.build_dunepy_from_template(self.dune_py_dir)
# create tag file so that dune-py is not rebuilt on the next build
open(tagfile, 'a').close()
else:
logger.debug('Using existing dune-py module in ' + self.dune_py_dir)
self.compile("Rebuilding dune-py module")
# Auto-clean up dune-py: Remove all modules that have not been used in the last 30 days.
removeGenerated(['30'], date=True)
comm.barrier()
try:
dune.__path__._path.insert(0,os.path.join(self.dune_py_dir, 'python', 'dune'))
except:
dune.__path__.insert(0,os.path.join(self.dune_py_dir, 'python', 'dune'))
self.initialized = True
return True
@staticmethod
def callCMake(cmake_args, cwd, infoTxt, verbose=False, active=False, logLevel=logging.DEBUG, env=None):
# print initial info, if we are verbose
# or we know that we have to build something
stdout, stderr = None, None
active = active or verbose
if active:
if infoTxt:
logger.log(logLevel,infoTxt)
# call the cmake process
if verbose:
print(cmake_args)
# make sure directory entries are properly written to avoid raceconditions on network storage.
# Builder.sync_dir(self.generated_dir)
with subprocess.Popen(cmake_args,
cwd=cwd,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as cmake:
try:
stdout, stderr = cmake.communicate(timeout=2) # no message if delay is < 2sec
except subprocess.TimeoutExpired:
if infoTxt and not active:
logger.log(logLevel, infoTxt)
# wait for cmd to finish
stdout, stderr = cmake.communicate()
# check return code
if cmake.returncode > 0:
# retrieve stderr output
print(stderr,stdout)
raise CompileError(buffer_to_str(stderr))
return stdout, stderr
def _compile(self, infoTxt, target='all', verbose=False):
# if called for other builders don't do anything here
assert isinstance(self, Builder)
cmake_command = getCMakeCommand()
cmake_args = [cmake_command, "--build", self.dune_py_dir,
"--target", target, "--parallel"]
make_args = []
if self.build_args is not None:
make_args += self.build_args
if cxxFlags is not None:
make_args += ['CXXFLAGS='+cxxFlags]
if noDepCheck:
make_args += ["-B"]
if cmake_args != []:
cmake_args += ["--"] + make_args
stdout, stderr = Builder.callCMake(cmake_args,
cwd=self.generated_dir,
infoTxt=infoTxt,
verbose=verbose,
logLevel=logging.INFO
)
writeOutput = self.savedOutput is not None and target != 'all' if self.skipTargetAll else self.savedOutput is not None
if writeOutput:
if stdout is not None:
out = buffer_to_str(stdout)
nlines = out.count('\n')
if nlines > 1:
self.savedOutput[0].write("###############################\n")
self.savedOutput[0].write("###" + " ".join(cmake_args)+"\n")
if nlines > 0:
self.savedOutput[0].write(out)
if nlines > 1:
self.savedOutput[0].write("\n###############################\n")
if stderr is not None:
err = buffer_to_str(stderr)
nlines = err.count('\n')
if nlines > 1:
self.savedOutput[1].write("###############################\n")
self.savedOutput[1].write("###" + " ".join(cmake_args)+"\n")
if nlines > 0:
self.savedOutput[1].write(err)
if nlines > 1:
self.savedOutput[1].write("\n###############################\n")
def compile(self, infoTxt, target='all', verbose=False):
return self._compile(infoTxt, target=target, verbose=verbose)
def _maybeConfigureWithCMake(self, moduleName, source, pythonName, extraCMake):
sourceFileName = os.path.join(self.generated_dir, moduleName + ".cc")
line = "dune_add_pybind11_module(NAME " + moduleName + " EXCLUDE_FROM_ALL)"
# first check if this line is already present in the CMakeLists file
# (possible if a previous script was stopped by user before module was compiled)
with open(os.path.join(self.generated_dir, "CMakeLists.txt"), 'r') as out:
found = line in out.read()
if not os.path.isfile(sourceFileName) or not found:
compilationInfoMessage = f"Compiling {pythonName} (new)"
code = str(source)
with open(os.path.join(sourceFileName), 'w') as out:
out.write(code)
assert os.path.isfile(sourceFileName), "Error in writing module .cc file"
if not found:
origPos = -1
with open(os.path.join(self.generated_dir, "CMakeLists.txt"), 'a') as out:
# store original file size
origPos = out.tell()
out.write(line+"\n")
if extraCMake is not None:
for x in extraCMake:
out.write(x.replace("TARGET",moduleName)+"\n")
# update build system
try:
Builder.callCMake(["cmake","."],
cwd=self.dune_py_dir,
infoTxt="Configuring module {} ({}) with CMake".format(
pythonName, moduleName
),
)
except: # all exceptions will cause a problem here
os.remove(os.path.join(sourceFileName))
# remove line from CMakeLists to avoid problems
with open(os.path.join(self.generated_dir, "CMakeLists.txt"), 'a') as out:
out.truncate(origPos)
raise
elif isString(source) and not source == open(os.path.join(sourceFileName), 'r').read():
compilationInfoMessage = f"Compiling {pythonName} (updated)"
code = str(source)
with open(os.path.join(sourceFileName), 'w') as out:
out.write(code)
else:
# we can directly load the module - after checking if headers have changed
compilationInfoMessage = f"Compiling {pythonName} (rebuilding)"
# sanity check
line = "dune_add_pybind11_module(NAME " + moduleName + " EXCLUDE_FROM_ALL)"
# the CMakeLists file should already include this line
with open(os.path.join(self.generated_dir, "CMakeLists.txt"), 'r') as out:
found = line in out.read()
assert found, "CMakeLists.txt file does not contain an entry to build"+moduleName
return compilationInfoMessage
def load(self, moduleName, source, pythonName, extraCMake=None):
self.initialize()
if pythonName is None:
pythonName = moduleName
# check whether module is already compiled and build it if necessary
# (only try to build module on rank 0!)
# TODO replace if rank with something better and remove barrier further down
if comm.rank == 0:
module = sys.modules.get("dune.generated." + moduleName)
if module is None:
self._buildModule( moduleName, source, pythonName, extraCMake )
## TODO remove barrier here
comm.barrier()
logger.debug("Loading " + moduleName)
module = importlib.import_module("dune.generated." + moduleName)
if self.force:
logger.info("Reloading " + pythonName)
module = reload_module(module)
return module
def _buildModule(self, moduleName, source, pythonName, extraCMake):
logger.debug("Module {} not loaded".format(moduleName))
# make sure nothing (compilation, generating and building) is taking place
with Lock(os.path.join(self.dune_py_dir, '..', 'lock-module.lock'), flags=LOCK_EX):
# module must be generated so lock the source file
with Lock(os.path.join(self.dune_py_dir, 'lock-'+moduleName+'.lock'), flags=LOCK_EX):
# the module might now be present, so check again
# (see #295)
module = sys.modules.get("dune.generated." + moduleName)
if module is None:
compilationMessage = self._maybeConfigureWithCMake(
moduleName, source, pythonName, extraCMake
)
else:
compilationMessage = f"Compiling {pythonName} (rebuilding after concurrent build)"
# end of exclusive dune-py lock
# we always compile even if the module is always compiled since it can happen
# that dune-py was updated in the mean time.
# This step is quite fast but there is room for optimization.
# for compilation a shared lock is enough
#
# A side effect is that during the parallel make calls
# for whatever reason cmake might be invoked due to
# changes to the module (i.e. a new target
# added). Parallel cmake calls are not allowed and as
# a consequence the complete build may fail. We take
# care of such parallel cmake calls by additional
# locking in the dune-py CMakeLists.txt
with Lock(os.path.join(self.dune_py_dir, '..', 'lock-module.lock'), flags=LOCK_SH):
# lock generated module
with Lock(os.path.join(self.dune_py_dir, 'lock-'+moduleName+'.lock'), flags=LOCK_EX):
self._compile(infoTxt=compilationMessage, target=moduleName)
return module
############################################################################
##
## Builder based on Makefiles
##
############################################################################
class MakefileBuilder(Builder):
# static vars holding command for bash and make
bashCmd = 'bash' # default values
makeCmd = 'make' # set in dunepy_from_template
@staticmethod
def build_dunepy_from_template(dunepy_dir, force=False):
# call base class dunepy_from_template
force = Builder.generate_dunepy_from_template(dunepy_dir, force=force)
if force:
# generate path for the buildScript and other filed to be created
generatedDir = os.path.join(dunepy_dir,'python','dune','generated')
buildScriptName = os.path.join(dunepy_dir,'python','dune','generated','buildScript.sh')
# check whether ninja is available with cmake or not
useNinja = False # to be revised in later versions
if useNinja:
try:
Builder.callCMake(["cmake", "-G", "Ninja"] + defaultCMakeFlags() + ["."],
cwd=dunepy_dir,
infoTxt="Configuring dune-py with CMake (ninja)",
active=True, # print details anyway
)
logger.debug("Using ninja as cmake generator!")
except:
logger.debug("Check for ninja failed!")
useNinja = False
if not useNinja:
# call base class dunepy_from_template (re-initialize)
force = Builder.generate_dunepy_from_template(dunepy_dir, force=True)
# extract compiler launcher if provided by user
launcher = getCMakeFlags().get('CMAKE_CXX_COMPILER_LAUNCHER',
os.path.join(dunepy_dir,"compiler_launcher.sh"))
# the default flags contain CXX_FLAGS but we need to keep
# those empty to get the correct compiler command
cmake_flags = defaultCMakeFlags(overwrite={'CMAKE_CXX_FLAGS':' '})
# the launcher might be given through a environment
# variable which is taken care of here
for flag in cmake_flags:
if flag.find('CMAKE_CXX_COMPILER_LAUNCHER') > 0:
launcher = flag.split("=")[1]
# adding CMAKE_EXPORT_COMPILE_COMMANDS=ON because only
# having that in the CMakeLists.txt file fails for some of
# the CI runs
# Builder.callCMake(["cmake"] + cmake_flags + ["."],
Builder.callCMake(["cmake","-DCMAKE_EXPORT_COMPILE_COMMANDS=ON","."],
cwd=dunepy_dir,
infoTxt="Configuring dune-py with CMake (make)",
active=True, # print details anyway
)
# obtain bash and make command generated by cmake
# when ninja is used makeCmd is the ninja command
try:
commandSourceName = os.path.join(dunepy_dir,'unix_commands.json')
with open(commandSourceName) as commandFile:
cmdList = json.load(commandFile)[0]
MakefileBuilder.bashCmd = cmdList["bash"] # shell command
MakefileBuilder.makeCmd = cmdList["make"] # make command
MakefileBuilder.generatorCmd = cmdList["generator"] # generator (make or ninja)
except FileNotFoundError:
pass
if not useNinja:
#########################################################################
# Remark about ninja:
# - the -B used below for the extractCompiler build needed for
# the second approach does not work with ninja
# - the link.txt used in approach 1/3 is not available with ninja
# - the CXXFLAGS overwrite seems not to work with ninja -
# the script is not generated so approach 2 will not work due to missing echo
# We don't have to use ninja explicitly for dune-py but have
# to make sure the user can't set up ninja for dune-py by # mistake
#########################################################################
try:
stdout, stderr = \
Builder.callCMake(["cmake"]+
['--build','.','--target',"extractCompiler"]+
['--','-B'],
cwd=dunepy_dir,
env={**os.environ,
"CXXFLAGS":" ",
},
infoTxt="extract compiler command",
active=True, # print details anyway
)
except CompileError:
deprecationMessage(dunepy_dir)
########################################################################
# Write buildScript.sh
########################################################################
# we already have link.txt from the previous cmake run (extractCompiler)
buildDirBase = os.path.join(generatedDir,'CMakeFiles','extractCompiler.dir')
linkerSourceName = os.path.join(buildDirBase,'link.txt')
buildSourceName = os.path.join(buildDirBase,'build.make')
commandSourceName = os.path.join(dunepy_dir,'compile_commands.json')
with open(buildScriptName, "w") as buildScript:
# write bash line
buildScript.write("#!" + MakefileBuilder.bashCmd + "\n")
# write compiler commands
with open(commandSourceName) as commandFile:
compilerCmd = json.load(commandFile)[0]["command"]
# replace target file
compilerCmd = compilerCmd.replace('extractCompiler', '$1')
# this line is extracted from build.make
usedBuildMake = False
#with open(buildSourceName, "r") as buildFile:
# buildCmd = buildFile.readlines()
# for idx,line in enumerate(buildCmd):
# # compiler command is next line after "Building CXX object ..."
# if "Building CXX object " in line and idx < len(buildCmd)-1:
# cmd = buildCmd[idx+1].partition("(CXX_FLAGS)")[2]
# cmd = cmd.partition(" -o ")[0].replace('extractCompiler', '$1')
# compilerCmd += cmd
# usedBuildMake = True
# break
# fall back in case something went wrong with parsing build.make
if not usedBuildMake:
compilerCmd = compilerCmd + " -MD -MT CMakeFiles/$1.dir/$1.cc.o -MF CMakeFiles/$1.dir/$1.cc.o.d"
# forward errors so that compilation failure will be caught
buildScript.write('set -e\n')
# add launcher before compiler
buildScript.write('if [ "$DUNE_CXX_COMPILER_LAUNCHER" == "" ]; then\n')
buildScript.write(' DUNE_CXX_COMPILER_LAUNCHER='+launcher+'\n')
buildScript.write('fi\n')
buildScript.write("$DUNE_CXX_COMPILER_LAUNCHER " + compilerCmd+"\n")
# write linker commands
with open(linkerSourceName, "r") as linkerSource:
linkerCmd = linkerSource.read()
linkerCmd = linkerCmd.replace('extractCompiler','$1')
buildScript.write(linkerCmd)
os.fsync(buildScript) # make sure files are correctly synced before calling make or cmake
else: # useNinja
ninja = MakefileBuilder.generatorCmd
assert ninja.find("ninja")
# make sure directory entries are properly written to avoid raceconditions on network storage.
Builder.sync_dir(self.generated_dir)
with subprocess.Popen([ninja, "-t","commands","extractCompiler"],
cwd=dunepy_dir,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as ninjaProcess:
stdout, stderr = ninjaProcess.communicate()
exit_code = ninjaProcess.returncode
assert exit_code == 0
########################################################################
# Write buildScript.sh
########################################################################
out = buffer_to_str(stdout).strip().split("\n")
assert len(out) == 2
with open(buildScriptName, "w") as buildScript:
# write bash line
buildScript.write("#!" + MakefileBuilder.bashCmd + "\n")
buildScript.write("set -e\n")
compilerCmd = out[0].replace('extractCompiler', '$1').\
replace(' python/dune/generated/',' ') # better to move the script to the root of dune-py then this can be kept
compilerCmd = compilerCmd.split(' ',1)
compilerCmd = compilerCmd[0] + " " + compilerCmd[1]
# add launcher before compiler
buildScript.write(launcher + " " + compilerCmd+"\n")
# this needs fixing: Issue is that at the linker line begins with ': && '
linkerCmd = out[1].replace('extractCompiler','$1').\
replace(' python/dune/generated/',' ') # better to move the script to the root of dune-py then this can be kept
linkerCmd = linkerCmd.split(' ',3)
linkerCmd = linkerCmd[2] + " " + linkerCmd[3].replace("&& :", "")
buildScript.write(linkerCmd+"\n")
os.fsync(buildScript) # make sure files are correctly synced before calling make or cmake
# constructor
def __init__(self, force=False, saveOutput=False):
# call __init__ of base class
super().__init__(force=force, saveOutput=saveOutput)
# just added to check for old versions of dune-py - can be removed once
# this check is not needed anymore
def initialize(self):
if not super().initialize():
return
# check that the compile script is available
script = os.path.join(self.generated_dir,"buildScript.sh")
if not os.path.exists(script):
deprecationMessage(self.dune_py_dir)
# nothing to be done in this class
def compile(self, infoTxt, target='all', verbose=False):
pass
# open and safely close source file and return content
def _equalToExistingFile(self, source, sourceFileName ):
with open(os.path.join(sourceFileName), 'r') as sFile:
content = sFile.read()
if len(content) == len(source):
return content == source
# by default return False
return False
def _configureWithMake(self, moduleName, source, pythonName):
sourceFileName = os.path.join(self.generated_dir, moduleName + ".cc")
if not os.path.isfile(sourceFileName):
compilationInfoMessage = f"Compiling {pythonName} (new)"
code = str(source)
with open(os.path.join(sourceFileName), 'w') as out:
out.write(code)
elif isString(source) and not self._equalToExistingFile(source, sourceFileName):
# if source is not equal to existing source then replace it
compilationInfoMessage = f"Compiling {pythonName} (updated)"
code = str(source)
with open(os.path.join(sourceFileName), 'w') as out:
out.write(code)
else:
compilationInfoMessage = f"{pythonName} (loading)"
os.makedirs(os.path.join(self.generated_dir,"CMakeFiles",moduleName+".dir"), exist_ok=True)
return compilationInfoMessage
def _buildModule(self, moduleName, source, pythonName, extraCMake ):
logger.debug("Module {} not loaded".format(moduleName))
# make sure nothing (compilation, generating and building) is taking place
# module must be generated so lock the source file
with Lock(os.path.join(self.dune_py_dir, '..', 'lock-module.lock'), flags=LOCK_SH):
with Lock(os.path.join(self.dune_py_dir, 'lock-'+moduleName+'.lock'), flags=LOCK_EX):
# the module might now be present, so check again
# (see #295)
module = sys.modules.get("dune.generated." + moduleName)
if module is None:
compilationMessage = self._configureWithMake( moduleName, source, pythonName )
else:
compilationMessage = f"Compiling {pythonName} (rebuilding after concurrent build)"
# we always compile even if the module is already compiled since it can happen
# that dune-py was updated in the mean time ?????
# This step is quite fast but there is room for optimization.
# we can always generate a new makefile - in case no
# dependency file has already been generate leave
# dependencies empty otherwise use existing depFile:
depFileName = os.path.join(self.generated_dir,"CMakeFiles",moduleName+'.dir',moduleName+'.cc.o.d')
makeFileName = self._makeFileName( moduleName )
with open(makeFileName, "w") as makeFile:
makeFile.write('.SUFFIXES:\n')
try:
with open(depFileName, "r") as depFile:
makeFile.write(depFile.read())
except FileNotFoundError:
makeFile.write(os.path.join("CMakeFiles",moduleName+'.dir',moduleName+'.cc.o')+':\n')
pass
makeFile.write('\t'+MakefileBuilder.bashCmd+ ' buildScript.sh '+moduleName+"\n")
makeFile.write(moduleName+'.so: '+os.path.join("CMakeFiles",moduleName+'.dir',moduleName+'.cc.o')+'\n')
os.fsync(makeFile) # make sure files are correctly synced before calling make or cmake
# make sure directory entries are properly written to avoid raceconditions on network storage.
# Builder.sync_dir(self.generated_dir)
# first just check if the makefile does not contain any error
# An issue could be that a file in the dependency list has
# moved (e.g. from installed to source build)
with subprocess.Popen([MakefileBuilder.makeCmd, "-q", "-f", makeFileName, moduleName+'.so'],
cwd=self.generated_dir,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as make:
stdout, stderr = make.communicate()
exit_code = make.returncode
# this means that there was a problem
# with the makefile so we remove the dependencies and the
# generated module so that it will be regenerated
if exit_code == 2 or noDepCheck:
with open(makeFileName, "w") as makeFile:
makeFile.write('.SUFFIXES:\n')
makeFile.write(os.path.join("CMakeFiles",moduleName+'.dir',moduleName+'.cc.o')+':\n')
makeFile.write('\t'+MakefileBuilder.bashCmd+ ' buildScript.sh '+moduleName+"\n")
makeFile.write(moduleName+'.so: '+os.path.join("CMakeFiles",moduleName+'.dir',moduleName+'.cc.o')+'\n')
os.fsync(makeFile) # make sure files are correctly synced before calling make or cmake
# make sure directory entries are properly written to avoid raceconditions on network storage.
Builder.sync_dir(self.generated_dir)
if exit_code > 0 or noDepCheck:
# make sure directory entries are properly written to avoid raceconditions on network storage.
# Builder.sync_dir(self.generated_dir)
# call make to build shared library
self.makeModule( moduleName, makeFileName, compilationMessage, force=noDepCheck )
def _makeFileName( self, moduleName ):
return os.path.join(self.generated_dir,"CMakeFiles",moduleName+'.dir',moduleName+'.make')
def makeModule(self, moduleName, makeFileName=None, compilationMessage=None, force=False ):
if makeFileName is None:
makeFileName = self._makeFileName( moduleName )
if compilationMessage is None:
compilationMessage = f"{moduleName} rebuilding"
makeArgs = [MakefileBuilder.makeCmd]
if force:
makeArgs += ["-B"]
makeEnv = os.environ.copy()
if cxxFlags is not None:
makeEnv['CXXFLAGS'] = cxxFlags
with subprocess.Popen(makeArgs + ["-f",makeFileName, moduleName+'.so'],
cwd=self.generated_dir,
env=makeEnv,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as make:
try:
stdout, stderr = make.communicate(timeout=2) # no message if delay is <2sec
except subprocess.TimeoutExpired:
# compilation is taking place, replace loading with rebuilding
compilationMessage = compilationMessage.replace("loading", "rebuilding")
logger.log(logging.INFO,compilationMessage)
# wait for cmd to finish
stdout, stderr = make.communicate()
exit_code = make.returncode
if self.savedOutput is not None:
self.savedOutput[0].write('make return:' + str(exit_code) + "\n")
self.savedOutput[0].write(str(stdout.decode()) + "\n")
self.savedOutput[1].write(str(stderr.decode()) + "\n")
# check return code
if exit_code > 0:
# retrieve stderr output
raise CompileError(buffer_to_str(stderr))
|