summaryrefslogtreecommitdiff
path: root/var/spack/repos/builtin/packages/foam-extend/package.py
blob: 43e70cf7447acc973f382491c9267c3a8f4069d1 (plain) (blame)
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
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

# Legal Notice
# ------------
# OPENFOAM is a trademark owned by OpenCFD Ltd
# (producer and distributor of the OpenFOAM software via www.openfoam.com).
# The trademark information must remain visible and unadulterated in this
# file and via the "spack info" and comply with the term set by
# http://openfoam.com/legal/trademark-policy.php
#
# This file is not part of OpenFOAM, nor does it constitute a component of an
# OpenFOAM distribution.
#
##############################################################################
#
# Notes
# - mpi handling: WM_MPLIB=USERMPI and generate mplibUSERMPI wmake rules.
#
# Changes
# 2017-03-28 Mark Olesen <mark.olesen@esi-group.com>
#  - avoid installing intermediate targets.
#  - reworked to mirror the openfoam package.
#    If changes are needed here, consider if they need applying there too.
#
# Known issues
# - Combining +parmgridgen with +float32 probably won't work.
#
##############################################################################
import glob
import os
import re

import llnl.util.tty as tty

from spack.package import *
from spack.pkg.builtin.openfoam import (
    OpenfoamArch,
    add_extra_files,
    rewrite_environ_files,
    write_environ,
)
from spack.util.environment import EnvironmentModifications


