Skip to content

Commit

Permalink
Add needed flags to run the C++ compiler
Browse files Browse the repository at this point in the history
We need to add Boost and Eigen directories to the
header search path, and on MacOS we need to point
to the SDK.
  • Loading branch information
benmwebb committed Feb 2, 2024
1 parent cfed949 commit 2996e8e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion modules/algebra/test/expensive_test_vectord_mismatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_vectord_mismatch(self):
"""Test combination of VectorD of different dimensions"""
# Should be a compile-time error to combine VectorD of different sizes
self.assertCompileFails(
includes=['IMP/algebra/VectorD.h'],
headers=['IMP/algebra/VectorD.h'],
body="""
IMP::algebra::Vector3D v1(1,2,3);
IMP::algebra::Vector4D v3(7,8,9,10);
Expand Down
2 changes: 1 addition & 1 deletion modules/kernel/test/expensive_test_get_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_get_array(self):
"""Test std::get<IMP::Array>"""
# Should be a compile-time error to access an out of range element
self.assertCompileFails(
includes=['IMP/base_types.h'],
headers=['IMP/base_types.h'],
body="""
IMP::ParticleIndexPair pp(IMP::ParticleIndex(1), IMP::ParticleIndex(3));
IMP::ParticleIndex p = std::get<2>(pp);""")
Expand Down
33 changes: 22 additions & 11 deletions modules/test/pyext/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,31 +320,42 @@ def assertSequenceAlmostEqual(self, first, second, places=None, msg=None,

def _read_cmake_cache(self, cmake_cache):
"""Parse CMakeCache and extract info on the C++ compiler"""
cxx = flags = None
cxx = flags = sysroot = None
includes = []
with open(cmake_cache) as fh:
for line in fh:
if line.startswith('CMAKE_CXX_COMPILER:'):
cxx = line.split('=', 1)[1].rstrip('\r\n')
cxx = line.split('=', 1)[1].strip()
elif line.startswith('CMAKE_CXX_FLAGS:'):
flags = line.split('=', 1)[1].rstrip('\r\n')
return cxx, flags

def assertCompileFails(self, includes, body):
flags = line.split('=', 1)[1].strip()
elif line.startswith('CMAKE_OSX_SYSROOT:'):
sysroot = line.split('=', 1)[1].strip()
elif line.startswith('Boost_INCLUDE_DIR:'):
includes.append(line.split('=', 1)[1].strip())
elif line.startswith('EIGEN3_INCLUDE_DIR:'):
includes.append(line.split('=', 1)[1].strip())
return cxx, flags, includes, sysroot

def assertCompileFails(self, headers, body):
"""Test that the given C++ code fails to compile with a static
assertion."""
libdir = os.path.dirname(IMP.__file__)
cmake_cache = os.path.join(libdir, '..', '..', 'CMakeCache.txt')
include = os.path.join(libdir, '..', '..', 'include')
if not os.path.exists(cmake_cache):
self.skipTest("cannot find CMakeCache.txt")
cxx, flags = self._read_cmake_cache(cmake_cache)
cxx, flags, includes, sysroot = self._read_cmake_cache(cmake_cache)
# On Mac we need to point to the SDK
if sys.platform == 'darwin' and sysroot:
flags = flags + " -isysroot" + sysroot
includes.append(os.path.join(libdir, '..', '..', 'include'))
include = " ".join("-I" + inc for inc in includes)
with temporary_directory() as tmpdir:
fname = os.path.join(tmpdir, 'test.cpp')
with open(fname, 'w') as fh:
for inc in includes:
fh.write("#include <%s>\n" % inc)
for h in headers:
fh.write("#include <%s>\n" % h)
fh.write("\nint main() {\n" + body + "\n return 0;\n}\n")
cmdline = "%s %s -I%s %s" % (cxx, flags, include, fname)
cmdline = "%s %s %s %s" % (cxx, flags, include, fname)
print(cmdline)
p = subprocess.Popen(cmdline, shell=True,
stdout=subprocess.PIPE,
Expand Down

0 comments on commit 2996e8e

Please sign in to comment.