35#include "CellMLToSharedLibraryConverter.hpp"
47#include "Warnings.hpp"
50#include "DynamicModelLoaderRegistry.hpp"
51#include "GetCurrentWorkingDirectory.hpp"
53#define IGNORE_EXCEPTIONS(code) \
68 std::string component)
69 : mPreserveGeneratedSources(preserveGeneratedSources),
70 mComponentName(component)
77 DynamicCellModelLoaderPtr p_loader;
80 size_t dot_position = absolute_path.find_last_of(
".");
81 if (dot_position == std::string::npos || absolute_path[dot_position+1] ==
'/')
84 EXCEPTION(
"File does not have an extension: " + absolute_path);
86 std::string extension = absolute_path.substr(dot_position+1);
91 if (extension ==
"so")
93 WARN_ONCE_ONLY(
"CellMLToSharedLibraryConverter asked to load a \".so\" file. On this architecture it should be \".dylib\"");
95 absolute_path.replace(dot_position+1, 5, extension);
100 if (!file_path_copy.
Exists())
102 EXCEPTION(
"Dynamically loadable cell model '" + absolute_path +
"' does not exist.");
104 if (extension ==
"cellml")
107 size_t slash_position = absolute_path.find_last_of(
"/\\");
108 assert(slash_position != std::string::npos);
109 std::string folder = absolute_path.substr(0, slash_position+1);
110 std::string leaf = absolute_path.substr(slash_position+1, dot_position-slash_position);
111 std::string so_path = folder +
"lib" + leaf +
msSoSuffix;
118 EXCEPTION(
"Unable to convert .cellml to .so unless called collectively, due to possible race conditions.");
133 EXCEPTION(
"Unsupported extension '." + extension +
"' of file '" + absolute_path +
"'; must be .so, .dylib or .cellml");
140 const std::string& rCellmlFolder)
145 std::string old_cwd = GetCurrentWorkingDirectory();
150 if (!chaste_root.
IsDir())
153 <<
"' - you need the source to use CellML models directly in Chaste.");
156 if (!component_dir.
IsDir())
168 std::stringstream folder_name;
169 folder_name <<
"dynamic/tmp_" << getpid() <<
"_" << time(NULL);
191 std::vector<FileFinder> cellml_files = cellml_folder.
FindMatches(cellml_leaf_name +
"*");
193 for (
const FileFinder& r_cellml_file : cellml_files)
195 r_cellml_file.CopyTo(tmp_folder);
198 std::string cmake_lists_filename = tmp_folder.
GetAbsolutePath() +
"/CMakeLists.txt";
199 std::ofstream cmake_lists_filestream(cmake_lists_filename.c_str());
200 cmake_lists_filestream <<
"cmake_minimum_required(VERSION 3.16.3)\n" <<
201 "set(CMAKE_CXX_STANDARD 17)\n" <<
202 "set(CMAKE_CXX_STANDARD_REQUIRED ON)\n" <<
203 "set(CMAKE_CXX_EXTENSIONS OFF)\n" <<
204 "project (ChasteCellMLToSharedLibraryConverter)\n" <<
205 "find_package(Python3 3.5 REQUIRED)\n" <<
206 "set(chaste_python3_venv " + chaste_root.
GetAbsolutePath() +
"/chaste_python3_venv/bin)\n" <<
209 "set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})\n" <<
210 "include_directories(${Chaste_THIRD_PARTY_INCLUDE_DIRS} ${Chaste_INCLUDE_DIRS})\n" <<
211 "add_library(" << cellml_leaf_name <<
" SHARED " <<
"${sources})\n" <<
212 "if (${CMAKE_SYSTEM_NAME} MATCHES \"Darwin\")\n" <<
213 " target_link_libraries(" << cellml_leaf_name <<
" \"-Wl,-undefined,dynamic_lookup\")\n" <<
215 "if (${CMAKE_CXX_COMPILER_ID} STREQUAL \"IntelLLVM\")\n" <<
216 " set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -fp-model precise\")\n" <<
218 cmake_lists_filestream.close();
219 std::string cmake_args =
" -DCMAKE_PREFIX_PATH=" + chaste_root.
GetAbsolutePath() +
221 " -DChaste_ENABLE_TESTING=OFF" +
222 " -DBUILD_SHARED_LIBS=ON";
224 EXPECT0(system,
"cmake" + cmake_args +
" .");
235 so_file.
CopyTo(destination_folder);
258 EXCEPTION(
"Conversion of CellML to Chaste shared object failed. Error was: " + e.GetMessage());
267 std::stringstream args_stream;
268 copy(rArgs.begin(), rArgs.end(), std::ostream_iterator<std::string>(args_stream,
" "));
std::string ChasteBuildType()
std::string ChasteBuildDirName()
#define EXPECT0(cmd, arg)
#define IGNORE_RET(cmd, arg)
#define EXCEPTION(message)
#define EXCEPT_IF_NOT(test)
DynamicCellModelLoaderPtr Convert(const FileFinder &rFilePath, bool isCollective=true)
bool mPreserveGeneratedSources
CellMLToSharedLibraryConverter(bool preserveGeneratedSources=false, std::string component="heart")
void SetOptions(const std::vector< std::string > &rArgs)
void ConvertCellmlToSo(const std::string &rCellmlFullPath, const std::string &rCellmlFolder)
static const std::string msSoSuffix
std::string mComponentName
DynamicCellModelLoaderPtr GetLoader(const std::string &rPath)
static DynamicModelLoaderRegistry * Instance()
std::string GetLeafNameNoExtension() const
bool IsNewerThan(const FileFinder &rOtherEntity) const
std::string GetAbsolutePath() const
void DangerousRemove() const
std::vector< FileFinder > FindMatches(const std::string &rPattern) const
FileFinder GetParent() const
virtual void SetPath(const std::string &rPath, RelativeTo::Value relativeTo)
FileFinder CopyTo(const FileFinder &rDest) const