Commit ac033072 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: improve consistency in version handling (issue #1010)

- use std::string instead of c-string for the string constants

- centralize some definitions of resources into foamVersion.H

  Now expose some of the hard-coded values used in foamEtcFiles()
  so that they can be known or even overridden as required.

  Relocate to src/OpenFOAM/include as a constant location.
parent 45d5d7bb
......@@ -29,34 +29,48 @@ Description
\*---------------------------------------------------------------------------*/
#include <iostream>
#include <typeinfo>
#include "foamVersion.H"
#include "IOstreams.H"
using namespace Foam;
int main()
{
std::cout
<< "\nVersion information (macros)\n"
<< "version " << Foam::FOAMversion << '\n'
<< "build " << Foam::FOAMbuild << '\n'
<< "buildArch " << Foam::FOAMbuildArch << '\n';
std::cout
<< "\nVersion information (namespace)\n"
<< "version " << foamVersion::version << '\n'
<< "build " << foamVersion::build << '\n'
<< "buildArch " << foamVersion::buildArch << '\n';
std::cout
<< "\nVerify memory addesses are identical:\n"
<< "macro " << long(&(Foam::FOAMversion)) << '\n'
<< "namespace " << long(&(foamVersion::version)) << '\n';
std::cout
<< "\nEnd\n";
Info
<< "\nVersion information (function)" << nl;
foamVersion::printBuildInfo();
Info
<< "\nVersion information (macros)" << nl
<< "version " << Foam::FOAMversion << nl
<< "build " << Foam::FOAMbuild << nl
<< "buildArch " << Foam::FOAMbuildArch << nl;
Info
<< "\nVersion information (namespace)" << nl
<< "api " << foamVersion::api << nl
<< "version " << foamVersion::version << nl
<< "build " << foamVersion::build << nl
<< "buildArch " << foamVersion::buildArch << nl;
Info
<< "\nTypes" << nl
<< "version " << typeid(foamVersion::version).name() << nl
<< "build " << typeid(foamVersion::build).name() << nl
<< "buildArch " << typeid(foamVersion::buildArch).name() << nl
<< "FOAMversion " << typeid(Foam::FOAMversion).name() << nl
<< "FOAMbuild " << typeid(Foam::FOAMbuild).name() << nl;
Info
<< "\nVerify memory addesses are identical:" << nl
<< "macro " << long(Foam::FOAMversion) << nl
<< "namespace " << long(&(foamVersion::version[0])) << nl;
Info
<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
......@@ -8,7 +8,16 @@ int USERD_get_reader_release
Info<< "Entering: USERD_get_reader_release" << endl;
#endif
strncpy(release_number, foamVersion::build, Z_MAX_USERD_NAME);
strncpy
(
release_number,
#if OPENFOAM
("OpenFOAM-" + std::to_string(OPENFOAM)).c_str(),
#else
"OpenFOAM-unknown",
#endif
Z_MAX_USERD_NAME
);
#ifdef ENSIGHTDEBUG
Info<< "Leaving: USERD_get_reader_release" << endl;
......
......@@ -36,7 +36,7 @@ Description
\*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <cstdio>
#include "fvCFD.H"
#include "IOobjectList.H"
......@@ -45,7 +45,6 @@ Description
#include "fvMesh.H"
#include "cellModel.H"
#include "globalFoam.H"
#include "foamVersion.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -48,13 +48,15 @@ Foam::Ostream& Foam::IOobject::writeBanner(Ostream& os, bool noHint)
if (!*paddedVersion)
{
// Populate: like strncpy but without trailing '\0'
const char *p = foamVersion::version;
memset(paddedVersion, ' ', 38);
for (int i = 0; *p && i < 38; ++i)
std::size_t len = foamVersion::version.length();
if (len > 38)
{
paddedVersion[i] = *p++;
len = 38;
}
std::memset(paddedVersion, ' ', 38);
std::memcpy(paddedVersion, foamVersion::version.c_str(), len);
paddedVersion[38] = '\0';
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -30,11 +30,22 @@ License
#include "Pstream.H"
#include "foamVersion.H"
// Fallback for job-control directory is in the user-directory
// ~/.OpenFOAM/jobControl
#ifndef FOAM_RESOURCE_USER_CONFIG_DIRNAME
#define FOAM_RESOURCE_USER_CONFIG_DIRNAME ".OpenFOAM"
#elif defined FULLDEBUG
#warning FOAM_RESOURCE_USER_CONFIG_DIRNAME \
is undefined (was this intentional?)
#endif
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
bool Foam::JobInfo::writeJobInfo(Foam::debug::infoSwitch("writeJobInfo", 0));
Foam::JobInfo Foam::jobInfo;
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
bool Foam::JobInfo::write(Ostream& os) const
......@@ -51,10 +62,8 @@ bool Foam::JobInfo::write(Ostream& os) const
return false;
}
}
else
{
return true;
}
return true;
}
......@@ -92,11 +101,10 @@ Foam::JobInfo::JobInfo()
if (writeJobInfo && Pstream::master())
{
string jobDir = getEnv("FOAM_JOB_DIR");
string jobDir = Foam::getEnv("FOAM_JOB_DIR");
if (jobDir.empty())
{
// Fallback: ~/.OpenFOAM/jobControl
jobDir = home()/WM_USER_RESOURCE_DIRNAME/"jobControl";
jobDir = home()/FOAM_RESOURCE_USER_CONFIG_DIRNAME/"jobControl";
}
jobFileName_ = hostName() + '.' + Foam::name(pid());
......@@ -106,19 +114,19 @@ Foam::JobInfo::JobInfo()
if (!isDir(jobDir) && !mkDir(jobDir))
{
FatalErrorInFunction
<< "No JobInfo directory: FOAM_JOB_DIR=" << jobDir
<< "No JobInfo directory: " << jobDir
<< Foam::exit(FatalError);
}
if (!isDir(runningDir_) && !mkDir(runningDir_))
{
FatalErrorInFunction
<< "No JobInfo directory: " << runningDir_
<< "No JobInfo directory: " << runningDir_
<< Foam::exit(FatalError);
}
if (!isDir(finishedDir_) && !mkDir(finishedDir_))
{
FatalErrorInFunction
<< "No JobInfo directory: " << finishedDir_
<< "No JobInfo directory: " << finishedDir_
<< Foam::exit(FatalError);
}
}
......
......@@ -900,7 +900,7 @@ void Foam::argList::parse
if (Pstream::master() && bannerEnabled())
{
IOobject::writeBanner(Info, true)
<< "Build : " << foamVersion::build
<< "Build : " << foamVersion::build.c_str()
#if OPENFOAM
<< " (OPENFOAM=" << OPENFOAM << ')'
#endif
......@@ -1003,7 +1003,7 @@ void Foam::argList::parse
Pstream::commsTypes::scheduled,
Pstream::masterNo()
);
toMaster << string(foamVersion::build) << hostName() << pid();
toMaster << foamVersion::build << hostName() << pid();
}
}
......
......@@ -25,7 +25,6 @@ License
#include "etcFiles.H"
#include "foamVersion.H"
#include "macros.H"
#include "OSspecific.H"
// * * * * * * * * * * * * * * Static Functions * * * * * * * * * * * * * * //
......@@ -45,8 +44,21 @@ namespace
// - ~/.OpenFOAM
static inline bool userResourceDir(Foam::fileName& queried)
{
queried = Foam::home()/WM_USER_RESOURCE_DIRNAME;
return Foam::isDir(queried);
#ifdef FOAM_RESOURCE_USER_CONFIG_DIRNAME
queried = Foam::home()/FOAM_RESOURCE_USER_CONFIG_DIRNAME;
if (Foam::isDir(queried))
{
// If home() fails, it will have actually queried "./.OpenFOAM"
// instead.
// But we would have worse problems elsewhere if that were the case.
return true;
}
#elif defined FULLDEBUG
#warning FOAM_RESOURCE_USER_CONFIG_DIRNAME \
is undefined (was this intentional?)
#endif
return false;
}
......@@ -59,22 +71,31 @@ static inline bool userResourceDir(Foam::fileName& queried)
// - $WM_PROJECT_INST_DIR/site
static inline bool groupResourceDir(Foam::fileName& queried)
{
queried = Foam::getEnv("WM_PROJECT_SITE");
#ifdef FOAM_RESOURCE_SITE_ENVNAME
queried = Foam::getEnv(FOAM_RESOURCE_SITE_ENVNAME);
if (queried.size())
{
return Foam::isDir(queried);
}
#elif defined FULLDEBUG
#warning FOAM_RESOURCE_SITE_ENVNAME \
is undefined (was this intentional?)
#endif
// Fallback when WM_PROJECT_SITE is unset
queried = Foam::getEnv("WM_PROJECT_INST_DIR")/"site";
return (queried.size() > 4 && Foam::isDir(queried));
#ifdef FOAM_RESOURCE_SITE_FALLBACK_ENVNAME
queried = Foam::getEnv(FOAM_RESOURCE_SITE_FALLBACK_ENVNAME)/"site";
if (queried.size() > 4)
{
return Foam::isDir(queried);
}
#elif defined FULLDEBUG
#warning FOAM_RESOURCE_SITE_FALLBACK_ENVNAME \
is undefined (was this intentional?)
#endif
// NOTE: this alternative bit of code corresponds to how we patch things
// for spack (and EasyBuild?) to avoid leaking out to the parent directory
//
// queried = Foam::getEnv("WM_PROJECT_DIR")/"site";
// return (queried.size() > 4 && Foam::isDir(queried));
return false;
}
......@@ -104,12 +125,13 @@ Foam::fileNameList searchEtc
if (version.empty())
{
#if OPENFOAM
version = STRING_QUOTE(OPENFOAM);
version.assign(std::to_string(OPENFOAM));
#else
version = foamVersion::version;
version.assign(foamVersion::version);
#endif
}
Foam::fileNameList list;
Foam::fileName dir, candidate;
......@@ -180,7 +202,6 @@ Foam::fileNameList searchEtc
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::fileNameList Foam::findEtcDirs
(
const fileName& name,
......@@ -233,15 +254,14 @@ Foam::fileNameList Foam::findEtcFiles
Foam::fileName Foam::findEtcFile(const fileName& name, const bool mandatory)
{
fileName file;
fileNameList list(findEtcFiles(name, mandatory, true));
if (list.size())
{
file = std::move(list.first());
return list.first();
}
return file;
return fileName();
}
......
......@@ -38,8 +38,30 @@ Description
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* const Foam::foamVersion::version = "VERSION_STRING";
const char* const Foam::foamVersion::build = "BUILD_STRING";
// Value from OPENFOAM, defined in wmake rules
const int Foam::foamVersion::api
(
#if OPENFOAM
OPENFOAM
#else
0
#endif
);
// Value of VERSION\_STRING generated by the build-script
const std::string Foam::foamVersion::version
(
"VERSION_STRING"
);
// Value of the BUILD\_STRING generated by the build-script
const std::string Foam::foamVersion::build
(
"BUILD_STRING"
);
// Information about machine endian, label and scalar sizes
const std::string Foam::foamVersion::buildArch
......
......@@ -29,9 +29,12 @@ License
#include "Ostream.H"
#include "OSspecific.H"
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
// file-scope function
namespace
{
// Write environment entry
inline static void printEnv
(
Foam::Ostream& os,
......@@ -46,6 +49,8 @@ inline static void printEnv
}
}
} // End anonymous namespace
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
......@@ -58,8 +63,8 @@ Foam::Ostream& Foam::profilingSysInfo::write
os.writeEntry("date", Foam::clock::dateTime());
// compile-time information
os.writeEntry("version", std::string(foamVersion::version));
os.writeEntry("build", std::string(foamVersion::build));
os.writeEntry("version", foamVersion::version);
os.writeEntry("build", foamVersion::build);
printEnv(os, "arch", "WM_ARCH");
printEnv(os, "compilerType", "WM_COMPILER_TYPE");
......
......@@ -32,9 +32,12 @@ License
void Foam::foamVersion::printBuildInfo(const bool full)
{
Info<<"Using: OpenFOAM-" << foamVersion::version
// Can use #if OPENFOAM directly
Info<<"Using: OpenFOAM-" << foamVersion::version.c_str()
<< " (see www.OpenFOAM.com)" << nl
<< "Build: " << foamVersion::build
<< "Build: " << foamVersion::build.c_str()
// The same as using foamVersion::api
#if OPENFOAM
<< " (OPENFOAM=" << OPENFOAM << ')'
#endif
......
......@@ -30,9 +30,13 @@ Description
Note
Compile-time version information is conveyed by the \b OPENFOAM define
provided in the wmake rules "General/general".
This value is mirrored by the \b OPENFOAM_COM and \b OPENFOAM_PLUS defines
provided via the stdFoam.H file.
The OPENFOAM_PLUS define was last used for 1712.
The foamVersion.H file is located directly in the src/OpenFOAM/include
directory for easier use by external packages and to allow easier
modification during packaging.
The \b OPENFOAM define is mirrored by the
\b OPENFOAM_COM and \b OPENFOAM_PLUS defines (in the stdFoam.H file).
The \b OPENFOAM_PLUS define was last used for 1712.
SourceFiles
foamVersion.C
......@@ -46,8 +50,31 @@ SourceFiles
#include "stdFoam.H"
#include <string>
// The directory name for user-resources (located in the HOME directory)
#define WM_USER_RESOURCE_DIRNAME ".OpenFOAM"
//- The directory name for user-resources within the HOME directory
//
// Default: ".OpenFOAM"
// Used by foamEtcFiles(), stringOps::expand(), Foam::JobInfo
#define FOAM_RESOURCE_USER_CONFIG_DIRNAME ".OpenFOAM"
//- The env name for site-resources to obtain a site-resources directory.
//
// Default: "WM_PROJECT_SITE"
// Used by foamEtcFiles() and stringOps::expand()
#define FOAM_RESOURCE_SITE_ENVNAME "WM_PROJECT_SITE"
//- The env name for determining a fallback directory name for site-resources
//- when the directory corresponding to FOAM_RESOURCE_SITE_ENVNAME is empty.
// The fallback search appends "/site" to the directory.
//
// Default: "WM_PROJECT_INST_DIR"
//
// \note If packaging OpenFOAM into a single directory (eg, spack, EasyBuild),
// this should be changed to "WM_PROJECT_DIR" to avoid searching within
// the prefix (parent) directory.
//
// Used by foamEtcFiles() and stringOps::expand()
#define FOAM_RESOURCE_SITE_FALLBACK_ENVNAME "WM_PROJECT_INST_DIR"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -56,23 +83,30 @@ namespace Foam
//- Version information
namespace foamVersion
{
//- OpenFOAM version (number or name) as a static c-string.
extern const char* const version;
//- OpenFOAM api number (integer) corresponding to the value of OPENFOAM
//- at the time of compilation.
// The value is 0 if OPENFOAM was not defined.
extern const int api;
//- OpenFOAM build information as a static c-string
extern const char* const build;
//- OpenFOAM version (name or stringified number) as a std::string
extern const std::string version;
//- OpenFOAM build information as a std::string
extern const std::string build;
//- OpenFOAM build architecture information
//- (machine endian, label/scalar sizes) as a static std::string
//- (machine endian, label/scalar sizes) as a std::string
extern const std::string buildArch;
//- Print information about version, build, arch to Info
//
// \verbatim
// Using: OpenFOAM-<VERSION> (see www.OpenFOAM.com)
// Build: <BUILD> (OPENFOAM=<API_LEVEL>)
// Arch: "<ARCH_INFO>"
// \endverbatim
// Eg,
// \code
// Using: OpenFOAM-<VERSION> (see www.OpenFOAM.com)
// Build: <BUILD> (OPENFOAM=<API_LEVEL>)
// Arch: "<ARCH_INFO>"
// \endcode
//
// \param full includes Arch information
void printBuildInfo(const bool full=true);
......@@ -82,10 +116,16 @@ namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Compatibility names (1806 and earlier)
// Compatibility names (1806 and earlier).
// Historically assumed to be called within the Foam namespace, so they are
// defined without the Foam namespace qualifier.
//
// - FOAMversion: c-string
// - FOAMbuild: c-string
// - FOAMbuildArch: std::string
#define FOAMversion foamVersion::version
#define FOAMbuild foamVersion::build
#define FOAMversion foamVersion::version.c_str()
#define FOAMbuild foamVersion::build.c_str()
#define FOAMbuildArch foamVersion::buildArch
#endif
......
......@@ -455,7 +455,7 @@ Foam::string Foam::stringOps::getVariable
}
else if (allowEnvVars)
{
value = getEnv(name);
value = Foam::getEnv(name);
if (value.empty() && !name.empty())
{
......@@ -468,7 +468,7 @@ Foam::string Foam::stringOps::getVariable
if (altType)
{
value = getEnv
value = Foam::getEnv
(
// var-name
word(name.substr(0, altPos), false)
......@@ -872,7 +872,7 @@ void Foam::stringOps::inplaceExpand
);
}
const string varValue = getEnv(varName);
const string varValue = Foam::getEnv(varName);
if (varValue.size())
{
if (altPos != std::string::npos && altType == '+')
......@@ -948,7 +948,7 @@ bool Foam::stringOps::inplaceReplaceVar(std::string& s, const word& varName)
return false;
}
const string content(getEnv(varName));
const string content(Foam::getEnv(varName));
if (content.empty())
{
return false;
......
......@@ -24,7 +24,6 @@ License
\*---------------------------------------------------------------------------*/
#include "ensightGeoFile.H"
#include "foamVersion.H"
#include "macros.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
......@@ -39,9 +38,9 @@ void Foam::ensightGeoFile::initialize()
// Description line 2
#if OPENFOAM
write("Written by OpenFOAM-" STRING_QUOTE(OPENFOAM));
write("Written by OpenFOAM " STRING_QUOTE(OPENFOAM));
#else
write(string("Written by OpenFOAM-" + string(foamVersion::version)));
write("Written by OpenFOAM");
#endif
newline();
......
......@@ -28,7 +28,6 @@ License
#include "OFstream.H"
#include "addToRunTimeSelectionTable.H"
#include "IOmanip.H"
#include "foamVersion.H"
#include "ensightPTraits.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......@@ -106,7 +105,7 @@ void Foam::ensightSetWriter<Type>::write
// Write .mesh file
{
string desc("Written by OpenFOAM-" + string(foamVersion::version));
string desc("Written by OpenFOAM");
OFstream os(meshFile);
os.setf(ios_base::scientific, ios_base::floatfield);
os.precision(5);
......@@ -223,7 +222,7 @@ void Foam::ensightSetWriter<Type>::write
// Write .mesh file
{