Skip to content

Commit

Permalink
Add compiler info to IMP CMake config
Browse files Browse the repository at this point in the history
Add information on the C++ compiler that was used
to build IMP itself, plus any needed flags, to the
generated CMake config file. This can be used to
build binaries that link against IMP using the same
compiler flags, and we can also use it to run our
compilation tests; this is more reliable than trying
to parse the CMake cache, since not all information is
in there.
  • Loading branch information
benmwebb committed Feb 5, 2024
1 parent 196959f commit 57e470d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,9 @@ file(WRITE ${CMAKE_BINARY_DIR}/IMPConfig.cmake
"set(IMP_SWIG_DIR \"${CMAKE_BINARY_DIR}/swig\" )\n"
"set(RMF_SWIG_DIR \"${CMAKE_SOURCE_DIR}/modules/rmf/dependency/RMF/swig\" )\n"
"set(IMP_MODULES_DIR \"${CMAKE_SOURCE_DIR}/cmake_modules\" )\n"
"set(IMP_CXX_COMPILER \"${CMAKE_CXX_COMPILER}\" )\n"
"set(IMP_CXX_FLAGS \"${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE}\" )\n"
"set(IMP_OSX_SYSROOT \"${CMAKE_OSX_SYSROOT}\" )\n"
"set(RMF_INCLUDE_PATH \"${RMF_INCLUDE_PATH}\" )\n"
"set(IMP_USE_FILE \"\${IMP_USE_DIR}/UseIMP.cmake\" )\n")
# Installed locations
Expand All @@ -505,6 +508,9 @@ file(WRITE ${CMAKE_BINARY_DIR}/cmake/IMPConfig.cmake
"set(RMF_SWIG_DIR \"${CMAKE_INSTALL_FULL_SWIGDIR}\" )\n"
"set(IMP_MODULES_DIR \"${CMAKE_INSTALL_FULL_CMAKEDIR}\" )\n"
"set(RMF_INCLUDE_PATH \"${CMAKE_INSTALL_FULL_INCLUDEDIR}\" )\n"
"set(IMP_CXX_COMPILER \"${CMAKE_CXX_COMPILER}\" )\n"
"set(IMP_CXX_FLAGS \"${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE}\" )\n"
"set(IMP_OSX_SYSROOT \"${CMAKE_OSX_SYSROOT}\" )\n"
"set(IMP_USE_FILE \"\${IMP_USE_DIR}/UseIMP.cmake\" )\n")
list(REMOVE_DUPLICATES IMP_ALL_DEPENDS_VARS)
foreach(cmakefile ${CMAKE_BINARY_DIR}/IMPConfig.cmake
Expand Down
36 changes: 19 additions & 17 deletions modules/test/pyext/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,22 +318,24 @@ def assertSequenceAlmostEqual(self, first, second, places=None, msg=None,
msg = self._formatMessage(msg, standardMsg)
raise self.failureException(msg)

def _read_cmake_cache(self, cmake_cache):
"""Parse CMakeCache and extract info on the C++ compiler"""
def _read_cmake_cfg(self, cmake_cfg):
"""Parse IMPConfig.cmake and extract info on the C++ compiler"""
cxx = flags = sysroot = None
includes = []
with open(cmake_cache) as fh:
with open(cmake_cfg) as fh:
for line in fh:
if line.startswith('CMAKE_CXX_COMPILER:'):
cxx = line.split('=', 1)[1].strip()
elif line.startswith('CMAKE_CXX_FLAGS:'):
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())
if line.startswith('set(IMP_CXX_COMPILER '):
cxx = line.split('"')[1]
elif line.startswith('set(IMP_CXX_FLAGS '):
flags = line.split('"')[1]
elif line.startswith('set(IMP_OSX_SYSROOT '):
sysroot = line.split('"')[1]
elif line.startswith('SET(Boost_INCLUDE_DIR '):
includes.append(line.split('"')[1])
elif line.startswith('SET(EIGEN3_INCLUDE_DIR '):
includes.append(line.split('"')[1])
elif line.startswith('SET(cereal_INCLUDE_DIRS '):
includes.append(line.split('"')[1])
return cxx, flags, includes, sysroot

def assertCompileFails(self, headers, body):
Expand All @@ -342,10 +344,10 @@ def assertCompileFails(self, headers, body):
if sys.platform == 'win32':
self.skipTest("No support for Windows yet")
libdir = os.path.dirname(IMP.__file__)
cmake_cache = os.path.join(libdir, '..', '..', 'CMakeCache.txt')
if not os.path.exists(cmake_cache):
self.skipTest("cannot find CMakeCache.txt")
cxx, flags, includes, sysroot = self._read_cmake_cache(cmake_cache)
cmake_cfg = os.path.join(libdir, '..', '..', 'IMPConfig.cmake')
if not os.path.exists(cmake_cfg):
self.skipTest("cannot find IMPConfig.cmake")
cxx, flags, includes, sysroot = self._read_cmake_cfg(cmake_cfg)
# On Mac we need to point to the SDK
if sys.platform == 'darwin' and sysroot:
flags = flags + " -isysroot" + sysroot
Expand Down

0 comments on commit 57e470d

Please sign in to comment.