class FoamExtend(Package):
    """The Extend Project is a fork of the OpenFOAM opensource library
    for Computational Fluid Dynamics (CFD).
    This offering is not approved or endorsed by OpenCFD Ltd,
    producer and distributor of the OpenFOAM software via www.openfoam.com,
    and owner of the OPENFOAM trademark.
    """

    homepage = "https://sourceforge.net/projects/foam-extend/"

    license("GPL-3.0-only")

    version("5.0", git="http://git.code.sf.net/p/foam-extend/foam-extend-5.0.git")
    version("4.1", git="http://git.code.sf.net/p/foam-extend/foam-extend-4.1.git")
    version("4.0", git="http://git.code.sf.net/p/foam-extend/foam-extend-4.0.git")
    version("3.2", git="http://git.code.sf.net/p/foam-extend/foam-extend-3.2.git")
    version("3.1", git="http://git.code.sf.net/p/foam-extend/foam-extend-3.1.git", deprecated=True)
    version("3.0", git="http://git.code.sf.net/p/foam-extend/foam-extend-3.0.git", deprecated=True)

    # variant('int64', default=False,
    #         description='Compile with 64-bit label')
    variant("float32", default=False, description="Compile with 32-bit scalar (single-precision)")
    variant("paraview", default=False, description="Build paraview plugins (eg, paraFoam)")
    variant("scotch", default=True, description="With scotch for decomposition")
    variant("ptscotch", default=True, description="With ptscotch for decomposition")
    variant("metis", default=True, description="With metis for decomposition")
    variant("parmetis", default=True, description="With parmetis for decomposition")
    variant("parmgridgen", default=True, description="With parmgridgen support")
    variant(
        "source", default=True, description="Install library/application sources and tutorials"
    )

    depends_on("mpi")
    depends_on("python")
    depends_on("zlib-api")
    depends_on("flex", type="build")
    depends_on("cmake", type="build")

    depends_on("scotch~metis", when="~ptscotch+scotch")
    depends_on("scotch~metis+mpi", when="+ptscotch")
    depends_on("metis@5:", when="+metis")
    depends_on("parmetis", when="+parmetis")
    # mgridgen is statically linked
    depends_on("parmgridgen", when="+parmgridgen", type="build")
    depends_on("paraview@:5.0.1", when="+paraview")
    depends_on("mesquite")

    # General patches
    common = ["spack-Allwmake", "README-spack"]
    assets = []  # type: List[str]

    # Some user config settings
    config = {"label-size": False, "mplib": "USERMPI"}  # <- No int32/int64 support  # USERMPI

    # The openfoam architecture, compiler information etc
    _foam_arch = None

    # Content for etc/prefs.{csh,sh}
    etc_prefs = {}  # type: Dict[str, str]

    # Content for etc/config.{csh,sh}/ files
    etc_config = {}  # type: Dict[str, str]

    phases = ["configure", "build", "install"]
    build_script = "./spack-Allwmake"  # <- Added by patch() method.

    #
    # - End of definitions / setup -
    #

    def setup_run_environment(self, env):
        """Add environment variables to the generated module file.
        These environment variables come from running:

        .. code-block:: console

           $ . $WM_PROJECT_DIR/etc/bashrc
        """
        bashrc = join_path(self.projectdir, "etc", "bashrc")
        minimal = True
        if os.path.isfile(bashrc):
            # post-install: source the installed bashrc
            try:
                mods = EnvironmentModifications.from_sourcing_file(
                    bashrc,
                    clean=True,  # Remove duplicate entries
                    blacklist=[  # Blacklist these
                        # Inadvertent changes
                        # -------------------
                        "PS1",  # Leave unaffected
                        "MANPATH",  # Leave unaffected
                        # Unneeded bits
                        # -------------
                        "FOAM_INST_DIR",  # Possibly incorrect
                        "FOAM_(APP|ETC|SRC|SOLVERS|UTILITIES)",
                        "FOAM_TEST_.*_DIR",
                        "WM_NCOMPPROCS",
                        # 'FOAM_TUTORIALS',  # can be useful
                        # Lots of third-party cruft
                        # -------------------------
                        "[A-Z].*_(BIN|LIB|INCLUDE)_DIR",
                        "[A-Z].*_SYSTEM",
                        "WM_THIRD_PARTY_.*",
                        "(BISON|FLEX|CMAKE|ZLIB)_DIR",
                        "(METIS|PARMETIS|PARMGRIDGEN|SCOTCH)_DIR",
                        # User-specific
                        # -------------
                        "FOAM_RUN",
                        "(FOAM|WM)_.*USER_.*",
                    ],
                    whitelist=[  # Whitelist these
                        "MPI_ARCH_PATH",  # Can be needed for compilation
                        "PYTHON_BIN_DIR",
                    ],
                )

                env.extend(mods)
                minimal = False
                tty.info("foam-extend env: {0}".format(bashrc))
            except Exception:
                minimal = True

        if minimal:
            # pre-build or minimal environment
            tty.info("foam-extend minimal env {0}".format(self.prefix))
            env.set("FOAM_INST_DIR", os.path.dirname(self.projectdir)),
            env.set("FOAM_PROJECT_DIR", self.projectdir)
            env.set("WM_PROJECT_DIR", self.projectdir)
            for d in ["wmake", self.archbin]:  # bin added automatically
                env.prepend_path("PATH", join_path(self.projectdir, d))

    def setup_dependent_build_environment(self, env, dependent_spec):
        """Location of the OpenFOAM project.
        This is identical to the WM_PROJECT_DIR value, but we avoid that
        variable since it would mask the normal OpenFOAM cleanup of
        previous versions.
        """
        env.set("FOAM_PROJECT_DIR", self.projectdir)

    @property
    def projectdir(self):
        """Absolute location of project directory: WM_PROJECT_DIR/"""
        return self.prefix  # <- install directly under prefix

    @property
    def foam_arch(self):
        if not self._foam_arch:
            self._foam_arch = OpenfoamArch(self.spec, **self.config)
        return self._foam_arch

    @property
    def archbin(self):
        """Relative location of architecture-specific executables"""
        return join_path("applications", "bin", self.foam_arch)

    @property
    def archlib(self):
        """Relative location of architecture-specific libraries"""
        return join_path("lib", self.foam_arch)

    def rename_source(self):
        """The github tarfiles have weird names that do not correspond to the
        canonical name. We need to rename these, but leave a symlink for
        spack to work with.
        """
        # Note that this particular Foam-Extned requires absolute directories
        # to build correctly!
        parent = os.path.dirname(self.stage.source_path)
        original = os.path.basename(self.stage.source_path)
        target = "foam-extend-{0}".format(self.version)
        # Could also grep through etc/bashrc for WM_PROJECT_VERSION
        with working_dir(parent):
            if original != target and not os.path.lexists(target):
                os.rename(original, target)
                os.symlink(target, original)
                tty.info("renamed {0} -> {1}".format(original, target))

    def patch(self):
        """Adjust OpenFOAM build for spack.
        Where needed, apply filter as an alternative to normal patching."""
        add_extra_files(self, self.common, self.assets)
        self.rename_source()

        # Adjust ParMGridGen - this is still a mess
        files = [
            "src/dbns/Make/options",
            "src/fvAgglomerationMethods/MGridGenGamgAgglomeration/Make/options",
        ]
        for f in files:
            filter_file(r"-lMGridGen", r"-lmgrid", f, backup=False)

        # Adjust for flex version check
        files = [
            (
                "src/thermophysicalModels/reactionThermo/chemistryReaders/chemkinReader/"
                "chemkinLexer.L"
            ),
            "src/surfMesh/surfaceFormats/stl/STLsurfaceFormatASCII.L",
            "src/meshTools/triSurface/triSurface/interfaces/STL/readSTLASCII.L",
            "applications/utilities/preProcessing/fluentDataToFoam/fluentDataToFoam.L",
            "applications/utilities/mesh/conversion/gambitToFoam/gambitToFoam.L",
            "applications/utilities/mesh/conversion/fluent3DMeshToFoam/fluent3DMeshToFoam.L",
            "applications/utilities/mesh/conversion/ansysToFoam/ansysToFoam.L",
            "applications/utilities/mesh/conversion/fluentMeshToFoam/fluentMeshToFoam.L",
            "applications/utilities/mesh/conversion/fluent3DMeshToElmer/fluent3DMeshToElmer.L",
        ]
        for f in files:
            filter_file(
                r"#if YY_FLEX_SUBMINOR_VERSION < 34",
                (
                    r"#if YY_FLEX_MAJOR_VERSION <= 2 "
                    r"&& YY_FLEX_MINOR_VERSION <= 5 "
                    r"&& YY_FLEX_SUBMINOR_VERSION < 34"
                ),
                f,
                backup=False,
            )

    def configure(self, spec, prefix):
        """Make adjustments to the OpenFOAM configuration files in their various
        locations: etc/bashrc, etc/config.sh/FEATURE and customizations that
        don't properly fit get placed in the etc/prefs.sh file (similiarly for
        csh).
        """
        # Content for etc/prefs.{csh,sh}
        self.etc_prefs = {
            "000": {"compilerInstall": "System"},  # Sort first
            "001": {},
            "cmake": {
                "CMAKE_DIR": spec["cmake"].prefix,
                "CMAKE_BIN_DIR": spec["cmake"].prefix.bin,
            },
            "python": {
                "PYTHON_DIR": spec["python"].home,
                "PYTHON_BIN_DIR": spec["python"].home.bin,
            },
            "flex": {"FLEX_SYSTEM": 1, "FLEX_DIR": spec["flex"].prefix},
            "bison": {"BISON_SYSTEM": 1, "BISON_DIR": spec["flex"].prefix},
            "zlib": {"ZLIB_SYSTEM": 1, "ZLIB_DIR": spec["zlib-api"].prefix},
            "mesquite": {
                "MESQUITE_SYSTEM": 1,
                "MESQUITE_DIR": spec["mesquite"].prefix,
                "MESQUITE_INCLUDE_DIR": spec["mesquite"].prefix.include,
                "NESQUITE_LIB_DIR": spec["mesquite"].prefix.lib,
            },
        }
        # Adjust configuration via prefs - sort second
        self.etc_prefs["001"].update(self.foam_arch.foam_dict())

        if "+scotch" in spec or "+ptscotch" in spec:
            pkg = spec["scotch"].prefix
            self.etc_prefs["scotch"] = {
                "SCOTCH_SYSTEM": 1,
                "SCOTCH_DIR": pkg,
                "SCOTCH_BIN_DIR": pkg.bin,
                "SCOTCH_LIB_DIR": pkg.lib,
                "SCOTCH_INCLUDE_DIR": pkg.include,
            }

        if "+metis" in spec:
            pkg = spec["metis"].prefix
            self.etc_prefs["metis"] = {
                "METIS_SYSTEM": 1,
                "METIS_DIR": pkg,
                "METIS_BIN_DIR": pkg.bin,
                "METIS_LIB_DIR": pkg.lib,
                "METIS_INCLUDE_DIR": pkg.include,
            }

        if "+parmetis" in spec:
            pkg = spec["parmetis"].prefix
            self.etc_prefs["parametis"] = {
                "PARMETIS_SYSTEM": 1,
                "PARMETIS_DIR": pkg,
                "PARMETIS_BIN_DIR": pkg.bin,
                "PARMETIS_LIB_DIR": pkg.lib,
                "PARMETIS_INCLUDE_DIR": pkg.include,
            }

        if "+parmgridgen" in spec:
            pkg = spec["parmgridgen"].prefix
            self.etc_prefs["parmgridgen"] = {
                "PARMGRIDGEN_SYSTEM": 1,
                "PARMGRIDGEN_DIR": pkg,
                "PARMGRIDGEN_BIN_DIR": pkg.bin,
                "PARMGRIDGEN_LIB_DIR": pkg.lib,
                "PARMGRIDGEN_INCLUDE_DIR": pkg.include,
            }

        if "+paraview" in self.spec:
            self.etc_prefs["paraview"] = {
                "PARAVIEW_SYSTEM": 1,
                "PARAVIEW_DIR": spec["paraview"].prefix,
                "PARAVIEW_BIN_DIR": spec["paraview"].prefix.bin,
            }
            self.etc_prefs["qt"] = {
                "QT_SYSTEM": 1,
                "QT_DIR": spec["qt"].prefix,
                "QT_BIN_DIR": spec["qt"].prefix.bin,
            }

        # Write prefs files according to the configuration.
        # Only need prefs.sh for building, but install both for end-users
        write_environ(
            self.etc_prefs,
            posix=join_path("etc", "prefs.sh"),
            cshell=join_path("etc", "prefs.csh"),
        )

    def build(self, spec, prefix):
        """Build using the OpenFOAM Allwmake script, with a wrapper to source
        its environment first.
        Only build if the compiler is known to be supported.
        """
        self.foam_arch.has_rule(self.stage.source_path)
        self.foam_arch.create_rules(self.stage.source_path, self)

        args = []
        if self.parallel:  # Build in parallel? - pass via the environment
            os.environ["WM_NCOMPPROCS"] = str(make_jobs)
        builder = Executable(self.build_script)
        builder(*args)

    def install(self, spec, prefix):
        """Install under the projectdir"""

        # Fairly ugly since intermediate targets are scattered inside sources
        appdir = "applications"
        projdir = os.path.basename(self.projectdir)
        mkdirp(self.projectdir, join_path(self.projectdir, appdir))
        # Filtering: bashrc, cshrc
        edits = {
            "WM_PROJECT_INST_DIR": os.path.dirname(self.projectdir),
            "WM_PROJECT_DIR": join_path("$WM_PROJECT_INST_DIR", projdir),
        }

        # All top-level files, except spack build info and possibly Allwmake
        if "+source" in spec:
            ignored = re.compile(r"^spack-.*")
        else:
            ignored = re.compile(r"^(Allclean|Allwmake|spack-).*")

        files = [f for f in glob.glob("*") if os.path.isfile(f) and not ignored.search(f)]
        for f in files:
            install(f, self.projectdir)

        # Install directories. install applications/bin directly
        # Install 'etc' before 'bin' (for symlinks)
        for d in ["etc", "bin", "wmake", "lib", join_path(appdir, "bin")]:
            install_tree(d, join_path(self.projectdir, d), symlinks=True)

        if "+source" in spec:
            subitem = join_path(appdir, "Allwmake")
            install(subitem, join_path(self.projectdir, subitem))

            foam_arch_str = str(self.foam_arch)
            # Ignore intermediate targets
            ignore = lambda p: os.path.basename(p) == foam_arch_str

            for d in ["src", "tutorials"]:
                install_tree(d, join_path(self.projectdir, d), ignore=ignore, symlinks=True)

            for d in ["solvers", "utilities"]:
                install_tree(
                    join_path(appdir, d),
                    join_path(self.projectdir, appdir, d),
                    ignore=ignore,
                    symlinks=True,
                )

        etc_dir = join_path(self.projectdir, "etc")
        rewrite_environ_files(  # Adjust etc/bashrc and etc/cshrc
            edits, posix=join_path(etc_dir, "bashrc"), cshell=join_path(etc_dir, "cshrc")
        )
        self.install_links()

    def install_links(self):
        """Add symlinks into bin/, lib/ (eg, for other applications)"""
        # Make build log visible - it contains OpenFOAM-specific information
        with working_dir(self.projectdir):
            os.symlink(
                join_path(os.path.relpath(self.install_log_path)),
                join_path("log." + str(self.foam_arch)),
            )


# -----------------------------------------------------------------------------